login.vue 14 KB

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