index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. <view class="input-row">
  10. <input
  11. class="input input-grow"
  12. v-model="mobile"
  13. placeholder="手机号"
  14. type="number"
  15. @focus="onMobileFocus"
  16. />
  17. <view class="history-btn" @click="showAccountHistory">
  18. <text>历史</text>
  19. </view>
  20. </view>
  21. <input v-if="loginType === 'password'" class="input" v-model="password" placeholder="密码" password />
  22. <view v-if="loginType === 'password'" class="remember-row" @click="rememberPassword = !rememberPassword">
  23. <view class="remember-box" :class="{ on: rememberPassword }">
  24. <text v-if="rememberPassword" class="remember-tick">✓</text>
  25. </view>
  26. <text class="remember-label">记住密码</text>
  27. </view>
  28. <view v-else class="sms-row">
  29. <input class="input sms-input" v-model="code" placeholder="验证码" type="number" />
  30. <view class="sms-btn" :class="{ disabled: sending || countdown > 0 }" @click="onSendCode">
  31. <text>{{ countdown > 0 ? countdown + 's' : (sending ? '发送中' : '获取验证码') }}</text>
  32. </view>
  33. </view>
  34. <view class="submit" :class="{ disabled: loading }" @click="onLogin">
  35. <text>{{ loading ? '登录中...' : '登录' }}</text>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { login, loginBySms, sendSmsCode, setToken, getCurrentUserInfo, getUserIdFromToken } from '../../utils/api'
  42. const USER_KEY = 'current_user'
  43. const LOGIN_ACCOUNTS_KEY = 'login_saved_accounts'
  44. const REMEMBER_PREF_KEY = 'login_remember_password_pref'
  45. function loadSavedAccounts() {
  46. try {
  47. const raw = uni.getStorageSync(LOGIN_ACCOUNTS_KEY)
  48. if (raw == null || raw === '') return []
  49. const arr = typeof raw === 'string' ? JSON.parse(raw) : raw
  50. return Array.isArray(arr) ? arr : []
  51. } catch (e) {
  52. return []
  53. }
  54. }
  55. function saveAccounts(list) {
  56. try {
  57. uni.setStorageSync(LOGIN_ACCOUNTS_KEY, JSON.stringify(list))
  58. } catch (e) {}
  59. }
  60. export default {
  61. data() {
  62. return {
  63. loginType: 'password',
  64. mobile: '',
  65. password: '',
  66. code: '',
  67. rememberPassword: false,
  68. loading: false,
  69. sending: false,
  70. countdown: 0,
  71. timer: null,
  72. _historyShownForFocus: false
  73. }
  74. },
  75. onLoad() {
  76. try {
  77. const pref = uni.getStorageSync(REMEMBER_PREF_KEY)
  78. if (pref === true || pref === 'true') this.rememberPassword = true
  79. } catch (e) {}
  80. const list = loadSavedAccounts()
  81. if (list.length && list[0].mobile) {
  82. this.mobile = list[0].mobile
  83. if (this.rememberPassword && list[0].password) this.password = list[0].password
  84. }
  85. },
  86. onUnload() {
  87. if (this.timer) clearInterval(this.timer)
  88. this.timer = null
  89. },
  90. methods: {
  91. onMobileFocus() {
  92. const list = loadSavedAccounts()
  93. if (!list.length || this._historyShownForFocus) return
  94. this._historyShownForFocus = true
  95. this.showAccountHistory()
  96. },
  97. showAccountHistory() {
  98. const accounts = loadSavedAccounts()
  99. if (!accounts.length) {
  100. uni.showToast({ title: '暂无历史账号', icon: 'none' })
  101. return
  102. }
  103. const itemList = accounts.map((a) => String(a.mobile || ''))
  104. uni.showActionSheet({
  105. itemList,
  106. success: (res) => {
  107. const acc = accounts[res.tapIndex]
  108. if (!acc || !acc.mobile) return
  109. this.mobile = String(acc.mobile)
  110. if (this.loginType === 'password' && acc.password) {
  111. this.password = acc.password
  112. this.$nextTick(() => this.onLogin())
  113. } else {
  114. this.password = ''
  115. }
  116. },
  117. fail: () => {}
  118. })
  119. },
  120. recordSuccessfulLogin() {
  121. const m = String(this.mobile || '').trim()
  122. if (!m) return
  123. const prevList = loadSavedAccounts()
  124. const prev = prevList.find((a) => a.mobile === m)
  125. const rest = prevList.filter((a) => a.mobile !== m)
  126. const item = { mobile: m }
  127. if (this.loginType === 'password') {
  128. if (this.rememberPassword) item.password = this.password
  129. } else if (prev && prev.password) {
  130. item.password = prev.password
  131. }
  132. try {
  133. uni.setStorageSync(REMEMBER_PREF_KEY, !!this.rememberPassword)
  134. } catch (e) {}
  135. rest.unshift(item)
  136. saveAccounts(rest.slice(0, 30))
  137. },
  138. async onSendCode() {
  139. if (this.sending || this.countdown > 0) return
  140. if (!this.mobile) {
  141. uni.showToast({ title: '请输入手机号', icon: 'none' })
  142. return
  143. }
  144. this.sending = true
  145. try {
  146. // 与桌面端保持一致,后端期望 platform: 'pc'
  147. await sendSmsCode(this.mobile)
  148. uni.showToast({ title: '验证码已发送', icon: 'none' })
  149. this.countdown = 60
  150. this.timer = setInterval(() => {
  151. this.countdown -= 1
  152. if (this.countdown <= 0) {
  153. clearInterval(this.timer)
  154. this.timer = null
  155. this.countdown = 0
  156. }
  157. }, 1000)
  158. } catch (e) {
  159. uni.showToast({ title: e.message || '发送失败', icon: 'none' })
  160. } finally {
  161. this.sending = false
  162. }
  163. },
  164. async onLogin() {
  165. if (this.loading) return
  166. if (!this.mobile) {
  167. uni.showToast({ title: '请输入手机号', icon: 'none' })
  168. return
  169. }
  170. if (this.loginType === 'password' && !this.password) {
  171. uni.showToast({ title: '请输入密码', icon: 'none' })
  172. return
  173. }
  174. if (this.loginType === 'sms' && !this.code) {
  175. uni.showToast({ title: '请输入验证码', icon: 'none' })
  176. return
  177. }
  178. this.loading = true
  179. try {
  180. const data =
  181. this.loginType === 'password'
  182. ? await login(this.mobile, this.password)
  183. : await loginBySms(this.mobile, this.code)
  184. const accessToken = data.access_token
  185. if (!accessToken) throw new Error('登录失败:未返回 access_token')
  186. setToken(accessToken)
  187. // 尽量补全 user 信息:优先用 login 返回的 user,否则拉 /users/me
  188. let user = data.user || null
  189. if (!user || !user.id) {
  190. const id = getUserIdFromToken(accessToken)
  191. user = user || {}
  192. if (id) user.id = id
  193. }
  194. if (!user || !user.id || !user.name) {
  195. try {
  196. const me = await getCurrentUserInfo(accessToken)
  197. // 兼容不同后端包裹
  198. user = me.user || me.data || me
  199. } catch (e) {}
  200. }
  201. try {
  202. uni.setStorageSync(USER_KEY, user || {})
  203. } catch (e) {}
  204. this.recordSuccessfulLogin()
  205. uni.reLaunch({ url: '/pages/index/index' })
  206. } catch (e) {
  207. uni.showToast({ title: e.message || '登录失败', icon: 'none' })
  208. } finally {
  209. this.loading = false
  210. }
  211. }
  212. }
  213. }
  214. </script>
  215. <style scoped>
  216. .login-page {
  217. min-height: 100vh;
  218. background: #fff;
  219. padding: 64rpx 48rpx;
  220. box-sizing: border-box;
  221. }
  222. .title {
  223. font-size: 44rpx;
  224. font-weight: 700;
  225. color: #222;
  226. margin-bottom: 48rpx;
  227. }
  228. .tabs {
  229. display: flex;
  230. gap: 24rpx;
  231. margin-bottom: 32rpx;
  232. }
  233. .tab {
  234. padding: 16rpx 24rpx;
  235. border-radius: 999rpx;
  236. background: #f0f0f0;
  237. color: #666;
  238. font-size: 26rpx;
  239. }
  240. .tab.active {
  241. background: rgba(37, 150, 83, 0.12);
  242. color: #259653;
  243. }
  244. .form {
  245. margin-top: 24rpx;
  246. }
  247. .input-row {
  248. display: flex;
  249. align-items: center;
  250. gap: 16rpx;
  251. margin-bottom: 20rpx;
  252. }
  253. .input-grow {
  254. flex: 1;
  255. margin-bottom: 0;
  256. }
  257. .history-btn {
  258. height: 88rpx;
  259. padding: 0 28rpx;
  260. border-radius: 16rpx;
  261. background: #f0f0f0;
  262. color: #259653;
  263. font-size: 26rpx;
  264. display: flex;
  265. align-items: center;
  266. justify-content: center;
  267. flex-shrink: 0;
  268. }
  269. .input {
  270. height: 88rpx;
  271. padding: 0 24rpx;
  272. background: #f7f7f7;
  273. border-radius: 16rpx;
  274. font-size: 28rpx;
  275. margin-bottom: 20rpx;
  276. }
  277. .remember-row {
  278. display: flex;
  279. align-items: center;
  280. gap: 16rpx;
  281. margin-bottom: 20rpx;
  282. margin-top: -8rpx;
  283. }
  284. .remember-box {
  285. width: 36rpx;
  286. height: 36rpx;
  287. border-radius: 8rpx;
  288. border: 2rpx solid #ccc;
  289. display: flex;
  290. align-items: center;
  291. justify-content: center;
  292. box-sizing: border-box;
  293. }
  294. .remember-box.on {
  295. background: #259653;
  296. border-color: #259653;
  297. }
  298. .remember-tick {
  299. color: #fff;
  300. font-size: 22rpx;
  301. line-height: 1;
  302. }
  303. .remember-label {
  304. font-size: 26rpx;
  305. color: #666;
  306. }
  307. .sms-row {
  308. display: flex;
  309. align-items: center;
  310. gap: 16rpx;
  311. }
  312. .sms-input {
  313. flex: 1;
  314. margin-bottom: 0;
  315. }
  316. .sms-btn {
  317. height: 88rpx;
  318. padding: 0 24rpx;
  319. border-radius: 16rpx;
  320. display: flex;
  321. align-items: center;
  322. justify-content: center;
  323. background: #259653;
  324. color: #fff;
  325. font-size: 26rpx;
  326. }
  327. .sms-btn.disabled {
  328. opacity: 0.6;
  329. }
  330. .submit {
  331. margin-top: 32rpx;
  332. height: 92rpx;
  333. border-radius: 18rpx;
  334. background: #259653;
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. color: #fff;
  339. font-size: 30rpx;
  340. }
  341. .submit.disabled {
  342. opacity: 0.7;
  343. }
  344. </style>