login.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. 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. uni.reLaunch({
  303. url: '/pages/index/index'
  304. })
  305. } else {
  306. uni.showToast({
  307. title: '保存登录信息失败',
  308. icon: 'none'
  309. })
  310. }
  311. } catch (error) {
  312. console.error('微信登录失败:', error)
  313. uni.showToast({
  314. title: '登录失败,请重试',
  315. icon: 'none'
  316. })
  317. }
  318. // #endif
  319. }
  320. // 微信登录(非小程序环境备用)
  321. const wechatLogin = async () => {
  322. // #ifndef MP-WEIXIN
  323. uni.showToast({
  324. title: '请在微信小程序中使用',
  325. icon: 'none'
  326. })
  327. // #endif
  328. }
  329. // 显示用户协议
  330. const showUserAgreement = () => {
  331. showUserAgreementPopup.value = true
  332. }
  333. // 关闭用户协议
  334. const closeUserAgreement = () => {
  335. showUserAgreementPopup.value = false
  336. }
  337. // 显示隐私政策
  338. const showPrivacyPolicy = () => {
  339. showPrivacyPolicyPopup.value = true
  340. }
  341. // 关闭隐私政策
  342. const closePrivacyPolicy = () => {
  343. showPrivacyPolicyPopup.value = false
  344. }
  345. // 页面卸载时清除定时器
  346. import {
  347. onUnmounted
  348. } from 'vue'
  349. onUnmounted(() => {
  350. if (timer) {
  351. clearInterval(timer)
  352. }
  353. })
  354. </script>
  355. <style lang="scss" scoped>
  356. .login-container {
  357. min-height: 100vh;
  358. background-color: #ffffff;
  359. padding: 0 40rpx;
  360. position: relative;
  361. }
  362. // 导航栏
  363. .nav-bar {
  364. height: 88rpx;
  365. display: flex;
  366. align-items: center;
  367. justify-content: center;
  368. .nav-title {
  369. font-size: 34rpx;
  370. font-weight: 600;
  371. color: #000000;
  372. }
  373. }
  374. // Logo 区域
  375. .logo-section {
  376. display: flex;
  377. flex-direction: column;
  378. align-items: center;
  379. margin-top: 60rpx;
  380. margin-bottom: 80rpx;
  381. .logo-icon {
  382. width: 160rpx;
  383. height: 160rpx;
  384. background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
  385. border-radius: 32rpx;
  386. box-shadow: 0 8rpx 24rpx rgba(24, 144, 255, 0.3);
  387. }
  388. .app-name {
  389. font-size: 40rpx;
  390. font-weight: 600;
  391. color: #000000;
  392. margin-top: 32rpx;
  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. .code-input {
  410. flex: 1;
  411. margin-right: 20rpx;
  412. }
  413. .get-code-btn {
  414. font-size: 26rpx;
  415. color: #1890ff;
  416. white-space: nowrap;
  417. &.disabled {
  418. color: #999999;
  419. }
  420. }
  421. }
  422. .login-btn-wrapper {
  423. margin-top: 48rpx;
  424. margin-bottom: 24rpx;
  425. .login-btn {
  426. width: 100%;
  427. height: 88rpx;
  428. background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
  429. border-radius: 16rpx;
  430. border: none;
  431. font-size: 32rpx;
  432. font-weight: 500;
  433. color: #ffffff;
  434. display: flex;
  435. align-items: center;
  436. justify-content: center;
  437. &::after {
  438. border: none;
  439. }
  440. }
  441. }
  442. .agreement-text {
  443. display: flex;
  444. flex-wrap: wrap;
  445. justify-content: center;
  446. font-size: 22rpx;
  447. .gray-text {
  448. color: #999999;
  449. margin: 0 4rpx;
  450. }
  451. .link-text {
  452. color: #1890ff;
  453. margin: 0 4rpx;
  454. }
  455. }
  456. }
  457. // 其他登录方式
  458. .other-method-section {
  459. margin-top: 120rpx;
  460. .divider {
  461. display: flex;
  462. align-items: center;
  463. justify-content: center;
  464. margin-bottom: 40rpx;
  465. .divider-line {
  466. width: 120rpx;
  467. height: 1rpx;
  468. background-color: #e5e5e5;
  469. }
  470. .divider-text {
  471. font-size: 24rpx;
  472. color: #999999;
  473. margin: 0 24rpx;
  474. }
  475. }
  476. .wechat-login-wrapper {
  477. .wechat-login-btn {
  478. width: 100%;
  479. height: 88rpx;
  480. background: linear-gradient(90deg, #07c160 0%, #06ad56 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. // 协议弹窗
  508. .agreement-popup {
  509. position: fixed;
  510. top: 0;
  511. left: 0;
  512. right: 0;
  513. bottom: 0;
  514. z-index: 1000;
  515. .popup-mask {
  516. position: absolute;
  517. top: 0;
  518. left: 0;
  519. right: 0;
  520. bottom: 0;
  521. background-color: rgba(0, 0, 0, 0.5);
  522. }
  523. .popup-content {
  524. position: absolute;
  525. bottom: 0;
  526. left: 0;
  527. right: 0;
  528. background-color: #ffffff;
  529. border-radius: 32rpx 32rpx 0 0;
  530. max-height: 80vh;
  531. display: flex;
  532. flex-direction: column;
  533. animation: slideUp 0.3s ease-out;
  534. .popup-header {
  535. display: flex;
  536. align-items: center;
  537. justify-content: space-between;
  538. padding: 32rpx;
  539. border-bottom: 1rpx solid #f0f0f0;
  540. .popup-title {
  541. font-size: 32rpx;
  542. font-weight: 600;
  543. color: #000000;
  544. }
  545. .popup-close {
  546. font-size: 40rpx;
  547. color: #999999;
  548. width: 60rpx;
  549. height: 60rpx;
  550. display: flex;
  551. align-items: center;
  552. justify-content: center;
  553. }
  554. }
  555. .popup-body {
  556. flex: 1;
  557. overflow-y: auto;
  558. padding: 32rpx;
  559. .agreement-content {
  560. display: flex;
  561. flex-direction: column;
  562. .agreement-title {
  563. font-size: 36rpx;
  564. font-weight: 600;
  565. color: #000000;
  566. display: block;
  567. text-align: center;
  568. margin-bottom: 20rpx;
  569. }
  570. .update-time {
  571. font-size: 24rpx;
  572. color: #999999;
  573. display: block;
  574. text-align: center;
  575. margin-bottom: 40rpx;
  576. }
  577. .section {
  578. margin-bottom: 32rpx;
  579. .section-title {
  580. font-size: 28rpx;
  581. font-weight: 600;
  582. color: #000000;
  583. display: block;
  584. margin-bottom: 16rpx;
  585. }
  586. .section-content {
  587. font-size: 24rpx;
  588. color: #333333;
  589. line-height: 1.8;
  590. display: block;
  591. white-space: pre-wrap;
  592. }
  593. }
  594. }
  595. }
  596. .popup-footer {
  597. padding: 24rpx 32rpx 40rpx;
  598. border-top: 1rpx solid #f0f0f0;
  599. .agree-btn {
  600. width: 100%;
  601. height: 80rpx;
  602. background: linear-gradient(90deg, #1890ff 0%, #096dd9 100%);
  603. border-radius: 16rpx;
  604. border: none;
  605. font-size: 30rpx;
  606. font-weight: 500;
  607. color: #ffffff;
  608. display: flex;
  609. align-items: center;
  610. justify-content: center;
  611. &::after {
  612. border: none;
  613. }
  614. }
  615. }
  616. }
  617. }
  618. @keyframes slideUp {
  619. from {
  620. transform: translateY(100%);
  621. }
  622. to {
  623. transform: translateY(0);
  624. }
  625. }
  626. </style>