request.js 2.3 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. let res = response.data;
  14. // 解析字符串为数字
  15. if (res.status !== 200) {
  16. if (res.status == 410000) {
  17. // 存储当前地址
  18. saveUrl()
  19. // 调用退出登录方法清空用户信息
  20. store.commit('user/logout');
  21. // 判断是否开启强制登录
  22. // 跳转页面
  23. interceptor()
  24. uni.showModal({
  25. title: "您未登录!是否马上登录?",
  26. success: (e) => {
  27. if (e.confirm) {
  28. // 保存当前页面地址
  29. }
  30. }
  31. })
  32. } else {
  33. if(res.msg!='系统出现异常'){
  34. uni.showToast({
  35. title: res.msg,
  36. duration: 1500,
  37. mask: false,
  38. icon: 'none',
  39. })
  40. }
  41. }
  42. console.log(res);
  43. //return Promise.reject(new Error(res.msg || 'Error'))
  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. let timestamp = new Date().getTime();
  66. let appSecret = 'c86fec63e23217b38f817b3e94dfdc33';
  67. let Appid = 'wx7f2fc8ec64602d06';
  68. let Sign = md5(sha1(Appid + timestamp + appSecret + timestamp + Appid + appSecret))
  69. console.log(Sign,Sign.length,'md5+++++++++++')
  70. console.log(timestamp,'timestamp')
  71. if (!config.header) {
  72. config.header = {
  73. "Authori-zation": 'Bearer ' + token,
  74. "App-id": Appid,
  75. "Sign": Sign,
  76. "Sign-time": timestamp
  77. }
  78. } else {
  79. // 添加key请求头
  80. config.header["Authori-zation"] = 'Bearer ' + token;
  81. config.header["App-id"] = Appid;
  82. config.header['Sign-time'] = timestamp;
  83. config.header['Sign'] = Sign;
  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;