workbench.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <script setup>
  2. import { computed, onUnmounted, ref } from 'vue'
  3. import { onHide, onShow } from '@dcloudio/uni-app'
  4. import { useUserStore } from '@/stores/user'
  5. import { WORKBENCH_MODULES } from '@/config/workbench-menus'
  6. import { PAGE_PATHS, WORKBENCH_ROUTE_MAP } from '@/config/api'
  7. import { filterWorkbenchModules } from '@/utils/workbenchMenuFilter'
  8. import { getSafeAreaStyle } from '@/utils/safeArea'
  9. import { useShareToLogin } from '@/utils/shareToLogin'
  10. import { fetchWechatNoticeStatus } from '@/api/wechat-notices'
  11. import { createWechatFollowReminder } from '@/utils/wechatFollowReminder'
  12. useShareToLogin()
  13. // #ifdef MP-WEIXIN
  14. import {
  15. checkMpUpdate,
  16. formatMpUpdateLine,
  17. isMpUpdateUrgent,
  18. subscribeMpUpdate
  19. } from '@/utils/mpUpdate'
  20. // #endif
  21. const safeAreaStyle = getSafeAreaStyle()
  22. const userStore = useUserStore()
  23. const modules = computed(() =>
  24. filterWorkbenchModules(WORKBENCH_MODULES, {
  25. menuAll: userStore.menuAll,
  26. menuCodes: userStore.menuCodes
  27. })
  28. )
  29. const loggingOut = ref(false)
  30. const pageActive = ref(false)
  31. const mpUpdateLine = ref('')
  32. const mpUpdateUrgent = ref(false)
  33. const mpUpdateStatus = ref('idle')
  34. const followReminder = createWechatFollowReminder({
  35. getSessionKey: () => userStore.isLoggedIn ? userStore.token : '',
  36. isActive: () => pageActive.value && !loggingOut.value,
  37. loadStatus: fetchWechatNoticeStatus,
  38. showPrompt: (options) => new Promise((resolve, reject) => {
  39. uni.showModal({
  40. ...options,
  41. success: (result) => resolve(result.confirm === true),
  42. fail: reject
  43. })
  44. }),
  45. openGuide: () => openPage(PAGE_PATHS.wechatOfficialAccount)
  46. })
  47. const stopFollowReminder = () => {
  48. pageActive.value = false
  49. followReminder.cancel()
  50. }
  51. onHide(stopFollowReminder)
  52. onUnmounted(stopFollowReminder)
  53. // #ifdef MP-WEIXIN
  54. const unsubMpUpdate = subscribeMpUpdate((s) => {
  55. mpUpdateLine.value = formatMpUpdateLine(s)
  56. mpUpdateUrgent.value = isMpUpdateUrgent(s)
  57. mpUpdateStatus.value = s.status || 'idle'
  58. })
  59. onUnmounted(() => {
  60. unsubMpUpdate()
  61. })
  62. // #endif
  63. const displayName = computed(() => {
  64. const info = userStore.userInfo
  65. if (!info) return '未登录'
  66. return info.realName || info.username || '用户'
  67. })
  68. /** 三列宫格补齐空白格,避免末行空位露出网格底色 */
  69. const menuPadCount = (len) => {
  70. const rem = Number(len) % 3
  71. return rem === 0 ? 0 : 3 - rem
  72. }
  73. onShow(async () => {
  74. pageActive.value = true
  75. userStore.checkLoginStatus()
  76. if (!userStore.isLoggedIn) {
  77. stopFollowReminder()
  78. uni.reLaunch({ url: PAGE_PATHS.login })
  79. return
  80. }
  81. // 关注状态查询不阻塞工作台菜单加载,也不会把查询失败误判为未关注。
  82. void followReminder.check()
  83. await userStore.fetchMenuCodes()
  84. // #ifdef MP-WEIXIN
  85. checkMpUpdate()
  86. // #endif
  87. })
  88. const handleMpUpdateTap = () => {
  89. // #ifdef MP-WEIXIN
  90. checkMpUpdate({ force: true, fromUser: true })
  91. // #endif
  92. }
  93. /** 含 web-view 的页用 redirectTo,避免真机 navigateTo:fail timeout 闪进又弹回工作台 */
  94. const isMpWebviewRoute = (url) =>
  95. String(url || '').includes('/pages/common/mp-webview')
  96. const openPage = (url) => {
  97. const failToast = () => uni.showToast({ title: '打开失败,请重试', icon: 'none' })
  98. if (isMpWebviewRoute(url)) {
  99. const ret = uni.redirectTo({
  100. url,
  101. fail: (err) => {
  102. // 栈异常时再退到 reLaunch
  103. const msg = String((err && err.errMsg) || '')
  104. if (/fail/i.test(msg)) {
  105. uni.reLaunch({ url, fail: failToast })
  106. return
  107. }
  108. failToast()
  109. }
  110. })
  111. if (ret && typeof ret.catch === 'function') ret.catch(() => {})
  112. return
  113. }
  114. const fail = (err) => {
  115. const msg = String((err && err.errMsg) || '')
  116. if (/timeout/i.test(msg)) {
  117. uni.redirectTo({ url, fail: failToast })
  118. return
  119. }
  120. failToast()
  121. }
  122. const ret = uni.navigateTo({ url, fail })
  123. if (ret && typeof ret.catch === 'function') {
  124. ret.catch(() => {})
  125. }
  126. }
  127. const openMenu = (module, menu) => {
  128. if (!menu.enabled) {
  129. uni.showToast({ title: '本期暂未开放', icon: 'none' })
  130. return
  131. }
  132. const route = WORKBENCH_ROUTE_MAP[menu.code]
  133. if (route) {
  134. openPage(route)
  135. return
  136. }
  137. const title = encodeURIComponent(menu.name || '')
  138. const moduleTitle = encodeURIComponent(module.title || '')
  139. const code = encodeURIComponent(menu.code || '')
  140. openPage(`${PAGE_PATHS.bizPlaceholder}?title=${title}&module=${moduleTitle}&code=${code}`)
  141. }
  142. const handleLogout = async () => {
  143. if (loggingOut.value) return
  144. loggingOut.value = true
  145. stopFollowReminder()
  146. try {
  147. await userStore.logout()
  148. } finally {
  149. loggingOut.value = false
  150. }
  151. }
  152. const openOfficialAccount = () => {
  153. if (loggingOut.value) return
  154. openPage(PAGE_PATHS.wechatOfficialAccount)
  155. }
  156. </script>
  157. <template>
  158. <view class="mp-page mp-page--tab">
  159. <view class="mp-header mp-header--workbench" :style="safeAreaStyle">
  160. <view class="mp-header__body">
  161. <view class="mp-header__titles">
  162. <text class="mp-header__title">山峡化工工作台</text>
  163. <!-- #ifdef MP-WEIXIN -->
  164. <view
  165. v-if="mpUpdateLine"
  166. class="mp-header-version"
  167. :class="{
  168. 'mp-header-version--urgent': mpUpdateUrgent,
  169. 'mp-header-version--ready': mpUpdateStatus === 'ready',
  170. 'mp-header-version--failed': mpUpdateStatus === 'failed'
  171. }"
  172. hover-class="mp-header-version--active"
  173. @click.stop="handleMpUpdateTap"
  174. >
  175. <text class="mp-header-version__text">{{ mpUpdateLine }}</text>
  176. <text v-if="mpUpdateStatus === 'ready'" class="mp-header-version__cta">立即更新</text>
  177. </view>
  178. <!-- #endif -->
  179. </view>
  180. <view class="mp-header__user-stack">
  181. <text class="mp-header__user-line">{{ displayName }},欢迎您</text>
  182. <text
  183. class="mp-header__logout"
  184. :class="{ 'mp-header__logout--disabled': loggingOut }"
  185. @click="handleLogout"
  186. >
  187. {{ loggingOut ? '退出中…' : '退出登录' }}
  188. </text>
  189. </view>
  190. </view>
  191. <button
  192. class="mp-header__follow"
  193. :disabled="loggingOut"
  194. hover-class="mp-header__follow--active"
  195. @click="openOfficialAccount"
  196. >
  197. 关注服务号
  198. </button>
  199. </view>
  200. <view class="mp-section">
  201. <view
  202. v-for="mod in modules"
  203. :key="mod.key"
  204. class="mp-module-block"
  205. :class="'mp-module-block--' + mod.key"
  206. >
  207. <view class="mp-module-block__head">
  208. <text class="mp-module-block__icon">{{ mod.iconText }}</text>
  209. <view class="mp-module-block__title-wrap">
  210. <text class="mp-module-block__title">{{ mod.title }}</text>
  211. <text class="mp-module-block__desc">{{ mod.desc }}</text>
  212. </view>
  213. </view>
  214. <view class="mp-menu-grid">
  215. <view
  216. v-for="(menu, idx) in mod.menus"
  217. :key="menu.code || menu.name + idx"
  218. class="mp-menu-card"
  219. :class="{ 'mp-menu-card--disabled': !menu.enabled }"
  220. @click="openMenu(mod, menu)"
  221. >
  222. <view class="mp-menu-card__icon">{{ menu.icon || '' }}</view>
  223. <text class="mp-menu-card__name">{{ menu.name }}</text>
  224. </view>
  225. <view
  226. v-for="i in menuPadCount(mod.menus.length)"
  227. :key="'pad-' + mod.key + '-' + i"
  228. class="mp-menu-card mp-menu-card--pad"
  229. />
  230. </view>
  231. </view>
  232. </view>
  233. <app-env-badge />
  234. </view>
  235. </template>
  236. <style scoped lang="scss">
  237. .mp-header--workbench {
  238. position: relative;
  239. padding-bottom: 68rpx;
  240. }
  241. .mp-header__follow {
  242. position: absolute;
  243. right: 32rpx;
  244. bottom: 20rpx;
  245. display: flex;
  246. align-items: center;
  247. justify-content: center;
  248. min-height: 0;
  249. height: auto;
  250. margin: 0;
  251. padding: 6rpx 16rpx;
  252. border: none;
  253. border-radius: 999rpx;
  254. background: #d6a343;
  255. color: #fff;
  256. font-size: 22rpx;
  257. font-weight: 600;
  258. box-shadow: 0 4rpx 14rpx rgba(0, 0, 0, 0.14);
  259. line-height: 1.3;
  260. }
  261. .mp-header__follow::after {
  262. border: none;
  263. }
  264. .mp-header__follow--active {
  265. background: #e8ba60;
  266. }
  267. .mp-header__follow[disabled] {
  268. color: rgba(255, 255, 255, 0.55);
  269. background: rgba(255, 255, 255, 0.08);
  270. }
  271. /* 标题下方版本行:常态半透明白字;有更新时大麦黄底醒目 */
  272. .mp-header-version {
  273. margin-top: 12rpx;
  274. max-width: 100%;
  275. min-height: 48rpx;
  276. padding: 6rpx 0;
  277. display: flex;
  278. flex-direction: row;
  279. flex-wrap: wrap;
  280. align-items: center;
  281. gap: 10rpx;
  282. box-sizing: border-box;
  283. }
  284. .mp-header-version--active {
  285. opacity: 0.88;
  286. }
  287. .mp-header-version__text {
  288. font-size: 22rpx;
  289. line-height: 1.4;
  290. color: rgba(255, 255, 255, 0.88);
  291. text-align: left;
  292. }
  293. .mp-header-version--urgent {
  294. margin-top: 14rpx;
  295. padding: 10rpx 16rpx;
  296. border-radius: 12rpx;
  297. background: #fff7e8;
  298. border: 1rpx solid rgba(214, 163, 67, 0.55);
  299. min-height: 64rpx;
  300. }
  301. .mp-header-version--urgent .mp-header-version__text {
  302. color: #8a5a10;
  303. font-size: 24rpx;
  304. font-weight: 600;
  305. }
  306. .mp-header-version--ready {
  307. background: #fff3cd;
  308. border-color: #d6a343;
  309. animation: mp-version-pulse 1.6s ease-in-out infinite;
  310. }
  311. .mp-header-version--ready .mp-header-version__text {
  312. color: #7a4e08;
  313. }
  314. .mp-header-version__cta {
  315. flex-shrink: 0;
  316. font-size: 22rpx;
  317. font-weight: 700;
  318. line-height: 1.2;
  319. color: #fff;
  320. background: #d6a343;
  321. padding: 6rpx 14rpx;
  322. border-radius: 999rpx;
  323. }
  324. .mp-header-version--failed {
  325. background: #fef2f2;
  326. border-color: #fecaca;
  327. }
  328. .mp-header-version--failed .mp-header-version__text {
  329. color: #b91c1c;
  330. font-weight: 600;
  331. }
  332. @keyframes mp-version-pulse {
  333. 0%,
  334. 100% {
  335. box-shadow: 0 0 0 0 rgba(214, 163, 67, 0.35);
  336. }
  337. 50% {
  338. box-shadow: 0 0 0 8rpx rgba(214, 163, 67, 0);
  339. }
  340. }
  341. </style>