123456789101112131415161718192021222324252627282930313233343536373839404142 |
- 'use strict';
- const _ = require('lodash');
- const idToData = async (ctx, id) => {
- let res = await ctx.service.cysci.coupons.fetch({ id });
- if (res) {
- res = JSON.parse(JSON.stringify(res));
- const { user_id } = res;
- if (user_id) {
- const user = await ctx.model.User.Admin.findById(user_id);
- if (user) res.user = user;
- }
- }
- return res;
- };
- module.exports = options => {
- return async function couponsApply(ctx, next) {
- const request = ctx.request;
- await next();
- const { method } = request;
- if (method === 'GET') {
- // 处理get的时coupons_id换成对应的数据
- let { data } = ctx.body;
- data = JSON.parse(JSON.stringify(data));
- if (_.isArray(data)) {
- // 数组,多个,循环查
- for (const i of data) {
- const { coupons_id } = i;
- const res = await idToData(ctx, coupons_id);
- if (res)i.coupons = res;
- }
- } else if (_.isObject(data)) {
- // 一个,找完就完事
- const { coupons_id } = data;
- const res = await idToData(ctx, coupons_id);
- if (res) data.coupons = res;
- }
- ctx.body.data = data;
- }
- };
- };
|