configs.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. const BaseService = require('./base.js');
  3. // 配置类
  4. class ConfigsService extends BaseService {
  5. /**
  6. * [cacheKey 缓存键]
  7. * @return {[type]} [description]
  8. */
  9. get cacheKey() {
  10. return `${process.env.APP_CUSTOME || 'universal'}_configs`;
  11. }
  12. /**
  13. * [cacheTime 缓存时间]
  14. * @return {[type]} [description]
  15. */
  16. get cacheTime() {
  17. return 10 * 60;
  18. }
  19. /**
  20. * [getConfig 获取单行配置]
  21. * @author szjcomo
  22. * @date 2021-03-20
  23. * @param {[type]} field_index [description]
  24. * @return {[type]} [description]
  25. */
  26. async getConfig(field_index = null) {
  27. const that = this;
  28. const configs = await that.getConfigAll();
  29. let result = false;
  30. configs.forEach(item => {
  31. if (item.field_index === field_index) result = item;
  32. });
  33. return result;
  34. }
  35. /**
  36. * [getConfigValue 只获配置项的值]
  37. * @author szjcomo
  38. * @date 2021-03-20
  39. * @param {[type]} field_index [description]
  40. * @return {[type]} [description]
  41. */
  42. async getConfigValue(field_index = null) {
  43. const that = this;
  44. const configs = await that.getConfigAll();
  45. let result = null;
  46. configs.forEach(item => {
  47. if (item.field_index === field_index) result = item.field_value;
  48. });
  49. return result;
  50. }
  51. /**
  52. * [getConfigMoreValue 获取多个配置的值]
  53. * @author szjcomo
  54. * @date 2021-03-20
  55. * @param {Array} keys [description]
  56. * @return {[type]} [description]
  57. */
  58. async getConfigMoreValue(keys = []) {
  59. const that = this;
  60. const configs = await that.getConfigAll();
  61. const result = {};
  62. configs.forEach(item => {
  63. keys.forEach(childItem => {
  64. if (childItem === item.field_index) {
  65. result[childItem] = item.field_value;
  66. }
  67. });
  68. });
  69. return result;
  70. }
  71. /**
  72. * 获取配置的分佣比例数值
  73. * @return {Promise<number>}
  74. */
  75. async getCommissionRatio() {
  76. const that = this;
  77. const config = await that.getConfigMoreValue([ 'shop_commission_ratio' ]);
  78. if (config.shop_commission_ratio) {
  79. return Number(config.shop_commission_ratio);
  80. }
  81. return 0;
  82. }
  83. /**
  84. * [getConfigGroup 获取一组配置项]
  85. * @author szjcomo
  86. * @date 2021-03-20
  87. * @param {[type]} group [description]
  88. * @return {[type]} [description]
  89. */
  90. async getConfigGroup(group = null) {
  91. const that = this;
  92. const configs = await that.getConfigAll();
  93. const result = [];
  94. configs.forEach(item => {
  95. if (item.field_group === group) result.push(item);
  96. });
  97. return result;
  98. }
  99. /**
  100. * [getConfigAll 获取所有配置]
  101. * @author szjcomo
  102. * @date 2021-03-20
  103. * @return {[type]} [description]
  104. */
  105. async getConfigAll() {
  106. const that = this;
  107. let result = await that.service.redis.get(that.cacheKey);
  108. if (!result) {
  109. result = await that.app.model.Configs.findAll({
  110. attributes: {
  111. exclude: [ 'create_time', 'update_time', 'admin_id', 'field_sort', 'field_desc', 'field_options' ],
  112. }, where: { field_status: 1 },
  113. });
  114. if (result) await that.service.redis.set(that.cacheKey, result, that.cacheTime);
  115. }
  116. return result;
  117. }
  118. /**
  119. * [clear 清除配置缓存]
  120. * @author szjcomo
  121. * @date 2021-03-20
  122. * @return {[type]} [description]
  123. */
  124. async clear() {
  125. const that = this;
  126. const res = await that.service.redis.del(that.cacheKey);
  127. return res;
  128. }
  129. }
  130. module.exports = ConfigsService;