curd.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import http from '@/lib/http'
  2. export interface ModelBase {
  3. id: number
  4. created_at: string
  5. updated_at: string
  6. }
  7. export interface Pagination {
  8. total: number
  9. per_page: number
  10. current_page: number
  11. total_pages: number
  12. }
  13. export interface GetListResponse<T> {
  14. data: T[]
  15. pagination?: Pagination
  16. }
  17. class Curd<T> {
  18. protected readonly baseUrl: string
  19. get_list = this._get_list.bind(this)
  20. get = this._get.bind(this)
  21. save = this._save.bind(this)
  22. import = this._import.bind(this)
  23. import_check = this._import_check.bind(this)
  24. destroy = this._destroy.bind(this)
  25. recover = this._recover.bind(this)
  26. update_order = this._update_order.bind(this)
  27. batch_save = this._batch_save.bind(this)
  28. batch_destroy = this._batch_destroy.bind(this)
  29. batch_recover = this._batch_recover.bind(this)
  30. constructor(baseUrl: string) {
  31. this.baseUrl = baseUrl
  32. }
  33. // eslint-disable-next-line ts/no-explicit-any
  34. _get_list(params: any = null): Promise<GetListResponse<T>> {
  35. return http.get(this.baseUrl, { params })
  36. }
  37. // eslint-disable-next-line ts/no-explicit-any
  38. _get(id: any = null, params: any = {}): Promise<T> {
  39. return http.get(this.baseUrl + (id ? `/${encodeURIComponent(id)}` : ''), { params })
  40. }
  41. // eslint-disable-next-line ts/no-explicit-any
  42. _save(id: any = null, data: any = {}, config: any = undefined): Promise<T> {
  43. return http.post(this.baseUrl + (id ? `/${encodeURIComponent(id)}` : ''), data, config)
  44. }
  45. // eslint-disable-next-line ts/no-explicit-any
  46. _import_check(formData: FormData, config: any = {}): Promise<T> {
  47. return http.post(`${this.baseUrl}/import_check`, formData, {
  48. headers: {
  49. 'Content-Type': 'multipart/form-data;charset=UTF-8',
  50. },
  51. ...config,
  52. })
  53. }
  54. // eslint-disable-next-line ts/no-explicit-any
  55. _import(data: any, config: any = {}): Promise<T> {
  56. return http.post(`${this.baseUrl}/import`, data, config)
  57. }
  58. // eslint-disable-next-line ts/no-explicit-any
  59. _destroy(id: any = null, params: any = {}) {
  60. return http.delete(`${this.baseUrl}/${encodeURIComponent(id)}`, { params })
  61. }
  62. // eslint-disable-next-line ts/no-explicit-any
  63. _recover(id: any = null) {
  64. return http.patch(`${this.baseUrl}/${encodeURIComponent(id)}`)
  65. }
  66. _update_order(data: { target_id: number, direction: number, affected_ids: number[] }) {
  67. return http.post(`${this.baseUrl}/order`, data)
  68. }
  69. // eslint-disable-next-line ts/no-explicit-any
  70. _batch_save(ids: any, data: any) {
  71. return http.put(this.baseUrl, {
  72. ids,
  73. data,
  74. })
  75. }
  76. // eslint-disable-next-line ts/no-explicit-any
  77. _batch_destroy(ids?: (string | number)[], params: any = {}) {
  78. return http.delete(this.baseUrl, {
  79. params,
  80. data: {
  81. ids,
  82. },
  83. })
  84. }
  85. _batch_recover(ids?: (string | number)[]) {
  86. return http.patch(this.baseUrl, {
  87. data: {
  88. ids,
  89. },
  90. })
  91. }
  92. }
  93. export default Curd