12345678910111213141516171819202122232425262728293031323334353637 |
- 'use strict';
- const _ = require('lodash');
- const idToData = async (ctx, id) => {
- let res = {};
- // 1,是产品
- res = await ctx.service.users.product.fetch({ id });
- if (res) return res;
- // 2,是专家
- res = await ctx.service.users.personal.fetch({ id });
- return res;
- };
- module.exports = options => {
- return async function person_room(ctx, next) {
- console.log('function in person_room middleware');
- await next();
- const request = ctx.request;
- const { method } = request;
- if (method === 'GET') {
- let { data } = ctx.body;
- data = JSON.parse(JSON.stringify(data));
- const dataKey = 'product_id';
- const key = 'product';
- if (_.isArray(data)) {
- // 数组,多个,循环查
- for (const i of data) {
- const res = await idToData(ctx, i[dataKey]);
- if (res)i[key] = res;
- }
- } else if (_.isObject(data)) {
- // 一个,找完就完事
- const res = await idToData(ctx, data[dataKey]);
- if (res) data[key] = res;
- }
- ctx.body.data = data;
- }
- };
- };
|