logReport.js 8.8 KB

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