channel.js 2.7 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
/**
 * @class Channel 渠道模型
 */
const BaseMod = require('./base')
const Scenes = require('./scenes')
const {
	DateTime
} = require('../lib')
module.exports = class Channel extends BaseMod {
	constructor() {
		super()
		this.tableName = 'app-channels'
		this.scenes = new Scenes()
	}

	/**
	 * 获取渠道信息
	 * @param {String} appid
	 * @param {String} platformId 平台编号
	 * @param {String} channel 渠道代码
	 */
	async getChannel(appid, platformId, channel) {
		const cacheKey = 'uni-stat-channel-' + appid + '-' + platformId + '-' + channel
		let channelData = await this.getCache(cacheKey)
		if (!channelData) {
			const channelInfo = await this.getCollection(this.tableName).where({
				appid: appid,
				platform_id: platformId,
				channel_code: channel
			}).limit(1).get()
			channelData = []
			if (channelInfo.data.length > 0) {
				channelData = channelInfo.data[0]
				if (channelData.channel_name === '') {
					const scenesName = await this.scenes.getScenesNameByPlatformId(platformId, channel)
					if (scenesName) {
						await this.update(this.tableName, {
							channel_name: scenesName,
							update_time: new DateTime().getTime()
						}, {
							_id: channelData._id
						})
					}
				}
				await this.setCache(cacheKey, channelData)
			}
		}
		return channelData
	}

	/**
	 * 获取渠道信息没有则进行创建
	 * @param {String} appid
	 * @param {String} platformId
	 * @param {String} channel
	 */
	async getChannelAndCreate(appid, platformId, channel) {
		if (!appid || !platformId) {
			return []
		}

		const channelInfo = await this.getChannel(appid, platformId, channel)
		if (channelInfo.length === 0) {
			const thisTime = new DateTime().getTime()
			const insertParam = {
				appid: appid,
				platform_id: platformId,
				channel_code: channel,
				channel_name: await this.scenes.getScenesNameByPlatformId(platformId, channel),
				create_time: thisTime,
				update_time: thisTime
			}
			const res = await this.insert(this.tableName, insertParam)
			if (res && res.id) {
				return Object.assign(insertParam, {
					_id: res.id
				})
			}
		}
		return channelInfo
	}

	/**
	 * 获取渠道_id
	 * @param {String} appid
	 * @param {String} platformId
	 * @param {String} channel
	 */
	async getChannelId(appid, platformId, channel) {
		const channelInfo = await this.getChannel(appid, platformId, channel)
		return channelInfo.length > 0 ? channelInfo._id : ''
	}

	/**
	 * 获取渠道码或者场景值
	 * @param {Object} params 上报参数
	 */
	getChannelCode(params) {
		//小程序未上报渠道则使用场景值
		if (params.ch) {
			return params.ch
		} else if (params.sc && params.ut.indexOf('mp-') === 0) {
			return params.sc
		}
		return this.scenes.defualtCode
	}
}