index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. <!-- 登录弹窗组件 -->
  75. <LoginModal v-model:open="showLoginModal" @success="handleLoginSuccess" @cancel="handleLoginCancel" />
  76. <!-- 底部导航 -->
  77. <TabBar />
  78. </view>
  79. </template>
  80. <script setup>
  81. import { ref, computed } from 'vue';
  82. import { onShow } from '@dcloudio/uni-app';
  83. import TabBar from '@/components/TabBar.vue';
  84. import LoginModal from '@/pages/components/LoginModal.vue';
  85. import { getFileUrl } from '@/utils/file.js';
  86. import { useUserStore } from '@/store/user.js';
  87. import { go } from '@/utils/utils.js';
  88. import { USER_INFO } from '@/settings/enums.js';
  89. const userStore = useUserStore();
  90. const showLoginModal = ref(false);
  91. const userInfoSync = ref({});
  92. function syncUserFromStorage() {
  93. try {
  94. const raw = uni.getStorageSync(USER_INFO);
  95. userInfoSync.value = raw && typeof raw === 'object' ? raw : {};
  96. } catch (e) {
  97. userInfoSync.value = {};
  98. }
  99. }
  100. onShow(() => {
  101. syncUserFromStorage();
  102. });
  103. syncUserFromStorage();
  104. // 展示用用户信息(优先 onShow 同步的 storage,保证登录后关弹窗能刷新)
  105. const displayUserInfo = computed(() => {
  106. const sync = userInfoSync.value;
  107. if (sync && typeof sync === 'object' && Object.keys(sync).length > 0) return sync;
  108. return userStore.getUserInfo || {};
  109. });
  110. // 头像:绑定缓存的 avatarUrl;本地/临时路径(如 127.0.0.1/tmp、wxfile://)会报 500,改用默认图
  111. const avatarDisplayUrl = computed(() => {
  112. const url = (displayUserInfo.value?.avatarUrl ?? '').trim();
  113. if (!url) return getFileUrl('img/personal/userDemo.png');
  114. const isLocalTemp = /127\.0\.0\.1|localhost|^\/tmp\/|wxfile:\/\/|^blob:/i.test(url);
  115. if (isLocalTemp) return getFileUrl('img/personal/userDemo.png');
  116. if (/^(https?|data):/i.test(url)) return url;
  117. return getFileUrl(url);
  118. });
  119. const displayNickName = computed(() => {
  120. const info = displayUserInfo.value;
  121. return info.nickName ?? info.nickname ?? info.name ?? '用户';
  122. });
  123. // 处理登录点击
  124. const handleLoginClick = () => {
  125. if (!userStore.getToken) {
  126. showLoginModal.value = true;
  127. }
  128. };
  129. // 登录成功回调:立即从 storage 同步用户信息,保证头像/昵称马上更新
  130. const handleLoginSuccess = () => {
  131. syncUserFromStorage();
  132. };
  133. // 取消登录回调
  134. const handleLoginCancel = () => {
  135. console.log('用户取消登录');
  136. };
  137. </script>
  138. <style scoped lang="scss">
  139. .topBox {
  140. width: 100%;
  141. height: 400rpx;
  142. position: relative;
  143. .top-Bg {
  144. width: 100%;
  145. height: 100%;
  146. }
  147. .top-uBg {
  148. width: 360rpx;
  149. height: 360rpx;
  150. position: absolute;
  151. top: 50rpx;
  152. left: 40%;
  153. z-index: 1;
  154. transform: translateX(-50%);
  155. }
  156. .top-title {
  157. position: absolute;
  158. top: 0;
  159. left: 0;
  160. width: 100%;
  161. text-align: center;
  162. line-height: 280rpx;
  163. font-size: 34rpx;
  164. color: #FFFFFF;
  165. }
  166. }
  167. .content-box {
  168. width: 100%;
  169. height: 100%;
  170. box-sizing: border-box;
  171. padding: 0 30rpx;
  172. margin-top: -100rpx;
  173. position: relative;
  174. z-index: 2;
  175. .card {
  176. width: 100%;
  177. height: 100%;
  178. background-color: #fff;
  179. border-radius: 20rpx;
  180. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  181. padding: 30rpx;
  182. }
  183. .card-header {
  184. display: flex;
  185. align-items: center;
  186. margin-top: -50rpx;
  187. .card-header-icon {
  188. width: 132rpx;
  189. height: 132rpx;
  190. }
  191. .card-header-title {
  192. font-weight: bold;
  193. font-size: 38rpx;
  194. color: #151515;
  195. margin-left: 20rpx;
  196. }
  197. }
  198. .hover-active {
  199. opacity: 0.7;
  200. }
  201. .card-list-container {
  202. margin-top: 40rpx;
  203. }
  204. .card-list {
  205. display: flex;
  206. align-items: center;
  207. justify-content: space-between;
  208. padding: 30rpx 0;
  209. border-bottom: 1rpx solid #F5F5F5;
  210. &:last-child {
  211. border-bottom: none;
  212. }
  213. .left {
  214. display: flex;
  215. align-items: center;
  216. flex: 1;
  217. .card-icon {
  218. width: 34rpx;
  219. height: 34rpx;
  220. margin-right: 20rpx;
  221. }
  222. .title {
  223. font-size: 32rpx;
  224. color: #151515;
  225. }
  226. }
  227. .right {
  228. .icon-right {
  229. width: 18rpx;
  230. height: 30rpx;
  231. }
  232. }
  233. }
  234. }
  235. </style>