index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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" @click="handleUserAgreement">
  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 { GetUserInfo } from '@/api/dining.js';
  91. const userStore = useUserStore();
  92. const showLoginModal = ref(false);
  93. // 仅用接口返回的用户信息展示,不使用缓存
  94. const userInfoFromApi = ref({});
  95. // 将接口返回的用户信息转为页面展示字段(头像、昵称等)
  96. function normalizeUserInfoFromApi(raw) {
  97. if (!raw || typeof raw !== 'object') return raw;
  98. return {
  99. ...raw,
  100. avatarUrl: raw.avatarUrl ?? raw.avatar ?? raw.headImgUrl ?? raw.icon ?? '',
  101. nickName: raw.nickName ?? raw.nickname ?? raw.name ?? raw.userName ?? ''
  102. };
  103. }
  104. async function fetchAndSetUserInfo() {
  105. try {
  106. const res = await GetUserInfo();
  107. const data = res?.data ?? res ?? {};
  108. const userData = data?.data ?? data;
  109. if (userData && typeof userData === 'object') {
  110. const normalized = normalizeUserInfoFromApi(userData);
  111. userInfoFromApi.value = normalized;
  112. userStore.setUserInfo({ ...userStore.getUserInfo, ...normalized });
  113. } else {
  114. userInfoFromApi.value = {};
  115. }
  116. } catch (e) {
  117. console.warn('个人中心获取用户信息失败:', e);
  118. userInfoFromApi.value = {};
  119. }
  120. }
  121. onShow(() => {
  122. fetchAndSetUserInfo();
  123. });
  124. // 展示用用户信息:仅来自接口,不使用缓存
  125. const displayUserInfo = computed(() => userInfoFromApi.value || {});
  126. // 头像:从接口获取并缓存后的用户信息中取 avatarUrl/avatar,无则默认图
  127. const avatarDisplayUrl = computed(() => {
  128. const info = displayUserInfo.value;
  129. const url = (info?.avatarUrl ?? info?.avatar ?? '').trim();
  130. if (!url) return getFileUrl('img/personal/userDemo.png');
  131. const isLocalTemp = /127\.0\.0\.1|localhost|^\/tmp\/|wxfile:\/\/|^blob:/i.test(url);
  132. if (isLocalTemp) return getFileUrl('img/personal/userDemo.png');
  133. if (/^(https?|data):/i.test(url)) return url;
  134. return getFileUrl(url);
  135. });
  136. // 昵称:从接口获取并缓存后的用户信息中取 nickName/nickname/name
  137. const displayNickName = computed(() => {
  138. const info = displayUserInfo.value;
  139. return info?.nickName ?? info?.nickname ?? info?.name ?? info?.userName ?? '用户';
  140. });
  141. // 处理登录点击
  142. const handleLoginClick = () => {
  143. if (!userStore.getToken) {
  144. showLoginModal.value = true;
  145. }
  146. };
  147. // 需登录才能访问的导航:未登录时禁止跳转,提示并弹出登录框
  148. const handleNavClick = (url) => {
  149. if (!userStore.getToken) {
  150. uni.showToast({ title: '请先登录', icon: 'none' });
  151. showLoginModal.value = true;
  152. return;
  153. }
  154. go(url);
  155. };
  156. // 登录成功回调:重新拉取用户信息接口,保证头像/昵称马上更新
  157. const handleLoginSuccess = () => {
  158. fetchAndSetUserInfo();
  159. };
  160. // 取消登录回调
  161. const handleLoginCancel = () => {
  162. console.log('用户取消登录');
  163. };
  164. // 隐私政策:跳转 webview 打开
  165. const PRIVACY_URL = 'https://ossfile.ailien.shop/privacy/%E9%9A%90%E7%A7%81%E5%8D%8F%E8%AE%AE931648.html';
  166. const handlePrivacyPolicy = () => {
  167. go(`/pages/webview/index?url=${encodeURIComponent(PRIVACY_URL)}`);
  168. };
  169. // 用户协议:跳转 webview 打开(U店在这-平台规则)
  170. const USER_AGREEMENT_URL = 'https://ossfile.ailien.shop/privacy/U%E5%BA%97%E5%9C%A8%E8%BF%99-%E5%B9%B3%E5%8F%B0%E8%A7%84%E5%88%99456793.htm';
  171. const handleUserAgreement = () => {
  172. go(`/pages/webview/index?url=${encodeURIComponent(USER_AGREEMENT_URL)}`);
  173. };
  174. </script>
  175. <style scoped lang="scss">
  176. .topBox {
  177. width: 100%;
  178. height: 400rpx;
  179. position: relative;
  180. .top-Bg {
  181. width: 100%;
  182. height: 100%;
  183. }
  184. .top-uBg {
  185. width: 360rpx;
  186. height: 360rpx;
  187. position: absolute;
  188. top: 50rpx;
  189. left: 40%;
  190. z-index: 1;
  191. transform: translateX(-50%);
  192. }
  193. .top-title {
  194. position: absolute;
  195. top: 0;
  196. left: 0;
  197. width: 100%;
  198. text-align: center;
  199. line-height: 280rpx;
  200. font-size: 34rpx;
  201. color: #FFFFFF;
  202. }
  203. }
  204. .content-box {
  205. width: 100%;
  206. height: 100%;
  207. box-sizing: border-box;
  208. padding: 0 30rpx;
  209. margin-top: -100rpx;
  210. position: relative;
  211. z-index: 2;
  212. .card {
  213. width: 100%;
  214. height: 100%;
  215. background-color: #fff;
  216. border-radius: 20rpx;
  217. box-shadow: 0rpx 0rpx 11rpx 0rpx rgba(0, 0, 0, 0.06);
  218. padding: 30rpx;
  219. }
  220. .card-header {
  221. display: flex;
  222. align-items: center;
  223. margin-top: -50rpx;
  224. .card-header-icon {
  225. width: 132rpx;
  226. height: 132rpx;
  227. border-radius: 50%;
  228. }
  229. .card-header-title {
  230. font-weight: bold;
  231. font-size: 38rpx;
  232. color: #151515;
  233. margin-left: 20rpx;
  234. }
  235. }
  236. .hover-active {
  237. opacity: 0.7;
  238. }
  239. .card-list-container {
  240. margin-top: 40rpx;
  241. }
  242. .card-list {
  243. display: flex;
  244. align-items: center;
  245. justify-content: space-between;
  246. padding: 30rpx 0;
  247. border-bottom: 1rpx solid #F5F5F5;
  248. &:last-child {
  249. border-bottom: none;
  250. }
  251. .left {
  252. display: flex;
  253. align-items: center;
  254. flex: 1;
  255. .card-icon {
  256. width: 34rpx;
  257. height: 34rpx;
  258. margin-right: 20rpx;
  259. }
  260. .title {
  261. font-size: 32rpx;
  262. color: #151515;
  263. }
  264. }
  265. .right {
  266. .icon-right {
  267. width: 18rpx;
  268. height: 30rpx;
  269. }
  270. }
  271. }
  272. }
  273. /* 登录弹窗层级设置(与 index 一致,盖住底部 tabbar) */
  274. .login-modal-wrapper {
  275. position: relative;
  276. z-index: 99999;
  277. :deep(.uni-popup) {
  278. z-index: 99999 !important;
  279. top: 0 !important;
  280. left: 0 !important;
  281. right: 0 !important;
  282. bottom: 0 !important;
  283. }
  284. :deep(.uni-popup__wrapper) {
  285. z-index: 99999 !important;
  286. }
  287. }
  288. </style>