service.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import request from '@/service/request';
  2. import { baseUrl } 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 (process.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. console.log(`[${options.method || 'GET'}]`, options.url);
  43. return request(options);
  44. }
  45. list(params) {
  46. return this.request({
  47. url: '/list',
  48. params: {
  49. ...params
  50. }
  51. }).then(res => {
  52. return {
  53. ...res,
  54. pagination: {
  55. totalPage: res.totalPage,
  56. total: parseInt(res.totalNum)
  57. }
  58. };
  59. });
  60. }
  61. page(params) {
  62. return this.request({
  63. url: '/list',
  64. params: {
  65. ...params
  66. }
  67. }).then(res => {
  68. return {
  69. ...res,
  70. pagination: {
  71. totalPage: res.totalPage,
  72. total: parseInt(res.totalNum)
  73. }
  74. };
  75. });
  76. }
  77. info(params) {
  78. return this.request({
  79. url: '/detail',
  80. params: {
  81. ...params
  82. }
  83. });
  84. }
  85. update(params) {
  86. return this.request({
  87. url: '/update',
  88. method: 'POST',
  89. data: {
  90. ...params
  91. }
  92. });
  93. }
  94. delete(params) {
  95. return this.request({
  96. url: '/delete',
  97. method: 'POST',
  98. data: {
  99. ...params
  100. }
  101. });
  102. }
  103. add(params) {
  104. return this.request({
  105. url: '/add',
  106. method: 'POST',
  107. data: {
  108. ...params
  109. }
  110. });
  111. }
  112. }