logReport.js 8.4 KB

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