router.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * 路由管理工具类
  3. * 支持动态路由跳转,无需在pages.json中预先配置
  4. */
  5. // 服务路由映射配置
  6. const SERVICE_ROUTES = {
  7. // 学工管理
  8. 'student:info': '/pages/xg/xsjcxx/xsjcxx/index',
  9. 'student:pwdreset': '/pages/xg/xsjcxx/studentPwdReset/index',
  10. 'student:dorm': '/pages/student/dorm/index',
  11. 'student:leave': '/pages/xg/xsjcxx/xsqj/leave/index',
  12. 'student:psychology': '/pages/student/psychology/index',
  13. 'student:club': '/pages/student/club/index',
  14. 'student:counsel': '/pages/student/counsel/index',
  15. // 后勤管理
  16. 'logistics:repair': '/pages/logistics/repair/index',
  17. 'logistics:venue': '/pages/logistics/venue/index',
  18. 'logistics:hall': '/pages/logistics/hall/index',
  19. 'logistics:survey': '/pages/logistics/survey/index',
  20. 'logistics:canteen': '/pages/logistics/canteen/index',
  21. 'logistics:property': '/pages/logistics/property/index',
  22. // 招生就业
  23. 'recruitment:student': '/pages/recruitment/student/index',
  24. 'recruitment:graduate': '/pages/recruitment/graduate/index',
  25. 'recruitment:job': '/pages/recruitment/job/index',
  26. // 人事管理
  27. 'hr:staff': '/pages/hr/staff/index',
  28. 'hr:abroad': '/pages/hr/abroad/index',
  29. 'hr:training': '/pages/hr/training/index',
  30. // 财务管理
  31. 'finance:budget': '/pages/finance/budget/index',
  32. 'finance:expense': '/pages/finance/expense/index',
  33. 'finance:invoice': '/pages/finance/invoice/index',
  34. // 系统管理
  35. 'system:user': '/pages/system/user/index',
  36. 'system:role': '/pages/system/role/index',
  37. 'system:menu': '/pages/system/menu/index',
  38. 'system:dept': '/pages/system/dept/index',
  39. 'system:config': '/pages/system/config/index',
  40. 'monitor:online': '/pages/monitor/online/index',
  41. };
  42. // 服务名称映射
  43. const SERVICE_NAMES = {
  44. 学生信息: 'student:info',
  45. 重置学生密码: 'student:pwdreset',
  46. 宿舍管理: 'student:dorm',
  47. 学生请假: 'student:leave',
  48. 心理健康: 'student:psychology',
  49. 社团管理: 'student:club',
  50. 心理咨询: 'student:counsel',
  51. 物业报修: 'logistics:repair',
  52. 场地预约: 'logistics:venue',
  53. 报告厅预约: 'logistics:hall',
  54. 满意度调查: 'logistics:survey',
  55. 食堂管理: 'logistics:canteen',
  56. 物业管理: 'logistics:property',
  57. 学生管理: 'recruitment:student',
  58. 毕业生信息: 'recruitment:graduate',
  59. 就业信息: 'recruitment:job',
  60. 员工管理: 'hr:staff',
  61. 出国申请: 'hr:abroad',
  62. 培训管理: 'hr:training',
  63. 预算管理: 'finance:budget',
  64. 费用报销: 'finance:expense',
  65. 发票管理: 'finance:invoice',
  66. 用户管理: 'system:user',
  67. 角色管理: 'system:role',
  68. 菜单管理: 'system:menu',
  69. 部门管理: 'system:dept',
  70. 参数设置: 'system:config',
  71. 在线用户: 'monitor:online',
  72. };
  73. /**
  74. * 路由管理类
  75. */
  76. class RouterManager {
  77. /**
  78. * 根据服务名称跳转到对应页面
  79. * @param {string} serviceName - 服务名称
  80. * @param {object} options - 跳转选项
  81. */
  82. static navigateToService(serviceName, options = {}) {
  83. const serviceKey = SERVICE_NAMES[serviceName];
  84. if (!serviceKey) {
  85. this.showServiceNotConfigured(serviceName);
  86. return;
  87. }
  88. const route = SERVICE_ROUTES[serviceKey];
  89. if (!route) {
  90. this.showServiceNotConfigured(serviceName);
  91. return;
  92. }
  93. // 构建完整URL(支持参数传递)
  94. let url = route;
  95. if (options.params) {
  96. const queryString = Object.keys(options.params)
  97. .map((key) => `${key}=${encodeURIComponent(options.params[key])}`)
  98. .join('&');
  99. url += `?${queryString}`;
  100. }
  101. // 执行跳转
  102. uni.navigateTo({
  103. url,
  104. success: () => {
  105. console.log(`✅ 成功跳转到: ${url}`);
  106. if (options.onSuccess) {
  107. options.onSuccess();
  108. }
  109. },
  110. fail: (error) => {
  111. console.warn(`⚠️ 页面跳转失败: ${url}`, error);
  112. this.handlePageNotExist(serviceName, route, options);
  113. },
  114. });
  115. }
  116. /**
  117. * 处理页面不存在的情况
  118. * @param {string} serviceName - 服务名称
  119. * @param {string} route - 路由路径
  120. * @param {object} options - 选项
  121. */
  122. static handlePageNotExist(serviceName, route, options = {}) {
  123. uni.showModal({
  124. title: '页面不存在',
  125. content: `${serviceName}页面尚未创建,是否创建该页面?`,
  126. confirmText: '创建页面',
  127. cancelText: '取消',
  128. success: (res) => {
  129. if (res.confirm) {
  130. this.createPageTemplate(serviceName, route, options);
  131. } else {
  132. this.showServiceNotImplemented(serviceName);
  133. }
  134. },
  135. });
  136. }
  137. /**
  138. * 创建页面模板
  139. * @param {string} serviceName - 服务名称
  140. * @param {string} route - 路由路径
  141. * @param {object} options - 选项
  142. */
  143. static createPageTemplate(serviceName, route, options = {}) {
  144. // 这里可以实现页面创建逻辑
  145. // 1. 调用后端API创建页面文件
  146. // 2. 跳转到页面创建向导
  147. // 3. 生成标准页面模板
  148. console.log(`📝 需要创建页面: ${route} - ${serviceName}`);
  149. // 临时提示
  150. uni.showToast({
  151. title: '页面创建功能开发中',
  152. icon: 'none',
  153. duration: 2000,
  154. });
  155. // 可以在这里添加实际的页面创建逻辑
  156. if (options.onCreatePage) {
  157. options.onCreatePage(serviceName, route);
  158. }
  159. }
  160. /**
  161. * 显示服务未配置提示
  162. * @param {string} serviceName - 服务名称
  163. */
  164. static showServiceNotConfigured(serviceName) {
  165. uni.showToast({
  166. title: `服务未配置: ${serviceName}`,
  167. icon: 'none',
  168. duration: 2000,
  169. });
  170. }
  171. /**
  172. * 显示服务未实现提示
  173. * @param {string} serviceName - 服务名称
  174. */
  175. static showServiceNotImplemented(serviceName) {
  176. uni.showToast({
  177. title: `功能开发中: ${serviceName}`,
  178. icon: 'none',
  179. duration: 2000,
  180. });
  181. }
  182. /**
  183. * 检查页面是否存在
  184. * @param {string} route - 路由路径
  185. * @returns {Promise<boolean>}
  186. */
  187. static async checkPageExists(route) {
  188. return new Promise((resolve) => {
  189. // 尝试跳转但不实际跳转,用于检查页面是否存在
  190. uni.navigateTo({
  191. url: route,
  192. success: () => {
  193. // 立即返回,避免实际跳转
  194. uni.navigateBack();
  195. resolve(true);
  196. },
  197. fail: () => {
  198. resolve(false);
  199. },
  200. });
  201. });
  202. }
  203. /**
  204. * 获取所有可用的服务路由
  205. * @returns {object}
  206. */
  207. static getAvailableRoutes() {
  208. return { ...SERVICE_ROUTES };
  209. }
  210. /**
  211. * 获取服务名称映射
  212. * @returns {object}
  213. */
  214. static getServiceNames() {
  215. return { ...SERVICE_NAMES };
  216. }
  217. }
  218. export default RouterManager;