cwg-right-drawer.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view class="notice-container">
  3. <cwg-dropdown ref="dropdownRef" :menu-list="[]">
  4. <view class="pc-header-btn">
  5. <cwg-icon name="icon_my" color="#141d22" @click="openNotice" />
  6. </view>
  7. <template #btn>
  8. <view class="right-drawer">
  9. <view class="drawer-header">
  10. <image class="avatar" src="/static/images/avatars.png" mode="aspectFill" />
  11. <view class="user-info">
  12. <text class="name">{{ _displayName }}</text>
  13. <text class="cid">CID: {{ _displayCid }}</text>
  14. </view>
  15. </view>
  16. <view class="menu-list">
  17. <view v-for="item in menuList" :key="item.id" class="menu-item"
  18. :class="{ active: _activePath === item.path }" @click="handleNavigate(item.path)">
  19. <cwg-icon :name="item.icon" :size="16" color="#0f172b" />
  20. <text v-t="item.name"></text>
  21. </view>
  22. </view>
  23. <view class="logout-wrap">
  24. <view class="logout-btn" @click="handleLogout">
  25. <cwg-icon name="logout" :size="16" color="#ff9800" />
  26. <text v-t="'language.i6'"></text>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. </cwg-dropdown>
  32. </view>
  33. </template>
  34. <script setup lang="ts">
  35. import { ref, watch, onMounted } from 'vue'
  36. import { onLoad, onShow, onLaunch } from '@dcloudio/uni-app'
  37. import useRoute from '@/hooks/useRoute'
  38. import useUserStore from '@/stores/use-user-store'
  39. import { userApi } from '@/api/user'
  40. import { useI18n } from "vue-i18n"
  41. import useRouter from "@/hooks/useRouter"
  42. const { t } = useI18n()
  43. const router = useRouter()
  44. const dropdownRef = ref(null)
  45. const userStore = useUserStore()
  46. const route = useRoute()
  47. // 强制用 ref 让插槽能渲染
  48. const menuList = ref([])
  49. const _displayName = ref('--')
  50. const _displayCid = ref('--')
  51. const _activePath = ref('')
  52. // 初始化菜单
  53. function initMenu() {
  54. menuList.value = [
  55. { id: 1, path: '/pages/mine/info?type=1', name: 'PersonalManagement.Title.PersonalInformation', icon: 'crm-circle-user' },
  56. { id: 2, path: '/pages/mine/info?type=2', name: 'PersonalManagement.Title.BankInformation', icon: 'crm-building-columns' },
  57. { id: 3, path: '/pages/mine/info?type=3', name: 'PersonalManagement.Title.FileManagement', icon: 'crm-file' },
  58. { id: 4, path: '/pages/mine/info?type=4', name: 'PersonalManagement.Title.SecurityCenter', icon: 'crm-lock' },
  59. ]
  60. }
  61. // 强制同步用户信息
  62. function syncUserInfo() {
  63. const info = userStore.userInfo?.customInfo || {}
  64. const firstName = info.firstName || ''
  65. const lastName = info.lastName || ''
  66. _displayName.value = (firstName + ' ' + lastName).trim() || info.name || info.email || '--'
  67. _displayCid.value = info.cId || info.id || '--'
  68. }
  69. // 强制同步路径
  70. function syncPath() {
  71. _activePath.value = route.path + (route.query?.type ? `?type=${route.query.type}` : '')
  72. }
  73. onMounted(() => {
  74. initMenu()
  75. syncUserInfo()
  76. syncPath()
  77. })
  78. onShow(() => {
  79. initMenu()
  80. syncUserInfo()
  81. syncPath()
  82. })
  83. // 监听变化自动更新
  84. watch(() => userStore.userInfo, () => {
  85. syncUserInfo()
  86. }, { deep: true })
  87. watch(() => route, () => {
  88. syncPath()
  89. }, { deep: true })
  90. // 打开抽屉
  91. function openNotice() {
  92. // dropdownRef.value?.open()
  93. }
  94. // 关闭
  95. function close() {
  96. dropdownRef.value?.close()
  97. }
  98. // 跳转
  99. function handleNavigate(path) {
  100. router.push({ path })
  101. close()
  102. }
  103. // 登出
  104. async function handleLogout() {
  105. try {
  106. await userApi.logout()
  107. } catch (e) { }
  108. userStore.clearUserInfo()
  109. router.push('/pages/login/index')
  110. close()
  111. }
  112. defineExpose({ openNotice, close })
  113. </script>
  114. <style scoped lang="scss">
  115. @import "@/uni.scss";
  116. .notice-container {
  117. :deep(.cwg-dropdown-menu-container) {
  118. left: px2rpx(-280) !important;
  119. right: px2rpx(0) !important;
  120. }
  121. @media screen and (max-width: 991px) {
  122. :deep(.cwg-dropdown-menu-container) {
  123. left: px2rpx(-270) !important;
  124. max-width: px2rpx(400);
  125. }
  126. }
  127. .pc-header-btn {
  128. position: relative;
  129. }
  130. .right-drawer {
  131. width: px2rpx(300);
  132. background-color: var(--color-white);
  133. display: flex;
  134. flex-direction: column;
  135. padding: 20px 16px;
  136. box-sizing: border-box;
  137. }
  138. .drawer-header {
  139. display: flex;
  140. align-items: center;
  141. gap: 12px;
  142. padding: 20px 16px;
  143. border-bottom: 1px solid #d9dde5;
  144. }
  145. .avatar {
  146. width: 76px;
  147. height: 76px;
  148. border-radius: 12px;
  149. background: #fff;
  150. }
  151. .user-info {
  152. display: flex;
  153. flex-direction: column;
  154. gap: 6px;
  155. }
  156. .name {
  157. font-size: 20px;
  158. font-weight: 600;
  159. color: #334155;
  160. }
  161. .cid {
  162. font-size: 14px;
  163. color: #ef4444;
  164. }
  165. .menu-list {
  166. padding: 12px 0;
  167. }
  168. .menu-item {
  169. height: 48px;
  170. display: flex;
  171. align-items: center;
  172. gap: 10px;
  173. padding: 0 16px;
  174. color: #0f172b;
  175. font-size: 16px;
  176. font-weight: 600;
  177. &:hover {
  178. background-color: rgba(0, 0, 0, 0.05);
  179. }
  180. }
  181. .menu-item.active {
  182. background: rgba(108, 133, 149, 0.12) !important;
  183. border-radius: 0.125rem;
  184. }
  185. .logout-wrap {
  186. margin-top: auto;
  187. padding: 20px 16px;
  188. margin-bottom: 20px;
  189. }
  190. .logout-btn {
  191. height: 44px;
  192. background: #f4eadf;
  193. display: flex;
  194. align-items: center;
  195. justify-content: center;
  196. gap: 8px;
  197. color: #ff9800;
  198. font-weight: 600;
  199. cursor: pointer;
  200. }
  201. }
  202. </style>