index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <view class="login-page">
  3. <view class="title">登录</view>
  4. <view class="tabs">
  5. <view class="tab" :class="{ active: loginType === 'password' }" @click="loginType = 'password'">密码登录</view>
  6. <view class="tab" :class="{ active: loginType === 'sms' }" @click="loginType = 'sms'">验证码登录</view>
  7. </view>
  8. <view class="form">
  9. <input class="input" v-model="mobile" placeholder="手机号" type="number" />
  10. <input v-if="loginType === 'password'" class="input" v-model="password" placeholder="密码" password />
  11. <view v-else class="sms-row">
  12. <input class="input sms-input" v-model="code" placeholder="验证码" type="number" />
  13. <view class="sms-btn" :class="{ disabled: sending || countdown > 0 }" @click="onSendCode">
  14. <text>{{ countdown > 0 ? countdown + 's' : (sending ? '发送中' : '获取验证码') }}</text>
  15. </view>
  16. </view>
  17. <view class="submit" :class="{ disabled: loading }" @click="onLogin">
  18. <text>{{ loading ? '登录中...' : '登录' }}</text>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import { login, loginBySms, sendSmsCode, setToken, getCurrentUserInfo, getUserIdFromToken } from '../../utils/api'
  25. const USER_KEY = 'current_user'
  26. export default {
  27. data() {
  28. return {
  29. loginType: 'password',
  30. mobile: '',
  31. password: '',
  32. code: '',
  33. loading: false,
  34. sending: false,
  35. countdown: 0,
  36. timer: null
  37. }
  38. },
  39. onUnload() {
  40. if (this.timer) clearInterval(this.timer)
  41. this.timer = null
  42. },
  43. methods: {
  44. async onSendCode() {
  45. if (this.sending || this.countdown > 0) return
  46. if (!this.mobile) {
  47. uni.showToast({ title: '请输入手机号', icon: 'none' })
  48. return
  49. }
  50. this.sending = true
  51. try {
  52. // 与桌面端保持一致,后端期望 platform: 'pc'
  53. await sendSmsCode(this.mobile)
  54. uni.showToast({ title: '验证码已发送', icon: 'none' })
  55. this.countdown = 60
  56. this.timer = setInterval(() => {
  57. this.countdown -= 1
  58. if (this.countdown <= 0) {
  59. clearInterval(this.timer)
  60. this.timer = null
  61. this.countdown = 0
  62. }
  63. }, 1000)
  64. } catch (e) {
  65. uni.showToast({ title: e.message || '发送失败', icon: 'none' })
  66. } finally {
  67. this.sending = false
  68. }
  69. },
  70. async onLogin() {
  71. if (this.loading) return
  72. if (!this.mobile) {
  73. uni.showToast({ title: '请输入手机号', icon: 'none' })
  74. return
  75. }
  76. if (this.loginType === 'password' && !this.password) {
  77. uni.showToast({ title: '请输入密码', icon: 'none' })
  78. return
  79. }
  80. if (this.loginType === 'sms' && !this.code) {
  81. uni.showToast({ title: '请输入验证码', icon: 'none' })
  82. return
  83. }
  84. this.loading = true
  85. try {
  86. const data =
  87. this.loginType === 'password'
  88. ? await login(this.mobile, this.password)
  89. : await loginBySms(this.mobile, this.code)
  90. const accessToken = data.access_token
  91. if (!accessToken) throw new Error('登录失败:未返回 access_token')
  92. setToken(accessToken)
  93. // 尽量补全 user 信息:优先用 login 返回的 user,否则拉 /users/me
  94. let user = data.user || null
  95. if (!user || !user.id) {
  96. const id = getUserIdFromToken(accessToken)
  97. user = user || {}
  98. if (id) user.id = id
  99. }
  100. if (!user || !user.id || !user.name) {
  101. try {
  102. const me = await getCurrentUserInfo(accessToken)
  103. // 兼容不同后端包裹
  104. user = me.user || me.data || me
  105. } catch (e) {}
  106. }
  107. try {
  108. uni.setStorageSync(USER_KEY, user || {})
  109. } catch (e) {}
  110. uni.reLaunch({ url: '/pages/index/index' })
  111. } catch (e) {
  112. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  113. } finally {
  114. this.loading = false
  115. }
  116. }
  117. }
  118. }
  119. </script>
  120. <style scoped>
  121. .login-page {
  122. min-height: 100vh;
  123. background: #fff;
  124. padding: 64rpx 48rpx;
  125. box-sizing: border-box;
  126. }
  127. .title {
  128. font-size: 44rpx;
  129. font-weight: 700;
  130. color: #222;
  131. margin-bottom: 48rpx;
  132. }
  133. .tabs {
  134. display: flex;
  135. gap: 24rpx;
  136. margin-bottom: 32rpx;
  137. }
  138. .tab {
  139. padding: 16rpx 24rpx;
  140. border-radius: 999rpx;
  141. background: #f0f0f0;
  142. color: #666;
  143. font-size: 26rpx;
  144. }
  145. .tab.active {
  146. background: rgba(37, 150, 83, 0.12);
  147. color: #259653;
  148. }
  149. .form {
  150. margin-top: 24rpx;
  151. }
  152. .input {
  153. height: 88rpx;
  154. padding: 0 24rpx;
  155. background: #f7f7f7;
  156. border-radius: 16rpx;
  157. font-size: 28rpx;
  158. margin-bottom: 20rpx;
  159. }
  160. .sms-row {
  161. display: flex;
  162. align-items: center;
  163. gap: 16rpx;
  164. }
  165. .sms-input {
  166. flex: 1;
  167. margin-bottom: 0;
  168. }
  169. .sms-btn {
  170. height: 88rpx;
  171. padding: 0 24rpx;
  172. border-radius: 16rpx;
  173. display: flex;
  174. align-items: center;
  175. justify-content: center;
  176. background: #259653;
  177. color: #fff;
  178. font-size: 26rpx;
  179. }
  180. .sms-btn.disabled {
  181. opacity: 0.6;
  182. }
  183. .submit {
  184. margin-top: 32rpx;
  185. height: 92rpx;
  186. border-radius: 18rpx;
  187. background: #259653;
  188. display: flex;
  189. align-items: center;
  190. justify-content: center;
  191. color: #fff;
  192. font-size: 30rpx;
  193. }
  194. .submit.disabled {
  195. opacity: 0.7;
  196. }
  197. </style>