scenes.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @class Scenes 场景值模型
  3. */
  4. const BaseMod = require('./base')
  5. const Platform = require('./platform')
  6. module.exports = class Scenes extends BaseMod {
  7. constructor() {
  8. super()
  9. this.tableName = 'mp-scenes'
  10. this.defualtCode = '1001'
  11. }
  12. /**
  13. * 获取场景值
  14. * @param {String} platform 平台代码
  15. * @param {String} code 场景值代码
  16. */
  17. async getScenes(platform, code) {
  18. const cacheKey = 'uni-stat-scenes-' + platform + '-' + code
  19. let scenesData = await this.getCache(cacheKey)
  20. if (!scenesData) {
  21. const scenesInfo = await this.getCollection(this.tableName).where({
  22. platform: platform,
  23. scene_code: code
  24. }).limit(1).get()
  25. scenesData = []
  26. if (scenesInfo.data.length > 0) {
  27. scenesData = scenesInfo.data[0]
  28. await this.setCache(cacheKey, scenesData)
  29. }
  30. }
  31. return scenesData
  32. }
  33. /**
  34. * 通过平台编号获取场景值
  35. * @param {String} platformId 平台编号
  36. * @param {String} code 场景值代码
  37. */
  38. async getScenesByPlatformId(platformId, code) {
  39. const platform = new Platform()
  40. let platformInfo = await this.getCollection(platform.tableName).where({
  41. _id: platformId
  42. }).limit(1).get()
  43. let scenesData
  44. if (platformInfo.data.length > 0) {
  45. platformInfo = platformInfo.data[0]
  46. scenesData = await this.getScenes(platformInfo.code, code)
  47. } else {
  48. scenesData = []
  49. }
  50. return scenesData
  51. }
  52. /**
  53. * 获取场景值名称
  54. * @param {String} platform 平台代码
  55. * @param {String} code 场景值代码
  56. */
  57. async getScenesName(platform, code) {
  58. const scenesData = await this.getScenes(platform, code)
  59. if (scenesData.length === 0) {
  60. return ''
  61. }
  62. return scenesData.scene_name
  63. }
  64. /**
  65. * 通过平台编号获取场景值名称
  66. * @param {String} platformId 平台编号
  67. * @param {String} code 场景值代码
  68. */
  69. async getScenesNameByPlatformId(platformId, code) {
  70. const scenesData = await this.getScenesByPlatformId(platformId, code)
  71. if (scenesData.length === 0) {
  72. return code === this.defualtCode ? '默认' : ''
  73. }
  74. return scenesData.scene_name
  75. }
  76. }