index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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='go("/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='go("/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='go("/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. // 登录成功回调:立即从 storage 同步用户信息,保证头像/昵称马上更新
  132. const handleLoginSuccess = () => {
  133. syncUserFromStorage();
  134. };
  135. // 取消登录回调
  136. const handleLoginCancel = () => {
  137. console.log('用户取消登录');
  138. };
  139. </script>
  140. <style scoped lang="scss">
  141. .topBox {
  142. width: 100%;
  143. height: 400rpx;
  144. position: relative;
  145. .top-Bg {
  146. width: 100%;
  147. height: 100%;
  148. }
  149. .top-uBg {
  150. width: 360rpx;
  151. height: 360rpx;
  152. position: absolute;
  153. top: 50rpx;
  154. left: 40%;
  155. z-index: 1;
  156. transform: translateX(-50%);
  157. }
  158. .top-title {
  159. position: absolute;
  160. top: 0;
  161. left: 0;
  162. width: 100%;
  163. text-align: center;
  164. line-height: 280rpx;
  165. font-size: 34rpx;
  166. color: #FFFFFF;
  167. }
  168. }
  169. .content-box {
  170. width: 100%;
  171. height: 100%;
  172. box-sizing: border-box;
  173. padding: 0 30rpx;
  174. margin-top: -100rpx;
  175. position: relative;
  176. z-index: 2;
  177. .card {
  178. width: 100%;
  179. height: 100%;
  180. background-color: #fff;
  181. border-radius: 20rpx;
  182. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  183. padding: 30rpx;
  184. }
  185. .card-header {
  186. display: flex;
  187. align-items: center;
  188. margin-top: -50rpx;
  189. .card-header-icon {
  190. width: 132rpx;
  191. height: 132rpx;
  192. }
  193. .card-header-title {
  194. font-weight: bold;
  195. font-size: 38rpx;
  196. color: #151515;
  197. margin-left: 20rpx;
  198. }
  199. }
  200. .hover-active {
  201. opacity: 0.7;
  202. }
  203. .card-list-container {
  204. margin-top: 40rpx;
  205. }
  206. .card-list {
  207. display: flex;
  208. align-items: center;
  209. justify-content: space-between;
  210. padding: 30rpx 0;
  211. border-bottom: 1rpx solid #F5F5F5;
  212. &:last-child {
  213. border-bottom: none;
  214. }
  215. .left {
  216. display: flex;
  217. align-items: center;
  218. flex: 1;
  219. .card-icon {
  220. width: 34rpx;
  221. height: 34rpx;
  222. margin-right: 20rpx;
  223. }
  224. .title {
  225. font-size: 32rpx;
  226. color: #151515;
  227. }
  228. }
  229. .right {
  230. .icon-right {
  231. width: 18rpx;
  232. height: 30rpx;
  233. }
  234. }
  235. }
  236. }
  237. /* 登录弹窗层级设置(与 index 一致,盖住底部 tabbar) */
  238. .login-modal-wrapper {
  239. position: relative;
  240. z-index: 99999;
  241. :deep(.uni-popup) {
  242. z-index: 99999 !important;
  243. top: 0 !important;
  244. left: 0 !important;
  245. right: 0 !important;
  246. bottom: 0 !important;
  247. }
  248. :deep(.uni-popup__wrapper) {
  249. z-index: 99999 !important;
  250. }
  251. }
  252. </style>