login.vue 15 KB

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