receiver.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @class UniStatReportDataReceiver uni统计上报数据接收器
  3. * @function report 上报数据调度处理函数
  4. */
  5. const {parseUrlParams} = require('../shared')
  6. const SessionLog = require('./mod/sessionLog')
  7. const PageLog = require('./mod/pageLog')
  8. const EventLog = require('./mod/eventLog')
  9. const ErrorLog = require('./mod/errorLog')
  10. const Device = require('./mod/device')
  11. class UniStatReportDataReceiver {
  12. /**
  13. * @description 上报数据调度处理函数
  14. * @param {Object} params 基础上报参数
  15. * @param {Object} context 请求附带的上下文信息
  16. */
  17. async report(params, context) {
  18. let res = {
  19. code: 0,
  20. msg: 'success'
  21. }
  22. if (!params || !params.requests) {
  23. return {
  24. code: 200,
  25. msg: 'Invild params'
  26. }
  27. }
  28. // JSON参数解析
  29. const requestParam = JSON.parse(params.requests)
  30. if (!requestParam || requestParam.length === 0) {
  31. return {
  32. code: 200,
  33. msg: 'Invild params'
  34. }
  35. }
  36. // 日志填充
  37. const sessionParams = []
  38. const pageParams = []
  39. const eventParams = []
  40. const errorParams = []
  41. const device = new Device()
  42. for (const ri in requestParam) {
  43. //参数解析
  44. const urlParams = parseUrlParams(requestParam[ri], context)
  45. if (!urlParams.ak) {
  46. return {
  47. code: 201,
  48. msg: 'Not found appid'
  49. }
  50. }
  51. if (!urlParams.lt) {
  52. return {
  53. code: 202,
  54. msg: 'Not found this log type'
  55. }
  56. }
  57. switch (parseInt(urlParams.lt)) {
  58. // 会话日志
  59. case 1: {
  60. sessionParams.push(urlParams)
  61. break
  62. }
  63. // // 页面日志
  64. // case 3:
  65. // case 11: {
  66. // pageParams.push(urlParams)
  67. // break
  68. // }
  69. // 事件日志
  70. case 21: {
  71. eventParams.push(urlParams)
  72. break
  73. }
  74. // 错误日志
  75. case 31: {
  76. errorParams.push(urlParams)
  77. break
  78. }
  79. //unipush信息绑定
  80. case 101: {
  81. res = await device.bindPush(urlParams)
  82. break
  83. }
  84. default: {
  85. console.log('Invalid type by param "lt:' + urlParams.lt + '"')
  86. break
  87. }
  88. }
  89. }
  90. //会话日志填充
  91. if (sessionParams.length > 0) {
  92. const sessionLog = new SessionLog()
  93. res = await sessionLog.batchFill(sessionParams)
  94. }
  95. //页面日志填充
  96. if (pageParams.length > 0) {
  97. const pageLog = new PageLog()
  98. res = await pageLog.fill(pageParams)
  99. }
  100. //事件日志填充
  101. if (eventParams.length > 0) {
  102. const eventLog = new EventLog()
  103. res = await eventLog.fill(eventParams)
  104. }
  105. //错误日志填充
  106. if (errorParams.length > 0) {
  107. const errorLog = new ErrorLog()
  108. res = await errorLog.fill(errorParams)
  109. }
  110. return res
  111. }
  112. }
  113. module.exports = UniStatReportDataReceiver