version.js 2.0 KB
Newer Older
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
/**
 * @class Version 应用版本模型
 */
const BaseMod = require('./base')
const {
	DateTime
} = require('../lib')
module.exports = class Version extends BaseMod {
	constructor() {
		super()
		this.tableName = 'opendb-app-versions'
		this.tablePrefix = false
		this.cacheKeyPre = 'uni-stat-app-version-'
	}

	/**
	 * 获取版本信息
	 * @param {String} appid DCloud-appid
	 * @param {String} platformId 平台编号
	 * @param {String} appVersion 平台版本号
	 */
	async getVersion(appid, platform, appVersion) {
		const cacheKey = this.cacheKeyPre + appid + '-' + platform + '-' + appVersion
		let versionData = await this.getCache(cacheKey)
		if (!versionData) {
			const versionInfo = await this.getCollection(this.tableName).where({
				appid: appid,
				uni_platform: platform,
				type: 'native_app',
				version: appVersion
			}).limit(1).get()
			versionData = []
			if (versionInfo.data.length > 0) {
				versionData = versionInfo.data[0]
				await this.setCache(cacheKey, versionData)
			}
		}
		return versionData
	}


	/**
	 * 获取版本信息没有则进行创建
	 * @param {String} appid DCloud-appid
	 * @param {String} platform 平台代码
	 * @param {String} appVersion 平台版本号
	 */
	async getVersionAndCreate(appid, platform, appVersion) {
		const versionInfo = await this.getVersion(appid, platform, appVersion)
		if (versionInfo.length === 0) {
			if (appVersion.length > 0 && !appVersion.includes('}')) {
				const thisTime = new DateTime().getTime()
				const insertParam = {
					appid: appid,
					platform: [],
					uni_platform: platform,
					type: 'native_app',
					version: appVersion,
					stable_publish: false,
					create_env: 'uni-stat',
					create_date: thisTime
				}
				const res = await this.insert(this.tableName, insertParam)
				if (res && res.id) {
					return Object.assign(insertParam, {
						_id: res.id
					})
				}
			}
		}
		return versionInfo
	}
}