receiver.js 2.6 KB

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