channel.js 2.7 KB

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