opera.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const _ = require('lodash');
  2. export default {
  3. //执行查询
  4. async search({ skip = 0, limit = this.$limit || 10, ...others } = {}) {
  5. let query = { skip, limit, ...others };
  6. if (this.searchInfo && Object.keys(this.searchInfo).length > 0) query = { ...query, ...this.searchInfo };
  7. if (this.defaultSearch && Object.keys(this.defaultSearch).length > 0) query = { ...query, ...this.defaultSearch };
  8. const res = await this.query(query);
  9. if (this.$checkRes(res)) {
  10. this.$set(this, `list`, res.data);
  11. this.$set(this, `total`, res.total);
  12. }
  13. },
  14. // 去添加
  15. async toAdd() {
  16. this.form = {};
  17. this.initAddData();
  18. this.view = 'info';
  19. },
  20. // 初始化添加数据,重写用
  21. initAddData() {},
  22. // 去编辑
  23. async toEdit({ data }) {
  24. const res = await this.fetch(data._id);
  25. if (this.$checkRes(res)) {
  26. this.$set(this, `form`, res.data);
  27. this.view = 'info';
  28. } else {
  29. this.$message.error('未找到指定数据');
  30. }
  31. },
  32. // 执行删除
  33. async toDelete({ data }) {
  34. const res = await this.delete(data._id);
  35. if (this.$checkRes(res, '操作成功', '操作失败')) this.toResearch();
  36. },
  37. // 执行返回
  38. toBack() {
  39. this.form = {};
  40. this.view = 'list';
  41. },
  42. //执行保存
  43. async toSave({ data }) {
  44. const id = _.get(data, 'id', _.get(data, '_id'));
  45. let res;
  46. if (id) res = await this.update(data);
  47. else res = await this.create(data);
  48. if (this.$checkRes(res, '操作成功', '操作失败')) {
  49. this.view = 'list';
  50. this.toResearch();
  51. }
  52. },
  53. //执行再查询
  54. toResearch() {
  55. if (this.search) this.search();
  56. },
  57. };