report.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * 连通上报js
  3. */
  4. var str_appid = 1252463788,
  5. str_platform = 'weixin',
  6. str_appversion = '1.2.477',
  7. str_sdkversion = '',
  8. str_common_version = '',
  9. str_nickname = '',
  10. str_device = '',
  11. str_device_type = '',
  12. reportData = {
  13. str_roomid: '',
  14. str_room_creator: '',
  15. str_userid: '',
  16. str_play_info: '',
  17. str_push_info: '',
  18. int64_ts_enter_room: -99999,
  19. int64_tc_join_group: -99999,
  20. int64_tc_get_pushers: -99999,
  21. int64_tc_play_stream: -99999,
  22. int64_tc_get_pushurl: -99999,
  23. int64_tc_push_stream: -99999,
  24. int64_tc_add_pusher: -99999,
  25. int64_tc_enter_room: -99999
  26. },
  27. streamData = {
  28. int64_ts_add_pusher: 0,
  29. int64_ts_play_stream: 0
  30. }
  31. // 获取用户信息
  32. // wx.getUserInfo({
  33. // withCredentials: false,
  34. // success: function (ret) {
  35. // str_nickname = ret.userInfo.nickName;
  36. // }
  37. // });
  38. // 获取设备信息
  39. var systemInfo = wx.getSystemInfoSync();
  40. str_sdkversion = systemInfo.version;
  41. str_common_version = systemInfo.SDKVersion;
  42. str_device = systemInfo.model;
  43. str_device_type = systemInfo.system;
  44. /**
  45. * 设置参数
  46. */
  47. function setReportData(options) {
  48. // 第一次进来重置数据
  49. if (options.int64_ts_enter_room) {
  50. console.log('第一次进来重置数据');
  51. clearData();
  52. }
  53. for(var item in reportData) {
  54. if(options[item]) {
  55. reportData[item] = options[item];
  56. }
  57. }
  58. for (var item in streamData) {
  59. if (options[item]) {
  60. streamData[item] = options[item];
  61. }
  62. }
  63. // console.warn('上报数据: ', reportData, streamData);
  64. // 连通率上报前做负值判断
  65. for (var item in reportData) {
  66. if (!isNaN(reportData[item]) && item != 'int64_tc_enter_room' && reportData[item] < 0)
  67. return;
  68. }
  69. if (streamData.int64_ts_add_pusher && streamData.int64_ts_play_stream) {
  70. reportData.int64_tc_enter_room = Math.max(streamData.int64_ts_add_pusher, streamData.int64_ts_play_stream) - reportData.int64_ts_enter_room;
  71. // 上报:只对进房进行上报
  72. // console.log('走完所有流程上报');
  73. reportData.str_room_creator && reportData.str_userid && reportData.str_room_creator != reportData.str_userid && report();
  74. }
  75. }
  76. /**
  77. * 上报cgi
  78. */
  79. function report() {
  80. // 有房间id与用户id才上报
  81. if (!reportData.str_roomid || !reportData.str_userid) {
  82. clearData();
  83. return;
  84. }
  85. // 创建房间不加入上报
  86. if (reportData.str_room_creator == reportData.str_userid) {
  87. clearData();
  88. return;
  89. }
  90. var data = reportData;
  91. data.str_appid = str_appid;
  92. data.str_platform = str_platform;
  93. data.str_appversion = str_appversion;
  94. data.str_sdkversion = str_sdkversion;
  95. data.str_common_version = str_common_version;
  96. data.str_nickname = str_nickname;
  97. data.str_device = str_device;
  98. data.str_device_type = str_device_type;
  99. console.log('真正上报数据: ', data);
  100. wx.request({
  101. url: 'https://roomtest.qcloud.com/weapp/utils/report',
  102. data: {
  103. reportID: 1,
  104. data: data
  105. },
  106. method: 'POST',
  107. header: {
  108. 'content-type': 'application/json' // 默认值
  109. },
  110. success: function (ret) {
  111. if(ret.data.code) {
  112. console.log('上报失败:' + ret.data.code + ret.data.message);
  113. } else {
  114. console.log('上报成功');
  115. }
  116. },
  117. fail: function () { console.log('report error') },
  118. complete: function () {}
  119. });
  120. clearData();
  121. }
  122. /**
  123. * 重置参数
  124. */
  125. function clearData() {
  126. reportData = {
  127. str_roomid: '',
  128. str_room_creator: '',
  129. str_userid: '',
  130. str_play_info: '',
  131. str_push_info: '',
  132. int64_ts_enter_room: -99999,
  133. int64_tc_join_group: -99999,
  134. int64_tc_get_pushers: -99999,
  135. int64_tc_play_stream: -99999,
  136. int64_tc_get_pushurl: -99999,
  137. int64_tc_push_stream: -99999,
  138. int64_tc_add_pusher: -99999,
  139. int64_tc_enter_room: -99999
  140. };
  141. streamData = {
  142. int64_ts_add_pusher: 0,
  143. int64_ts_play_stream: 0
  144. };
  145. }
  146. /**
  147. * 对外暴露函数
  148. * @type {Object}
  149. */
  150. module.exports = {
  151. setReportData: setReportData,
  152. report: report,
  153. clearData: clearData
  154. }