mock平台

follow.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const yapi = require('../yapi.js');
  2. const baseController = require('./base.js');
  3. const followModel = require('../models/follow');
  4. const projectModel = require('../models/project');
  5. class followController extends baseController {
  6. constructor(ctx) {
  7. super(ctx);
  8. this.Model = yapi.getInst(followModel);
  9. this.projectModel = yapi.getInst(projectModel);
  10. }
  11. /**
  12. * 获取关注项目列表
  13. * @interface /follow/list
  14. * @method GET
  15. * @category follow
  16. * @foldnumber 10
  17. * @param {Number} [page] 分页页码
  18. * @param {Number} [limit] 分页大小
  19. * @returns {Object}
  20. * @example /follow/list
  21. */
  22. async list(ctx) {
  23. let uid = this.getUid();
  24. // 关注列表暂时不分页 page & limit 为分页配置
  25. // page = ctx.request.query.page || 1,
  26. // limit = ctx.request.query.limit || 10;
  27. if (!uid) {
  28. return (ctx.body = yapi.commons.resReturn(null, 400, '用户id不能为空'));
  29. }
  30. try {
  31. let result = await this.Model.list(uid);
  32. ctx.body = yapi.commons.resReturn({
  33. list: result
  34. });
  35. } catch (err) {
  36. ctx.body = yapi.commons.resReturn(null, 402, err.message);
  37. }
  38. }
  39. /**
  40. * 取消关注
  41. * @interface /follow/del
  42. * @method POST
  43. * @category follow
  44. * @foldnumber 10
  45. * @param {Number} projectid
  46. * @returns {Object}
  47. * @example /follow/del
  48. */
  49. async del(ctx) {
  50. let params = ctx.request.body,
  51. uid = this.getUid();
  52. if (!params.projectid) {
  53. return (ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空'));
  54. }
  55. let checkRepeat = await this.Model.checkProjectRepeat(uid, params.projectid);
  56. if (checkRepeat == 0) {
  57. return (ctx.body = yapi.commons.resReturn(null, 401, '项目未关注'));
  58. }
  59. try {
  60. let result = await this.Model.del(params.projectid, this.getUid());
  61. ctx.body = yapi.commons.resReturn(result);
  62. } catch (e) {
  63. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  64. }
  65. }
  66. /**
  67. * 添加关注
  68. * @interface /follow/add
  69. * @method GET
  70. * @category follow
  71. * @foldnumber 10
  72. * @param {Number} projectid 项目id
  73. * @param {String} projectname 项目名
  74. * @param {String} icon 项目icon
  75. * @returns {Object}
  76. * @example /follow/add
  77. */
  78. async add(ctx) {
  79. let params = ctx.request.body;
  80. params = yapi.commons.handleParams(params, {
  81. projectid: 'number'
  82. });
  83. let uid = this.getUid();
  84. if (!params.projectid) {
  85. return (ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空'));
  86. }
  87. let checkRepeat = await this.Model.checkProjectRepeat(uid, params.projectid);
  88. if (checkRepeat) {
  89. return (ctx.body = yapi.commons.resReturn(null, 401, '项目已关注'));
  90. }
  91. try {
  92. let project = await this.projectModel.get(params.projectid);
  93. let data = {
  94. uid: uid,
  95. projectid: params.projectid,
  96. projectname: project.name,
  97. icon: project.icon,
  98. color: project.color
  99. };
  100. let result = await this.Model.save(data);
  101. result = yapi.commons.fieldSelect(result, [
  102. '_id',
  103. 'uid',
  104. 'projectid',
  105. 'projectname',
  106. 'icon',
  107. 'color'
  108. ]);
  109. ctx.body = yapi.commons.resReturn(result);
  110. } catch (e) {
  111. ctx.body = yapi.commons.resReturn(null, 402, e.message);
  112. }
  113. }
  114. }
  115. module.exports = followController;