request.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (res.status == 410000) {
  16. // 存储当前地址
  17. saveUrl()
  18. // 调用退出登录方法清空用户信息
  19. store.commit('logout');
  20. // 判断是否开启强制登录
  21. uni.showModal({
  22. title: "您未登录!是否马上登录?",
  23. success: (e) => {
  24. if (e.confirm) {
  25. // 保存当前页面地址
  26. // 跳转页面
  27. interceptor()
  28. }
  29. }
  30. })
  31. } else {
  32. if (res.msg.indexOf('人数过多') == -1) {
  33. if (res.msg != '系统出现异常' && res.msg != '0' && res.msg != 'ok' && res.msg != '找不到订单') {
  34. uni.showToast({
  35. title: res.msg,
  36. duration: 1500,
  37. mask: true,
  38. icon: 'none',
  39. })
  40. }
  41. }
  42. }
  43. // return res
  44. return Promise.reject(new Error(res.msg || 'Error'))
  45. } else {
  46. return res
  47. }
  48. } catch (e) {
  49. console.log(e);
  50. }
  51. },
  52. error => {
  53. uni.showToast({
  54. title: "加载错误请重试",
  55. duration: 1500,
  56. mask: false,
  57. icon: 'none',
  58. })
  59. return Promise.reject(error)
  60. }
  61. )
  62. // 请求前拦截器
  63. service.interceptors.request(
  64. config => {
  65. let token = uni.getStorageSync('token');
  66. if (config.data) {
  67. if (config.data.fuck) {
  68. let uid = config.data.fuck + ''
  69. let timestamp = new Date().getTime();
  70. timestamp = parseInt(timestamp / 1000)
  71. let secret =
  72. '0a989ebc4a77b56a6e2bb7b19d995d185ce44090c13e2984b7ecc6d446d4b61ea9991b76a4c2f04b1b4d244841449454';
  73. let action = config.data.fuck_name
  74. let key = 'fuckrml'
  75. let str1 = uid + timestamp + secret + action + secret + key + timestamp + uid + secret
  76. console.log(str1, '1234567899874351');
  77. str1 = md5(str1) + secret
  78. str1 = md5(str1)
  79. if (!config.header) {
  80. config.header = {
  81. "Authori-zation": 'Bearer ' + token,
  82. "key": key,
  83. 'time': timestamp,
  84. 'sign': str1
  85. }
  86. } else {
  87. // 添加key请求头
  88. config.header["Authori-zation"] = 'Bearer ' + token;
  89. config.header["key"] = key;
  90. config.header['time'] = timestamp;
  91. config.header['sign'] = str1;
  92. }
  93. } else {
  94. if (!config.header) {
  95. config.header = {
  96. "Authori-zation": 'Bearer ' + token,
  97. }
  98. } else {
  99. // 添加key请求头
  100. config.header["Authori-zation"] = 'Bearer ' + token;
  101. }
  102. }
  103. } else {
  104. if (!config.header) {
  105. config.header = {
  106. "Authori-zation": 'Bearer ' + token,
  107. }
  108. } else {
  109. // 添加key请求头
  110. config.header["Authori-zation"] = 'Bearer ' + token;
  111. }
  112. }
  113. return config
  114. },
  115. error => {
  116. // 错误处理
  117. console.log(error)
  118. return Promise.reject(error)
  119. }
  120. )
  121. let upFilse = service.upFilse;
  122. let upFilse1 = service.upFilse1;
  123. export {
  124. upFilse,
  125. upFilse1
  126. };
  127. export default service.open;