platform.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @class Platform 应用平台模型
  3. */
  4. const BaseMod = require('./base')
  5. const {
  6. DateTime
  7. } = require('../lib')
  8. module.exports = class Platform extends BaseMod {
  9. constructor() {
  10. super()
  11. this.tableName = 'app-platforms'
  12. }
  13. /**
  14. * 获取平台信息
  15. * @param {String} platform 平台代码
  16. * @param {String} os 系统
  17. */
  18. async getPlatform(platform, os) {
  19. const cacheKey = 'uni-stat-platform-' + platform + '-' + os
  20. let platformData = await this.getCache(cacheKey)
  21. if (!platformData) {
  22. const platformCode = this.getPlatformCode(platform, os)
  23. const platformInfo = await this.getCollection(this.tableName).where({
  24. code: platformCode
  25. }).limit(1).get()
  26. platformData = []
  27. if (platformInfo.data.length > 0) {
  28. platformData = platformInfo.data[0]
  29. await this.setCache(cacheKey, platformData)
  30. }
  31. }
  32. return platformData
  33. }
  34. /**
  35. * 获取平台信息没有则创建
  36. * @param {String} platform 平台代码
  37. * @param {String} os 系统
  38. */
  39. async getPlatformAndCreate(platform, os) {
  40. if (!platform) {
  41. return false
  42. }
  43. const platformInfo = await this.getPlatform(platform, os)
  44. if (platformInfo.length === 0) {
  45. const platformCode = this.getPlatformCode(platform, os)
  46. const insertParam = {
  47. code: platformCode,
  48. name: platformCode,
  49. create_time: new DateTime().getTime()
  50. }
  51. const res = await this.insert(this.tableName, insertParam)
  52. if (res && res.id) {
  53. return Object.assign(insertParam, {
  54. _id: res.id
  55. })
  56. }
  57. }
  58. return platformInfo
  59. }
  60. /**
  61. * 获取平台代码
  62. * @param {String} platform 平台代码
  63. * @param {String} os 系统
  64. */
  65. getPlatformCode(platform, os) {
  66. let platformCode = platform
  67. //兼容客户端上报参数
  68. switch(platform) {
  69. //h5|web
  70. case 'h5':
  71. platformCode = 'web'
  72. break
  73. //微信小程序
  74. case 'wx':
  75. platformCode = 'mp-weixin'
  76. break
  77. //百度小程序
  78. case 'bd':
  79. platformCode = 'mp-baidu'
  80. break
  81. //支付宝小程序
  82. case 'ali':
  83. platformCode = 'mp-alipay'
  84. break
  85. //字节跳动小程序
  86. case 'tt':
  87. platformCode = 'mp-toutiao'
  88. break
  89. //qq小程序
  90. case 'qq':
  91. platformCode = 'mp-qq'
  92. break
  93. //快应用联盟
  94. case 'qn':
  95. platformCode = 'quickapp-webview-union'
  96. break
  97. //快应用(webview)
  98. case 'qw':
  99. platformCode = 'quickapp-webview'
  100. break
  101. //快应用华为
  102. case 'qi':
  103. platformCode = 'quickapp-webview-huawei'
  104. break
  105. //360小程序
  106. case '360':
  107. platformCode = 'mp-360'
  108. break
  109. //京东小程序
  110. case 'jd':
  111. platformCode = 'mp-jd'
  112. break
  113. //钉钉小程序
  114. case 'dt':
  115. platformCode = 'mp-dingtalk'
  116. break
  117. //快手小程序
  118. case 'ks':
  119. platformCode = 'mp-kuaishou'
  120. break
  121. //飞书小程序
  122. case 'lark':
  123. platformCode = 'mp-lark'
  124. break
  125. //原生应用
  126. case 'n':
  127. case 'app-plus':
  128. case 'app':
  129. os = this.getOsName(os)
  130. if (os === 'ios') {
  131. platformCode = 'ios'
  132. } else {
  133. platformCode = 'android'
  134. }
  135. break
  136. }
  137. return platformCode
  138. }
  139. /**
  140. * 获取系统名称
  141. * @param {Object} os系统标识
  142. */
  143. getOsName(os) {
  144. if(!os) {
  145. return ''
  146. }
  147. //兼容老版上报参数
  148. const osSetting = {
  149. i: 'ios',
  150. a: 'android'
  151. }
  152. return osSetting[os] ? osSetting[os] : os
  153. }
  154. }