request.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. break;
  74. default:
  75. if (res.data.msg != '系统出现异常') {
  76. uni.showToast({
  77. title: res.data.msg,
  78. duration: 1500,
  79. mask: false,
  80. icon: 'none',
  81. })
  82. }
  83. break;
  84. }
  85. return Promise.reject(res.data)
  86. } else {
  87. if (res.data.code == 1) {
  88. return res.data
  89. } else {
  90. return Promise.reject(res.data)
  91. }
  92. }
  93. } catch (e) {
  94. console.log(e);
  95. }
  96. },
  97. error => {
  98. if (error) {
  99. console.log(error, 'error');
  100. }
  101. uni.showToast({
  102. title: "加载错误请重试",
  103. duration: 1500,
  104. mask: false,
  105. icon: 'none',
  106. })
  107. return Promise.reject(error)
  108. }
  109. )
  110. // 请求前拦截器
  111. service.interceptors.request(
  112. config => {
  113. let token = uni.getStorageSync('token') || '';
  114. // console.log(config);
  115. if (!config.header) {
  116. config.header = {
  117. "Token": token,
  118. }
  119. } else {
  120. // 添加key请求头
  121. config.header["Token"] = token;
  122. }
  123. return config
  124. },
  125. error => {
  126. // 错误处理
  127. console.log(error)
  128. return Promise.reject(error)
  129. }
  130. )
  131. let upFilse = service.upFilse;
  132. export {
  133. upFilse
  134. };
  135. export default service.open;