image.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import BaseService from '../../base';
  2. import { In } from 'typeorm';
  3. /**
  4. * 图片空间Service
  5. */
  6. export default class ImageSpaceService extends BaseService {
  7. /**
  8. * 查找是否存在当前图片空间类型
  9. */
  10. async find(typeId: number) {
  11. const result = await this.getRepo().admin.image.space.Type.findOne({ id: typeId });
  12. return result;
  13. }
  14. /**
  15. * 查找所有图片空间类型
  16. */
  17. async type() {
  18. return await this.getRepo().admin.image.space.Type.find();
  19. }
  20. /**
  21. * 增加分类
  22. */
  23. async addType(name: string) {
  24. await this.getRepo().admin.image.space.Type.insert({ name });
  25. }
  26. /**
  27. * 删除分类
  28. */
  29. async deleteType(id: number) {
  30. await this.getRepo().admin.image.space.Type.delete(id);
  31. }
  32. /**
  33. * 查找当前分类下是否有图片
  34. */
  35. async findCurrentTypeHasImage(id: number) {
  36. const count = await this.getRepo().admin.image.space.Info.count({ typeId: id });
  37. return count > 0;
  38. }
  39. /**
  40. * 获取当前图片空间下的所有图片数量
  41. */
  42. async count(typeId: number) {
  43. return await this.getRepo().admin.image.space.Info.count(typeId === -1 ? {} : { typeId });
  44. }
  45. /**
  46. * 根据数据id删除图片
  47. */
  48. async deleteImageByIds(ids: number[]) {
  49. const result = await this.getRepo().admin.image.space.Info.find({ id: In(ids) });
  50. this.service.admin.comm.oss.delete(result);
  51. await this.getRepo().admin.image.space.Info.delete(ids);
  52. }
  53. /**
  54. * 分页获取图片空间下的图片
  55. */
  56. async page(typeId: number, page: number, count: number) {
  57. const result = await this.getRepo().admin.image.space.Info.find({
  58. where: typeId === -1 ? {} : { typeId },
  59. order: {
  60. id: 'DESC',
  61. },
  62. take: count,
  63. skip: page * count,
  64. });
  65. return result;
  66. }
  67. /**
  68. * add info item
  69. */
  70. async add(typeId: number, url: string, extra?: string) {
  71. await this.getRepo().admin.image.space.Info.save({ typeId, url, extra });
  72. }
  73. }