version.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @class Version 应用版本模型
  3. */
  4. const BaseMod = require('./base')
  5. const {
  6. DateTime
  7. } = require('../lib')
  8. module.exports = class Version extends BaseMod {
  9. constructor() {
  10. super()
  11. this.tableName = 'opendb-app-versions'
  12. this.tablePrefix = false
  13. this.cacheKeyPre = 'uni-stat-app-version-'
  14. }
  15. /**
  16. * 获取版本信息
  17. * @param {String} appid DCloud-appid
  18. * @param {String} platformId 平台编号
  19. * @param {String} appVersion 平台版本号
  20. */
  21. async getVersion(appid, platform, appVersion) {
  22. const cacheKey = this.cacheKeyPre + appid + '-' + platform + '-' + appVersion
  23. let versionData = await this.getCache(cacheKey)
  24. if (!versionData) {
  25. const versionInfo = await this.getCollection(this.tableName).where({
  26. appid: appid,
  27. uni_platform: platform,
  28. type: 'native_app',
  29. version: appVersion
  30. }).limit(1).get()
  31. versionData = []
  32. if (versionInfo.data.length > 0) {
  33. versionData = versionInfo.data[0]
  34. await this.setCache(cacheKey, versionData)
  35. }
  36. }
  37. return versionData
  38. }
  39. /**
  40. * 获取版本信息没有则进行创建
  41. * @param {String} appid DCloud-appid
  42. * @param {String} platform 平台代码
  43. * @param {String} appVersion 平台版本号
  44. */
  45. async getVersionAndCreate(appid, platform, appVersion) {
  46. const versionInfo = await this.getVersion(appid, platform, appVersion)
  47. if (versionInfo.length === 0) {
  48. if (appVersion.length > 0 && !appVersion.includes('}')) {
  49. const thisTime = new DateTime().getTime()
  50. const insertParam = {
  51. appid: appid,
  52. platform: [],
  53. uni_platform: platform,
  54. type: 'native_app',
  55. version: appVersion,
  56. stable_publish: false,
  57. create_env: 'uni-stat',
  58. create_date: thisTime
  59. }
  60. const res = await this.insert(this.tableName, insertParam)
  61. if (res && res.id) {
  62. return Object.assign(insertParam, {
  63. _id: res.id
  64. })
  65. }
  66. }
  67. }
  68. return versionInfo
  69. }
  70. }