channel.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @class Channel 渠道模型
  3. */
  4. const BaseMod = require('./base')
  5. const Scenes = require('./scenes')
  6. const Platform = require('./platform')
  7. const {
  8. DateTime
  9. } = require('../lib')
  10. module.exports = class Channel extends BaseMod {
  11. constructor() {
  12. super()
  13. this.tableName = 'app-channels'
  14. this.scenes = new Scenes()
  15. }
  16. /**
  17. * 获取渠道信息
  18. * @param {String} appid
  19. * @param {String} platformId 平台编号
  20. * @param {String} channel 渠道代码
  21. */
  22. async getChannel(appid, platformId, channel) {
  23. const cacheKey = 'uni-stat-channel-' + appid + '-' + platformId + '-' + channel
  24. let channelData = await this.getCache(cacheKey)
  25. if (!channelData) {
  26. const channelInfo = await this.getCollection(this.tableName).where({
  27. appid: appid,
  28. platform_id: platformId,
  29. channel_code: channel
  30. }).limit(1).get()
  31. channelData = []
  32. if (channelInfo.data.length > 0) {
  33. channelData = channelInfo.data[0]
  34. if (channelData.channel_name === '') {
  35. const scenesName = await this.scenes.getScenesNameByPlatformId(platformId, channel)
  36. if (scenesName) {
  37. await this.update(this.tableName, {
  38. channel_name: scenesName,
  39. update_time: new DateTime().getTime()
  40. }, {
  41. _id: channelData._id
  42. })
  43. }
  44. }
  45. await this.setCache(cacheKey, channelData)
  46. }
  47. }
  48. return channelData
  49. }
  50. /**
  51. * 获取渠道信息没有则进行创建
  52. * @param {String} appid
  53. * @param {String} platformId
  54. * @param {String} channel
  55. */
  56. async getChannelAndCreate(appid, platformId, channel) {
  57. if (!appid || !platformId) {
  58. return []
  59. }
  60. const channelInfo = await this.getChannel(appid, platformId, channel)
  61. if (channelInfo.length === 0) {
  62. const thisTime = new DateTime().getTime()
  63. const insertParam = {
  64. appid: appid,
  65. platform_id: platformId,
  66. channel_code: channel,
  67. channel_name: await this.scenes.getScenesNameByPlatformId(platformId, channel),
  68. create_time: thisTime,
  69. update_time: thisTime
  70. }
  71. const res = await this.insert(this.tableName, insertParam)
  72. if (res && res.id) {
  73. return Object.assign(insertParam, {
  74. _id: res.id
  75. })
  76. }
  77. }
  78. return channelInfo
  79. }
  80. /**
  81. * 获取渠道_id
  82. * @param {String} appid
  83. * @param {String} platformId
  84. * @param {String} channel
  85. */
  86. async getChannelId(appid, platformId, channel) {
  87. const channelInfo = await this.getChannel(appid, platformId, channel)
  88. return channelInfo.length > 0 ? channelInfo._id : ''
  89. }
  90. /**
  91. * 获取渠道码或者场景值
  92. * @param {Object} params 上报参数
  93. */
  94. getChannelCode(params) {
  95. //小程序未上报渠道则使用场景值
  96. const platform = new Platform()
  97. const platformCode = platform.getPlatformCode(params.ut, params.p)
  98. if (params.ch) {
  99. return params.ch
  100. } else if (params.sc && platformCode.indexOf('mp-') === 0) {
  101. return params.sc
  102. }
  103. return this.scenes.defualtCode
  104. }
  105. }