home.js 968 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict';
  2. const _ = require('lodash');
  3. const Controller = require('egg').Controller;
  4. class HomeController extends Controller {
  5. async index() {
  6. const { ctx } = this;
  7. ctx.body = 'hi, egg';
  8. }
  9. async utilMethod() {
  10. const res = await this.ctx.model.RoadShow.find();
  11. for (const i of res) {
  12. const { picture, filepath } = i;
  13. if (_.isArray(picture) && picture.length > 0) {
  14. const arr = [];
  15. for (let i = 0; i < picture.length; i++) {
  16. if (_.isString(picture[i])) arr.push({ name: `img${i + 1}`, url: picture[i] });
  17. }
  18. i.picture = arr;
  19. }
  20. if (_.isArray(filepath) && filepath.length > 0) {
  21. const arr = [];
  22. for (let i = 0; i < filepath.length; i++) {
  23. if (_.isString(filepath[i])) arr.push({ name: `file${i + 1}`, url: filepath[i] });
  24. }
  25. i.filepath = arr;
  26. }
  27. await i.save();
  28. }
  29. this.ctx.ok();
  30. }
  31. }
  32. module.exports = HomeController;