mine.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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" v-if="dept">
  25. {{dept}}
  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 dept = computed(() => {
  135. return appStore.userInfo?.dept?.deptName || "";
  136. });
  137. const userAccount = computed(() => {
  138. return appStore.userInfo?.userName || appStore.userInfo?.phonenumber || '';
  139. });
  140. const loginButtonText = computed(() => {
  141. if (isLogging.value) return '登录中...';
  142. if (isProcessing.value) return '处理中...';
  143. return '请点击登录';
  144. });
  145. onShareAppMessage((res) => {
  146. return appStore.onShareAppMessageObj;
  147. });
  148. onLoad(() => {
  149. getAdServicePhoneFn();
  150. // 检查用户登录状态
  151. checkUserStatus();
  152. });
  153. onShow(() => {
  154. // 页面显示时检查是否需要重新获取用户信息
  155. if (appStore.token && !appStore.userInfo?.userPhone) {
  156. appStore.USERINFO();
  157. }
  158. });
  159. // 检查用户状态
  160. async function checkUserStatus() {
  161. if (appStore.token && !appStore.userName) {
  162. pageLoading.value = true;
  163. try {
  164. await appStore.USERINFO();
  165. } catch (error) {
  166. console.error('获取用户信息失败:', error);
  167. } finally {
  168. pageLoading.value = false;
  169. }
  170. }
  171. }
  172. // 获取客服电话
  173. function getAdServicePhoneFn() {
  174. getAdServicePhone({}).then(res => {
  175. phoneNumber.value = res.data?.servicePhone || '';
  176. }).catch(err => {
  177. console.log(err);
  178. });
  179. }
  180. // 跳转到用户信息页
  181. async function toUser() {
  182. if (!await checkLoginShowModal()) return;
  183. uni.navigateTo({
  184. url: '/pages/user/user'
  185. });
  186. }
  187. // 跳转到订单列表
  188. async function toOrderList() {
  189. if (!await checkLoginShowModal()) return;
  190. uni.navigateTo({
  191. url: '/pages/order/index'
  192. });
  193. }
  194. // 跳转到地址簿
  195. async function toAddressBook() {
  196. if (!await checkLoginShowModal()) return;
  197. uni.navigateTo({
  198. url: '/pages/address/address_list'
  199. });
  200. }
  201. // 跳转到月结码
  202. async function toSettlementCode() {
  203. if (!await checkLoginShowModal()) return;
  204. uni.navigateTo({
  205. url: '/pages/mine/settlementCode'
  206. });
  207. }
  208. // 跳转到隐私政策
  209. async function toPrivacyPolicy() {
  210. uni.navigateTo({
  211. url: '/pages/webview/webview?url=' + encodeURIComponent('https://your-domain.com/privacy')
  212. });
  213. }
  214. // 跳转到关于我们
  215. async function toAboutUs() {
  216. uni.navigateTo({
  217. url: '/pages/about/index'
  218. });
  219. }
  220. // 处理获取手机号登录 - 使用优化后的quickLogin
  221. async function handleGetPhoneNumber(e) {
  222. if (isLogging.value || isProcessing.value) return;
  223. isLogging.value = true;
  224. isProcessing.value = true;
  225. // 先隐藏loading(如果有)
  226. uni.hideLoading();
  227. try {
  228. // 显示加载提示
  229. uni.showLoading({
  230. title: '登录中...',
  231. mask: true
  232. });
  233. // 调用快速登录,并传入自定义回调
  234. await quickLogin(e, {
  235. onSuccess: (result) => {
  236. console.log('页面登录成功回调:', result);
  237. appStore.UPDATE_USERINFO(result.data)
  238. },
  239. onFail: (error) => {
  240. console.log('页面登录失败回调:', error);
  241. // 页面特定的失败逻辑
  242. let errorMessage = error.message || '登录失败,请重试';
  243. if (error.type === 'auth_denied') {
  244. errorMessage = '您拒绝了授权,无法登录';
  245. // 如果是授权拒绝,可以显示引导重新授权的提示
  246. uni.showModal({
  247. title: '提示',
  248. content: '需要手机号授权才能正常使用,请点击右上角菜单,选择「重新进入小程序」后重新授权',
  249. showCancel: false,
  250. confirmText: '知道了'
  251. });
  252. } else if (error.type === 'auth_code_missing') {
  253. errorMessage = '授权信息不完整';
  254. } else if (error.type === 'login_failed') {
  255. errorMessage = error.originalError || error.message;
  256. }
  257. // 只在需要时显示Toast(非授权拒绝的情况)
  258. if (error.type !== 'auth_denied') {
  259. uni.showToast({
  260. title: errorMessage,
  261. icon: 'none',
  262. duration: 3000
  263. });
  264. }
  265. },
  266. redirectUrl: '/pages/mine/mine'
  267. });
  268. } catch (error) {
  269. console.error('登录处理异常:', error);
  270. // 这里是未在onFail中捕获的错误(应该是很少出现的)
  271. uni.showToast({
  272. title: error,
  273. icon: 'none'
  274. });
  275. } finally {
  276. isLogging.value = false;
  277. isProcessing.value = false;
  278. // 确保loading被隐藏
  279. setTimeout(() => {
  280. uni.hideLoading();
  281. }, 300);
  282. }
  283. }
  284. // 拨打电话
  285. const makePhoneCall = () => {
  286. if (!phoneNumber.value) {
  287. uni.showToast({
  288. title: '客服电话暂时无法接通',
  289. icon: 'none'
  290. });
  291. return;
  292. }
  293. uni.makePhoneCall({
  294. phoneNumber: phoneNumber.value,
  295. success: () => {
  296. console.log('成功唤起拨号界面');
  297. },
  298. fail: (err) => {
  299. console.error('唤起拨号界面失败', err);
  300. uni.showToast({
  301. title: '拨号失败',
  302. icon: 'none'
  303. });
  304. }
  305. });
  306. };
  307. // 退出登录
  308. function userLogoutFn() {
  309. uni.showModal({
  310. title: '提示',
  311. content: '确认要退出登录吗?',
  312. success: function(res) {
  313. if (res.confirm) {
  314. uni.showLoading({
  315. title: '正在退出...'
  316. });
  317. userLogout({}).then(res => {
  318. appStore.LOGOUT();
  319. uni.hideLoading();
  320. uni.showToast({
  321. title: '已退出登录',
  322. icon: 'success'
  323. });
  324. // 延迟刷新页面状态
  325. setTimeout(() => {
  326. uni.$emit('userLogoutSuccess');
  327. }, 500);
  328. }).catch(err => {
  329. uni.hideLoading();
  330. uni.showToast({
  331. title: '退出失败',
  332. icon: 'none'
  333. });
  334. });
  335. }
  336. }
  337. });
  338. }
  339. </script>
  340. <style lang="less" scoped>
  341. .mine-container {
  342. height: 100vh;
  343. background: linear-gradient(135deg, #CFE9FF 0%, #F5F7FA 50.86%);
  344. position: relative;
  345. // 用户信息区域
  346. .user-header {
  347. padding: 0rpx 24rpx;
  348. .user-info-section {
  349. margin-top: 32rpx;
  350. display: flex;
  351. flex-direction: column;
  352. gap: 20rpx;
  353. .avatar-section {
  354. display: flex;
  355. align-items: center;
  356. min-height: 144rpx; // 添加最小高度防止闪烁
  357. .user-text-info {
  358. flex: 1;
  359. display: flex;
  360. flex-direction: column;
  361. justify-content: center;
  362. min-height: 144rpx; // 添加最小高度防止闪烁
  363. }
  364. .user-name-section {
  365. margin-left: 16rpx;
  366. min-height: 100rpx; // 固定高度防止闪烁
  367. display: flex;
  368. flex-direction: column;
  369. justify-content: center;
  370. .name-section {
  371. width: 530rpx;
  372. height: 52rpx;
  373. display: flex;
  374. align-items: center;
  375. margin-bottom: 8rpx;
  376. }
  377. .user-name {
  378. font-weight: bold;
  379. font-size: 36rpx;
  380. color: #333333;
  381. text-align: center;
  382. line-height: 1;
  383. white-space: nowrap; // 强制文本在一行内显示
  384. overflow: hidden; // 隐藏溢出部分
  385. text-overflow: ellipsis; // 用省略号代替被隐藏的文本
  386. }
  387. .login-button {
  388. background: transparent;
  389. font-size: 40rpx;
  390. font-weight: 600;
  391. color: #333;
  392. line-height: 56rpx;
  393. padding: 0;
  394. margin: 0;
  395. text-align: left;
  396. height: auto;
  397. min-height: 56rpx;
  398. display: flex;
  399. align-items: center;
  400. justify-content: flex-start;
  401. &::after {
  402. border: none;
  403. }
  404. &[disabled] {
  405. opacity: 0.7;
  406. }
  407. }
  408. }
  409. .user-account {
  410. height: 44rpx;
  411. font-weight: 400;
  412. font-size: 28rpx;
  413. color: #333333;
  414. line-height: 44rpx;
  415. margin-top: 4rpx;
  416. }
  417. }
  418. .department-badge {
  419. background: #1B64F0;
  420. border-radius: 8rpx;
  421. font-size: 24rpx;
  422. color: #FFFFFF;
  423. text-align: center;
  424. margin-left: 8rpx;
  425. padding: 7rpx 16rpx;
  426. line-height: 1;
  427. flex-shrink: 0;
  428. max-width: 250rpx;
  429. white-space: nowrap; // 强制文本在一行内显示
  430. overflow: hidden; // 隐藏溢出部分
  431. text-overflow: ellipsis; // 用省略号代替被隐藏的文本
  432. }
  433. }
  434. }
  435. // 菜单区域
  436. .menu-section {
  437. padding: 0 20rpx;
  438. margin-top: 20rpx;
  439. .menu-card {
  440. overflow: hidden;
  441. }
  442. .menu-item {
  443. display: flex;
  444. align-items: center;
  445. justify-content: space-between;
  446. padding: 32rpx;
  447. background: #FFFFFF;
  448. border-radius: 24rpx;
  449. margin-bottom: 20rpx;
  450. min-height: 112rpx; // 固定高度防止闪烁
  451. &:last-child {
  452. border-bottom: none;
  453. margin-bottom: 0;
  454. }
  455. .menu-item-left {
  456. display: flex;
  457. align-items: center;
  458. gap: 24rpx;
  459. flex: 1;
  460. }
  461. .menu-icon {
  462. width: 48rpx;
  463. height: 48rpx;
  464. flex-shrink: 0;
  465. }
  466. .menu-text {
  467. font-size: 32rpx;
  468. font-weight: 500;
  469. color: #333333;
  470. line-height: 45rpx;
  471. }
  472. .arrow-icon {
  473. width: 48rpx;
  474. height: 48rpx;
  475. opacity: 0.5;
  476. flex-shrink: 0;
  477. }
  478. &.logout-item {
  479. height: 88rpx;
  480. background: #F52929;
  481. border-radius: 32rpx;
  482. display: flex;
  483. justify-content: center;
  484. align-items: center;
  485. .logout-text {
  486. font-weight: 400;
  487. font-size: 32rpx;
  488. color: #FFFFFF;
  489. text-align: center;
  490. }
  491. }
  492. }
  493. }
  494. }
  495. // 动画效果
  496. .menu-item {
  497. transition: all 0.3s ease;
  498. opacity: 1;
  499. &:active {
  500. background-color: rgba(66, 133, 244, 0.05);
  501. transform: translateX(8rpx);
  502. }
  503. &[disabled] {
  504. opacity: 0.6;
  505. pointer-events: none;
  506. }
  507. }
  508. .department-badge {
  509. transition: all 0.3s ease;
  510. &:active {
  511. transform: scale(0.95);
  512. }
  513. }
  514. // 响应式适配
  515. @media (max-width: 375px) {
  516. .menu-section {
  517. padding: 0 20rpx !important;
  518. }
  519. .menu-card {
  520. padding: 0 20rpx !important;
  521. }
  522. .menu-text {
  523. font-size: 30rpx !important;
  524. }
  525. }
  526. // 防止闪烁的全局样式
  527. view, text, image {
  528. will-change: auto;
  529. backface-visibility: hidden;
  530. -webkit-backface-visibility: hidden;
  531. }
  532. </style>