login.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <script lang="ts" setup>
  2. import { useTokenStore } from '@/store/token'
  3. import { isPageTabbar } from '@/tabbar/store'
  4. import { HOME_PAGE } from '@/utils/index'
  5. // 使用自定义导航栏样式
  6. definePage({
  7. style: {
  8. navigationBarTitleText: '',
  9. navigationStyle: 'custom',
  10. backgroundColor: '#f7f7f7',
  11. },
  12. })
  13. const tokenStore = useTokenStore()
  14. async function doLogin() {
  15. if (tokenStore.hasLogin) {
  16. uni.navigateBack()
  17. return
  18. }
  19. try {
  20. uni.showLoading({
  21. title: '登录中...',
  22. })
  23. // 调用登录接口
  24. await tokenStore.wxLogin()
  25. // 登录成功后,检查是否有重定向参数
  26. const pages = getCurrentPages()
  27. const currentPage = pages[pages.length - 1]
  28. const redirect = currentPage?.options?.redirect
  29. uni.hideLoading()
  30. if (redirect) {
  31. const redirectUrl = decodeURIComponent(redirect)
  32. if (isPageTabbar(redirectUrl)) {
  33. uni.switchTab({ url: redirectUrl })
  34. }
  35. else {
  36. uni.redirectTo({ url: redirectUrl })
  37. }
  38. }
  39. else {
  40. // 默认跳转到首页
  41. if (isPageTabbar(HOME_PAGE)) {
  42. uni.switchTab({ url: HOME_PAGE })
  43. }
  44. else {
  45. uni.redirectTo({ url: HOME_PAGE })
  46. }
  47. }
  48. }
  49. catch (error) {
  50. uni.hideLoading()
  51. console.log('登录失败', error)
  52. uni.showToast({
  53. title: '登录失败,请稍后重试',
  54. icon: 'none',
  55. })
  56. }
  57. }
  58. </script>
  59. <template>
  60. <view class="login-container">
  61. <!-- Logo区域 -->
  62. <view class="logo-section">
  63. <image class="logo" src="~@/static/images/index/logo.jpg" mode="aspectFit" />
  64. </view>
  65. <!-- 登录按钮区域 -->
  66. <view class="login-btn-section">
  67. <button class="login-btn" open-type="getUserInfo" @click="doLogin">
  68. <text class="btn-text">微信快捷登录</text>
  69. </button>
  70. </view>
  71. </view>
  72. </template>
  73. <style lang="scss" scoped>
  74. .login-container {
  75. display: flex;
  76. flex-direction: column;
  77. height: 100vh;
  78. background-color: #f7f7f7;
  79. padding: 0 30px;
  80. }
  81. .logo-section {
  82. flex: 1;
  83. display: flex;
  84. justify-content: center;
  85. align-items: center;
  86. .logo {
  87. width: 160px;
  88. height: 160px;
  89. border-radius: 20px;
  90. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  91. }
  92. }
  93. .login-btn-section {
  94. margin-bottom: 390rpx;
  95. .login-btn {
  96. background-color: #07c160;
  97. color: white;
  98. font-size: 16px;
  99. font-weight: 500;
  100. width: 100%;
  101. height: 48px;
  102. line-height: 48px;
  103. border-radius: 24px;
  104. border: none;
  105. box-shadow: 0 2px 8px rgba(7, 193, 96, 0.3);
  106. &:active {
  107. background-color: #06b356;
  108. }
  109. }
  110. }
  111. .agreement-section {
  112. text-align: center;
  113. padding-bottom: 30px;
  114. .agreement-text {
  115. font-size: 12px;
  116. color: #999;
  117. .link {
  118. color: #07c160;
  119. text-decoration: underline;
  120. }
  121. }
  122. }
  123. </style>