base.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import request from "@/service/request";
  2. import { baseUrl } from "@/config/env";
  3. export default class BaseService {
  4. constructor() {
  5. const crud = {
  6. page: "page",
  7. list: "list",
  8. info: "info",
  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. if (options.url.indexOf("http") !== 0) {
  42. if (options.url[0] === "@") {
  43. options.url = options.url.replace("@", "");
  44. } else {
  45. options.url = path + options.url;
  46. }
  47. }
  48. return request(options);
  49. }
  50. list(params) {
  51. return this.request({
  52. url: "/list",
  53. method: "POST",
  54. data: {
  55. ...params
  56. }
  57. });
  58. }
  59. page(params) {
  60. return this.request({
  61. url: "/page",
  62. method: "POST",
  63. data: {
  64. ...params
  65. }
  66. });
  67. }
  68. info(params) {
  69. return this.request({
  70. url: "/info",
  71. params: {
  72. ...params
  73. }
  74. });
  75. }
  76. update(params) {
  77. return this.request({
  78. url: "/update",
  79. method: "POST",
  80. data: {
  81. ...params
  82. }
  83. });
  84. }
  85. delete(params) {
  86. return this.request({
  87. url: "/delete",
  88. method: "POST",
  89. data: {
  90. ...params
  91. }
  92. });
  93. }
  94. add(params) {
  95. return this.request({
  96. url: "/add",
  97. method: "POST",
  98. data: {
  99. ...params
  100. }
  101. });
  102. }
  103. }