index.vue 9.3 KB

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