update-paths.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import os
  2. import re
  3. ROOT = "D:/work/绿水青山/清道夫App/src"
  4. # 路径替换映射:旧路径 -> 新路径
  5. PATH_REPLACEMENTS = {
  6. "/pages/common/projectList": "/subPackages/pages-common/projectList",
  7. "/pages/common/projectDetail": "/subPackages/pages-common/projectDetail",
  8. "/pages/common/addProject": "/subPackages/pages-common/addProject",
  9. "/pages/common/publishTask": "/subPackages/pages-common/publishTask",
  10. "/pages/common/taskDetail": "/subPackages/pages-common/taskDetail",
  11. "/pages/common/changePassword": "/subPackages/pages-common/changePassword",
  12. "/pages/common/noticeList": "/subPackages/pages-common/noticeList",
  13. "/pages/common/noticeDetail": "/subPackages/pages-common/noticeDetail",
  14. "/pages/common/messageList": "/subPackages/pages-common/messageList",
  15. "/pages/common/messageDetail": "/subPackages/pages-common/messageDetail",
  16. "/pages/common/helpCenter": "/subPackages/pages-common/helpCenter",
  17. "/pages/common/knowledgeList": "/subPackages/pages-common/knowledgeList",
  18. "/pages/common/knowledgeDetail": "/subPackages/pages-common/knowledgeDetail",
  19. "/pages/sales/taskReminder": "/subPackages/pages-sales/taskReminder",
  20. "/pages/sales/notification": "/subPackages/pages-sales/notification",
  21. "/pages/sales/knowledgeList": "/subPackages/pages-sales/knowledgeList",
  22. "/pages/sales/knowledgeDetail": "/subPackages/pages-sales/knowledgeDetail",
  23. "/pages/sales/messageList": "/subPackages/pages-sales/messageList",
  24. "/pages/sales/messageDetail": "/subPackages/pages-sales/messageDetail",
  25. "/pages/dispatch/schedule": "/subPackages/pages-dispatch/schedule",
  26. "/pages/dispatch/taskDetail": "/subPackages/pages-dispatch/taskDetail",
  27. "/pages/dispatch/publishTask": "/subPackages/pages-dispatch/publishTask",
  28. "/pages/dispatch/vehicleList": "/subPackages/pages-dispatch/vehicleList",
  29. "/pages/dispatch/availableVehicles": "/subPackages/pages-dispatch/availableVehicles",
  30. "/pages/dispatch/personnel": "/subPackages/pages-dispatch/personnel",
  31. "/pages/dispatch/onDutyStaff": "/subPackages/pages-dispatch/onDutyStaff",
  32. "/pages/dispatch/emergencyTeam": "/subPackages/pages-dispatch/emergencyTeam",
  33. "/pages/dispatch/emergencyStandby": "/subPackages/pages-dispatch/emergencyStandby",
  34. "/pages/dispatch/visualization": "/subPackages/pages-dispatch/visualization",
  35. "/pages/dispatch/noticeList": "/subPackages/pages-dispatch/noticeList",
  36. "/pages/dispatch/noticeDetail": "/subPackages/pages-dispatch/noticeDetail",
  37. "/pages/dispatch/notification": "/subPackages/pages-dispatch/notification",
  38. "/pages/construction/taskDetail": "/subPackages/pages-construction/taskDetail",
  39. "/pages/construction/dailyTask": "/subPackages/pages-construction/dailyTask",
  40. "/pages/construction/historyTask": "/subPackages/pages-construction/historyTask",
  41. "/pages/construction/tomorrowTask": "/subPackages/pages-construction/tomorrowTask",
  42. "/pages/construction/noticeList": "/subPackages/pages-construction/noticeList",
  43. "/pages/construction/noticeDetail": "/subPackages/pages-construction/noticeDetail",
  44. "/pages/construction/notification": "/subPackages/pages-construction/notification",
  45. # 被删除/合并的页面统一指向 common
  46. "/pages/sales/changePassword": "/subPackages/pages-common/changePassword",
  47. "/pages/sales/helpCenter": "/subPackages/pages-common/helpCenter",
  48. "/pages/dispatch/changePassword": "/subPackages/pages-common/changePassword",
  49. "/pages/dispatch/helpCenter": "/subPackages/pages-common/helpCenter",
  50. "/pages/construction/changePassword": "/subPackages/pages-common/changePassword",
  51. "/pages/construction/helpCenter": "/subPackages/pages-common/helpCenter",
  52. # tabBar 页
  53. "/pages/common/taskList": "/pages/task/task",
  54. "/pages/common/profile": "/pages/my/my",
  55. "/pages/sales/home": "/pages/home/home",
  56. "/pages/dispatch/home": "/pages/home/home",
  57. "/pages/dispatch/taskList": "/pages/task/task",
  58. "/pages/dispatch/myPage": "/pages/my/my",
  59. "/pages/construction/home": "/pages/home/home",
  60. "/pages/construction/taskList": "/pages/task/task",
  61. "/pages/construction/myPage": "/pages/my/my",
  62. }
  63. # tabBar 目标路径,需要把 navigateTo/reLaunch 改为 switchTab
  64. TABBAR_PATHS = [
  65. "/pages/home/home",
  66. "/pages/task/task",
  67. "/pages/my/my",
  68. ]
  69. # 扫描的文件后缀
  70. EXTS = (".vue", ".ts", ".js")
  71. def replace_paths(content: str) -> str:
  72. # 1. 替换路径字符串
  73. for old, new in PATH_REPLACEMENTS.items():
  74. content = content.replace(old, new)
  75. return content
  76. def fix_tabbar_navigation(content: str) -> str:
  77. # 把指向 tabBar 页面的 navigateTo/reLaunch 改成 switchTab
  78. # 匹配 uni.navigateTo({ url: '...' }) 或 uni.reLaunch({ url: '...' })
  79. pattern = re.compile(
  80. r"uni\.(navigateTo|reLaunch)\(\{\s*url\s*:\s*['\"]([" + "".join(re.escape(p) for p in TABBAR_PATHS) + r"][^'\"]*)['\"]\s*\}\)"
  81. )
  82. def replacer(match):
  83. method = match.group(1)
  84. url = match.group(2)
  85. return f"uni.switchTab({{ url: '{url}' }})"
  86. # 由于 TABBAR_PATHS 是固定的简单字符串,直接用更简单的方式
  87. for path in TABBAR_PATHS:
  88. for method in ("navigateTo", "reLaunch"):
  89. content = re.sub(
  90. rf"uni\.{method}\(\{{\s*url\s*:\s*['\"]{re.escape(path)}[^'\"]*['\"]\s*\}}\)",
  91. lambda m, p=path: f"uni.switchTab({{ url: '{p}' }})",
  92. content,
  93. )
  94. return content
  95. def process_file(filepath: str):
  96. with open(filepath, "r", encoding="utf-8") as f:
  97. content = f.read()
  98. new_content = replace_paths(content)
  99. new_content = fix_tabbar_navigation(new_content)
  100. if new_content != content:
  101. with open(filepath, "w", encoding="utf-8") as f:
  102. f.write(new_content)
  103. print(f"Updated: {filepath}")
  104. def main():
  105. for dirpath, _, filenames in os.walk(ROOT):
  106. for filename in filenames:
  107. if filename.endswith(EXTS):
  108. process_file(os.path.join(dirpath, filename))
  109. if __name__ == "__main__":
  110. main()