requestConfig.js 3.9 KB

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