http.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // 请求拦截器
  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.original_url = options.url;
  57. options.url = this.config.baseUrl + options.url;
  58. console.log('options.url ', options.url)
  59. return new Promise( (resolve, reject) => {
  60. options.success = async (e) => {
  61. console.log(e, 'successsuccesssuccesssuccesssuccess')
  62. let res = e.data;
  63. if (e.statusCode === 200) {
  64. if (res.code == 500201) {
  65. // 未登录
  66. clearToken();
  67. console.log('111111111');
  68. await getToken();
  69. if (filterNoToken.indexOf(options.original_url) === -1) {
  70. console.log('222222222',options);
  71. return this.request(options);
  72. }
  73. } else {
  74. if (res.code != 0) {
  75. uni.showToast({
  76. title: res.msg,
  77. duration: 1000,
  78. icon: 'none'
  79. });
  80. }
  81. }
  82. resolve(res.data);
  83. } else {
  84. uni.showToast({
  85. title: "网络异常,请稍后再试",
  86. duration: 1000,
  87. icon: 'none'
  88. });
  89. reject(new Error('Request failed'));
  90. }
  91. }
  92. options.fail = (err) => {
  93. console.log(err)
  94. reject(err);
  95. }
  96. uni.request(options)
  97. })
  98. },
  99. get(url, data, options = {}) {
  100. options.url = url;
  101. options.data = data;
  102. options.method = 'GET';
  103. return this.request(options);
  104. },
  105. post(url, data, options = {}) {
  106. options.url = url;
  107. options.data = data;
  108. options.method = 'POST';
  109. return this.request(options);
  110. },
  111. // 上传图片
  112. upload(url, options = {}) {
  113. options.url = this.config.baseUrl + url;
  114. options.header = options.header || this.config.header;
  115. options.fileType = options.fileType || "image";
  116. options.formData = options.formData || {};
  117. options.filePath = options.filePath;
  118. options.name = options.name;
  119. // TODO:token增加等操作
  120. if (options.token) {
  121. // 验证是否登录
  122. if (!this.checkToken(options.checkToken)) return;
  123. // 验证权限
  124. if (!this.checkAuth(options.checkAuth)) return;
  125. options.header.token = User.token;
  126. }
  127. return uni.uploadFile(options);
  128. },
  129. // 错误处理
  130. errorCheck(err, res, errfun = false, resfun = false) {
  131. if (err) {
  132. typeof errfun === 'function' && errfun();
  133. uni.showToast({
  134. title: '加载失败',
  135. icon: "none"
  136. });
  137. return false;
  138. }
  139. if (res.data.errorCode) {
  140. typeof errfun === 'function' && resfun();
  141. uni.showToast({
  142. title: res.data.msg,
  143. icon: "none"
  144. });
  145. return false;
  146. }
  147. return true;
  148. },
  149. async getTokenString() {
  150. // console.log('getTokenString-----start');
  151. await getToken();
  152. let token = uni.getStorageSync('token');
  153. // token格式化
  154. if (token && typeof token === "string") {
  155. token = JSON.parse(token);
  156. }
  157. // console.log('getTokenString--end', token);
  158. return token.token;
  159. },
  160. async setRanSe(link_id){
  161. }
  162. }