request.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import service from './newRequest.js'
  2. import store from '../store';
  3. import md5 from 'js-md5'
  4. import {
  5. saveUrl,
  6. interceptor
  7. } from '@/utils/loginUtils.js';
  8. // 请求完成后拦截
  9. service.interceptors.response(
  10. response => {
  11. try {
  12. let res = response.data;
  13. // 解析字符串为数字
  14. if (res.status !== 200) {
  15. console.log(res.status);
  16. if (res.status == 410000) {
  17. // 调用退出登录方法清空用户信息
  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. var timestamp = new Date().getTime();
  66. var appSecret = '9034D2526CE4C02CDFFA4CA0CF05D2B5';
  67. var Appid = 'LaF961E07F0CC3';
  68. var Sign = md5(Appid+appSecret+timestamp+Appid+timestamp+appSecret+appSecret+Appid);
  69. // console.log(config);
  70. if (!config.header) {
  71. config.header = {
  72. "Authori-zation": 'Bearer ' + token,
  73. "Appid": 'UtilLa LaF961E07F0CC3',
  74. 'SignTime': timestamp,
  75. 'Sign': md5(Sign).toUpperCase()
  76. }
  77. } else {
  78. // 添加key请求头
  79. config.header["Authori-zation"] = 'Bearer ' + token;
  80. config.header["Appid"] = 'UtilLa LaF961E07F0CC3';
  81. config.header['SignTime'] = timestamp;
  82. config.header['Sign'] = md5(Sign).toUpperCase();
  83. }
  84. return config
  85. },
  86. error => {
  87. // 错误处理
  88. console.log(error)
  89. return Promise.reject(error)
  90. }
  91. )
  92. let upFilse = service.upFilse;
  93. export {
  94. upFilse
  95. };
  96. export default service.open;