login.vue 14 KB

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