stats-service.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. class DataHandler {
  2. constructor() {
  3. this[CONSTANTS.KEY_MCH_SERVICE_RATE] = [];
  4. this.structureMapping = {
  5. // mch_service_rate: 机构id(mchid), 日期(day), 服务费比率(service_rate)
  6. [CONSTANTS.KEY_MCH_SERVICE_RATE]: ['mchid', 'day', 'service_rate'],
  7. };
  8. }
  9. translateToIndex(structure, data) {
  10. return this.structureMapping[structure].map((key, index) => data[key]);
  11. }
  12. translateToField(structure, data) {
  13. let translatedData = {};
  14. this.structureMapping[structure].forEach((key, index) => {
  15. translatedData[key] = data[index];
  16. });
  17. return translatedData;
  18. }
  19. getServiceRateList() {
  20. const list = [], that = this;
  21. this[CONSTANTS.KEY_MCH_SERVICE_RATE].forEach(v => {
  22. list.push(that.translateToField(CONSTANTS.KEY_MCH_SERVICE_RATE, v));
  23. });
  24. return list;
  25. }
  26. async addServiceRate(structure, newData, backend = true) {
  27. const list = this[structure];
  28. const translatedData = this.translateToIndex(structure, newData);
  29. const mchid = translatedData[0];
  30. const day = translatedData[1];
  31. let mchidData = list.filter(item => item[0] === mchid);
  32. const index = mchidData.findIndex(item => item[1] === day);
  33. if (index !== -1) {
  34. mchidData[index] = translatedData;
  35. } else {
  36. mchidData.push(translatedData);
  37. }
  38. if (mchidData.length > 90) {
  39. mchidData.shift();
  40. }
  41. this[structure] = list.filter(item => item[0] !== mchid);
  42. this[structure] = [...this[structure], ...mchidData];
  43. if (backend === true) {
  44. await this.saveDataToBackend(structure);
  45. }
  46. }
  47. async removeData(structure, mchid, day) {
  48. let list = this[structure];
  49. const index = list.findIndex(item => item[0] === mchid && item[1] === day);
  50. if (index !== -1) {
  51. list.splice(index, 1);
  52. }
  53. }
  54. async saveDataToBackend(structure) {
  55. this.checkAndFixData(structure);
  56. const compressedData = this.compressData(structure);
  57. await statsApi.setStatsSettings(structure, compressedData);
  58. }
  59. checkAndFixData(structure) {
  60. let list = this[structure];
  61. let structureLength = this.structureMapping[structure].length;
  62. list.forEach(item => {
  63. if (item.length < structureLength) {
  64. for (let i = item.length; i < structureLength; i++) {
  65. item.push('');
  66. }
  67. }
  68. });
  69. }
  70. compressData(structure) {
  71. return this[structure];
  72. }
  73. decompressData(structure, data) {
  74. this[structure] = data[structure] || [];
  75. this.checkAndFixData(structure);
  76. }
  77. async loadServiceRate() {
  78. const data = await statsApi.getStatsSettings([CONSTANTS.KEY_MCH_SERVICE_RATE]);
  79. if (data.state !== true) {
  80. showErr('网络异常,请刷新页面重试');
  81. return;
  82. }
  83. this.decompressData(CONSTANTS.KEY_MCH_SERVICE_RATE, data);
  84. }
  85. }
  86. class ServicefeeApiService extends BaseService {
  87. getStatsSettings(keys = []) {
  88. const endpoint = '?act=refill_service_stats&op=stats_settings&key=' + keys.join(',');
  89. return this.request(endpoint, 'GET', {});
  90. };
  91. setStatsSettings(key, value) {
  92. const endpoint = '?act=refill_service_stats&op=stats_settings_save';
  93. let data = {
  94. key: key,
  95. value: value
  96. };
  97. return this.request(endpoint, 'POST', data);
  98. };
  99. }
  100. const statsApi = new ServicefeeApiService('');
  101. const dataHelper = new DataHandler();