service.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import request from '@/service/request';
  2. import { baseUrl, env } from '@/config/env';
  3. export class BaseService {
  4. constructor() {
  5. const crud = {
  6. page: 'list',
  7. list: 'list',
  8. info: 'detail',
  9. add: 'add',
  10. delete: 'delete',
  11. update: 'update'
  12. };
  13. if (!this.permission) {
  14. this.permission = {};
  15. }
  16. for (let i in crud) {
  17. if (this.namespace) {
  18. this.permission[i] = this.namespace.replace(/\//g, ':') + ':' + crud[i];
  19. } else {
  20. this.permission[i] = crud[i];
  21. }
  22. }
  23. }
  24. request(options = {}) {
  25. if (!options.params) {
  26. options.params = {};
  27. }
  28. let path = '';
  29. if (env.NODE_ENV == 'development') {
  30. path = this.proxy || baseUrl;
  31. } else {
  32. if (this.proxy) {
  33. path = this.url;
  34. } else {
  35. path = baseUrl;
  36. }
  37. }
  38. if (this.namespace) {
  39. path += '/' + this.namespace;
  40. }
  41. options.url = path + options.url;
  42. // options.params.t = Date.parse(new Date());
  43. console.log(`[${options.method || 'GET'}]`, options.url);
  44. return request(options);
  45. }
  46. list(params) {
  47. return this.request({
  48. url: '/list',
  49. params: {
  50. ...params
  51. }
  52. });
  53. }
  54. page(params) {
  55. return this.request({
  56. url: '/list',
  57. params: {
  58. ...params
  59. }
  60. });
  61. }
  62. info(params) {
  63. return this.request({
  64. url: '/detail',
  65. params: {
  66. ...params
  67. }
  68. });
  69. }
  70. update(params) {
  71. console.log('this', this);
  72. return this.request({
  73. url: '/update',
  74. method: 'POST',
  75. data: {
  76. ...params
  77. }
  78. });
  79. }
  80. delete(params) {
  81. return this.request({
  82. url: '/delete',
  83. method: 'POST',
  84. data: {
  85. ...params
  86. }
  87. });
  88. }
  89. add(params) {
  90. return this.request({
  91. url: '/add',
  92. method: 'POST',
  93. data: {
  94. ...params
  95. }
  96. });
  97. }
  98. }