requestConfig.js 4.0 KB

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