request.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = store.state.sys_appid
  70. let sys_appsecret = store.state.sys_appsecret
  71. // let sys_appid = 'xgred20211207'
  72. // let sys_appsecret = '70211202dergxxgred20211207'
  73. let sys_token = md5(md5(sys_appid + sys_appsecret) + sign_time + sha1(sys_appsecret + sign_time))
  74. if (!config.header) {
  75. config.header = {
  76. // 'Accept':'application/json',
  77. // 'Content-Type': 'application/json; charset=utf-8',
  78. "sys-appid": sys_appid,
  79. "sign-time": sign_time,
  80. "sys-token": sys_token,
  81. "token": token
  82. }
  83. } else {
  84. // config.header['Accept'] = 'application/json'
  85. // config.header['Content-Type'] = 'application/json; charset=utf-8'
  86. config.header['sys-appid'] = sys_appid
  87. config.header['sign-time'] = sign_time
  88. config.header['sys-token'] = sys_token
  89. config.header['token'] = token
  90. }
  91. return config
  92. },
  93. error => {
  94. // 错误处理
  95. console.log(error)
  96. return Promise.reject(error)
  97. }
  98. )
  99. let upFilse = service.upFilse;
  100. export {
  101. upFilse
  102. };
  103. export default service.open;