login.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. <template>
  2. <view class="login-container">
  3. <!-- 顶部导航栏 -->
  4. <view class="nav-bar">
  5. <text class="nav-title">登录</text>
  6. </view>
  7. <!-- Logo 区域 -->
  8. <view class="logo-section">
  9. <image class="logo-icon" src="/static/image/public/logo.png" mode="aspectFill"></image>
  10. <text class="app-name">{{appName}}</text>
  11. </view>
  12. <!-- 表单区域 -->
  13. <view class="form-section">
  14. <!-- 手机号输入 -->
  15. <view class="input-item">
  16. <uni-easyinput v-model="phoneNumber" placeholder="请输入手机号" type="number" maxlength="11"
  17. :clearable="false" class="phone-input" />
  18. </view>
  19. <!-- 验证码输入 -->
  20. <view class="input-item verify-code-row">
  21. <uni-easyinput v-model="verifyCode" placeholder="验证码" type="number" maxlength="6" :clearable="false"
  22. class="code-input" />
  23. <text class="get-code-btn" :class="{ disabled: countdown > 0 }" @click="getVerifyCode">
  24. {{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
  25. </text>
  26. </view>
  27. <!-- 登录按钮 -->
  28. <view class="login-btn-wrapper">
  29. <button class="login-btn" @click="handleLogin" :loading="isLoading">登录</button>
  30. </view>
  31. <!-- 协议说明 -->
  32. <view class="agreement-text">
  33. <text class="gray-text">登录即代表您已同意</text>
  34. <text class="link-text" @click="showUserAgreement">《用户协议》</text>
  35. <text class="gray-text">与</text>
  36. <text class="link-text" @click="showPrivacyPolicy">《隐私政策》</text>
  37. </view>
  38. </view>
  39. <!-- 其他登录方式 -->
  40. <view class="other-method-section">
  41. <view class="divider">
  42. <view class="divider-line"></view>
  43. <text class="divider-text">其他方式</text>
  44. <view class="divider-line"></view>
  45. </view>
  46. <!-- 微信一键登录 -->
  47. <view class="wechat-login-wrapper">
  48. <!-- #ifdef MP-WEIXIN -->
  49. <button class="wechat-login-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  50. <image class="wechat-icon-img" src="/static/image/public/wechat-icon.png" mode="aspectFit" />
  51. <text class="wechat-text">微信一键登录</text>
  52. </button>
  53. <!-- #endif -->
  54. <!-- #ifndef MP-WEIXIN -->
  55. <button class="wechat-login-btn" @click="wechatLogin">
  56. <text class="wechat-icon"></text>
  57. <text class="wechat-text">微信一键登录</text>
  58. </button>
  59. <!-- #endif -->
  60. </view>
  61. </view>
  62. <!-- 用户协议弹窗 -->
  63. <view class="agreement-popup" v-if="showUserAgreementPopup" @click="closeUserAgreement">
  64. <view class="popup-mask"></view>
  65. <view class="popup-content" @click.stop>
  66. <view class="popup-header">
  67. <text class="popup-title">用户协议</text>
  68. <text class="popup-close" @click="closeUserAgreement">✕</text>
  69. </view>
  70. <scroll-view scroll-y class="popup-body">
  71. <view class="agreement-content">
  72. <yonghuxieyi/>
  73. </view>
  74. </scroll-view>
  75. <view class="popup-footer">
  76. <button class="agree-btn" @click="closeUserAgreement">我已知晓</button>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- 隐私政策弹窗 -->
  81. <view class="agreement-popup" v-if="showPrivacyPolicyPopup" @click="closePrivacyPolicy">
  82. <view class="popup-mask"></view>
  83. <view class="popup-content" @click.stop>
  84. <view class="popup-header">
  85. <text class="popup-title">隐私政策</text>
  86. <text class="popup-close" @click="closePrivacyPolicy">✕</text>
  87. </view>
  88. <scroll-view scroll-y class="popup-body">
  89. <view class="agreement-content">
  90. <yonghuzhengce/>
  91. </view>
  92. </scroll-view>
  93. <view class="popup-footer">
  94. <button class="agree-btn" @click="closePrivacyPolicy">我已知晓</button>
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. </template>
  100. <script setup>
  101. import yonghuxieyi from "./yonghuxieyi.vue"
  102. import yonghuzhengce from "./yonghuzhengce.vue"
  103. import {
  104. getCurrentConfig
  105. } from '@/config/index.js'
  106. import {
  107. ref,
  108. onMounted
  109. } from 'vue'
  110. import {
  111. sendCode,
  112. codeLogin,
  113. wxgetLogin,
  114. getUserInfo,
  115. wxLogin
  116. } from '@/api/login.js'
  117. import {
  118. saveUserInfo,
  119. getToken
  120. } from '@/utils/userCache.js'
  121. let appName = ref('')
  122. let version = ref('')
  123. onMounted(async () => {
  124. const config = await getCurrentConfig()
  125. appName.value = config.appName
  126. version.value = config.appVersion
  127. })
  128. // 表单数据
  129. const phoneNumber = ref('')
  130. const verifyCode = ref('')
  131. const countdown = ref(0)
  132. const isLoading = ref(false)
  133. let timer = null
  134. // 弹窗控制
  135. const showUserAgreementPopup = ref(false)
  136. const showPrivacyPolicyPopup = ref(false)
  137. // 获取验证码
  138. const getVerifyCode = async () => {
  139. if (countdown.value > 0) return
  140. if (!phoneNumber.value || phoneNumber.value.length !== 11) {
  141. uni.showToast({
  142. title: '请输入正确的手机号',
  143. icon: 'none'
  144. })
  145. return
  146. }
  147. try {
  148. const res = await sendCode({
  149. phone: phoneNumber.value,
  150. type: 3
  151. })
  152. if (res.code != 200) {
  153. uni.showToast({
  154. title: res.msg,
  155. icon: 'error'
  156. })
  157. return
  158. }
  159. console.log('验证码发送成功:', res)
  160. // 开始倒计时
  161. countdown.value = 60
  162. timer = setInterval(() => {
  163. countdown.value--
  164. if (countdown.value <= 0) {
  165. clearInterval(timer)
  166. }
  167. }, 1000)
  168. uni.showToast({
  169. title: '验证码已发送',
  170. icon: 'success'
  171. })
  172. } catch (error) {
  173. console.error('发送验证码失败:', error)
  174. }
  175. }
  176. // 处理登录
  177. const handleLogin = async () => {
  178. if (isLoading.value) return
  179. if (!phoneNumber.value || phoneNumber.value.length !== 11) {
  180. uni.showToast({
  181. title: '请输入正确的手机号',
  182. icon: 'none'
  183. })
  184. return
  185. }
  186. if (!verifyCode.value) {
  187. uni.showToast({
  188. title: '请输入验证码',
  189. icon: 'none'
  190. })
  191. return
  192. }
  193. isLoading.value = true
  194. try {
  195. // 第一步:调用登录接口获取 token
  196. const res = await codeLogin({phonenumber:phoneNumber.value, smsCode:verifyCode.value})
  197. console.log('登录接口返回:', res)
  198. if (res.code != 200) {
  199. uni.showToast({
  200. title: res.msg || '登录失败',
  201. icon: 'none'
  202. })
  203. isLoading.value = false
  204. return
  205. }
  206. // 获取 token
  207. const { token } = res.data || res
  208. if (!token) {
  209. uni.showToast({
  210. title: '未获取到 token',
  211. icon: 'none'
  212. })
  213. isLoading.value = false
  214. return
  215. }
  216. // 第二步:调用 getUserInfo 获取用户信息
  217. const userInfoRes = await getUserInfo({})
  218. console.log('用户信息接口返回:', userInfoRes)
  219. if (userInfoRes.code != 200) {
  220. uni.showToast({
  221. title: userInfoRes.msg || '获取用户信息失败',
  222. icon: 'none'
  223. })
  224. isLoading.value = false
  225. return
  226. }
  227. // 第三步:保存用户信息和 token
  228. const userInfo = userInfoRes.data || userInfoRes
  229. const saveSuccess = saveUserInfo(userInfo, token)
  230. if (saveSuccess) {
  231. uni.showToast({
  232. title: '登录成功',
  233. icon: 'success'
  234. })
  235. // 延迟跳转
  236. setTimeout(() => {
  237. uni.reLaunch({
  238. url: '/pages/index/index'
  239. })
  240. }, 1500)
  241. } else {
  242. uni.showToast({
  243. title: '保存登录信息失败',
  244. icon: 'none'
  245. })
  246. }
  247. } catch (error) {
  248. console.error('登录失败:', error)
  249. uni.showToast({
  250. title: '登录失败,请重试',
  251. icon: 'none'
  252. })
  253. } finally {
  254. isLoading.value = false
  255. }
  256. }
  257. // 微信登录 - 获取手机号
  258. const getPhoneNumber = async (e) => {
  259. // #ifdef MP-WEIXIN
  260. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  261. uni.showToast({
  262. title: '您取消了授权',
  263. icon: 'none'
  264. })
  265. return
  266. }
  267. try {
  268. // 先获取微信登录 code
  269. const code = await wxLogin()
  270. // 调用 wxgetLogin 接口,传入 code 和手机号信息
  271. console.log(e,"eeeeeeee");
  272. const res = await wxgetLogin({
  273. wxCode: code,
  274. phoneCode: e.detail.code,
  275. iv: e.detail.iv,
  276. type: 1
  277. })
  278. if (res.code != 200) {
  279. uni.showToast({
  280. title: res.msg || '登录失败',
  281. icon: 'none'
  282. })
  283. return
  284. }
  285. console.log('微信登录成功:', res)
  286. // 获取 token
  287. const { token } = res.data || res
  288. if (!token) {
  289. uni.showToast({
  290. title: '未获取到 token',
  291. icon: 'none'
  292. })
  293. return
  294. }
  295. // 第二步:调用 getUserInfo 获取用户信息
  296. const userInfoRes = await getUserInfo({})
  297. console.log('用户信息接口返回:', userInfoRes)
  298. if (userInfoRes.code != 200) {
  299. uni.showToast({
  300. title: userInfoRes.msg || '获取用户信息失败',
  301. icon: 'none'
  302. })
  303. return
  304. }
  305. // 第三步:保存用户信息和 token
  306. const userInfo = userInfoRes.data || userInfoRes
  307. const saveSuccess = saveUserInfo(userInfo, token)
  308. if (saveSuccess) {
  309. uni.showToast({
  310. title: '登录成功',
  311. icon: 'success'
  312. })
  313. // 延迟跳转到首页
  314. setTimeout(() => {
  315. uni.reLaunch({
  316. url: '/pages/index/index'
  317. })
  318. }, .800)
  319. } else {
  320. uni.showToast({
  321. title: '保存登录信息失败',
  322. icon: 'none'
  323. })
  324. }
  325. } catch (error) {
  326. console.error('微信登录失败:', error)
  327. uni.showToast({
  328. title: '登录失败,请重试',
  329. icon: 'none'
  330. })
  331. }
  332. // #endif
  333. }
  334. // 微信登录(非小程序环境备用)
  335. const wechatLogin = async () => {
  336. // #ifndef MP-WEIXIN
  337. uni.showToast({
  338. title: '请在微信小程序中使用',
  339. icon: 'none'
  340. })
  341. // #endif
  342. }
  343. // 显示用户协议
  344. const showUserAgreement = () => {
  345. showUserAgreementPopup.value = true
  346. }
  347. // 关闭用户协议
  348. const closeUserAgreement = () => {
  349. showUserAgreementPopup.value = false
  350. }
  351. // 显示隐私政策
  352. const showPrivacyPolicy = () => {
  353. showPrivacyPolicyPopup.value = true
  354. }
  355. // 关闭隐私政策
  356. const closePrivacyPolicy = () => {
  357. showPrivacyPolicyPopup.value = false
  358. }
  359. // 页面卸载时清除定时器
  360. import {
  361. onUnmounted
  362. } from 'vue'
  363. onUnmounted(() => {
  364. if (timer) {
  365. clearInterval(timer)
  366. }
  367. })
  368. </script>
  369. <style lang="scss" scoped>
  370. .login-container {
  371. min-height: 100vh;
  372. background-color: #ffffff;
  373. padding: 0 40rpx;
  374. position: relative;
  375. }
  376. // 导航栏
  377. .nav-bar {
  378. height: 88rpx;
  379. display: flex;
  380. align-items: center;
  381. justify-content: center;
  382. .nav-title {
  383. font-size: 34rpx;
  384. font-weight: 600;
  385. color: #000000;
  386. }
  387. }
  388. // Logo 区域
  389. .logo-section {
  390. display: flex;
  391. flex-direction: column;
  392. align-items: center;
  393. margin-top: 60rpx;
  394. margin-bottom: 80rpx;
  395. .logo-icon {
  396. width: 160rpx;
  397. height: 160rpx;
  398. background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  399. border-radius: 32rpx;
  400. box-shadow: 0 8rpx 24rpx rgba(24, 144, 255, 0.3);
  401. }
  402. .app-name {
  403. font-size: 40rpx;
  404. font-weight: 600;
  405. color: #000000;
  406. margin-top: 32rpx;
  407. }
  408. }
  409. // 表单区域
  410. .form-section {
  411. .input-item {
  412. margin-bottom: 24rpx;
  413. .phone-input,
  414. .code-input {
  415. background-color: #f5f7fa;
  416. border-radius: 16rpx;
  417. font-size: 28rpx;
  418. }
  419. }
  420. .verify-code-row {
  421. display: flex;
  422. align-items: center;
  423. .code-input {
  424. flex: 1;
  425. margin-right: 20rpx;
  426. }
  427. .get-code-btn {
  428. font-size: 26rpx;
  429. color: #1890ff;
  430. white-space: nowrap;
  431. &.disabled {
  432. color: #999999;
  433. }
  434. }
  435. }
  436. .login-btn-wrapper {
  437. margin-top: 48rpx;
  438. margin-bottom: 24rpx;
  439. .login-btn {
  440. width: 100%;
  441. height: 88rpx;
  442. background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
  443. border-radius: 16rpx;
  444. border: none;
  445. font-size: 32rpx;
  446. font-weight: 500;
  447. color: #ffffff;
  448. display: flex;
  449. align-items: center;
  450. justify-content: center;
  451. &::after {
  452. border: none;
  453. }
  454. }
  455. }
  456. .agreement-text {
  457. display: flex;
  458. flex-wrap: wrap;
  459. justify-content: center;
  460. font-size: 22rpx;
  461. .gray-text {
  462. color: #999999;
  463. margin: 0 4rpx;
  464. }
  465. .link-text {
  466. color: #1890ff;
  467. margin: 0 4rpx;
  468. }
  469. }
  470. }
  471. // 其他登录方式
  472. .other-method-section {
  473. margin-top: 120rpx;
  474. .divider {
  475. display: flex;
  476. align-items: center;
  477. justify-content: center;
  478. margin-bottom: 40rpx;
  479. .divider-line {
  480. width: 120rpx;
  481. height: 1rpx;
  482. background-color: #e5e5e5;
  483. }
  484. .divider-text {
  485. font-size: 24rpx;
  486. color: #999999;
  487. margin: 0 24rpx;
  488. }
  489. }
  490. .wechat-login-wrapper {
  491. .wechat-login-btn {
  492. width: 100%;
  493. height: 88rpx;
  494. background: linear-gradient(90deg, #07c160 0%, #06ad56 100%);
  495. border-radius: 16rpx;
  496. border: none;
  497. font-size: 32rpx;
  498. font-weight: 500;
  499. color: #ffffff;
  500. display: flex;
  501. align-items: center;
  502. justify-content: center;
  503. &::after {
  504. border: none;
  505. }
  506. .wechat-icon-img {
  507. width: 40rpx;
  508. height: 40rpx;
  509. margin-right: 12rpx;
  510. }
  511. .wechat-icon {
  512. margin-right: 12rpx;
  513. font-size: 36rpx;
  514. }
  515. .wechat-text {
  516. color: #ffffff;
  517. }
  518. }
  519. }
  520. }
  521. // 协议弹窗
  522. .agreement-popup {
  523. position: fixed;
  524. top: 0;
  525. left: 0;
  526. right: 0;
  527. bottom: 0;
  528. z-index: 1000;
  529. .popup-mask {
  530. position: absolute;
  531. top: 0;
  532. left: 0;
  533. right: 0;
  534. bottom: 0;
  535. background-color: rgba(0, 0, 0, 0.5);
  536. }
  537. .popup-content {
  538. position: absolute;
  539. bottom: 0;
  540. left: 0;
  541. right: 0;
  542. background-color: #ffffff;
  543. border-radius: 32rpx 32rpx 0 0;
  544. max-height: 80vh;
  545. display: flex;
  546. flex-direction: column;
  547. animation: slideUp 0.3s ease-out;
  548. .popup-header {
  549. display: flex;
  550. align-items: center;
  551. justify-content: space-between;
  552. padding: 32rpx;
  553. border-bottom: 1rpx solid #f0f0f0;
  554. .popup-title {
  555. font-size: 32rpx;
  556. font-weight: 600;
  557. color: #000000;
  558. }
  559. .popup-close {
  560. font-size: 40rpx;
  561. color: #999999;
  562. width: 60rpx;
  563. height: 60rpx;
  564. display: flex;
  565. align-items: center;
  566. justify-content: center;
  567. }
  568. }
  569. .popup-body {
  570. flex: 1;
  571. overflow-y: auto;
  572. padding: 32rpx;
  573. .agreement-content {
  574. display: flex;
  575. flex-direction: column;
  576. .agreement-title {
  577. font-size: 36rpx;
  578. font-weight: 600;
  579. color: #000000;
  580. display: block;
  581. text-align: center;
  582. margin-bottom: 20rpx;
  583. }
  584. .update-time {
  585. font-size: 24rpx;
  586. color: #999999;
  587. display: block;
  588. text-align: center;
  589. margin-bottom: 40rpx;
  590. }
  591. .section {
  592. margin-bottom: 32rpx;
  593. .section-title {
  594. font-size: 28rpx;
  595. font-weight: 600;
  596. color: #000000;
  597. display: block;
  598. margin-bottom: 16rpx;
  599. }
  600. .section-content {
  601. font-size: 24rpx;
  602. color: #333333;
  603. line-height: 1.8;
  604. display: block;
  605. white-space: pre-wrap;
  606. }
  607. }
  608. }
  609. }
  610. .popup-footer {
  611. padding: 24rpx 32rpx 40rpx;
  612. border-top: 1rpx solid #f0f0f0;
  613. .agree-btn {
  614. width: 100%;
  615. height: 80rpx;
  616. background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
  617. border-radius: 16rpx;
  618. border: none;
  619. font-size: 30rpx;
  620. font-weight: 500;
  621. color: #ffffff;
  622. display: flex;
  623. align-items: center;
  624. justify-content: center;
  625. &::after {
  626. border: none;
  627. }
  628. }
  629. }
  630. }
  631. }
  632. @keyframes slideUp {
  633. from {
  634. transform: translateY(100%);
  635. }
  636. to {
  637. transform: translateY(0);
  638. }
  639. }
  640. </style>