mine.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <!-- pages/mine/mine.vue -->
  2. <template>
  3. <view class="mine-container">
  4. <!-- 顶部用户信息区域 -->
  5. <view class="user-header ">
  6. <headerInfo title="个人中心"></headerInfo>
  7. <view class="user-info-section">
  8. <view class="avatar-section">
  9. <u-avatar
  10. @click="toUser"
  11. size="144rpx"
  12. shape="circle"
  13. :src="userAvatar"
  14. mode="aspectFill"
  15. :show-level="false"
  16. ></u-avatar>
  17. <view class="user-text-info">
  18. <view class="user-name-section">
  19. <view v-if="isLoggedIn">
  20. <view class="name-section">
  21. <view class="user-name" @click="toUser">
  22. {{ displayName }}
  23. </view>
  24. <view class="department-badge">
  25. 储运部
  26. </view>
  27. </view>
  28. <view class="user-account">
  29. 账号:{{ userAccount }}
  30. </view>
  31. </view>
  32. <button v-else
  33. class="login-button"
  34. open-type="getPhoneNumber"
  35. @getphonenumber="handleGetPhoneNumber"
  36. :loading="isLogging"
  37. :disabled="isLogging || isProcessing"
  38. >
  39. {{ loginButtonText }}
  40. </button>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. <!-- 菜单功能区域 -->
  47. <view class="menu-section">
  48. <view class="menu-card">
  49. <!-- 订单列表 -->
  50. <view class="menu-item" @click="toOrderList">
  51. <view class="menu-item-left">
  52. <image src="/static/img/mine/icon-mine-order.png" class="menu-icon"></image>
  53. <text class="menu-text">订单列表</text>
  54. </view>
  55. <image src="/static/img/arrow-right.png" class="arrow-icon"></image>
  56. </view>
  57. <!-- 地址簿 -->
  58. <view class="menu-item" @click="toAddressBook">
  59. <view class="menu-item-left">
  60. <image src="/static/img/mine/icon-mine-address.png" class="menu-icon"></image>
  61. <text class="menu-text">地址簿</text>
  62. </view>
  63. <image src="/static/img/arrow-right.png" class="arrow-icon"></image>
  64. </view>
  65. <!-- 到付月结码 -->
  66. <view class="menu-item" @click="toSettlementCode">
  67. <view class="menu-item-left">
  68. <image src="/static/img/mine/icon-mine-qr.png" class="menu-icon"></image>
  69. <text class="menu-text">到付月结码</text>
  70. </view>
  71. <image src="/static/img/arrow-right.png" class="arrow-icon"></image>
  72. </view>
  73. <!-- 联系客服 -->
  74. <view class="menu-item" @click="makePhoneCall">
  75. <view class="menu-item-left">
  76. <image src="/static/img/mine/icon-mine-server.png" class="menu-icon"></image>
  77. <text class="menu-text">联系客服</text>
  78. </view>
  79. <image src="/static/img/arrow-right.png" class="arrow-icon"></image>
  80. </view>
  81. <!-- 隐私政策 -->
  82. <view class="menu-item" @click="toPrivacyPolicy">
  83. <view class="menu-item-left">
  84. <image src="/static/img/mine/icon-mine-policy.png" class="menu-icon"></image>
  85. <text class="menu-text">隐私政策</text>
  86. </view>
  87. <image src="/static/img/arrow-right.png" class="arrow-icon"></image>
  88. </view>
  89. <!-- 关于我们 -->
  90. <view class="menu-item" @click="toAboutUs">
  91. <view class="menu-item-left">
  92. <image src="/static/img/mine/icon-mine-about.png" class="menu-icon"></image>
  93. <text class="menu-text">关于我们</text>
  94. </view>
  95. <image src="/static/img/arrow-right.png" class="arrow-icon"></image>
  96. </view>
  97. <!-- 退出登录(仅登录后显示) -->
  98. <view
  99. class="menu-item logout-item"
  100. v-if="isLoggedIn"
  101. @click="userLogoutFn">
  102. <text class="menu-text logout-text">退出登录</text>
  103. </view>
  104. </view>
  105. </view>
  106. <!-- 加载提示 -->
  107. <u-loading-page :loading="pageLoading" bg-color="transparent"></u-loading-page>
  108. </view>
  109. </template>
  110. <script setup>
  111. import { ref, computed, onMounted } from "vue";
  112. import headerInfo from "@/components/headerInfo.vue";
  113. import { userLogout } from "@/api/user.js";
  114. import { getAdServicePhone } from "@/api/home.js";
  115. import { checkLoginShowModal, quickLogin } from "@/utils/util.js";
  116. import { onLoad, onShow, onShareAppMessage } from '@dcloudio/uni-app'
  117. import { useAppStore } from "@/stores/app";
  118. const appStore = useAppStore();
  119. // 响应式数据
  120. const isLogging = ref(false);
  121. const isProcessing = ref(false);
  122. const pageLoading = ref(false);
  123. const phoneNumber = ref('');
  124. // 计算属性
  125. const isLoggedIn = computed(() => {
  126. return !!(appStore.userInfo?.phonenumber && appStore.token);
  127. });
  128. const userAvatar = computed(() => {
  129. return appStore.userInfo?.avatar || '/static/img/default-avatar.png';
  130. });
  131. const displayName = computed(() => {
  132. return appStore.userInfo?.nickName || appStore.userInfo?.userName;
  133. });
  134. const userAccount = computed(() => {
  135. return appStore.userInfo?.phonenumber || '';
  136. });
  137. const loginButtonText = computed(() => {
  138. if (isLogging.value) return '登录中...';
  139. if (isProcessing.value) return '处理中...';
  140. return '请点击登录';
  141. });
  142. onShareAppMessage((res) => {
  143. return appStore.onShareAppMessageObj;
  144. });
  145. onLoad(() => {
  146. getAdServicePhoneFn();
  147. // 检查用户登录状态
  148. checkUserStatus();
  149. });
  150. onShow(() => {
  151. // 页面显示时检查是否需要重新获取用户信息
  152. if (appStore.token && !appStore.userInfo?.userPhone) {
  153. appStore.USERINFO();
  154. }
  155. });
  156. // 检查用户状态
  157. async function checkUserStatus() {
  158. if (appStore.token && !appStore.userInfo) {
  159. pageLoading.value = true;
  160. try {
  161. await appStore.USERINFO();
  162. } catch (error) {
  163. console.error('获取用户信息失败:', error);
  164. } finally {
  165. pageLoading.value = false;
  166. }
  167. }
  168. }
  169. // 获取客服电话
  170. function getAdServicePhoneFn() {
  171. getAdServicePhone({}).then(res => {
  172. phoneNumber.value = res.data?.servicePhone || '';
  173. }).catch(err => {
  174. console.log(err);
  175. });
  176. }
  177. // 跳转到用户信息页
  178. async function toUser() {
  179. if (!await checkLoginShowModal()) return;
  180. uni.navigateTo({
  181. url: '/pages/user/user'
  182. });
  183. }
  184. // 跳转到订单列表
  185. async function toOrderList() {
  186. if (!await checkLoginShowModal()) return;
  187. uni.navigateTo({
  188. url: '/pages/order/index'
  189. });
  190. }
  191. // 跳转到地址簿
  192. async function toAddressBook() {
  193. if (!await checkLoginShowModal()) return;
  194. uni.navigateTo({
  195. url: '/pages/address/address_list'
  196. });
  197. }
  198. // 跳转到月结码
  199. async function toSettlementCode() {
  200. if (!await checkLoginShowModal()) return;
  201. uni.navigateTo({
  202. url: '/pages/mine/settlementCode'
  203. });
  204. }
  205. // 跳转到隐私政策
  206. async function toPrivacyPolicy() {
  207. uni.navigateTo({
  208. url: '/pages/webview/webview?url=' + encodeURIComponent('https://your-domain.com/privacy')
  209. });
  210. }
  211. // 跳转到关于我们
  212. async function toAboutUs() {
  213. uni.navigateTo({
  214. url: '/pages/about/index'
  215. });
  216. }
  217. // 处理获取手机号登录 - 使用优化后的quickLogin
  218. async function handleGetPhoneNumber(e) {
  219. if (isLogging.value || isProcessing.value) return;
  220. isLogging.value = true;
  221. isProcessing.value = true;
  222. // 先隐藏loading(如果有)
  223. uni.hideLoading();
  224. try {
  225. // 显示加载提示
  226. uni.showLoading({
  227. title: '登录中...',
  228. mask: true
  229. });
  230. // 调用快速登录,并传入自定义回调
  231. await quickLogin(e, {
  232. onSuccess: (result) => {
  233. console.log('页面登录成功回调:', result);
  234. // 页面特定的成功逻辑
  235. uni.showToast({
  236. title: result.message || `欢迎回来,${result.userInfo?.userName || '用户'}`,
  237. icon: 'success',
  238. duration: 2000
  239. });
  240. // 由于appStore.USERINFO()已经在工具函数中调用,这里不需要再调用
  241. // 但我们可以触发页面更新
  242. uni.$emit('pageRefresh');
  243. },
  244. onFail: (error) => {
  245. console.log('页面登录失败回调:', error);
  246. // 页面特定的失败逻辑
  247. let errorMessage = error.message || '登录失败,请重试';
  248. if (error.type === 'auth_denied') {
  249. errorMessage = '您拒绝了授权,无法登录';
  250. // 如果是授权拒绝,可以显示引导重新授权的提示
  251. uni.showModal({
  252. title: '提示',
  253. content: '需要手机号授权才能正常使用,请点击右上角菜单,选择「重新进入小程序」后重新授权',
  254. showCancel: false,
  255. confirmText: '知道了'
  256. });
  257. } else if (error.type === 'auth_code_missing') {
  258. errorMessage = '授权信息不完整';
  259. } else if (error.type === 'login_failed') {
  260. errorMessage = error.originalError || error.message;
  261. }
  262. // 只在需要时显示Toast(非授权拒绝的情况)
  263. if (error.type !== 'auth_denied') {
  264. uni.showToast({
  265. title: errorMessage,
  266. icon: 'none',
  267. duration: 3000
  268. });
  269. }
  270. },
  271. redirectUrl: '/pages/mine/mine'
  272. });
  273. } catch (error) {
  274. console.error('登录处理异常:', error);
  275. // 这里是未在onFail中捕获的错误(应该是很少出现的)
  276. uni.showToast({
  277. title: error,
  278. icon: 'none'
  279. });
  280. } finally {
  281. isLogging.value = false;
  282. isProcessing.value = false;
  283. // 确保loading被隐藏
  284. setTimeout(() => {
  285. uni.hideLoading();
  286. }, 300);
  287. }
  288. }
  289. // 拨打电话
  290. const makePhoneCall = () => {
  291. if (!phoneNumber.value) {
  292. uni.showToast({
  293. title: '客服电话暂时无法接通',
  294. icon: 'none'
  295. });
  296. return;
  297. }
  298. uni.makePhoneCall({
  299. phoneNumber: phoneNumber.value,
  300. success: () => {
  301. console.log('成功唤起拨号界面');
  302. },
  303. fail: (err) => {
  304. console.error('唤起拨号界面失败', err);
  305. uni.showToast({
  306. title: '拨号失败',
  307. icon: 'none'
  308. });
  309. }
  310. });
  311. };
  312. // 退出登录
  313. function userLogoutFn() {
  314. uni.showModal({
  315. title: '提示',
  316. content: '确认要退出登录吗?',
  317. success: function(res) {
  318. if (res.confirm) {
  319. uni.showLoading({
  320. title: '正在退出...'
  321. });
  322. userLogout({}).then(res => {
  323. appStore.LOGOUT();
  324. uni.hideLoading();
  325. uni.showToast({
  326. title: '已退出登录',
  327. icon: 'success'
  328. });
  329. // 延迟刷新页面状态
  330. setTimeout(() => {
  331. uni.$emit('userLogoutSuccess');
  332. }, 500);
  333. }).catch(err => {
  334. uni.hideLoading();
  335. uni.showToast({
  336. title: '退出失败',
  337. icon: 'none'
  338. });
  339. });
  340. }
  341. }
  342. });
  343. }
  344. </script>
  345. <style lang="less" scoped>
  346. .mine-container {
  347. height: 100vh;
  348. background: linear-gradient(135deg, #CFE9FF 0%, #F5F7FA 50.86%);
  349. position: relative;
  350. // 用户信息区域
  351. .user-header {
  352. padding: 0rpx 24rpx;
  353. .user-info-section {
  354. margin-top: 32rpx;
  355. display: flex;
  356. flex-direction: column;
  357. gap: 20rpx;
  358. .avatar-section {
  359. display: flex;
  360. align-items: center;
  361. min-height: 144rpx; // 添加最小高度防止闪烁
  362. .user-text-info {
  363. flex: 1;
  364. display: flex;
  365. flex-direction: column;
  366. justify-content: center;
  367. min-height: 144rpx; // 添加最小高度防止闪烁
  368. }
  369. .user-name-section {
  370. margin-left: 16rpx;
  371. min-height: 100rpx; // 固定高度防止闪烁
  372. display: flex;
  373. flex-direction: column;
  374. justify-content: center;
  375. .name-section {
  376. height: 52rpx;
  377. display: flex;
  378. align-items: center;
  379. margin-bottom: 8rpx;
  380. }
  381. .user-name {
  382. font-weight: bold;
  383. font-size: 36rpx;
  384. color: #333333;
  385. text-align: center;
  386. line-height: 1;
  387. }
  388. .login-button {
  389. background: transparent;
  390. font-size: 40rpx;
  391. font-weight: 600;
  392. color: #333;
  393. line-height: 56rpx;
  394. padding: 0;
  395. margin: 0;
  396. text-align: left;
  397. height: auto;
  398. min-height: 56rpx;
  399. display: flex;
  400. align-items: center;
  401. justify-content: flex-start;
  402. &::after {
  403. border: none;
  404. }
  405. &[disabled] {
  406. opacity: 0.7;
  407. }
  408. }
  409. }
  410. .user-account {
  411. height: 44rpx;
  412. font-weight: 400;
  413. font-size: 28rpx;
  414. color: #333333;
  415. line-height: 44rpx;
  416. margin-top: 4rpx;
  417. }
  418. }
  419. .department-badge {
  420. width: 104rpx;
  421. background: #1B64F0;
  422. border-radius: 8rpx;
  423. font-size: 24rpx;
  424. color: #FFFFFF;
  425. text-align: center;
  426. margin-left: 8rpx;
  427. padding: 7rpx 16rpx;
  428. line-height: 1;
  429. }
  430. }
  431. }
  432. // 菜单区域
  433. .menu-section {
  434. padding: 0 20rpx;
  435. margin-top: 20rpx;
  436. .menu-card {
  437. overflow: hidden;
  438. }
  439. .menu-item {
  440. display: flex;
  441. align-items: center;
  442. justify-content: space-between;
  443. padding: 32rpx;
  444. background: #FFFFFF;
  445. border-radius: 24rpx;
  446. margin-bottom: 20rpx;
  447. min-height: 112rpx; // 固定高度防止闪烁
  448. &:last-child {
  449. border-bottom: none;
  450. margin-bottom: 0;
  451. }
  452. .menu-item-left {
  453. display: flex;
  454. align-items: center;
  455. gap: 24rpx;
  456. flex: 1;
  457. }
  458. .menu-icon {
  459. width: 48rpx;
  460. height: 48rpx;
  461. flex-shrink: 0;
  462. }
  463. .menu-text {
  464. font-size: 32rpx;
  465. font-weight: 500;
  466. color: #333333;
  467. line-height: 45rpx;
  468. }
  469. .arrow-icon {
  470. width: 48rpx;
  471. height: 48rpx;
  472. opacity: 0.5;
  473. flex-shrink: 0;
  474. }
  475. &.logout-item {
  476. height: 88rpx;
  477. background: #F52929;
  478. border-radius: 32rpx;
  479. display: flex;
  480. justify-content: center;
  481. align-items: center;
  482. .logout-text {
  483. font-weight: 400;
  484. font-size: 32rpx;
  485. color: #FFFFFF;
  486. text-align: center;
  487. }
  488. }
  489. }
  490. }
  491. }
  492. // 动画效果
  493. .menu-item {
  494. transition: all 0.3s ease;
  495. opacity: 1;
  496. &:active {
  497. background-color: rgba(66, 133, 244, 0.05);
  498. transform: translateX(8rpx);
  499. }
  500. &[disabled] {
  501. opacity: 0.6;
  502. pointer-events: none;
  503. }
  504. }
  505. .department-badge {
  506. transition: all 0.3s ease;
  507. &:active {
  508. transform: scale(0.95);
  509. }
  510. }
  511. // 响应式适配
  512. @media (max-width: 375px) {
  513. .menu-section {
  514. padding: 0 20rpx !important;
  515. }
  516. .menu-card {
  517. padding: 0 20rpx !important;
  518. }
  519. .menu-text {
  520. font-size: 30rpx !important;
  521. }
  522. }
  523. // 防止闪烁的全局样式
  524. view, text, image {
  525. will-change: auto;
  526. backface-visibility: hidden;
  527. -webkit-backface-visibility: hidden;
  528. }
  529. </style>