index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <!-- 个人中心页面 -->
  3. <view class="content">
  4. <!-- 顶部 -->
  5. <view class="topBox">
  6. <image :src="getFileUrl('img/personal/bg.png')" mode="widthFix" class="top-Bg"></image>
  7. <image :src="getFileUrl('img/personal/uBg.png')" mode="widthFix" class="top-uBg"></image>
  8. <view class='top-title'>个人中心</view>
  9. </view>
  10. <!-- 内容 -->
  11. <view class="content-box">
  12. <view class="card">
  13. <view class="card-header">
  14. <image :src="avatarDisplayUrl" mode="aspectFill" class="card-header-icon">
  15. </image>
  16. <view class="card-header-title" hover-class="none" @click="handleLoginClick">{{ userStore.getToken ?
  17. (displayNickName) : '立即登录' }}</view>
  18. </view>
  19. <!-- 列表 -->
  20. <view class="card-list-container">
  21. <view class="card-list" hover-class="hover-active" @click='handleNavClick("/pages/personal/userInfo")'>
  22. <view class='left'>
  23. <image :src="getFileUrl('img/personal/wszl.png')" mode="widthFix" class="card-icon"></image>
  24. <view class="title">完善资料</view>
  25. </view>
  26. <view class='right'>
  27. <image :src="getFileUrl('img/personal/right.png')" mode="widthFix" class="icon-right">
  28. </image>
  29. </view>
  30. </view>
  31. <view class="card-list" hover-class="hover-active" @click='handleNavClick("/pages/orderInfo/index")'>
  32. <view class='left'>
  33. <image :src="getFileUrl('img/personal/wddd.png')" mode="widthFix" class="card-icon"></image>
  34. <view class="title">我的订单</view>
  35. </view>
  36. <view class='right'>
  37. <image :src="getFileUrl('img/personal/right.png')" mode="widthFix" class="icon-right">
  38. </image>
  39. </view>
  40. </view>
  41. <view class="card-list" hover-class="hover-active" @click='handleNavClick("/pages/coupon/index")'>
  42. <view class='left'>
  43. <image :src="getFileUrl('img/personal/wdqb.png')" mode="widthFix" class="card-icon"></image>
  44. <view class="title">我的券包</view>
  45. </view>
  46. <view class='right'>
  47. <image :src="getFileUrl('img/personal/right.png')" mode="widthFix" class="icon-right">
  48. </image>
  49. </view>
  50. </view>
  51. <view class="card-list" hover-class="hover-active">
  52. <view class='left'>
  53. <image :src="getFileUrl('img/personal/yhxy.png')" mode="widthFix" class="card-icon"></image>
  54. <view class="title">用户协议</view>
  55. </view>
  56. <view class='right'>
  57. <image :src="getFileUrl('img/personal/right.png')" mode="widthFix" class="icon-right">
  58. </image>
  59. </view>
  60. </view>
  61. <view class="card-list" hover-class="hover-active">
  62. <view class='left'>
  63. <image :src="getFileUrl('img/personal/ysxy.png')" mode="widthFix" class="card-icon"></image>
  64. <view class="title">隐私政策</view>
  65. </view>
  66. <view class='right'>
  67. <image :src="getFileUrl('img/personal/right.png')" mode="widthFix" class="icon-right">
  68. </image>
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. <!-- 登录弹窗组件(与 index 同结构,盖住底部 tabbar) -->
  75. <view class="login-modal-wrapper">
  76. <LoginModal v-model:open="showLoginModal" @success="handleLoginSuccess" @cancel="handleLoginCancel" />
  77. </view>
  78. <!-- 底部导航 -->
  79. <TabBar />
  80. </view>
  81. </template>
  82. <script setup>
  83. import { ref, computed } from 'vue';
  84. import { onShow } from '@dcloudio/uni-app';
  85. import TabBar from '@/components/TabBar.vue';
  86. import LoginModal from '@/pages/components/LoginModal.vue';
  87. import { getFileUrl } from '@/utils/file.js';
  88. import { useUserStore } from '@/store/user.js';
  89. import { go } from '@/utils/utils.js';
  90. import { USER_INFO } from '@/settings/enums.js';
  91. const userStore = useUserStore();
  92. const showLoginModal = ref(false);
  93. const userInfoSync = ref({});
  94. function syncUserFromStorage() {
  95. try {
  96. const raw = uni.getStorageSync(USER_INFO);
  97. userInfoSync.value = raw && typeof raw === 'object' ? raw : {};
  98. } catch (e) {
  99. userInfoSync.value = {};
  100. }
  101. }
  102. onShow(() => {
  103. syncUserFromStorage();
  104. });
  105. syncUserFromStorage();
  106. // 展示用用户信息(优先 onShow 同步的 storage,保证登录后关弹窗能刷新)
  107. const displayUserInfo = computed(() => {
  108. const sync = userInfoSync.value;
  109. if (sync && typeof sync === 'object' && Object.keys(sync).length > 0) return sync;
  110. return userStore.getUserInfo || {};
  111. });
  112. // 头像:绑定缓存的 avatarUrl;本地/临时路径(如 127.0.0.1/tmp、wxfile://)会报 500,改用默认图
  113. const avatarDisplayUrl = computed(() => {
  114. const url = (displayUserInfo.value?.avatarUrl ?? '').trim();
  115. if (!url) return getFileUrl('img/personal/userDemo.png');
  116. const isLocalTemp = /127\.0\.0\.1|localhost|^\/tmp\/|wxfile:\/\/|^blob:/i.test(url);
  117. if (isLocalTemp) return getFileUrl('img/personal/userDemo.png');
  118. if (/^(https?|data):/i.test(url)) return url;
  119. return getFileUrl(url);
  120. });
  121. const displayNickName = computed(() => {
  122. const info = displayUserInfo.value;
  123. return info.nickName ?? info.nickname ?? info.name ?? '用户';
  124. });
  125. // 处理登录点击
  126. const handleLoginClick = () => {
  127. if (!userStore.getToken) {
  128. showLoginModal.value = true;
  129. }
  130. };
  131. // 需登录才能访问的导航:未登录时禁止跳转,提示并弹出登录框
  132. const handleNavClick = (url) => {
  133. if (!userStore.getToken) {
  134. uni.showToast({ title: '请先登录', icon: 'none' });
  135. showLoginModal.value = true;
  136. return;
  137. }
  138. go(url);
  139. };
  140. // 登录成功回调:立即从 storage 同步用户信息,保证头像/昵称马上更新
  141. const handleLoginSuccess = () => {
  142. syncUserFromStorage();
  143. };
  144. // 取消登录回调
  145. const handleLoginCancel = () => {
  146. console.log('用户取消登录');
  147. };
  148. </script>
  149. <style scoped lang="scss">
  150. .topBox {
  151. width: 100%;
  152. height: 400rpx;
  153. position: relative;
  154. .top-Bg {
  155. width: 100%;
  156. height: 100%;
  157. }
  158. .top-uBg {
  159. width: 360rpx;
  160. height: 360rpx;
  161. position: absolute;
  162. top: 50rpx;
  163. left: 40%;
  164. z-index: 1;
  165. transform: translateX(-50%);
  166. }
  167. .top-title {
  168. position: absolute;
  169. top: 0;
  170. left: 0;
  171. width: 100%;
  172. text-align: center;
  173. line-height: 280rpx;
  174. font-size: 34rpx;
  175. color: #FFFFFF;
  176. }
  177. }
  178. .content-box {
  179. width: 100%;
  180. height: 100%;
  181. box-sizing: border-box;
  182. padding: 0 30rpx;
  183. margin-top: -100rpx;
  184. position: relative;
  185. z-index: 2;
  186. .card {
  187. width: 100%;
  188. height: 100%;
  189. background-color: #fff;
  190. border-radius: 20rpx;
  191. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  192. padding: 30rpx;
  193. }
  194. .card-header {
  195. display: flex;
  196. align-items: center;
  197. margin-top: -50rpx;
  198. .card-header-icon {
  199. width: 132rpx;
  200. height: 132rpx;
  201. border-radius: 50%;
  202. }
  203. .card-header-title {
  204. font-weight: bold;
  205. font-size: 38rpx;
  206. color: #151515;
  207. margin-left: 20rpx;
  208. }
  209. }
  210. .hover-active {
  211. opacity: 0.7;
  212. }
  213. .card-list-container {
  214. margin-top: 40rpx;
  215. }
  216. .card-list {
  217. display: flex;
  218. align-items: center;
  219. justify-content: space-between;
  220. padding: 30rpx 0;
  221. border-bottom: 1rpx solid #F5F5F5;
  222. &:last-child {
  223. border-bottom: none;
  224. }
  225. .left {
  226. display: flex;
  227. align-items: center;
  228. flex: 1;
  229. .card-icon {
  230. width: 34rpx;
  231. height: 34rpx;
  232. margin-right: 20rpx;
  233. }
  234. .title {
  235. font-size: 32rpx;
  236. color: #151515;
  237. }
  238. }
  239. .right {
  240. .icon-right {
  241. width: 18rpx;
  242. height: 30rpx;
  243. }
  244. }
  245. }
  246. }
  247. /* 登录弹窗层级设置(与 index 一致,盖住底部 tabbar) */
  248. .login-modal-wrapper {
  249. position: relative;
  250. z-index: 99999;
  251. :deep(.uni-popup) {
  252. z-index: 99999 !important;
  253. top: 0 !important;
  254. left: 0 !important;
  255. right: 0 !important;
  256. bottom: 0 !important;
  257. }
  258. :deep(.uni-popup__wrapper) {
  259. z-index: 99999 !important;
  260. }
  261. }
  262. </style>