logReport.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * @description 日志上报
  3. */
  4. export default class LogReport {
  5. URL_API = 'http://www.androidscloud.com:8002';
  6. URL_SWITCH = '/api/public/v5/log/card/getLinkLogReportSwitch'; // 日志开关查询接口地址
  7. URL_ADDRESS = '/api/public/v5/log/card/reportCardLinkLog'; // 日志上报地址
  8. $Request = null; // 用于发送 HTTP 请求的对象
  9. version = ''; // 版本号 可通过$Request版本获取
  10. // 上报参数
  11. paramsJson = {
  12. 'clientVersion': '', // 客户端版本号 // 通过$Request版本获取
  13. 'clientType': '', // 客户端类型 // 目前判断出wx或h5
  14. 'phoneModel': '', // 手机型号 // 无法获取
  15. 'phoneSystemVersion': '', // 手机系统版本号 // 无法获取
  16. 'phoneNetwork': '', // 手机网络类型 // 无法获取
  17. 'videoType': '', // 视频类型 H265 // 可以获取
  18. 'imageQuality': '', // 推流质量 [高清 | 流畅] // 可以判断
  19. 'userCardId': '', // 可以获取
  20. 'cardInfoId': '', // 无法获取
  21. 'resourceId': '', // 资源ID // 可以获取
  22. 'transferServerIp': '', // 中转服务器IP // 无法获取
  23. 'linkStartTime': '', // 链接开始时间 格式 yyyy-MM-dd HH:mm:ss
  24. 'linkEndTime': '', // 链接结束时间 格式 yyyy-MM-dd HH:mm:ss
  25. // 'linkTime': '', // 链接时间 格式 yyyy-MM-dd HH:mm:ss
  26. 'linkScene': '直连', // 链接场景[直连 | 重连] // 可以判断
  27. 'linkWay': 1, // 链接方式(1:中转链接、2:打洞链接、3:安卓卡网络状态差、4:接口返回链接信息缺失)// 可以判断
  28. 'plugFowStatus': '', // 推流状态 int: 1 成功 2:失败 // 可以判断
  29. 'logContent': '' // 日志内容
  30. };
  31. reportSwitchStatus = false; // 上报日志开关状态
  32. logs = []; // 用于存储日志的数组
  33. // 客户端类型 枚举值
  34. CLIENT_TYPE = Object.freeze({
  35. 'android': 1,
  36. 'ios': 2,
  37. 'pc': 3,
  38. 'wx': 5, // wx小程序
  39. 'h5': 7,
  40. });
  41. // 连接状态 枚举值
  42. LINK_WAY = Object.freeze({
  43. 1: '中转连接',
  44. 2: '打洞连接',
  45. 3: '安卓卡网络状态差',
  46. 4: '接口返回链接信息缺失',
  47. });
  48. // 码率 枚举值
  49. BITRATE = Object.freeze({
  50. 1800: '标清',
  51. 2200: '标清',
  52. 2800: '标清',
  53. 6000: '高清',
  54. 1243000: '超清',
  55. });
  56. // 接口响应码 枚举值 对应 linkWay字段状态
  57. RESPONSE_CODE = Object.freeze({
  58. 5200: 3, // RBD资源挂载中
  59. 5220: 3, // 云手机正在一键修复中
  60. 5203: 3, // 入使用排队9.9,年卡
  61. 5204: 3, // 9.9年卡连接异常,重新进入排队
  62. 5228: 3, // '卡的网络状态为差'
  63. 5229: 4, // '接口返回链接信息缺失'
  64. });
  65. maxLogs = 1; // 存储最大日志数量
  66. timer = null; // 定时器
  67. timerTime = 6000; // // 日志上报间隔时间
  68. /**
  69. * 构造函数,初始化 LogReport 类的实例
  70. * @param {Object} $Request - 用于发送 HTTP 请求的对象
  71. * @param {Object} opt - 可选的配置对象
  72. */
  73. constructor(opt) {
  74. /**
  75. // 扩展API是否准备好,如果没有则监听“plusready"事件
  76. // if (window.plus) {
  77. // this.plusReady()
  78. // } else {
  79. // document.addEventListener('plusready', this.plusReady, false)
  80. // }
  81. document.addEventListener('plusready', ()=> {
  82. console.log('plusReady')
  83. console.log(plus)
  84. console.log(plus.device.model)
  85. console.log(plus.networkinfo.getCurrentType())
  86. // plus.device.model
  87. // plus.networkinfo.getCurrentType()
  88. // plus.device.networkinfo.getCurrentType()
  89. }, false);
  90. */
  91. // 初始化 $Request 属性
  92. this.$Request = opt.request;
  93. this.version = this.$Request.defaults.headers.versionname;
  94. this.init();
  95. }
  96. // 初始化
  97. async init() {
  98. // 调用 checkSwitch 方法,检查后端上报日志开关是否打开
  99. await this.checkSwitch();
  100. // 创建定时器
  101. // await this.createTimer();
  102. }
  103. /**
  104. * 检查后端上报日志开关是否打开
  105. */
  106. async checkSwitch() {
  107. try{
  108. const res = await this.getLinkLogReportSwitch();
  109. if(res.status === 0 && res.success){
  110. let { cardLinkRodeSwitch } = res.data;
  111. this.reportSwitchStatus = cardLinkRodeSwitch;
  112. }else{
  113. console.error('检查日志上报开关失败')
  114. }
  115. }catch(e){
  116. console.error(e)
  117. }
  118. }
  119. /**
  120. * 查询webRTC日志上报开关状态
  121. * 无参
  122. * @return {Request}
  123. * {
  124. * "status": 0,
  125. * "msg": "",
  126. * "data": {
  127. * "cardLinkRodeSwitch":true, // 是否上报记录
  128. * "cardLinkLogFileSwitch":true // 是否收集错误日志
  129. * }
  130. * }
  131. */
  132. getLinkLogReportSwitch() {
  133. return this.$Request.get(this.URL_API + this.URL_SWITCH);
  134. }
  135. // 采集日志, 等待日志收集到一定数量或一定时间后再上报
  136. collectLog(log) {
  137. try {
  138. // 日志开关关闭,直接返回
  139. if(!this.reportSwitchStatus) {return;}
  140. // 组装本次日志上报参数
  141. let logData = this.combinativeParam();
  142. logData.logContent = log;
  143. // 设置本次日志上报时间
  144. const date = new Date();
  145. let formattedDate = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
  146. logData.linkStartTime = formattedDate;
  147. this.logs.push(logData);
  148. // 超过最大日志数量,上报日志
  149. if(this.logs.length >= this.maxLogs && this.reportSwitchStatus){
  150. this.report();
  151. // 重置定时器
  152. // this.createTimer();
  153. }
  154. } catch (error) {
  155. console.error(error);
  156. }
  157. }
  158. // 设置日志上报参数
  159. setParams(obj) {
  160. try {
  161. // 合并参数
  162. this.paramsJson = {
  163. ...this.paramsJson,
  164. ...obj
  165. }
  166. } catch (error) {
  167. console.error(error);
  168. }
  169. }
  170. // 组装日志上报固定参数
  171. combinativeParam() {
  172. try {
  173. let params = {
  174. ...this.paramsJson,
  175. };
  176. params.clientVersion = this.version;
  177. // 客户端类型 枚举值赋值
  178. params.clientType = this.enumAssignment(this.CLIENT_TYPE, this.paramsJson.clientType);
  179. params.imageQuality = this.enumAssignment(this.BITRATE, this.paramsJson.imageQuality);
  180. params.linkWay = this.enumAssignment(this.BITRATE, this.paramsJson.linkWay);
  181. return params;
  182. } catch (error) {
  183. console.error(error);
  184. }
  185. }
  186. // 日志记录上报 字符串日志上报
  187. report() {
  188. this.logs.forEach(() => {
  189. this.$Request.post(this.URL_API + this.URL_ADDRESS, { ...this.logs.shift() })
  190. .then(res => {
  191. console.log('日志上报成功', res);
  192. });
  193. })
  194. }
  195. // 生成or重置定时器
  196. async createTimer() {
  197. await this.clearTimer();
  198. if(this.reportSwitchStatus){
  199. this.timer = setInterval(() => {
  200. if(this.logs.length > 0){
  201. this.report();
  202. }
  203. }, this.timerTime);
  204. }
  205. }
  206. // 清空定时器
  207. async clearTimer() {
  208. this.timer && clearInterval(this.timer);
  209. return true;
  210. }
  211. // 检查是否为枚举值
  212. isCheckEnum(enumObj, velue) {
  213. return Object.values(enumObj).includes(velue);
  214. }
  215. // 使用枚举值赋值
  216. enumAssignment(enumObj, velue){
  217. let str = '';
  218. if(velue.toString() !== '') {
  219. // 判断是否已为枚举值
  220. str = this.isCheckEnum(enumObj, velue) ? velue : enumObj[velue];
  221. }
  222. return str;
  223. }
  224. // 关闭销毁
  225. async destroy() {
  226. // 清空日志
  227. this.logs = [];
  228. // 关闭日志上报开关
  229. this.reportSwitchStatus = false;
  230. // 清空定时器
  231. await this.clearTimer();
  232. }
  233. }