login.vue 14 KB

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