image.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { AdminRoute } from '../../../decorator/router_register';
  2. import BaseController from '../../base';
  3. import * as _ from 'lodash';
  4. import { QueryImageDto, CreateTypeDto, DeleteTypeDto, DeleteImageDto, UploadImageDto } from '../../../dto/admin/space/image';
  5. /**
  6. * 图片空间控制器
  7. */
  8. export default class ImageSpaceController extends BaseController {
  9. /**
  10. * @api {get} /admin/space/image/page 获取图片信息列表
  11. * @apiGroup 图片空间
  12. * @apiUse Auth
  13. * @apiUse BaseRes
  14. * @apiParam {Number} typeId 类别编号
  15. * @apiUse Page
  16. * @apiSuccess {ImageSpaceInfo[]} data.list 图片信息列表
  17. */
  18. @AdminRoute('/space/image/page', 'get')
  19. async page() {
  20. const dto = await this.ctx.validate<QueryImageDto>(QueryImageDto, this.getQuery());
  21. this.res({
  22. data: {
  23. list: await this.service.admin.space.image.page(parseInt(dto.typeId), dto.current - 1, dto.pageSize),
  24. pagination: {
  25. page: dto.current,
  26. size: dto.pageSize,
  27. total: await this.service.admin.space.image.count(parseInt(dto.typeId)),
  28. },
  29. },
  30. });
  31. }
  32. /**
  33. * @api {get} /admin/space/image/type/list 获取图片空间类别列表
  34. * @apiGroup 图片空间
  35. * @apiUse Auth
  36. * @apiUse BaseRes
  37. * @apiSuccess {Array} data 图片空间类别列表
  38. */
  39. @AdminRoute('/space/image/type/list', 'get')
  40. async typeList() {
  41. this.res({
  42. data: await this.service.admin.space.image.type(),
  43. });
  44. }
  45. /**
  46. * @api {post} /admin/space/image/type/add 新增图片空间类别
  47. * @apiGroup 图片空间
  48. * @apiUse Auth
  49. * @apiUse BaseRes
  50. * @apiParam {String} name 类别名称
  51. */
  52. @AdminRoute('/space/image/type/add', 'post')
  53. async addType() {
  54. const dto = await this.ctx.validate<CreateTypeDto>(CreateTypeDto);
  55. await this.service.admin.space.image.addType(dto.name);
  56. this.res();
  57. }
  58. /**
  59. * @api {post} /admin/space/image/type/delete 删除图片空间类别
  60. * @apiGroup 图片空间
  61. * @apiUse Auth
  62. * @apiUse BaseRes
  63. * @apiParam {Number} typeId 类别编号
  64. */
  65. @AdminRoute('/space/image/type/delete', 'post')
  66. async deleteType() {
  67. const dto = await this.ctx.validate<DeleteTypeDto>(DeleteTypeDto);
  68. const hasImage = await this.service.admin.space.image.findCurrentTypeHasImage(dto.typeId);
  69. if (hasImage) {
  70. this.res({
  71. code: 20003,
  72. });
  73. return;
  74. }
  75. await this.service.admin.space.image.deleteType(dto.typeId);
  76. this.res();
  77. }
  78. /**
  79. * @api {post} /admin/space/image/delete 删除空间类别下的图片列表
  80. * @apiGroup 图片空间
  81. * @apiUse Auth
  82. * @apiUse BaseRes
  83. * @apiParam {Number[]} imageIds 图片ID列表
  84. */
  85. @AdminRoute('/space/image/delete', 'post')
  86. async deleteImage() {
  87. const dto = await this.ctx.validate<DeleteImageDto>(DeleteImageDto);
  88. await this.service.admin.space.image.deleteImageByIds(dto.imageIds);
  89. this.res();
  90. }
  91. /**
  92. * @api {post} /admin/space/image/upload 图片上传(表单)
  93. * @apiGroup 图片空间
  94. * @apiUse Auth
  95. * @apiUse BaseRes
  96. * @apiParam {Number} typeId 图片ID列表
  97. */
  98. @AdminRoute('/space/image/upload', 'post')
  99. async upload() {
  100. const dto = await this.ctx.validate<UploadImageDto>(UploadImageDto);
  101. const typeId = parseInt(dto.typeId);
  102. if (typeId === -1) {
  103. this.res({ code: 10000 });
  104. return;
  105. }
  106. const file = this.ctx.request.files[0];
  107. if (!file) {
  108. this.res({ code: 20001 });
  109. return;
  110. }
  111. const type = await this.service.admin.space.image.find(typeId);
  112. // 查找图片空间是否存在
  113. if (_.isEmpty(type)) {
  114. this.res({ code: 20002 });
  115. return;
  116. }
  117. const { cdnUrl } = this.config.qiniu;
  118. try {
  119. const data: any = await this.service.admin.comm.oss.upload(file, type!.name);
  120. const { key, hash } = data;
  121. const url = `${cdnUrl}/${key}`;
  122. await this.service.admin.space.image.add(typeId, url, JSON.stringify({ key, hash }));
  123. this.res({
  124. data: {
  125. url,
  126. hash,
  127. },
  128. });
  129. } catch (e) {
  130. this.res({ code: 20001 });
  131. }
  132. }
  133. }