person_room.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const _ = require('lodash');
  3. const idToData = async (ctx, id) => {
  4. let res = {};
  5. // 1,是产品
  6. res = await ctx.service.users.product.fetch({ id });
  7. if (res) return res;
  8. // 2,是专家
  9. res = await ctx.service.users.personal.fetch({ id });
  10. return res;
  11. };
  12. module.exports = options => {
  13. return async function person_room(ctx, next) {
  14. console.log('function in person_room middleware');
  15. await next();
  16. const request = ctx.request;
  17. const { method } = request;
  18. if (method === 'GET') {
  19. let { data } = ctx.body;
  20. data = JSON.parse(JSON.stringify(data));
  21. const dataKey = 'product_id';
  22. const key = 'product';
  23. if (_.isArray(data)) {
  24. // 数组,多个,循环查
  25. for (const i of data) {
  26. const res = await idToData(ctx, i[dataKey]);
  27. if (res)i[key] = res;
  28. }
  29. } else if (_.isObject(data)) {
  30. // 一个,找完就完事
  31. const res = await idToData(ctx, data[dataKey]);
  32. if (res) data[key] = res;
  33. }
  34. ctx.body.data = data;
  35. }
  36. };
  37. };