cwg-right-drawer.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. router.push('/pages/login/index')
  123. close()
  124. }
  125. defineExpose({ openNotice, close })
  126. </script>
  127. <style scoped lang="scss">
  128. @import "@/uni.scss";
  129. .notice-container {
  130. :deep(.cwg-dropdown-menu-container) {
  131. left: px2rpx(-280) !important;
  132. right: px2rpx(0) !important;
  133. }
  134. @media screen and (max-width: 991px) {
  135. :deep(.cwg-dropdown-menu-container) {
  136. left: px2rpx(-270) !important;
  137. max-width: px2rpx(400);
  138. }
  139. }
  140. .pc-header-btn {
  141. position: relative;
  142. }
  143. .right-drawer {
  144. width: px2rpx(300);
  145. background-color: var(--color-white);
  146. display: flex;
  147. flex-direction: column;
  148. padding: 20px 16px;
  149. box-sizing: border-box;
  150. }
  151. .drawer-header {
  152. display: flex;
  153. align-items: center;
  154. gap: 12px;
  155. padding: 20px 16px;
  156. border-bottom: 1px solid #d9dde5;
  157. }
  158. .avatar {
  159. width: 76px;
  160. height: 76px;
  161. border-radius: 12px;
  162. background: #fff;
  163. }
  164. .user-info {
  165. display: flex;
  166. flex-direction: column;
  167. gap: 6px;
  168. }
  169. .name {
  170. font-size: 20px;
  171. font-weight: 600;
  172. color: #334155;
  173. }
  174. .cid {
  175. font-size: 14px;
  176. color: #ef4444;
  177. }
  178. .menu-list {
  179. padding: 12px 0;
  180. }
  181. .menu-item {
  182. height: 48px;
  183. display: flex;
  184. align-items: center;
  185. gap: 10px;
  186. padding: 0 16px;
  187. color: #0f172b;
  188. font-size: 16px;
  189. font-weight: 600;
  190. &:hover {
  191. background-color: rgba(0, 0, 0, 0.05);
  192. }
  193. }
  194. .menu-item.active {
  195. background: rgba(108, 133, 149, 0.12) !important;
  196. border-radius: 0.125rem;
  197. }
  198. .logout-wrap {
  199. margin-top: auto;
  200. padding: 20px 16px;
  201. margin-bottom: 20px;
  202. }
  203. .logout-btn {
  204. height: 44px;
  205. background: #f4eadf;
  206. display: flex;
  207. align-items: center;
  208. justify-content: center;
  209. gap: 8px;
  210. color: #ff9800;
  211. font-weight: 600;
  212. cursor: pointer;
  213. }
  214. }
  215. </style>