logReport.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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': 1, // 链接场景
  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. // 链接场景 枚举值
  57. LINK_SCENE = Object.freeze({
  58. '直连': 1,
  59. '重连': 2,
  60. '通道断开重连': 3,
  61. '信令连接失败重连': 4,
  62. '鉴权失败重连': 5
  63. });
  64. // 视频类型 枚举值
  65. VIDEO_TYPE = Object.freeze({
  66. 'h265': 1,
  67. 'h264': 2,
  68. });
  69. // 接口响应码 枚举值 对应 linkWay字段状态
  70. RESPONSE_CODE = Object.freeze({
  71. 5200: 3, // RBD资源挂载中
  72. 5220: 3, // 云手机正在一键修复中
  73. 5203: 3, // 入使用排队9.9,年卡
  74. 5204: 3, // 9.9年卡连接异常,重新进入排队
  75. 5228: 3, // '卡的网络状态为差'
  76. 5229: 4, // '接口返回链接信息缺失'
  77. });
  78. maxLogs = 1; // 存储最大日志数量
  79. timer = null; // 定时器
  80. timerTime = 6000; // // 日志上报间隔时间
  81. /**
  82. * 构造函数,初始化 LogReport 类的实例
  83. * @param {Object} $Request - 用于发送 HTTP 请求的对象
  84. * @param {Object} opt - 可选的配置对象
  85. */
  86. constructor(opt) {
  87. /**
  88. // 扩展API是否准备好,如果没有则监听“plusready"事件
  89. // if (window.plus) {
  90. // this.plusReady()
  91. // } else {
  92. // document.addEventListener('plusready', this.plusReady, false)
  93. // }
  94. document.addEventListener('plusready', ()=> {
  95. console.log('plusReady')
  96. console.log(plus)
  97. console.log(plus.device.model)
  98. console.log(plus.networkinfo.getCurrentType())
  99. // plus.device.model
  100. // plus.networkinfo.getCurrentType()
  101. // plus.device.networkinfo.getCurrentType()
  102. }, false);
  103. */
  104. // 初始化 $Request 属性
  105. this.$Request = opt.request;
  106. this.version = this.$Request.defaults.headers.versionname;
  107. this.init();
  108. }
  109. // 初始化
  110. async init() {
  111. // 调用 checkSwitch 方法,检查后端上报日志开关是否打开
  112. await this.checkSwitch();
  113. // 创建定时器
  114. // await this.createTimer();
  115. }
  116. /**
  117. * 检查后端上报日志开关是否打开
  118. */
  119. async checkSwitch() {
  120. try{
  121. const res = await this.getLinkLogReportSwitch();
  122. if(res.status === 0 && res.success){
  123. let { cardLinkRodeSwitch } = res.data;
  124. this.reportSwitchStatus = cardLinkRodeSwitch;
  125. }else{
  126. console.error('检查日志上报开关失败')
  127. }
  128. }catch(e){
  129. console.error(e)
  130. }
  131. }
  132. /**
  133. * 查询webRTC日志上报开关状态
  134. * 无参
  135. * @return {Request}
  136. * {
  137. * "status": 0,
  138. * "msg": "",
  139. * "data": {
  140. * "cardLinkRodeSwitch":true, // 是否上报记录
  141. * "cardLinkLogFileSwitch":true // 是否收集错误日志
  142. * }
  143. * }
  144. */
  145. getLinkLogReportSwitch() {
  146. return this.$Request.get(this.URL_API + this.URL_SWITCH);
  147. }
  148. // 采集日志, 等待日志收集到一定数量或一定时间后再上报
  149. collectLog(log) {
  150. try {
  151. // 日志开关关闭,直接返回
  152. if(!this.reportSwitchStatus) {return;}
  153. // 组装本次日志上报参数
  154. let logData = this.combinativeParam();
  155. logData.logContent = log;
  156. // 设置本次日志上报时间
  157. const date = new Date();
  158. 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)}`;
  159. logData.linkStartTime = formattedDate;
  160. this.logs.push(logData);
  161. // 超过最大日志数量,上报日志
  162. if(this.logs.length >= this.maxLogs && this.reportSwitchStatus){
  163. this.report();
  164. // 重置定时器
  165. // this.createTimer();
  166. }
  167. } catch (error) {
  168. console.error(error);
  169. }
  170. }
  171. // 设置日志上报参数
  172. setParams(obj) {
  173. try {
  174. // 合并参数
  175. this.paramsJson = {
  176. ...this.paramsJson,
  177. ...obj
  178. }
  179. } catch (error) {
  180. console.error(error);
  181. }
  182. }
  183. // 组装日志上报固定参数
  184. combinativeParam() {
  185. try {
  186. let params = {
  187. ...this.paramsJson,
  188. };
  189. params.clientVersion = this.version;
  190. // 客户端类型 枚举值赋值
  191. params.clientType = this.enumAssignment(this.CLIENT_TYPE, this.paramsJson.clientType);
  192. params.imageQuality = this.enumAssignment(this.BITRATE, this.paramsJson.imageQuality);
  193. params.linkWay = this.enumAssignment(this.BITRATE, this.paramsJson.linkWay);
  194. params.linkScene = this.enumAssignment(this.LINK_SCENE, this.paramsJson.linkScene);
  195. params.videoType = this.enumAssignment(this.VIDEO_TYPE, this.paramsJson.videoType);
  196. return params;
  197. } catch (error) {
  198. console.error(error);
  199. }
  200. }
  201. // 日志记录上报 字符串日志上报
  202. report() {
  203. this.logs.forEach(() => {
  204. this.$Request.post(this.URL_API + this.URL_ADDRESS, { ...this.logs.shift() })
  205. .then(res => {
  206. console.log('日志上报成功', res);
  207. });
  208. })
  209. }
  210. // 生成or重置定时器
  211. async createTimer() {
  212. await this.clearTimer();
  213. if(this.reportSwitchStatus){
  214. this.timer = setInterval(() => {
  215. if(this.logs.length > 0){
  216. this.report();
  217. }
  218. }, this.timerTime);
  219. }
  220. }
  221. // 清空定时器
  222. async clearTimer() {
  223. this.timer && clearInterval(this.timer);
  224. return true;
  225. }
  226. // 检查是否为枚举值
  227. isCheckEnum(enumObj, velue) {
  228. return Object.values(enumObj).includes(velue);
  229. }
  230. // 使用枚举值赋值
  231. enumAssignment(enumObj, velue){
  232. let str = '';
  233. if(velue.toString() !== '') {
  234. // 判断是否已为枚举值
  235. str = this.isCheckEnum(enumObj, velue) ? velue : enumObj[velue];
  236. }
  237. return str;
  238. }
  239. // 关闭销毁
  240. async destroy() {
  241. // 清空日志
  242. this.logs = [];
  243. // 关闭日志上报开关
  244. this.reportSwitchStatus = false;
  245. // 清空定时器
  246. await this.clearTimer();
  247. }
  248. }