platform.js 3.2 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
/**
 * @class Platform 应用平台模型
 */
const BaseMod = require('./base')
const {
	DateTime
} = require('../lib')
module.exports = class Platform extends BaseMod {
	constructor() {
		super()
		this.tableName = 'app-platforms'
	}

	/**
	 * 获取平台信息
	 * @param {String} platform 平台代码
	 * @param {String} os 系统
	 */
	async getPlatform(platform, os) {
		const cacheKey = 'uni-stat-platform-' + platform + '-' + os
		let platformData = await this.getCache(cacheKey)
		if (!platformData) {
			const platformCode = this.getPlatformCode(platform, os)
			const platformInfo = await this.getCollection(this.tableName).where({
				code: platformCode
			}).limit(1).get()
			platformData = []
			if (platformInfo.data.length > 0) {
				platformData = platformInfo.data[0]
				await this.setCache(cacheKey, platformData)
			}
		}
		return platformData
	}

	/**
	 * 获取平台信息没有则创建
	 * @param {String} platform 平台代码
	 * @param {String} os 系统
	 */
	async getPlatformAndCreate(platform, os) {
		if (!platform) {
			return false
		}
		const platformInfo = await this.getPlatform(platform, os)

		if (platformInfo.length === 0) {
			const platformCode = this.getPlatformCode(platform, os)
			const insertParam = {
				code: platformCode,
				name: platformCode,
				create_time: new DateTime().getTime()
			}
			const res = await this.insert(this.tableName, insertParam)
			if (res && res.id) {
				return Object.assign(insertParam, {
					_id: res.id
				})
			}
		}
		return platformInfo
	}

	/**
	 * 获取平台代码
	 * @param {String} platform 平台代码
	 * @param {String} os 系统
	 */
	getPlatformCode(platform, os) {
		let platformCode = platform

		//兼容客户端上报参数
		switch(platform) {
			//h5|web
			case 'h5':
				platformCode = 'web'
				break
			//微信小程序
			case 'wx':
				platformCode = 'mp-weixin'
				break
			//百度小程序
			case 'bd':
				platformCode = 'mp-baidu'
				break
			//支付宝小程序
			case 'ali':
				platformCode = 'mp-alipay'
				break
			//字节跳动小程序
			case 'tt':
				platformCode = 'mp-toutiao'
				break
			//qq小程序
			case 'qq':
				platformCode = 'mp-qq'
				break
			//快应用联盟
			case 'qn':
				platformCode = 'quickapp-webview-union'
				break
			//快应用(webview)
			case 'qw':
				platformCode = 'quickapp-webview'
				break
			//快应用华为
			case 'qi':
				platformCode = 'quickapp-webview-huawei'
				break
			//360小程序
			case '360':
				platformCode = 'mp-360'
				break
			//京东小程序
			case 'jd':
				platformCode = 'mp-jd'
				break
			//钉钉小程序
			case 'dt':
				platformCode = 'mp-dingtalk'
				break
			//快手小程序
			case 'ks':
				platformCode = 'mp-kuaishou'
				break
			//飞书小程序
			case 'lark':
				platformCode = 'mp-lark'
				break
			//原生应用
			case 'n':
			case 'app-plus':
			case 'app':
				os = this.getOsName(os)
				if (os === 'ios') {
					platformCode = 'ios'
				} else {
					platformCode = 'android'
				}
				break
		}
		return platformCode
	}

	/**
	 * 获取系统名称
	 * @param {Object} os系统标识
	 */
	getOsName(os) {
		if(!os) {
			return ''
		}
		//兼容老版上报参数
		const osSetting = {
			i: 'ios',
			a: 'android'
		}
		return osSetting[os] ? osSetting[os] : os
	}
}