requestConfig.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import request from "./request";
  2. import config from "config";
  3. // import store from "../../store/index.js";
  4. uni.getStorage({
  5. key: 'storage_key',
  6. success: function (res) {
  7. console.log(res.data);
  8. }
  9. });
  10. //可以new多个request来支持多个域名请求
  11. let $http = new request({
  12. //接口请求地址
  13. baseUrl: config.baseUrl,
  14. //服务器本地上传文件地址
  15. fileUrl: config.baseUrl,
  16. //设置请求头
  17. headers: {
  18. 'Content-Type': 'application/x-www-form-urlencoded;',
  19. 'ApiVersion': config.version,
  20. 'Device': config.device,
  21. 'Platform': config.platform,
  22. "Authorization": 'Bearer ' + uni.getStorageSync('laocui_user_token')
  23. },
  24. //以下是默认值可不写
  25. //是否提示--默认提示
  26. isPrompt: true,
  27. //是否显示请求动画
  28. load: true,
  29. //是否使用处理数据模板
  30. isFactory: true,
  31. //列表接口是否有加载判断
  32. loadMore: true,
  33. });
  34. //当前接口请求数
  35. let requestNum = 0;
  36. //请求开始拦截器
  37. $http.requestStart = function (options) {
  38. // console.log(`$http.requestSta`, options)
  39. for (const key in options.data) {
  40. if (options.data[key] == null) {
  41. delete options.data[key]
  42. }
  43. }
  44. if (requestNum <= 0) {
  45. uni.showNavigationBarLoading();
  46. if (options.load) {
  47. //打开加载动画
  48. uni.showLoading({
  49. title: '加载中',
  50. mask: true
  51. });
  52. }
  53. }
  54. requestNum += 1;
  55. //请求前加入token
  56. options.headers['Authorization'] = 'Bearer ' + uni.getStorageSync('laocui_user_token');
  57. // console.log(uni.getStorageSync('USERINFO'))
  58. options.headers['requestHanderAuth'] = uni.getStorageSync('USERINFO') ? JSON.parse(uni.getStorageSync('USERINFO')).requestHanderAuth : '';
  59. // console.log("请求开始前", options);
  60. return options;
  61. }
  62. //请求结束
  63. $http.requestEnd = function (options, resolve) {
  64. //判断当前接口是否需要加载动画
  65. requestNum = requestNum - 1;
  66. if (requestNum <= 0) {
  67. uni.hideLoading();
  68. uni.hideNavigationBarLoading();
  69. }
  70. if (resolve.errMsg && (resolve.errMsg != "request:ok" || resolve.statusCode && resolve.statusCode != 200)) {
  71. uni.showToast({
  72. title: "网络错误,请检查一下网络",
  73. mask: true,
  74. icon: "none"
  75. });
  76. }
  77. }
  78. //登录弹窗次数
  79. let loginPopupNum = 0;
  80. //所有接口数据处理(可在接口里设置不调用此方法)
  81. $http.dataFactory = function (options, resolve) {
  82. //设置回调默认值
  83. var callback = {
  84. //success数据是否请求成功状态
  85. success: false,
  86. //这里返回的数据就是调用请求方法收到的数据
  87. result: ""
  88. };
  89. // 特殊接口处理 接口报请先登录 跳转登录
  90. if (resolve.data.code === 101) {
  91. uni.showModal({
  92. title: '提示',
  93. content: resolve.data.msg,
  94. showCancel: false,
  95. success: () => {
  96. uni.setStorageSync('token', null);
  97. uni.navigateTo({
  98. url: '/pages/login/login'
  99. });
  100. }
  101. });
  102. return false;
  103. }
  104. if (resolve.data) {
  105. callback.result = resolve.data;
  106. callback.success = true;
  107. uni.stopPullDownRefresh();
  108. return callback;
  109. }
  110. //判断数据是否请求成功
  111. if (resolve.data.errmsg == "成功") {
  112. callback.success = true;
  113. callback.result = resolve.data;
  114. } else if (resolve.data.errno == 303) {
  115. uni.redirectTo({
  116. url: '/pages/subPackages/pages/login/login'
  117. });
  118. this.$showToast('令牌过期,请重新登陆')
  119. } else {
  120. uni.showToast({
  121. title: resolve.data.errmsg,
  122. icon: "none",
  123. mask: true,
  124. duration: 2000
  125. });
  126. //设置可以提示的时候
  127. // if (options.isPrompt) {
  128. // setTimeout(function () {
  129. //提示后台接口抛出的错误信息
  130. // uni.showToast({
  131. // title: resolve.data.msg,
  132. // icon: "none",
  133. // duration: 3000
  134. // });
  135. // }, 500);
  136. // }
  137. callback.result = resolve.data;
  138. callback.success = true;
  139. }
  140. uni.stopPullDownRefresh();
  141. console.log('callback', callback)
  142. return callback;
  143. };
  144. export default $http;