integralConfig.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. class integralConfigService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.model = this.ctx.model.IntegralConfig;
  8. }
  9. async create({ service, module, method }) {
  10. try {
  11. const res = await this.model.create({ service, module, method });
  12. return { errcode: 0, errmsg: 'ok', data: res };
  13. } catch (error) {
  14. throw error;
  15. }
  16. }
  17. async update({ id, service, module, method }) {
  18. assert(id, 'id不存在');
  19. try {
  20. await this.model.updateOne({ _id: id }, { service, module, method });
  21. return { errcode: 0, errmsg: 'ok', data: '' };
  22. } catch (error) {
  23. throw error;
  24. }
  25. }
  26. async delete({ id }) {
  27. assert(id, 'id不存在');
  28. try {
  29. await this.model.deleteOne({ _id: id });
  30. return { errcode: 0, errmsg: 'ok', data: '' };
  31. } catch (error) {
  32. throw error;
  33. }
  34. }
  35. async query({ skip, limit, service, module, method }) {
  36. const filter = {};
  37. const arr = { service, module, method };
  38. for (const e in arr) {
  39. const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  40. if (arr[e]) {
  41. filter.$or = [];
  42. filter.$or.push(JSON.parse(data));
  43. }
  44. }
  45. try {
  46. const total = await this.model.find({ ...filter });
  47. let res;
  48. if (skip && limit) {
  49. res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  50. } else {
  51. res = await this.model.find({ ...filter });
  52. }
  53. return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
  54. } catch (error) {
  55. throw error;
  56. }
  57. }
  58. }
  59. module.exports = integralConfigService;