brand.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. import Base from './base.js';
  3. const _ = require('lodash');
  4. const fs = require('fs');
  5. let gm = require('gm').subClass({imageMagick: true});
  6. export default class extends Base {
  7. /**
  8. * index action
  9. * @return {Promise} []
  10. */
  11. async indexAction() {
  12. let page = this.get('page') || 1;
  13. let size = this.get('size') || 10;
  14. let name = this.get('name') || '';
  15. let model = this.model('brand');
  16. let data = await model.field(['id', 'name', 'floor_price', 'app_list_pic_url', 'is_new', 'sort_order', 'is_show']).where({name: ['like', `%${name}%`]}).page(page, size).countSelect();
  17. return this.success(data);
  18. }
  19. async infoAction() {
  20. let id = this.get('id');
  21. let model = this.model('brand');
  22. let data = await model.where({id: id}).find();
  23. return this.success(data);
  24. }
  25. async storeAction(){
  26. if (!this.isPost()) {
  27. return false;
  28. }
  29. let values = this.post();
  30. let id = this.post('id');
  31. let model = this.model('brand');
  32. values.is_show = values.is_show ? 1 : 0;
  33. values.is_new = values.is_new ? 1 : 0;
  34. if (id > 0) {
  35. console.log('update')
  36. await model.where({id: id}).update(values);
  37. } else {
  38. delete values.id;
  39. console.log('add')
  40. await model.add(values);
  41. }
  42. return this.success(values);
  43. }
  44. async destoryAction(){
  45. let id = this.post('id');
  46. await this.model('brand').where({id: id}).limit(1).delete();
  47. return this.success();
  48. }
  49. async uploadAction() {
  50. let brandFile = this.file('brand_pic');
  51. if (think.isEmpty(brandFile)) {
  52. return this.fail('保存失败');
  53. }
  54. let that = this;
  55. let filename = '/static/brand/' + think.uuid(32) + '.jpg';
  56. gm(brandFile.path)
  57. .resize(750, 420, "!")
  58. .write(think.RESOURCE_PATH + filename, function (err) {
  59. if (err) {
  60. that.fail('图片上传失败');
  61. }
  62. that.success({
  63. fileUrl: 'http://127.0.0.1:8360' + filename
  64. });
  65. });
  66. }
  67. }