http.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // 引入配置文件
  2. import config from "@/common/config.js";
  3. // #ifdef MP-WEIXIN
  4. import wxApp from "@/common/wecaht.min.app.js"
  5. import {
  6. getToken,
  7. clearToken
  8. } from '@/common/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. // 请求拦截器
  23. uni.addInterceptor('request', {
  24. invoke: (request) => {
  25. // 在发送请求之前的处理逻辑,例如添加请求头、请求日志等
  26. // request.header = {
  27. // 'Content-Type': 'application/json', // 设置请求头
  28. // // 在这里可以添加其他自定义请求头
  29. // };
  30. // console.log('Request:', request);
  31. return request;
  32. },
  33. });
  34. // 响应拦截器
  35. uni.addInterceptor('response', {
  36. invoke: (response) => {
  37. // 在接收到响应数据之后的处理逻辑,例如统一处理错误码、响应日志等
  38. // console.log('Response:', response);
  39. return response;
  40. },
  41. });
  42. options.header = options.header || this.config.header;
  43. options.method = options.method || this.config.method;
  44. options.dataType = options.dataType || this.config.dataType;
  45. let version = 'web';
  46. // #ifdef MP-WEIXIN
  47. // 微信小程序处理
  48. options.header.appid = wxApp.getWXAppId();
  49. options.header.source = 'wxapp';
  50. // 过滤不需要token的api
  51. if (filterNoToken.indexOf(options.url) === -1) {
  52. // console.log('getTokenString ',options.url )
  53. options.header.Authorization = await (this.getTokenString());
  54. }
  55. // #endif
  56. options.url = this.config.baseUrl + options.url;
  57. console.log('options.url ', options.url)
  58. return new Promise((resolve, reject) => {
  59. options.success = (e) => {
  60. // console.log(e, 'successsuccesssuccesssuccesssuccess')
  61. let res = e.data;
  62. if (e.statusCode === 200) {
  63. if (res.code == 500201) {
  64. // 未登录
  65. clearToken();
  66. getToken();
  67. if (filterNoToken.indexOf(options.url) === -1) {
  68. return this.request(options);
  69. }
  70. } else {
  71. if (res.code != 0) {
  72. uni.showToast({
  73. title: res.msg,
  74. duration: 1000,
  75. icon: 'none'
  76. });
  77. }
  78. }
  79. resolve(res);
  80. } else {
  81. uni.showToast({
  82. title: "网络异常,请稍后再试",
  83. duration: 1000,
  84. icon: 'none'
  85. });
  86. reject(new Error('Request failed'));
  87. }
  88. }
  89. options.fail = (err) => {
  90. console.log(err)
  91. reject(err);
  92. }
  93. uni.request(options)
  94. })
  95. },
  96. get(url, data, options = {}) {
  97. options.url = url;
  98. options.data = data;
  99. options.method = 'GET';
  100. return this.request(options);
  101. },
  102. post(url, data, options = {}) {
  103. options.url = url;
  104. options.data = data;
  105. options.method = 'POST';
  106. return this.request(options);
  107. },
  108. // 上传图片
  109. upload(url, options = {}) {
  110. options.url = this.config.baseUrl + url;
  111. options.header = options.header || this.config.header;
  112. options.fileType = options.fileType || "image";
  113. options.formData = options.formData || {};
  114. options.filePath = options.filePath;
  115. options.name = options.name;
  116. // TODO:token增加等操作
  117. if (options.token) {
  118. // 验证是否登录
  119. if (!this.checkToken(options.checkToken)) return;
  120. // 验证权限
  121. if (!this.checkAuth(options.checkAuth)) return;
  122. options.header.token = User.token;
  123. }
  124. return uni.uploadFile(options);
  125. },
  126. // 错误处理
  127. errorCheck(err, res, errfun = false, resfun = false) {
  128. if (err) {
  129. typeof errfun === 'function' && errfun();
  130. uni.showToast({
  131. title: '加载失败',
  132. icon: "none"
  133. });
  134. return false;
  135. }
  136. if (res.data.errorCode) {
  137. typeof errfun === 'function' && resfun();
  138. uni.showToast({
  139. title: res.data.msg,
  140. icon: "none"
  141. });
  142. return false;
  143. }
  144. return true;
  145. },
  146. async getTokenString() {
  147. // console.log('getTokenString-----start');
  148. await getToken();
  149. let token = uni.getStorageSync('token');
  150. // token格式化
  151. if (token && typeof token === "string") {
  152. token = JSON.parse(token);
  153. }
  154. // console.log('getTokenString--end', token);
  155. return token.token;
  156. }
  157. }