http.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // 引入配置文件
  2. import config from "./config.js";
  3. // #ifdef MP-WEIXIN
  4. import wxApp from "./wecaht.min.app.js"
  5. import {
  6. getToken,
  7. clearToken
  8. } from './token.js'
  9. // #endif
  10. const filterNoToken = ["/api/auth/refreshToken", "/api/auth/login"];
  11. export default {
  12. config: {
  13. baseUrl: config.apiUrl,
  14. header: {
  15. 'Content-Type': 'application/json;charset=UTF-8',
  16. },
  17. data: {},
  18. method: "GET",
  19. dataType: "json",
  20. },
  21. async request(options = {}) {
  22. options.header = options.header || this.config.header;
  23. options.method = options.method || this.config.method;
  24. options.dataType = options.dataType || this.config.dataType;
  25. let version = 'web';
  26. // #ifdef MP-WEIXIN
  27. // 微信小程序处理
  28. options.header.appid = wxApp.getWXAppId();
  29. options.header.source = 'wxapp';
  30. // 过滤不需要token的api
  31. if (filterNoToken.indexOf(options.url) === -1) {
  32. options.header.Authorization = await (this.getTokenString());
  33. }
  34. // #endif
  35. options.url = this.config.baseUrl + options.url;
  36. console.log('options.url ',options.url )
  37. return uni.request(options).then(res => {
  38. res = res[1].data;
  39. if (res.code == 500201) {
  40. // 未登录
  41. clearToken();
  42. getToken();
  43. if (filterNoToken.indexOf(options.url) === -1) {
  44. return this.request(options);
  45. }
  46. }
  47. return res.data;
  48. });
  49. },
  50. get(url, data, options = {}) {
  51. options.url = url;
  52. options.data = data;
  53. options.method = 'GET';
  54. return this.request(options);
  55. },
  56. post(url, data, options = {}) {
  57. options.url = url;
  58. options.data = data;
  59. options.method = 'POST';
  60. return this.request(options);
  61. },
  62. // 上传图片
  63. upload(url, options = {}) {
  64. options.url = this.config.baseUrl + url;
  65. options.header = options.header || this.config.header;
  66. options.fileType = options.fileType || "image";
  67. options.formData = options.formData || {};
  68. options.filePath = options.filePath;
  69. options.name = options.name;
  70. // TODO:token增加等操作
  71. if (options.token) {
  72. // 验证是否登录
  73. if (!this.checkToken(options.checkToken)) return;
  74. // 验证权限
  75. if (!this.checkAuth(options.checkAuth)) return;
  76. options.header.token = User.token;
  77. }
  78. return uni.uploadFile(options);
  79. },
  80. // 错误处理
  81. errorCheck(err, res, errfun = false, resfun = false) {
  82. if (err) {
  83. typeof errfun === 'function' && errfun();
  84. uni.showToast({
  85. title: '加载失败',
  86. icon: "none"
  87. });
  88. return false;
  89. }
  90. if (res.data.errorCode) {
  91. typeof errfun === 'function' && resfun();
  92. uni.showToast({
  93. title: res.data.msg,
  94. icon: "none"
  95. });
  96. return false;
  97. }
  98. return true;
  99. },
  100. async getTokenString() {
  101. // console.log('getTokenString-----start');
  102. await getToken();
  103. let token = uni.getStorageSync('token');
  104. // token格式化
  105. if (token && typeof token === "string") {
  106. token = JSON.parse(token);
  107. }
  108. // console.log('getTokenString--end', token);
  109. return token.token;
  110. }
  111. }