logReport.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. 'timeConsuming': '', // 进入云机耗时字段 毫秒单位
  16. 'clientVersion': '', // 客户端版本号 // 通过$Request版本获取
  17. 'clientType': '', // 客户端类型 // 目前判断出wx或h5
  18. 'phoneModel': '', // 手机型号 // 无法获取
  19. 'phoneSystemVersion': '', // 手机系统版本号 // 无法获取
  20. 'phoneNetwork': '', // 手机网络类型
  21. 'videoType': '', // 视频类型
  22. 'imageQuality': '', // 推流质量 [高清 | 流畅 | ...]
  23. 'userCardId': '',
  24. 'cardInfoId': '', // 无法获取
  25. 'resourceId': '', // 资源ID
  26. 'transferServerIp': '', // 中转服务器IP
  27. 'linkStartTime': '', // 链接开始时间 格式 yyyy-MM-dd HH:mm:ss
  28. 'linkEndTime': '', // 链接结束时间 格式 yyyy-MM-dd HH:mm:ss
  29. // 'linkTime': '', // 链接时间 格式 yyyy-MM-dd HH:mm:ss
  30. 'linkScene': 1, // 链接场景
  31. 'linkWay': 0, // 链接方式(0:其它原因 1:中转链接、2:打洞链接、3:安卓卡网络状态差、4:接口返回链接信息缺失)
  32. 'plugFowStatus': '', // 推流状态 int: 1 成功 2:失败
  33. 'logContent': '' // 日志内容
  34. };
  35. reportSwitchStatus = false; // 上报日志开关状态
  36. logs = []; // 用于存储日志的数组
  37. // 客户端类型 枚举值
  38. CLIENT_TYPE = Object.freeze({
  39. 'android': 1,
  40. 'ios': 2,
  41. 'pc': 3,
  42. 'miniprogram': 5, // wx小程序 在 uniapp获取类型为miniprogram
  43. 'h5': 7,
  44. });
  45. // 码率 枚举值
  46. BITRATE = Object.freeze({
  47. '自动': 2800,
  48. '高清': 2800,
  49. '标清': 1500,
  50. '流畅': 1000,
  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. timeStartTime = 0; // 链接开始时间
  78. /**
  79. * 构造函数,初始化 LogReport 类的实例
  80. * @param {Object} $Request - 用于发送 HTTP 请求的对象
  81. * @param {Object} opt - 可选的配置对象
  82. */
  83. constructor(opt) {
  84. /**
  85. // 扩展API是否准备好,如果没有则监听“plusready"事件
  86. // if (window.plus) {
  87. // this.plusReady()
  88. // } else {
  89. // document.addEventListener('plusready', this.plusReady, false)
  90. // }
  91. document.addEventListener('plusready', ()=> {
  92. console.log('plusReady')
  93. console.log(plus)
  94. console.log(plus.device.model)
  95. console.log(plus.networkinfo.getCurrentType())
  96. // plus.device.model
  97. // plus.networkinfo.getCurrentType()
  98. // plus.device.networkinfo.getCurrentType()
  99. }, false);
  100. */
  101. // 初始化 $Request 属性
  102. this.$Request = opt.request;
  103. this.version = this.$Request.defaults.headers.versionname;
  104. this.init();
  105. }
  106. // 初始化
  107. async init() {
  108. const now = new Date();
  109. // 开始连接的时间戳
  110. this.timeStartTime = now.getTime();
  111. // 设置本次日志上报时间
  112. this.paramsJson.linkStartTime = this.formatDate(now);
  113. // 调用 checkSwitch 方法,检查后端上报日志开关是否打开
  114. await this.checkSwitch();
  115. this.netWorkChange();
  116. // 创建定时器
  117. // await this.createTimer();
  118. }
  119. // 获取浏览器网络类型 isDestroy 是否移除监听
  120. netWorkChange(isDestroy = false) {
  121. if(!isDestroy && 'connection' in navigator) {
  122. const connection = navigator.connection;
  123. // 初始化时获取一次
  124. this.paramsJson.phoneNetwork = connection.effectiveType;
  125. // 监听网络类型变化
  126. const updateResourceLoad = () => {
  127. this.paramsJson.phoneNetwork = connection.effectiveType;
  128. }
  129. // 监听网络类型变化
  130. connection.addEventListener('change', updateResourceLoad);
  131. if(isDestroy) {
  132. // 移除事件监听
  133. connection.removeEventListener('change', updateResourceLoad);
  134. }
  135. }
  136. }
  137. /**
  138. * 检查后端上报日志开关是否打开
  139. */
  140. async checkSwitch() {
  141. try{
  142. const res = await this.getLinkLogReportSwitch();
  143. if(res.status === 0 && res.success){
  144. let { cardLinkRodeSwitch } = res.data;
  145. this.reportSwitchStatus = cardLinkRodeSwitch;
  146. }else{
  147. console.error('检查日志上报开关失败')
  148. }
  149. }catch(e){
  150. console.error(e)
  151. }
  152. }
  153. /**
  154. * 查询webRTC日志上报开关状态
  155. * 无参
  156. * @return {Request}
  157. * {
  158. * "status": 0,
  159. * "msg": "",
  160. * "data": {
  161. * "cardLinkRodeSwitch":true, // 是否上报记录
  162. * "cardLinkLogFileSwitch":true // 是否收集错误日志
  163. * }
  164. * }
  165. */
  166. getLinkLogReportSwitch() {
  167. return this.$Request.get(this.URL_API[location.protocol] + this.URL_SWITCH);
  168. }
  169. // 采集日志, 等待日志收集到一定数量或一定时间后再上报
  170. collectLog(log) {
  171. try {
  172. // 日志开关关闭,直接返回
  173. if(!this.reportSwitchStatus) {return;}
  174. // 组装本次日志上报参数
  175. let logData = this.combinativeParam();
  176. logData.logContent = log;
  177. this.logs.push(logData);
  178. // 超过最大日志数量,上报日志
  179. if(this.logs.length >= this.maxLogs && this.reportSwitchStatus){
  180. this.report();
  181. // 重置定时器
  182. // this.createTimer();
  183. }
  184. } catch (error) {
  185. console.error('collectLog内部错误');
  186. console.log(error);
  187. console.dir(error);
  188. console.log('log', log);
  189. }
  190. }
  191. // 设置日志上报参数
  192. setParams(obj) {
  193. try {
  194. // 合并参数
  195. this.paramsJson = {
  196. ...this.paramsJson,
  197. ...obj
  198. }
  199. } catch (error) {
  200. console.error(error);
  201. }
  202. }
  203. // 组装日志上报固定参数
  204. combinativeParam() {
  205. try {
  206. let params = {
  207. ...this.paramsJson,
  208. };
  209. params.clientVersion = this.version;
  210. // 客户端类型 枚举值赋值
  211. params.clientType = this.enumAssignment(this.CLIENT_TYPE, this.paramsJson.clientType);
  212. params.imageQuality = this.paramsJson.imageQuality;
  213. params.linkScene = this.enumAssignment(this.LINK_SCENE, this.paramsJson.linkScene);
  214. params.videoType = this.enumAssignment(this.VIDEO_TYPE, this.paramsJson.videoType);
  215. return params;
  216. } catch (error) {
  217. console.error(error);
  218. }
  219. }
  220. // 日志记录上报 字符串日志上报
  221. report() {
  222. this.logs.forEach(() => {
  223. this.$Request.post(this.URL_API[location.protocol] + this.URL_ADDRESS, { ...this.logs.shift() })
  224. .then(res => {
  225. console.log('日志上报成功', res);
  226. });
  227. })
  228. }
  229. // 生成or重置定时器
  230. async createTimer() {
  231. await this.clearTimer();
  232. if(this.reportSwitchStatus){
  233. this.timer = setInterval(() => {
  234. if(this.logs.length > 0){
  235. this.report();
  236. }
  237. }, this.timerTime);
  238. }
  239. }
  240. // 清空定时器
  241. async clearTimer() {
  242. this.timer && clearInterval(this.timer);
  243. return true;
  244. }
  245. // 检查是否为枚举值
  246. isCheckEnum(enumObj, velue) {
  247. return Object.values(enumObj).includes(velue);
  248. }
  249. // 使用枚举值赋值
  250. enumAssignment(enumObj, velue) {
  251. try {
  252. let str = '';
  253. if(velue.toString() !== '') {
  254. // 判断是否已为枚举值
  255. str = this.isCheckEnum(enumObj, velue) ? velue : enumObj?.[velue];
  256. }
  257. return str;
  258. } catch (error) {
  259. console.error('enumAssignment内部错误');
  260. console.log(error);
  261. console.dir(error);
  262. console.log('enumObj', enumObj);
  263. console.log('velue', velue);
  264. }
  265. }
  266. // 关闭销毁
  267. async destroy() {
  268. // 清空日志
  269. this.logs = [];
  270. // 关闭日志上报开关
  271. this.reportSwitchStatus = false;
  272. // 清空定时器
  273. await this.clearTimer();
  274. // 移除事件监听
  275. this.netWorkChange(true);
  276. }
  277. /**
  278. * 格式化日期
  279. * @param {Date} date - 要格式化的日期对象
  280. * @param {string} format - 格式化字符串,使用以下占位符:
  281. * - 'Y' 表示年份(4 位数)
  282. * - 'm' 表示月份(2 位数)
  283. * - 'd' 表示日期(2 位数)
  284. * - 'H' 表示小时(24 小时制)
  285. * - 'h' 表示小时(12 小时制)
  286. * - 'i' 表示分钟
  287. * - 's' 表示秒数
  288. * - 'u' 表示毫秒
  289. * - 'a' 表示上午/下午标识(小写)
  290. * - 'A' 表示上午/下午标识(大写)
  291. * @returns {string} 格式化后的日期字符串
  292. */
  293. formatDate(date, format='Y-m-d H:i:s.u') {
  294. // 判断输入是否为 Date 对象
  295. if (!(date instanceof Date)) {
  296. throw new TypeError('The first parameter must be a Date object.');
  297. }
  298. // 定义时间单位的获取方法
  299. const formatObj = {
  300. 'Y': date.getFullYear(), // 年份
  301. 'm': date.getMonth() + 1, // 月份(+1 是因为 getMonth() 返回的月份是从 0 开始的)
  302. 'd': date.getDate(), // 日期
  303. 'H': date.getHours(), // 小时(24 小时制)
  304. 'h': date.getHours() % 12 || 12, // 小时(12 小时制)
  305. 'i': date.getMinutes(), // 分钟
  306. 's': date.getSeconds(), // 秒数
  307. 'u': date.getMilliseconds(), // 毫秒
  308. 'a': date.getHours() >= 12 ? 'pm' : 'am', // 上午/下午标识
  309. 'A': date.getHours() >= 12 ? 'PM' : 'AM' // 上午/下午标识(大写)
  310. };
  311. // 替换格式字符串中的占位符
  312. return format.replace(/Y|m|d|H|h|i|s|u|a|A/g, (match) => {
  313. return formatObj[match];
  314. });
  315. }
  316. }