home.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. class HomeController extends Controller {
  4. async index() {
  5. const { ctx } = this;
  6. ctx.body = 'hi, egg';
  7. // 模拟优惠券发行
  8. // const data = await this.makeCoupons();
  9. // this.ctx.ok({ data, total: data.length });
  10. }
  11. async makeCoupons() {
  12. const dictDataModel = this.ctx.model.Dev.DictData;
  13. const couponModel = this.ctx.model.Trade.Coupon;
  14. // 过期字典
  15. const expList = await dictDataModel.find({ code: 'coupon_expire_type' });
  16. // 减免字典
  17. const disList = await dictDataModel.find({ code: 'coupon_discount_type' });
  18. // 使用字典
  19. const useList = await dictDataModel.find({ code: 'coupon_use_limit' });
  20. // 领取字典
  21. const getList = await dictDataModel.find({ code: 'coupon_get_limit' });
  22. const arr = [];
  23. for (const e of expList) {
  24. const { value: expire_type, label: el } = e;
  25. for (const d of disList) {
  26. const { value: discount_type, label: dl } = d;
  27. for (const u of useList) {
  28. const { value: use_limit, label: ul } = u;
  29. for (const g of getList) {
  30. const { value: get_limit, label: gl } = g;
  31. const obj = { issue: '0', expire_type, discount_type, use_limit, get_limit, num: 100, status: '0' };
  32. obj.name = `平台测试优惠券-${el}-${dl}-${ul}-${gl}`;
  33. arr.push(obj);
  34. }
  35. }
  36. }
  37. }
  38. await couponModel.insertMany(arr);
  39. return arr;
  40. }
  41. }
  42. module.exports = HomeController;