request.js 2.2 KB

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