draft.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. class DrafttService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.model = this.ctx.model.Draft;
  8. }
  9. async create({ title, name, phone, workUnit, address, url, openid, status }) {
  10. assert(title, '标题不存在');
  11. assert(name, '姓名不存在');
  12. assert(phone, '电话不存在');
  13. assert(workUnit, '单位不存在');
  14. assert(address, '地址不存在');
  15. assert(openid, '用户ID不存在');
  16. assert(status, '状态不存在');
  17. try {
  18. const res = await this.model.create({ title, name, phone, workUnit, address, url, openid, status });
  19. return { errcode: 0, errmsg: 'ok', data: res };
  20. } catch (error) {
  21. throw error;
  22. }
  23. }
  24. async update({ id, title, name, phone, workUnit, address, url, openid, status }) {
  25. assert(id, 'id不存在');
  26. try {
  27. await this.model.updateOne({ _id: id }, { title, name, phone, workUnit, address, url, openid, status });
  28. return { errcode: 0, errmsg: 'ok', data: '' };
  29. } catch (error) {
  30. throw error;
  31. }
  32. }
  33. async delete({ id }) {
  34. assert(id, 'id不存在');
  35. try {
  36. await this.model.deleteOne({ _id: id });
  37. return { errcode: 0, errmsg: 'ok', data: '' };
  38. } catch (error) {
  39. throw error;
  40. }
  41. }
  42. async query({ skip, limit, title, name, phone, status }) {
  43. const filter = {};
  44. const arr = { name, title, phone, status };
  45. for (const e in arr) {
  46. const data = `{ "${e}": { "$regex": "${arr[e]}" } }`;
  47. if (arr[e]) {
  48. filter.$or = [];
  49. filter.$or.push(JSON.parse(data));
  50. }
  51. }
  52. try {
  53. const total = await this.model.find({ ...filter });
  54. let res;
  55. if (skip && limit) {
  56. res = await this.model.find({ ...filter }).skip(Number(skip) * Number(limit)).limit(Number(limit));
  57. } else {
  58. res = await this.model.find({ ...filter });
  59. }
  60. return { errcode: 0, errmsg: 'ok', data: res, total: total.length };
  61. } catch (error) {
  62. throw error;
  63. }
  64. }
  65. }
  66. module.exports = DrafttService;