coupons_apply.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. const _ = require('lodash');
  3. const idToData = async (ctx, id) => {
  4. let res = await ctx.service.cysci.coupons.fetch({ id });
  5. if (res) {
  6. res = JSON.parse(JSON.stringify(res));
  7. const { user_id } = res;
  8. if (user_id) {
  9. const user = await ctx.model.User.Admin.findById(user_id);
  10. if (user) res.user = user;
  11. }
  12. }
  13. return res;
  14. };
  15. module.exports = options => {
  16. return async function couponsApply(ctx, next) {
  17. const request = ctx.request;
  18. await next();
  19. const { method } = request;
  20. if (method === 'GET') {
  21. // 处理get的时coupons_id换成对应的数据
  22. let { data } = ctx.body;
  23. data = JSON.parse(JSON.stringify(data));
  24. if (_.isArray(data)) {
  25. // 数组,多个,循环查
  26. for (const i of data) {
  27. const { coupons_id } = i;
  28. const res = await idToData(ctx, coupons_id);
  29. if (res)i.coupons = res;
  30. }
  31. } else if (_.isObject(data)) {
  32. // 一个,找完就完事
  33. const { coupons_id } = data;
  34. const res = await idToData(ctx, coupons_id);
  35. if (res) data.coupons = res;
  36. }
  37. ctx.body.data = data;
  38. }
  39. };
  40. };