index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <view class="user-info" v-if="isLoggedIn">
  5. <view class="avatar">
  6. <text class="avatar-text">{{ userInfo.nickname ? userInfo.nickname.charAt(0) : '用' }}</text>
  7. </view>
  8. <view class="user-details">
  9. <text class="nickname">{{ userInfo.nickname || '酒谷用户' }}</text>
  10. <text class="phone">{{ userInfo.phone || '' }}</text>
  11. </view>
  12. </view>
  13. <view class="login-prompt" v-else>
  14. <view class="avatar">
  15. <text class="avatar-text">未</text>
  16. </view>
  17. <view class="user-details">
  18. <text class="nickname">未登录</text>
  19. <button class="login-btn" @click="goLogin">立即登录</button>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="menu-section">
  24. <view class="menu-group">
  25. <view class="menu-item" @click="handleMenuClick('orders')">
  26. <text class="menu-icon">📦</text>
  27. <text class="menu-text">我的订单</text>
  28. <text class="menu-arrow">></text>
  29. </view>
  30. <view class="menu-item" @click="handleMenuClick('favorites')">
  31. <text class="menu-icon">❤️</text>
  32. <text class="menu-text">我的收藏</text>
  33. <text class="menu-arrow">></text>
  34. </view>
  35. <view class="menu-item" @click="handleMenuClick('address')">
  36. <text class="menu-icon">📍</text>
  37. <text class="menu-text">收货地址</text>
  38. <text class="menu-arrow">></text>
  39. </view>
  40. </view>
  41. <view class="menu-group">
  42. <view class="menu-item" @click="handleMenuClick('coupon')">
  43. <text class="menu-icon">🎫</text>
  44. <text class="menu-text">优惠券</text>
  45. <text class="menu-arrow">></text>
  46. </view>
  47. <view class="menu-item" @click="handleMenuClick('points')">
  48. <text class="menu-icon">💎</text>
  49. <text class="menu-text">积分中心</text>
  50. <text class="menu-arrow">></text>
  51. </view>
  52. <view class="menu-item" @click="handleMenuClick('service')">
  53. <text class="menu-icon">🎧</text>
  54. <text class="menu-text">客服中心</text>
  55. <text class="menu-arrow">></text>
  56. </view>
  57. </view>
  58. <view class="menu-group">
  59. <view class="menu-item" @click="handleMenuClick('settings')">
  60. <text class="menu-icon">⚙️</text>
  61. <text class="menu-text">设置</text>
  62. <text class="menu-arrow">></text>
  63. </view>
  64. </view>
  65. </view>
  66. <view class="logout-section" v-if="isLoggedIn">
  67. <button class="logout-btn" @click="handleLogout">
  68. 退出登录
  69. </button>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import { isLoggedIn, getUserInfo, requireAuth, navigateToLogin, clearUserData, PROTECTED_ACTIONS } from '@/utils/auth.js'
  75. export default {
  76. name: 'User',
  77. data() {
  78. return {
  79. isLoggedIn: false,
  80. userInfo: {}
  81. }
  82. },
  83. onLoad() {
  84. console.log('我的页面加载')
  85. },
  86. onShow() {
  87. this.checkLoginStatus()
  88. // 无论是否登录都进行权限检查,确保游客模式下能正确触发登录
  89. this.checkAuthAccess()
  90. // 更新自定义tabBar选中状态
  91. this.updateTabBar()
  92. },
  93. methods: {
  94. // 更新tabBar选中状态
  95. updateTabBar() {
  96. try {
  97. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  98. this.getTabBar().updateSelected(4) // 我的索引为4
  99. }
  100. } catch (error) {
  101. console.log('更新tabBar状态失败:', error)
  102. }
  103. },
  104. checkLoginStatus() {
  105. this.isLoggedIn = isLoggedIn()
  106. if (this.isLoggedIn) {
  107. this.userInfo = getUserInfo() || {}
  108. }
  109. },
  110. checkAuthAccess() {
  111. requireAuth(() => {
  112. console.log('用户已登录,可以访问个人中心')
  113. this.checkLoginStatus()
  114. }, {
  115. action: PROTECTED_ACTIONS.VIEW_MY_PAGE,
  116. message: '访问个人中心需要先登录'
  117. })
  118. },
  119. goLogin() {
  120. navigateToLogin('/pages/user/index')
  121. },
  122. handleMenuClick(type) {
  123. requireAuth(() => {
  124. switch(type) {
  125. case 'orders':
  126. uni.showToast({ title: '我的订单', icon: 'none' })
  127. break
  128. case 'favorites':
  129. uni.showToast({ title: '我的收藏', icon: 'none' })
  130. break
  131. case 'address':
  132. uni.showToast({ title: '收货地址', icon: 'none' })
  133. break
  134. case 'coupon':
  135. uni.showToast({ title: '优惠券', icon: 'none' })
  136. break
  137. case 'points':
  138. uni.showToast({ title: '积分中心', icon: 'none' })
  139. break
  140. case 'service':
  141. uni.showToast({ title: '客服中心', icon: 'none' })
  142. break
  143. case 'settings':
  144. uni.showToast({ title: '设置', icon: 'none' })
  145. break
  146. default:
  147. break
  148. }
  149. }, {
  150. message: '此功能需要先登录',
  151. returnUrl: '/pages/user/index'
  152. })
  153. },
  154. handleLogout() {
  155. uni.showModal({
  156. title: '提示',
  157. content: '确定要退出登录吗?',
  158. success: (res) => {
  159. if (res.confirm) {
  160. clearUserData()
  161. this.checkLoginStatus()
  162. uni.showToast({
  163. title: '已退出登录',
  164. icon: 'success'
  165. })
  166. }
  167. }
  168. })
  169. }
  170. }
  171. }
  172. </script>
  173. <style lang="scss" scoped>
  174. .container {
  175. min-height: 100vh;
  176. background: #f5f5f5;
  177. }
  178. .header {
  179. background: linear-gradient(135deg, #FF6600 0%, #FF8533 100%);
  180. padding: 60rpx 30rpx 40rpx;
  181. color: white;
  182. .user-info, .login-prompt {
  183. display: flex;
  184. align-items: center;
  185. .avatar {
  186. width: 120rpx;
  187. height: 120rpx;
  188. border-radius: 60rpx;
  189. background: rgba(255, 255, 255, 0.3);
  190. display: flex;
  191. align-items: center;
  192. justify-content: center;
  193. margin-right: 30rpx;
  194. .avatar-text {
  195. font-size: 48rpx;
  196. font-weight: bold;
  197. color: white;
  198. }
  199. }
  200. .user-details {
  201. flex: 1;
  202. .nickname {
  203. display: block;
  204. font-size: 36rpx;
  205. font-weight: bold;
  206. margin-bottom: 10rpx;
  207. }
  208. .phone {
  209. display: block;
  210. font-size: 28rpx;
  211. opacity: 0.8;
  212. }
  213. .login-btn {
  214. background: rgba(255, 255, 255, 0.2);
  215. color: white;
  216. border: 2rpx solid rgba(255, 255, 255, 0.5);
  217. border-radius: 30rpx;
  218. padding: 15rpx 30rpx;
  219. font-size: 28rpx;
  220. margin-top: 10rpx;
  221. &:active {
  222. background: rgba(255, 255, 255, 0.3);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. .menu-section {
  229. padding: 20rpx;
  230. .menu-group {
  231. background: white;
  232. border-radius: 20rpx;
  233. margin-bottom: 20rpx;
  234. overflow: hidden;
  235. .menu-item {
  236. display: flex;
  237. align-items: center;
  238. padding: 30rpx;
  239. border-bottom: 1rpx solid #f0f0f0;
  240. &:last-child {
  241. border-bottom: none;
  242. }
  243. &:active {
  244. background: #f8f8f8;
  245. }
  246. .menu-icon {
  247. font-size: 40rpx;
  248. margin-right: 30rpx;
  249. }
  250. .menu-text {
  251. flex: 1;
  252. font-size: 32rpx;
  253. color: #333;
  254. }
  255. .menu-arrow {
  256. font-size: 32rpx;
  257. color: #ccc;
  258. }
  259. }
  260. }
  261. }
  262. .logout-section {
  263. padding: 40rpx 20rpx;
  264. .logout-btn {
  265. width: 100%;
  266. background: #fff;
  267. color: #ff4757;
  268. border: 2rpx solid #ff4757;
  269. border-radius: 40rpx;
  270. padding: 30rpx;
  271. font-size: 32rpx;
  272. &:active {
  273. background: #ff4757;
  274. color: white;
  275. }
  276. }
  277. }
  278. </style>