receiver.js 2.6 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/**
 * @class UniStatReportDataReceiver uni统计上报数据接收器
 * @function report 上报数据调度处理函数
 */
const {
	parseUrlParams
} = require('../shared')
const SessionLog = require('./mod/sessionLog')
const PageLog = require('./mod/pageLog')
const EventLog = require('./mod/eventLog')
const ErrorLog = require('./mod/errorLog')
const Device = require('./mod/device')
class UniStatReportDataReceiver {
	/**
	 * @description 上报数据调度处理函数
	 * @param {Object} params 基础上报参数
	 * @param {Object} context 请求附带的上下文信息
	 */
	async report(params, context) {
		let res = {
			code: 0,
			msg: 'success'
		}

		if (!params || !params.requests) {
			return {
				code: 200,
				msg: 'Invild params'
			}
		}

		// JSON参数解析
		const requestParam = JSON.parse(params.requests)
		if (!requestParam || requestParam.length === 0) {
			return {
				code: 200,
				msg: 'Invild params'
			}
		}

		// 日志填充
		const sessionParams = []
		const pageParams = []
		const eventParams = []
		const errorParams = []
		const device = new Device()
		for (const ri in requestParam) {
			//参数解析
			const urlParams = parseUrlParams(requestParam[ri], context)
			if (!urlParams.ak) {
				return {
					code: 201,
					msg: 'Not found appid'
				}
			}

			if (!urlParams.lt) {
				return {
					code: 202,
					msg: 'Not found this log type'
				}
			}

			switch (parseInt(urlParams.lt)) {
				// 会话日志
				case 1: {
					sessionParams.push(urlParams)
					break
				}
				// 页面日志
				case 3:
				case 11: {
					pageParams.push(urlParams)
					break
				}
				// 事件日志
				case 21: {
					eventParams.push(urlParams)
					break
				}
				// 错误日志
				case 31: {
					errorParams.push(urlParams)
					break
				}
				//unipush信息绑定
				case 101: {
					res = await device.bindPush(urlParams)
					break
				}
				default: {
					console.log('Invalid type by param "lt:' + urlParams.lt + '"')
					break
				}
			}
		}

		//会话日志填充
		if (sessionParams.length > 0) {
			const sessionLog = new SessionLog()
			res = await sessionLog.batchFill(sessionParams)
		}

		//页面日志填充
		if (pageParams.length > 0) {
			const pageLog = new PageLog()
			res = await pageLog.fill(pageParams)
		}

		//事件日志填充
		if (eventParams.length > 0) {
			const eventLog = new EventLog()
			res = await eventLog.fill(eventParams)
		}

		//错误日志填充
		if (errorParams.length > 0) {
			const errorLog = new ErrorLog()
			res = await errorLog.fill(errorParams)
		}

		return res
	}
}

module.exports = UniStatReportDataReceiver