cwg-right-drawer.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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: <text class="cwg-cursor" @click="copy(_displayCid)">{{ _displayCid }}</text></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. const copy = (text: string) => {
  54. uni.setClipboardData({
  55. data: text,
  56. success: function () {
  57. uni.showToast({
  58. title: t('Btn.item8'),
  59. icon: 'none',
  60. duration: 2000
  61. });
  62. }
  63. });
  64. };
  65. // 初始化菜单
  66. function initMenu() {
  67. menuList.value = [
  68. { id: 1, path: '/pages/mine/info?type=1', name: 'PersonalManagement.Title.PersonalInformation', icon: 'crm-circle-user' },
  69. { id: 2, path: '/pages/mine/info?type=2', name: 'PersonalManagement.Title.BankInformation', icon: 'crm-building-columns' },
  70. { id: 3, path: '/pages/mine/info?type=3', name: 'PersonalManagement.Title.FileManagement', icon: 'crm-file' },
  71. { id: 4, path: '/pages/mine/info?type=4', name: 'PersonalManagement.Title.SecurityCenter', icon: 'crm-lock' },
  72. ]
  73. }
  74. // 强制同步用户信息
  75. function syncUserInfo() {
  76. const info = userStore.userInfo?.customInfo || {}
  77. const firstName = info.firstName || ''
  78. const lastName = info.lastName || ''
  79. _displayName.value = (firstName + ' ' + lastName).trim() || info.name || info.email || '--'
  80. _displayCid.value = info.cId || info.id || '--'
  81. }
  82. // 强制同步路径
  83. function syncPath() {
  84. _activePath.value = route.path + (route.query?.type ? `?type=${route.query.type}` : '')
  85. }
  86. onMounted(() => {
  87. initMenu()
  88. syncUserInfo()
  89. syncPath()
  90. })
  91. onShow(() => {
  92. initMenu()
  93. syncUserInfo()
  94. syncPath()
  95. })
  96. // 监听变化自动更新
  97. watch(() => userStore.userInfo, () => {
  98. syncUserInfo()
  99. }, { deep: true })
  100. watch(() => route, () => {
  101. syncPath()
  102. }, { deep: true })
  103. // 打开抽屉
  104. function openNotice() {
  105. // dropdownRef.value?.open()
  106. }
  107. // 关闭
  108. function close() {
  109. dropdownRef.value?.close()
  110. }
  111. // 跳转
  112. function handleNavigate(path) {
  113. router.push({ path })
  114. close()
  115. }
  116. // 登出
  117. async function handleLogout() {
  118. try {
  119. await userApi.logout()
  120. } catch (e) { }
  121. userStore.clearUserInfo()
  122. uni.$emit('updateSystemList')
  123. router.push('/pages/login/index')
  124. close()
  125. }
  126. defineExpose({ openNotice, close })
  127. </script>
  128. <style scoped lang="scss">
  129. @import "@/uni.scss";
  130. .notice-container {
  131. :deep(.cwg-dropdown-menu-container) {
  132. left: px2rpx(-280) !important;
  133. right: px2rpx(0) !important;
  134. }
  135. @media screen and (max-width: 991px) {
  136. :deep(.cwg-dropdown-menu-container) {
  137. left: px2rpx(-270) !important;
  138. max-width: px2rpx(400);
  139. }
  140. }
  141. .pc-header-btn {
  142. position: relative;
  143. }
  144. .right-drawer {
  145. width: px2rpx(300);
  146. background-color: var(--color-white);
  147. display: flex;
  148. flex-direction: column;
  149. padding: 20px 16px;
  150. box-sizing: border-box;
  151. }
  152. .drawer-header {
  153. display: flex;
  154. align-items: center;
  155. gap: 12px;
  156. padding: 20px 16px;
  157. border-bottom: 1px solid #d9dde5;
  158. }
  159. .avatar {
  160. width: 76px;
  161. height: 76px;
  162. border-radius: 12px;
  163. background: #fff;
  164. }
  165. .user-info {
  166. display: flex;
  167. flex-direction: column;
  168. gap: 6px;
  169. }
  170. .name {
  171. font-size: 20px;
  172. font-weight: 600;
  173. color: #334155;
  174. }
  175. .cid {
  176. font-size: 14px;
  177. color: #ef4444;
  178. }
  179. .menu-list {
  180. padding: 12px 0;
  181. }
  182. .menu-item {
  183. height: 48px;
  184. display: flex;
  185. align-items: center;
  186. gap: 10px;
  187. padding: 0 16px;
  188. color: #0f172b;
  189. font-size: 16px;
  190. font-weight: 600;
  191. &:hover {
  192. background-color: rgba(0, 0, 0, 0.05);
  193. }
  194. }
  195. .menu-item.active {
  196. background: rgba(108, 133, 149, 0.12) !important;
  197. border-radius: 0.125rem;
  198. }
  199. .logout-wrap {
  200. margin-top: auto;
  201. padding: 20px 16px;
  202. margin-bottom: 20px;
  203. }
  204. .logout-btn {
  205. height: 44px;
  206. background: #f4eadf;
  207. display: flex;
  208. align-items: center;
  209. justify-content: center;
  210. gap: 8px;
  211. color: #ff9800;
  212. font-weight: 600;
  213. cursor: pointer;
  214. }
  215. }
  216. </style>