category.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/9/26
  7. * @version
  8. */
  9. const settingConst = require('../const/setting.js');
  10. module.exports = app => {
  11. class Category extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'category';
  21. }
  22. async addCategory(pid, name, type) {
  23. const count = await this.count({pid: pid, name: name});
  24. if (count > 0) {
  25. throw '存在同名类别';
  26. }
  27. const category = {
  28. pid: pid,
  29. name: name,
  30. type: type,
  31. };
  32. const result = await this.db.insert(this.tableName, category);
  33. if (result.affectedRows !== 1) {
  34. throw '提交数据失败';
  35. } else {
  36. category.id = result.insertId;
  37. }
  38. category.value = category.value && category.value !== '' ? JSON.parse(category.value) : [];
  39. return category;
  40. }
  41. async getCategory(condition) {
  42. const data = await this.getDataByCondition(condition);
  43. data.value = data.value && data.value !== '' ? JSON.parse(data.value) : [];
  44. return data;
  45. }
  46. async getAllCategory(condition) {
  47. const data = await this.getAllDataByCondition(condition);
  48. for (const d of data) {
  49. d.value = d.value && d.value !== '' ? JSON.parse(d.value) : [];
  50. }
  51. return data;
  52. }
  53. }
  54. return Category;
  55. };