medium.js 976 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. const _ = require('lodash');
  3. const idToData = async (ctx, id) => {
  4. const res = await ctx.service.util.httpUtil.cget(`/mechanism/${id}`, 'hnhmain');
  5. return res;
  6. };
  7. module.exports = options => {
  8. return async function medium(ctx, next) {
  9. await next();
  10. const request = ctx.request;
  11. const { method } = request;
  12. if (method === 'GET') {
  13. // 处理get的时coupons_id换成对应的数据
  14. let { data } = ctx.body;
  15. data = JSON.parse(JSON.stringify(data));
  16. if (_.isArray(data)) {
  17. // 数组,多个,循环查
  18. for (const i of data) {
  19. const { medium_id } = i;
  20. const res = await idToData(ctx, medium_id);
  21. if (res)i.medium = res;
  22. }
  23. } else if (_.isObject(data)) {
  24. // 一个,找完就完事
  25. const { medium_id } = data;
  26. const res = await idToData(ctx, medium_id);
  27. if (res) data.medium = res;
  28. }
  29. ctx.body.data = data;
  30. }
  31. };
  32. };