index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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" @click="handlePrivacyPolicy">
  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. // 隐私政策:跳转 webview 打开
  149. const PRIVACY_URL = 'https://ossfile.ailien.shop/privacy/U店在哪-隐私协议946827.htm';
  150. const handlePrivacyPolicy = () => {
  151. go(`/pages/webview/index?url=${encodeURIComponent(PRIVACY_URL)}`);
  152. };
  153. </script>
  154. <style scoped lang="scss">
  155. .topBox {
  156. width: 100%;
  157. height: 400rpx;
  158. position: relative;
  159. .top-Bg {
  160. width: 100%;
  161. height: 100%;
  162. }
  163. .top-uBg {
  164. width: 360rpx;
  165. height: 360rpx;
  166. position: absolute;
  167. top: 50rpx;
  168. left: 40%;
  169. z-index: 1;
  170. transform: translateX(-50%);
  171. }
  172. .top-title {
  173. position: absolute;
  174. top: 0;
  175. left: 0;
  176. width: 100%;
  177. text-align: center;
  178. line-height: 280rpx;
  179. font-size: 34rpx;
  180. color: #FFFFFF;
  181. }
  182. }
  183. .content-box {
  184. width: 100%;
  185. height: 100%;
  186. box-sizing: border-box;
  187. padding: 0 30rpx;
  188. margin-top: -100rpx;
  189. position: relative;
  190. z-index: 2;
  191. .card {
  192. width: 100%;
  193. height: 100%;
  194. background-color: #fff;
  195. border-radius: 20rpx;
  196. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  197. padding: 30rpx;
  198. }
  199. .card-header {
  200. display: flex;
  201. align-items: center;
  202. margin-top: -50rpx;
  203. .card-header-icon {
  204. width: 132rpx;
  205. height: 132rpx;
  206. border-radius: 50%;
  207. }
  208. .card-header-title {
  209. font-weight: bold;
  210. font-size: 38rpx;
  211. color: #151515;
  212. margin-left: 20rpx;
  213. }
  214. }
  215. .hover-active {
  216. opacity: 0.7;
  217. }
  218. .card-list-container {
  219. margin-top: 40rpx;
  220. }
  221. .card-list {
  222. display: flex;
  223. align-items: center;
  224. justify-content: space-between;
  225. padding: 30rpx 0;
  226. border-bottom: 1rpx solid #F5F5F5;
  227. &:last-child {
  228. border-bottom: none;
  229. }
  230. .left {
  231. display: flex;
  232. align-items: center;
  233. flex: 1;
  234. .card-icon {
  235. width: 34rpx;
  236. height: 34rpx;
  237. margin-right: 20rpx;
  238. }
  239. .title {
  240. font-size: 32rpx;
  241. color: #151515;
  242. }
  243. }
  244. .right {
  245. .icon-right {
  246. width: 18rpx;
  247. height: 30rpx;
  248. }
  249. }
  250. }
  251. }
  252. /* 登录弹窗层级设置(与 index 一致,盖住底部 tabbar) */
  253. .login-modal-wrapper {
  254. position: relative;
  255. z-index: 99999;
  256. :deep(.uni-popup) {
  257. z-index: 99999 !important;
  258. top: 0 !important;
  259. left: 0 !important;
  260. right: 0 !important;
  261. bottom: 0 !important;
  262. }
  263. :deep(.uni-popup__wrapper) {
  264. z-index: 99999 !important;
  265. }
  266. }
  267. </style>