request.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import service from './newRequest.js'
  2. import store from '../store';
  3. import {
  4. saveUrl,
  5. interceptor
  6. } from '@/utils/loginUtils.js';
  7. // 请求完成后拦截
  8. service.interceptors.response(
  9. response => {
  10. try {
  11. let res = response.data;
  12. // 解析字符串为数字
  13. if (res.code !== 1) {
  14. console.log(res.code);
  15. if (res.code == 401) {
  16. // 调用退出登录方法清空用户信息
  17. console.log('清空登录信息')
  18. store.commit('user/logout');
  19. // 判断是否开启强制登录
  20. uni.showModal({
  21. title: "登陆",
  22. content: '您未登录!是否马上登录?',
  23. success: (e) => {
  24. // 判断是否点击确认按钮
  25. if (e.confirm) {
  26. // 保存当前页面地址
  27. saveUrl()
  28. // 跳转页面
  29. interceptor()
  30. }
  31. }
  32. })
  33. } else {
  34. if (res.msg != '系统出现异常') {
  35. uni.showToast({
  36. title: res.msg,
  37. duration: 1500,
  38. mask: false,
  39. icon: 'none',
  40. })
  41. }
  42. }
  43. return Promise.reject(res)
  44. } else {
  45. return res
  46. }
  47. } catch (e) {
  48. console.log(e);
  49. }
  50. },
  51. error => {
  52. uni.showToast({
  53. title: "加载错误请重试",
  54. duration: 1500,
  55. mask: false,
  56. icon: 'none',
  57. })
  58. return Promise.reject(error)
  59. }
  60. )
  61. // 请求前拦截器
  62. service.interceptors.request(
  63. config => {
  64. let token = uni.getStorageSync('token') || '';
  65. const langlist=['zh_cn', 'en'];
  66. let lang = store.state.lang||langlist[uni.getStorageSync('lang')];
  67. if (!config.header) {
  68. config.header = {
  69. "bowintoken": token,
  70. "bowinlang": lang
  71. }
  72. } else {
  73. // 添加key请求头
  74. config.header["bowintoken"] = token
  75. config.header["bowinlang"] = lang
  76. }
  77. return config
  78. },
  79. error => {
  80. // 错误处理
  81. console.log(error)
  82. return Promise.reject(error)
  83. }
  84. )
  85. let upFilse = service.upFilse;
  86. export {
  87. upFilse
  88. };
  89. export default service.open;