四零语境后端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.9 KiB

1 month ago
  1. import { createRouter } from '@/plugin/uni-mini-router'
  2. // 导入pages.json
  3. import pagesJson from '../pages.json'
  4. console.log("pagesJson::",pagesJson);
  5. // 引入uni-parse-pages
  6. import pagesJsonToRoutes from 'uni-parse-pages'
  7. import { useUserStore } from '@/store/user'
  8. import {
  9. HOME_PAGE,
  10. ROUTE_PARAMS
  11. } from '@/common/constants'
  12. import { cache,isOAuth2AppEnv } from '@/common/uitls'
  13. import {isEmpty} from "@/utils/is";
  14. // 生成路由表
  15. const routes = pagesJsonToRoutes(pagesJson)
  16. setRouteName(routes)
  17. const router = createRouter({
  18. routes: [...routes], // 路由表信息
  19. })
  20. export const whiteList = ['/pages/login/login','/pages/login/loginOauth2']
  21. export const loginPage = '/pages/login/login'
  22. interface CacheRoute {
  23. path: string;
  24. query: Record<string, string | number | boolean>;
  25. }
  26. export const beforEach = (to, from, next) => {
  27. const userStore = useUserStore()
  28. //update-begin-author:liusq---date:2025-03-20--for: 防止移动端oauth地址和PC不一致的问题
  29. if(to.path == '/oauth2-app/login'){
  30. let temp = location.href;
  31. location.href = temp.replace('/oauth2-app/login','/pages/login/loginOauth2')
  32. return;
  33. }
  34. //update-end-author:liusq---date:2025-03-20--for:防止移动端oauth地址和PC不一致的问题
  35. if (userStore.isLogined) {
  36. // 有登录态
  37. if(to.path === '/pages/login/loginOauth2'){
  38. //有跳转地址
  39. if(to.query && to.query.redirect){
  40. next()
  41. }else{
  42. //跳转到首页
  43. next({path: HOME_PAGE})
  44. }
  45. }
  46. //update-begin-author:liusq---date:2025-03-20--for: 流程路由逻辑
  47. let flowRoute = getPathAndQuery(to);
  48. if(flowRoute===false){
  49. let cacheRoute = cache(ROUTE_PARAMS);
  50. if(cacheRoute && !isEmpty(cacheRoute) && (cacheRoute as any)?.path == to.path
  51. && isEmpty(to.query)
  52. && !isEmpty((cacheRoute as any)?.query)){
  53. to.query = (cacheRoute as any)?.query;
  54. next({ path: to.path, query: (cacheRoute as any)?.query })
  55. }else{
  56. cache(ROUTE_PARAMS,to,1*60*10);
  57. next()
  58. }
  59. }else{
  60. next({ path: flowRoute.path, query: flowRoute.query })
  61. }
  62. //update-end-author:liusq---date:2025-03-20--for: 流程路由逻辑
  63. } else {
  64. // 无登录态
  65. if (whiteList.includes(to.path)) {
  66. // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
  67. if (to.path === '/pages/login/login' && isOAuth2AppEnv()) {
  68. next({path: '/pages/login/loginOauth2'})
  69. } else {
  70. // 在免登录白名单,直接进入
  71. next()
  72. }
  73. } else {
  74. // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
  75. let path = isOAuth2AppEnv() ? '/pages/login/loginOauth2' : '/pages/login/login';
  76. let temp = to.path;
  77. if(isOAuth2AppEnv() && temp=='/pages/flow/myTaskDetail'){
  78. if(to.query && to.query.info){
  79. temp+='?info='+to.query.info
  80. }
  81. next({ path: path, query: { redirect: encodeURIComponent(temp) } })
  82. }else{
  83. next({ path: path })
  84. }
  85. }
  86. }
  87. }
  88. // 全局前置守卫
  89. router.beforeEach(beforEach)
  90. // 路由的最后一级为路由名字不可重复
  91. function setRouteName(routes) {
  92. routes.forEach((item) => {
  93. if (item.path) {
  94. const name = item.path.split('/').pop()
  95. item.name = name
  96. }
  97. })
  98. }
  99. /**
  100. * - next会导致参数丢失loginOauth2页面的逻辑
  101. * @param to
  102. * @returns {{path: string, query: {redirect: string}}|boolean}
  103. */
  104. function getPathAndQuery(to){
  105. if(to.path === '/pages/flow/myTaskDetail'){
  106. if(to.query && to.query.info){
  107. let info = JSON.parse(to.query.info)
  108. info['hasToken'] = 1;
  109. let temp = '/pages/flow/myTaskDetail?info='+ JSON.stringify(info)
  110. return {
  111. path: '/pages/login/loginOauth2',
  112. query: { redirect: encodeURI(temp) }
  113. }
  114. }
  115. }
  116. return false
  117. }
  118. export default router