requestConfig.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if (requestNum <= 0) {
  39. uni.showNavigationBarLoading();
  40. if (options.load) {
  41. //打开加载动画
  42. uni.showLoading({
  43. title: '加载中',
  44. mask: true
  45. });
  46. }
  47. }
  48. requestNum += 1;
  49. //请求前加入token
  50. options.headers['Authorization'] = 'Bearer ' + uni.getStorageSync('laocui_user_token');
  51. options.headers['requestHanderAuth'] = uni.getStorageSync('requestHanderAuth');
  52. // console.log("请求开始前", options);
  53. return options;
  54. }
  55. //请求结束
  56. $http.requestEnd = function (options, resolve) {
  57. //判断当前接口是否需要加载动画
  58. requestNum = requestNum - 1;
  59. if (requestNum <= 0) {
  60. uni.hideLoading();
  61. uni.hideNavigationBarLoading();
  62. }
  63. // 特殊接口处理 接口报请先登录 跳转登录
  64. console.log(resolve.statusCode)
  65. if (resolve.errMsg && (resolve.errMsg != "request:ok" || resolve.statusCode && resolve.statusCode != 200)) {
  66. uni.showToast({
  67. title: "网络错误,请检查一下网络",
  68. mask: true,
  69. icon: "none"
  70. });
  71. }
  72. }
  73. //登录弹窗次数
  74. let loginPopupNum = 0;
  75. //所有接口数据处理(可在接口里设置不调用此方法)
  76. $http.dataFactory = function (options, resolve) {
  77. //设置回调默认值
  78. var callback = {
  79. //success数据是否请求成功状态
  80. success: false,
  81. //这里返回的数据就是调用请求方法收到的数据
  82. result: ""
  83. };
  84. console.log('resolve.data.code ', resolve.data.code)
  85. // 特殊接口处理 接口报请先登录 跳转登录
  86. if (resolve.data.code == 409) {
  87. uni.clearStorageSync();
  88. uni.reLaunch({
  89. url: '/pages/login/login'
  90. });
  91. uni.showToast({
  92. title: '令牌过期,请重新登陆',
  93. icon: 'none',
  94. mask: true,
  95. duration: 1000
  96. });
  97. }
  98. if (resolve.data.code === 101) {
  99. uni.showModal({
  100. title: '提示',
  101. content: resolve.data.msg,
  102. showCancel: false,
  103. success: () => {
  104. uni.setStorageSync('token', null);
  105. uni.redirectTo({
  106. url: '/pages/login/login'
  107. });
  108. }
  109. });
  110. return false;
  111. }
  112. //判断数据是否请求成功
  113. if (resolve.data.errmsg == "成功") {
  114. callback.success = true;
  115. callback.result = resolve.data;
  116. } else if (resolve.data.errno == 303) {
  117. uni.redirectTo({
  118. url: '/pages/subPackages/pages/login/login'
  119. });
  120. this.$showToast('令牌过期,请重新登陆')
  121. } else {
  122. uni.showToast({
  123. title: resolve.data.errmsg,
  124. icon: "none",
  125. mask: true,
  126. duration: 2000
  127. });
  128. //设置可以提示的时候
  129. // if (options.isPrompt) {
  130. // setTimeout(function () {
  131. //提示后台接口抛出的错误信息
  132. // uni.showToast({
  133. // title: resolve.data.msg,
  134. // icon: "none",
  135. // duration: 3000
  136. // });
  137. // }, 500);
  138. // }
  139. callback.result = resolve.data;
  140. }
  141. uni.stopPullDownRefresh();
  142. return callback;
  143. };
  144. export default $http;