interceptors.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * 功能:http拦截
  3. * 作者:小莫唐尼
  4. * 邮箱:studio@925i.cn
  5. * 时间:2022年07月21日 19:02:14
  6. * 版本:v0.1.0
  7. * 修改记录:
  8. * 修改内容:
  9. * 修改人员:
  10. * 修改时间:
  11. */
  12. import {delCache, setCache} from "@/utils/storage";
  13. const getCategoryNameByUrl = (url) => {
  14. const reg = '(?<=/api/content/categories/).+(?=/posts)'
  15. return url.match(reg)[0] || '无分类名'
  16. }
  17. const showCategoryInputPasswordModal = (response, category) => {
  18. uni.showModal({
  19. title: `[ ${category} ] 分类已加密`, // TODO 这里应该获取分类的名字,可以在弹窗之前请求后台拿到所有分类根据分类code拿到名称,但是不会在这之前发送请求
  20. content: '',
  21. editable: true,
  22. placeholderText: '请输入分类密码后访问',
  23. confirmText: '验证密码',
  24. cancelText: '暂不访问',
  25. showCancel: true,
  26. cancelColor: '#999999',
  27. confirmColor: '#03a9f4',
  28. success: (res) => {
  29. if (res.confirm) {
  30. // TODO 这里如果没有输入密码点击确认应该阻止窗口关闭,但是没找到方法
  31. if (!res.content) {
  32. uni.showToast({
  33. title: '提示:请输入密码', icon: 'none', success: () => {
  34. setTimeout(() => {
  35. showCategoryInputPasswordModal(response, category);
  36. }, 800)
  37. }
  38. })
  39. return;
  40. }
  41. // 根据请求URL正则匹配分类code,然后把输入的密码根据分类code放入缓存,然后在category.getCategoryPostList中获取,解决多个分类加密输入密码后点错的问题
  42. // 目前存在一个问题,比如前两个都需要密码,如果先输入第二个的密码之后,重新进来默认打开第一个还会弹窗,所以想在弹窗标题上增加分类名字
  43. // 另外有以下两种方式科技解决
  44. // TODO 1.其实这里获取到密码之后可以直接发送一个请求追加上password参数,因为后台会缓存权限,后续不输入密码也可以访问,可惜不会
  45. // TODO 2.另外也可以拿到密码之后,直接选中该分类追加password参数,重新请求,可惜也不会
  46. setCache('APP_CATEGORY_PWD_' + category, res.content)
  47. uni.reLaunch({
  48. url: '/pages/tabbar/category/category'
  49. });
  50. } else if (res.cancel) {
  51. }
  52. },
  53. })
  54. }
  55. export const setInterceptors = (http) => {
  56. http.interceptors.request.use((config) => {
  57. console.log("config", config)
  58. // 可使用async await 做异步操作
  59. config.header = {
  60. ...config.header
  61. // ... 可以直接加参数
  62. };
  63. if (config.custom.personalToken) {
  64. config.header['Authorization'] = `Bearer ${config.custom.personalToken}`
  65. }
  66. return config;
  67. }, (config) => {
  68. // 可使用async await 做异步操作
  69. return Promise.reject(config);
  70. });
  71. http.interceptors.response.use((response) => {
  72. /* 对响应成功做点什么 可使用async await 做异步操作*/
  73. // if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
  74. // return Promise.reject(response) // return Promise.reject 可使promise状态进入catch
  75. // if (response.config.custom.verification) { // 演示自定义参数的作用
  76. // return response.data
  77. // }
  78. if (response.statusCode == 200) {
  79. return response.data;
  80. } else {
  81. return Promise.reject(response);
  82. }
  83. }, (response) => {
  84. /* 对响应错误做点什么 (statusCode !== 200)*/
  85. if (!response.data) {
  86. return Promise.reject({
  87. status: 500, message: 'API接口服务异常!'
  88. })
  89. } else if (response.data.status == 401) {
  90. uni.$tm.toast(response.data.message);
  91. // 如果是请求分类之后报401说明密码错误,那么清除该密码,下次点击会报403弹窗再次输入密码
  92. if (response.config.url.indexOf('/api/content/categories') >= 0) {
  93. const category = getCategoryNameByUrl(response.config.url)
  94. delCache('APP_CATEGORY_PWD_' + category);
  95. uni.showToast({
  96. title: '提示:密码不正确', icon: 'none', success: () => {
  97. setTimeout(() => {
  98. showCategoryInputPasswordModal(response, category);
  99. }, 800)
  100. }
  101. })
  102. } else {
  103. // 其他情况维持原来的逻辑
  104. delCache('APP_ADMIN_LOGIN_TOKEN');
  105. uni.$eShowModal({
  106. title: '提示',
  107. content: '您未登录超管账号或登录已过期,是否重新登录?',
  108. showCancel: true,
  109. cancelText: '否',
  110. cancelColor: '#999999',
  111. confirmText: '是',
  112. confirmColor: '#03a9f4'
  113. }).then(res => {
  114. uni.navigateTo({
  115. url: '/pagesB/login/login'
  116. })
  117. }).catch(err => {
  118. uni.switchTab({
  119. url: '/pages/tabbar/about/about'
  120. })
  121. })
  122. }
  123. return Promise.reject(response.data);
  124. } else if (response.data.status == 403) {
  125. // 如果报403是请求分类文章接口(您没有该分类的访问权限)的话说明是私密分类,需要输入密码请求
  126. if (response.config.url.indexOf('/api/content/categories') >= 0) {
  127. const category = getCategoryNameByUrl(response.config.url);
  128. showCategoryInputPasswordModal(response, category);
  129. }
  130. return Promise.reject(response.data);
  131. } else {
  132. return Promise.reject(response.data);
  133. }
  134. });
  135. };