login.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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.queryCompanyInfo()
  228. await userStore.queryCardQrcode()
  229. console.log(cardInfo, "cardInfocardInfocardInfo");
  230. uni.showToast({
  231. title: '登录成功',
  232. icon: 'success'
  233. })
  234. // 延迟跳转
  235. uni.reLaunch({
  236. url: '/pages/index/index'
  237. })
  238. } else {
  239. uni.showToast({
  240. title: '保存登录信息失败',
  241. icon: 'none'
  242. })
  243. }
  244. } catch (error) {
  245. console.error('登录失败:', error)
  246. uni.showToast({
  247. title: '登录失败,请重试',
  248. icon: 'none'
  249. })
  250. } finally {
  251. isLoading.value = false
  252. }
  253. }
  254. // 微信登录 - 获取手机号
  255. const getPhoneNumber = async (e) => {
  256. // #ifdef MP-WEIXIN
  257. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  258. uni.showToast({
  259. title: '您取消了授权',
  260. icon: 'none'
  261. })
  262. return
  263. }
  264. try {
  265. // 先获取微信登录 code
  266. const code = await wxLogin()
  267. // 调用 wxgetLogin 接口,传入 code 和手机号信息
  268. console.log(e, "eeeeeeee");
  269. const res = await wxgetLogin({
  270. wxCode: code,
  271. phoneCode: e.detail.code,
  272. iv: e.detail.iv,
  273. type: 1
  274. })
  275. if (res.code != 200) {
  276. uni.showToast({
  277. title: res.msg || '登录失败',
  278. icon: 'none'
  279. })
  280. return
  281. }
  282. console.log('微信登录成功:', res)
  283. // 获取 token
  284. const {
  285. token
  286. } = res.data || res
  287. if (!token) {
  288. uni.showToast({
  289. title: '未获取到 token',
  290. icon: 'none'
  291. })
  292. return
  293. }
  294. if (token) {
  295. saveToekn(token)
  296. await userStore.queryCardInfo()
  297. await userStore.queryCompanyInfo()
  298. uni.showToast({
  299. title: '登录成功',
  300. icon: 'success'
  301. })
  302. await userStore.queryCardPoster()
  303. // await userStore.queryCardQrcode()
  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: 132rpx;
  385. height: 132rpx;
  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. margin-top: 42rpx;
  392. font-size: 48rpx;
  393. font-weight: bold;
  394. text-align: LEFT;
  395. color: #101828;
  396. }
  397. }
  398. // 表单区域
  399. .form-section {
  400. .input-item {
  401. margin-bottom: 24rpx;
  402. .phone-input,
  403. .code-input {
  404. background-color: #f5f7fa;
  405. border-radius: 16rpx;
  406. font-size: 28rpx;
  407. }
  408. }
  409. .verify-code-row {
  410. display: flex;
  411. align-items: center;
  412. gap: 6rpx;
  413. .code-input {
  414. flex: 1;
  415. margin-right: 20rpx;
  416. }
  417. .get-code-btn {
  418. font-size: 28rpx;
  419. color: #155DFC;
  420. white-space: nowrap;
  421. &.disabled {
  422. color: #999999;
  423. }
  424. }
  425. }
  426. .login-btn-wrapper {
  427. margin-top: 48rpx;
  428. margin-bottom: 24rpx;
  429. .login-btn {
  430. width: 100%;
  431. height: 88rpx;
  432. background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
  433. border-radius: 16rpx;
  434. border: none;
  435. font-size: 32rpx;
  436. font-weight: 500;
  437. color: #ffffff;
  438. display: flex;
  439. align-items: center;
  440. justify-content: center;
  441. &::after {
  442. border: none;
  443. }
  444. }
  445. }
  446. .agreement-text {
  447. display: flex;
  448. flex-wrap: wrap;
  449. justify-content: center;
  450. font-size: 22rpx;
  451. .gray-text {
  452. color: #999999;
  453. margin: 0 4rpx;
  454. }
  455. .link-text {
  456. color: #1890ff;
  457. margin: 0 4rpx;
  458. }
  459. }
  460. }
  461. // 其他登录方式
  462. .other-method-section {
  463. margin-top: 120rpx;
  464. .divider {
  465. display: flex;
  466. align-items: center;
  467. justify-content: center;
  468. margin-bottom: 40rpx;
  469. .divider-line {
  470. width: 120rpx;
  471. height: 1rpx;
  472. background-color: #e5e5e5;
  473. }
  474. .divider-text {
  475. font-size: 24rpx;
  476. color: #999999;
  477. margin: 0 24rpx;
  478. }
  479. }
  480. }
  481. .wechat-login-wrapper {
  482. .wechat-login-btn {
  483. width: 100%;
  484. height: 88rpx;
  485. background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
  486. border-radius: 16rpx;
  487. border: none;
  488. font-size: 32rpx;
  489. font-weight: 500;
  490. color: #ffffff;
  491. display: flex;
  492. align-items: center;
  493. justify-content: center;
  494. &::after {
  495. border: none;
  496. }
  497. .wechat-icon-img {
  498. width: 40rpx;
  499. height: 40rpx;
  500. margin-right: 12rpx;
  501. }
  502. .wechat-icon {
  503. margin-right: 12rpx;
  504. font-size: 36rpx;
  505. }
  506. .wechat-text {
  507. color: #ffffff;
  508. }
  509. }
  510. }
  511. // 协议弹窗
  512. .agreement-popup {
  513. position: fixed;
  514. top: 0;
  515. left: 0;
  516. right: 0;
  517. bottom: 0;
  518. z-index: 1000;
  519. .popup-mask {
  520. position: absolute;
  521. top: 0;
  522. left: 0;
  523. right: 0;
  524. bottom: 0;
  525. background-color: rgba(0, 0, 0, 0.5);
  526. }
  527. .popup-content {
  528. position: absolute;
  529. bottom: 0;
  530. left: 0;
  531. right: 0;
  532. background-color: #ffffff;
  533. border-radius: 32rpx 32rpx 0 0;
  534. max-height: 80vh;
  535. display: flex;
  536. flex-direction: column;
  537. animation: slideUp 0.3s ease-out;
  538. .popup-header {
  539. display: flex;
  540. align-items: center;
  541. justify-content: space-between;
  542. padding: 32rpx;
  543. border-bottom: 1rpx solid #f0f0f0;
  544. .popup-title {
  545. font-size: 32rpx;
  546. font-weight: 600;
  547. color: #000000;
  548. }
  549. .popup-close {
  550. font-size: 40rpx;
  551. color: #999999;
  552. width: 60rpx;
  553. height: 60rpx;
  554. display: flex;
  555. align-items: center;
  556. justify-content: center;
  557. }
  558. }
  559. .popup-body {
  560. flex: 1;
  561. overflow-y: auto;
  562. padding: 32rpx;
  563. .agreement-content {
  564. display: flex;
  565. flex-direction: column;
  566. .agreement-title {
  567. font-size: 36rpx;
  568. font-weight: 600;
  569. color: #000000;
  570. display: block;
  571. text-align: center;
  572. margin-bottom: 20rpx;
  573. }
  574. .update-time {
  575. font-size: 24rpx;
  576. color: #999999;
  577. display: block;
  578. text-align: center;
  579. margin-bottom: 40rpx;
  580. }
  581. .section {
  582. margin-bottom: 32rpx;
  583. .section-title {
  584. font-size: 28rpx;
  585. font-weight: 600;
  586. color: #000000;
  587. display: block;
  588. margin-bottom: 16rpx;
  589. }
  590. .section-content {
  591. font-size: 24rpx;
  592. color: #333333;
  593. line-height: 1.8;
  594. display: block;
  595. white-space: pre-wrap;
  596. }
  597. }
  598. }
  599. }
  600. .popup-footer {
  601. padding: 24rpx 32rpx 40rpx;
  602. border-top: 1rpx solid #f0f0f0;
  603. .agree-btn {
  604. width: 100%;
  605. height: 80rpx;
  606. background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
  607. border-radius: 16rpx;
  608. border: none;
  609. font-size: 30rpx;
  610. font-weight: 500;
  611. color: #ffffff;
  612. display: flex;
  613. align-items: center;
  614. justify-content: center;
  615. &::after {
  616. border: none;
  617. }
  618. }
  619. }
  620. }
  621. }
  622. @keyframes slideUp {
  623. from {
  624. transform: translateY(100%);
  625. }
  626. to {
  627. transform: translateY(0);
  628. }
  629. }
  630. .input-container {
  631. position: relative;
  632. width: 670rpx;
  633. // margin-bottom: 40rpx;
  634. .error-message {
  635. color: #ff4444;
  636. font-size: 24rpx;
  637. margin-left: 10rpx;
  638. position: relative;
  639. height: 100%;
  640. top: -10rpx;
  641. left: 0;
  642. }
  643. .password-eye {
  644. position: absolute;
  645. right: 0rpx;
  646. top: 16rpx;
  647. width: 100rpx;
  648. height: 65rpx;
  649. z-index: 2;
  650. cursor: pointer;
  651. }
  652. }
  653. .account {
  654. width: 640rpx;
  655. height: 92rpx;
  656. background: #fff;
  657. border-radius: 20rpx;
  658. padding-left: 30rpx;
  659. font-size: 28rpx;
  660. background-color: #F5F5F5;
  661. border: 2rpx solid transparent;
  662. transition: border-color 0.3s;
  663. &.error-border {
  664. border-color: #ff4444;
  665. // background-color: #fff5f5;
  666. }
  667. }
  668. // 隐藏的 canvas 容器
  669. .hidden-canvas-box {
  670. position: fixed;
  671. left: -9999px;
  672. top: -9999px;
  673. width: 1px;
  674. height: 1px;
  675. overflow: hidden;
  676. }
  677. </style>