request.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import service from './newRequest.js'
  2. import store from '../store';
  3. import {
  4. saveUrl,
  5. interceptor
  6. } from '@/utils/loginUtils.js';
  7. import {
  8. unfreeze
  9. } from '@/api/index.js';
  10. // 请求完成后拦截
  11. service.interceptors.response(
  12. response => {
  13. try {
  14. const that = getApp();
  15. let res = response;
  16. // 解析字符串为数字
  17. if (res.statusCode !== 200) {
  18. switch (res.statusCode) {
  19. case 401:
  20. // 调用退出登录方法清空用户信息
  21. store.commit('user/logout');
  22. // 判断是否开启强制登录
  23. // 保存当前页面地址
  24. saveUrl()
  25. // 跳转页面
  26. interceptor()
  27. break;
  28. case 402:
  29. that.$util.Tips({
  30. title: that.$t('base.请先绑定'),
  31. icon: 'error',
  32. }, '/pages/index/bind')
  33. break;
  34. case 408:
  35. uni.showModal({
  36. title: that.$t('base.温馨提示'),
  37. content: that.$t('base.请先解冻'),
  38. cancelText: that.$t('base.关闭'),
  39. confirmText: that.$t('base.确定'),
  40. success: res => {
  41. if (res.confirm) {
  42. const UNFREEZE = 'UNFREEZE' + (new Date()).getTime();
  43. ethereum.request({
  44. "method": "personal_sign",
  45. "params": [
  46. UNFREEZE,
  47. store.state.user.userInfo.address
  48. ]
  49. }).then((res) => {
  50. unfreeze({
  51. sign: res,
  52. msg: UNFREEZE,
  53. }).then((res) => {
  54. uni.showToast({
  55. title: that.$t('base.解冻成功'),
  56. mask: false,
  57. })
  58. console.log(res, 'res');
  59. }).catch((res) => {
  60. })
  61. });
  62. }
  63. },
  64. });
  65. break;
  66. case 500:
  67. uni.showToast({
  68. title: "服务器500错误",
  69. duration: 1500,
  70. mask: false,
  71. icon: 'none',
  72. })
  73. // uni.showModal({
  74. // content: JSON.stringify(res),
  75. // success: res => {
  76. // },
  77. // });
  78. break;
  79. default:
  80. if (res.data.msg != '系统出现异常') {
  81. uni.showToast({
  82. title: res.data.msg,
  83. duration: 1500,
  84. mask: false,
  85. icon: 'none',
  86. })
  87. }
  88. break;
  89. }
  90. return Promise.reject(res.data)
  91. } else {
  92. if (res.data.code == 1) {
  93. return res.data
  94. } else {
  95. return Promise.reject(res.data)
  96. }
  97. }
  98. } catch (e) {
  99. console.log(e);
  100. }
  101. },
  102. error => {
  103. if (error) {
  104. console.log(error, 'error');
  105. }
  106. uni.showToast({
  107. title: "加载错误请重试",
  108. duration: 1500,
  109. mask: false,
  110. icon: 'none',
  111. })
  112. return Promise.reject(error)
  113. }
  114. )
  115. // 请求前拦截器
  116. service.interceptors.request(
  117. config => {
  118. let token = uni.getStorageSync('token') || '';
  119. // console.log(config);
  120. if (!config.header) {
  121. config.header = {
  122. "Token": token,
  123. }
  124. } else {
  125. // 添加key请求头
  126. config.header["Token"] = token;
  127. }
  128. return config
  129. },
  130. error => {
  131. // 错误处理
  132. console.log(error)
  133. return Promise.reject(error)
  134. }
  135. )
  136. let upFilse = service.upFilse;
  137. export {
  138. upFilse
  139. };
  140. export default service.open;