index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <template>
  2. <view class="login-container">
  3. <!-- 暂不登录-->
  4. <view class="page-header">
  5. <text class="back-icon" @click="goBack">←</text>
  6. <text class="page-title">登录</text>
  7. <text class="placeholder"></text>
  8. </view>
  9. <view class="logo-section">
  10. <image class="logo" src="/static/images/logo.png" mode="aspectFit"></image>
  11. <text class="app-name">农小禹</text>
  12. <text class="app-slogan">智慧农业,从此开始</text>
  13. </view>
  14. <view class="login-section">
  15. <!-- #ifndef MP-WEIXIN -->
  16. <view class="form-container">
  17. <view class="input-group">
  18. <input type="text" v-model="phoneNumber" placeholder="请输入手机号" class="input-field" maxlength="11" />
  19. </view>
  20. <!-- 密码登录 -->
  21. <view v-if="loginType === 'password'" class="input-group">
  22. <input type="password" v-model="password" placeholder="请输入密码" class="input-field" />
  23. </view>
  24. <!-- 验证码登录 -->
  25. <view v-if="loginType === 'code'" class="input-group verification-group">
  26. <input type="text" v-model="verificationCode" placeholder="请输入验证码" class="input-field code-input" maxlength="6" />
  27. <button
  28. class="send-code-btn"
  29. :disabled="countdown > 0"
  30. @click="sendVerificationCode"
  31. >
  32. {{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
  33. </button>
  34. </view>
  35. <!-- <view class="form-actions">
  36. <text v-if="loginType === 'password'" class="forgot-password" @click="navigateToForgetPassword">忘记密码?</text>
  37. </view> -->
  38. <button class="login-btn" @click="handleLogin">登录</button>
  39. <!-- 底部操作区 -->
  40. <view class="bottom-actions">
  41. <text class="action-link" @click="switchLoginType(loginType === 'password' ? 'code' : 'password')">
  42. {{ loginType === 'password' ? '验证码登录' : '密码登录' }}
  43. </text>
  44. <text class="action-link" @click="navigateToRegister">注册账号</text>
  45. <text v-if="loginType === 'password'" class="action-link" @click="navigateToForgetPassword">忘记密码</text>
  46. </view>
  47. </view>
  48. <!-- #endif -->
  49. <!-- #ifdef MP-WEIXIN -->
  50. <button type="primary" class="wechat-login-btn" bindtap="getUserProfile" @click="getUserProfile()">
  51. <view class="wechat-icon">微</view>
  52. <text>一键登录</text>
  53. </button>
  54. <!-- #endif -->
  55. </view>
  56. <view class="privacy-agreement">
  57. <checkbox :checked="agreed" @click="toggleAgreement"></checkbox>
  58. <text class="agreement-text">
  59. 登录即表示您已同意
  60. <text class="link" @click="navigateToTerms">用户协议</text>
  61. <text class="link" @click="navigateToPrivacy">隐私政策</text>
  62. </text>
  63. </view>
  64. </view>
  65. </template>
  66. <script setup>
  67. import { ref, onMounted } from 'vue'
  68. import {
  69. mpAutoLogin, phoneLogin, phoneCodeLogin, sendVcode, sendPhoneVcodeNoUnique
  70. } from "@/api/services/connect.js";
  71. import { fetchUserFieldList } from "@/api/services/field.js";
  72. import storage from "@/utils/storage.js";
  73. import Foundation from "@/utils/Foundation.js";
  74. // 获取并设置默认地块信息
  75. const fetchAndSetDefaultField = async () => {
  76. try {
  77. const res = await fetchUserFieldList(1, 10);
  78. console.log("获取地块列表", res);
  79. if (res.data && res.data.code === 200 && res.data.data && res.data.data.list && res.data.data.list.length > 0) {
  80. const firstField = res.data.data.list[0];
  81. // 保存默认地块到本地存储
  82. storage.setPlots(JSON.stringify({
  83. id: firstField.id,
  84. code: firstField.code,
  85. name: firstField.name,
  86. growCrops: firstField.crop,
  87. managerName: firstField.manager,
  88. size: firstField.area,
  89. farmId: firstField.farmId,
  90. timestamp: Date.now()
  91. }));
  92. console.log("已设置默认地块:", firstField.name);
  93. return true;
  94. } else {
  95. console.log("暂无可用地块");
  96. return false;
  97. }
  98. } catch (err) {
  99. console.error("获取地块列表失败", err);
  100. return false;
  101. }
  102. };
  103. // Reactive data
  104. const agreed = ref(false)
  105. const code = ref("")
  106. const nickName = ref("")
  107. const avatarUrl = ref("")
  108. const gender = ref("")
  109. const phoneNumber = ref("")
  110. const password = ref("")
  111. const verificationCode = ref("")
  112. const loginType = ref("password") // 'password' 或 'code'
  113. const countdown = ref(0)
  114. const loading = ref(false)
  115. const logingFlag = ref(false)
  116. const isLogin = ref(0)
  117. let timer = null
  118. // Lifecycle hooks - uni-app specific lifecycle
  119. onMounted(() => {
  120. // // 检查是否已登录
  121. // if (isLoggedIn()) {
  122. // redirectToHome();
  123. // }
  124. // #ifdef MP-WEIXIN
  125. //获取code
  126. uni.login({
  127. success: (res) => {
  128. if (res.errMsg === "login:ok") {
  129. code.value = res.code
  130. } else {
  131. uni.showToast({
  132. title: "系统异常,请联系管理员!"
  133. })
  134. }
  135. },
  136. });
  137. // #endif
  138. })
  139. // Methods
  140. const goBack = () => {
  141. uni.navigateBack();
  142. }
  143. const toggleAgreement = () => {
  144. agreed.value = !agreed.value;
  145. console.log("agreed", agreed.value);
  146. }
  147. const navigateToTerms = () => {
  148. uni.navigateTo({
  149. url: '/pages/login/terms'
  150. });
  151. }
  152. const navigateToPrivacy = () => {
  153. uni.navigateTo({
  154. url: '/pages/login/privacy'
  155. })
  156. }
  157. const navigateToForgetPassword = () => {
  158. uni.navigateTo({
  159. url: '/pages/login/forget-password'
  160. });
  161. }
  162. const navigateToRegister = () => {
  163. uni.navigateTo({
  164. url: '/pages/login/register'
  165. });
  166. }
  167. // 切换登录方式
  168. const switchLoginType = (type) => {
  169. loginType.value = type;
  170. // 清空输入
  171. password.value = "";
  172. verificationCode.value = "";
  173. }
  174. // 发送验证码
  175. const sendVerificationCode = () => {
  176. if (!phoneNumber.value) {
  177. uni.showToast({
  178. title: "请输入手机号",
  179. icon: 'none'
  180. });
  181. return;
  182. }
  183. // 手机号验证
  184. const result = Foundation.validatePhoneNumber(phoneNumber.value);
  185. if (!result.valid) {
  186. uni.showToast({
  187. title: result.message,
  188. icon: 'none'
  189. });
  190. return;
  191. }
  192. // 发送验证码
  193. uni.showLoading({ title: '发送中...' });
  194. sendPhoneVcodeNoUnique({ terminal: phoneNumber.value,scene:'LOGIN' })
  195. .then(res => {
  196. console.log("发送验证码", res);
  197. if (res.data.code === 200) {
  198. uni.showToast({
  199. title: "验证码已发送",
  200. icon: 'success'
  201. });
  202. // 开始倒计时
  203. countdown.value = 60;
  204. timer = setInterval(() => {
  205. countdown.value--;
  206. if (countdown.value <= 0) {
  207. clearInterval(timer);
  208. }
  209. }, 1000);
  210. } else {
  211. uni.showToast({
  212. title: res.data.msg || "发送失败",
  213. icon: 'none'
  214. });
  215. }
  216. })
  217. .catch(err => {
  218. console.error(err);
  219. uni.showToast({
  220. title: "网络异常,请稍后重试",
  221. icon: 'none'
  222. });
  223. })
  224. .finally(() => {
  225. uni.hideLoading();
  226. });
  227. }
  228. // 统一登录处理
  229. const handleLogin = () => {
  230. if (loginType.value === 'password') {
  231. handlePhoneLogin();
  232. } else {
  233. handleCodeLogin();
  234. }
  235. }
  236. // 验证码登录
  237. const handleCodeLogin = () => {
  238. if (!agreed.value) {
  239. uni.showToast({
  240. title: "请同意用户协议和隐私政策",
  241. icon: 'none'
  242. });
  243. return;
  244. }
  245. if (!phoneNumber.value || !verificationCode.value) {
  246. uni.showToast({
  247. title: "请输入手机号和验证码",
  248. icon: 'none'
  249. });
  250. return;
  251. }
  252. // 手机号验证
  253. const result = Foundation.validatePhoneNumber(phoneNumber.value);
  254. if (!result.valid) {
  255. uni.showToast({
  256. title: result.message,
  257. icon: 'none'
  258. });
  259. return;
  260. }
  261. loading.value = true;
  262. uni.showLoading({ title: '登录中...' });
  263. const data = {
  264. phonenumber: phoneNumber.value,
  265. vcode: verificationCode.value
  266. };
  267. phoneCodeLogin(data).then(async res => {
  268. console.log("验证码登录", res);
  269. if (res.data.code === 200) {
  270. storage.setAccessToken(res.data.data.access_token);
  271. storage.setUserInfo(res.data.data.userInfo);
  272. storage.setHasLogin(true);
  273. await fetchAndSetDefaultField();
  274. uni.showToast({
  275. title: "登录成功!",
  276. icon: "success",
  277. });
  278. setTimeout(() => {
  279. uni.navigateBack({
  280. delta: 1,
  281. });
  282. }, 1500);
  283. } else {
  284. uni.showToast({
  285. title: res.data.msg || "登录失败,请检查验证码",
  286. icon: 'none'
  287. });
  288. }
  289. })
  290. .catch(err => {
  291. uni.showToast({
  292. title: "网络异常,请稍后重试",
  293. icon: 'none'
  294. });
  295. console.error(err);
  296. })
  297. .finally(() => {
  298. uni.hideLoading();
  299. loading.value = false;
  300. });
  301. }
  302. // H5平台手机号密码登录方法
  303. const handlePhoneLogin = () => {
  304. if (!agreed.value) {
  305. uni.showToast({
  306. title: "请同意用户协议和隐私政策",
  307. icon: 'none'
  308. })
  309. return
  310. }
  311. if (!phoneNumber.value || !password.value) {
  312. uni.showToast({
  313. title: "请输入手机号和密码",
  314. icon: 'none'
  315. })
  316. return
  317. }
  318. // 手机号验证
  319. const phone = phoneNumber.value; // 获取用户输入的手机号
  320. const result = Foundation.validatePhoneNumber(phone);
  321. if (!result.valid) {
  322. uni.showToast({
  323. title: result.message,
  324. icon: 'none'
  325. });
  326. return
  327. }
  328. loading.value = true;
  329. uni.showLoading({ title: '登录中...' })
  330. const data = {
  331. username: phoneNumber.value,
  332. password: password.value,
  333. phoneNumber: phoneNumber.value,
  334. }
  335. phoneLogin(data).then(async res => {
  336. console.log("res登录", res);
  337. if (res.data.code === 200) {
  338. // 登录成功
  339. storage.setAccessToken(res.data.data.access_token);
  340. storage.setUserInfo(res.data.data.userInfo);
  341. storage.setHasLogin(true);
  342. // 获取并设置默认地块信息
  343. await fetchAndSetDefaultField();
  344. uni.showToast({
  345. title: "登录成功!",
  346. icon: "success",
  347. });
  348. // 登录成功后返回或跳转到首页
  349. setTimeout(() => {
  350. uni.navigateBack({
  351. delta: 1,
  352. });
  353. }, 1500);
  354. } else {
  355. // 登录失败
  356. uni.showToast({
  357. title: res.data.msg || "登录失败,请检查账号密码",
  358. icon: 'none'
  359. });
  360. }
  361. })
  362. .catch(err => {
  363. uni.showToast({
  364. title: "网络异常,请稍后重试",
  365. icon: 'none'
  366. });
  367. console.error(err);
  368. })
  369. .finally(() => {
  370. uni.hideLoading();
  371. loading.value = false;
  372. });
  373. }
  374. // 微信小程序登录方法
  375. const getUserProfile = () => {
  376. if (!agreed.value) {
  377. uni.showToast({
  378. title: "请同意用户协议和隐私政策",
  379. icon: 'none'
  380. })
  381. return
  382. }
  383. logingFlag.value = true;
  384. // #ifdef MP-WEIXIN
  385. if (code.value) {
  386. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  387. uni.getUserProfile({
  388. desc: "获取你的昵称、头像、地区及性别", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  389. success: (res) => {
  390. console.log("success", res)
  391. nickName.value = res.userInfo.nickName;
  392. avatarUrl.value = res.userInfo.avatarUrl;
  393. gender.value = res.userInfo.gender;
  394. let iv = res.iv;
  395. let encryptedData = res.encryptedData;
  396. let codeVal = code.value;
  397. let avatarUrlVal = avatarUrl.value;
  398. let nickNameVal = nickName.value;
  399. let genderVal = gender.value;
  400. isLogin.value = 2
  401. mpAutoLogin({
  402. encryptedData,
  403. iv,
  404. code: codeVal,
  405. avatarUrl: avatarUrlVal,
  406. nickName: nickNameVal,
  407. gender: genderVal,
  408. }).then(async (apiRes) => {
  409. console.log("apiRes", apiRes);
  410. storage.setAccessToken(apiRes.data.data.token);
  411. // storage.setRefreshToken(apiRes.data.result.refreshToken);
  412. uni.showToast({
  413. title: "登录成功!",
  414. icon: "none",
  415. });
  416. //存储用户信息
  417. storage.setUserInfo(apiRes.data.data.userInfo);
  418. storage.setHasLogin(true);
  419. // 获取并设置默认地块信息
  420. await fetchAndSetDefaultField();
  421. // 用户手动授权头像昵称
  422. if (apiRes.data.data.isNewUser) {
  423. // 新用户跳转上传头像昵称界面
  424. uni.navigateTo({
  425. url: `/pages/userInfo/index?openId=${apiRes.data.data.userInfo.openId}`
  426. });
  427. } else {
  428. // 老用户直接跳转首页
  429. // wx.switchTab({
  430. // url: '/pages/user/index'
  431. // });
  432. uni.navigateBack({
  433. delta: 1,
  434. });
  435. }
  436. });
  437. },
  438. fail: (res) => {
  439. console.log("fail", res)
  440. },
  441. });
  442. logingFlag.value = false;
  443. }
  444. // #endif
  445. }
  446. </script>
  447. <style>
  448. .login-container {
  449. min-height: 100vh;
  450. padding: 0 40rpx;
  451. background-color: #fff;
  452. display: flex;
  453. flex-direction: column;
  454. }
  455. .page-header {
  456. display: flex;
  457. justify-content: space-between;
  458. align-items: center;
  459. padding-top: calc(15rpx + constant(safe-area-inset-top));
  460. padding-top: calc(15rpx + env(safe-area-inset-top));
  461. /* padding: 40rpx 0; */
  462. }
  463. .back-icon {
  464. font-size: 50rpx;
  465. color: #333;
  466. width: 60rpx;
  467. }
  468. .page-title {
  469. font-size: 36rpx;
  470. font-weight: bold;
  471. color: #333;
  472. }
  473. .placeholder {
  474. width: 60rpx;
  475. }
  476. .logo-section {
  477. display: flex;
  478. flex-direction: column;
  479. align-items: center;
  480. margin-bottom: 60rpx;
  481. }
  482. .logo {
  483. width: 150rpx;
  484. height: 150rpx;
  485. margin-bottom: 20rpx;
  486. }
  487. .app-name {
  488. font-size: 48rpx;
  489. font-weight: bold;
  490. color: #4CAF50;
  491. margin-bottom: 10rpx;
  492. }
  493. .app-slogan {
  494. font-size: 28rpx;
  495. color: #666;
  496. }
  497. .login-section {
  498. display: flex;
  499. flex-direction: column;
  500. align-items: center;
  501. padding: 20rpx 0;
  502. position: relative;
  503. width: 100%;
  504. }
  505. /* H5和APP表单样式 */
  506. .form-container {
  507. width: 100%;
  508. margin-bottom: 30rpx;
  509. }
  510. .input-group {
  511. margin-bottom: 30rpx;
  512. width: 100%;
  513. }
  514. .verification-group {
  515. display: flex;
  516. align-items: center;
  517. gap: 20rpx;
  518. }
  519. .code-input {
  520. flex: 1;
  521. }
  522. .send-code-btn {
  523. width: 200rpx;
  524. height: 90rpx;
  525. background-color: #4CAF50;
  526. color: #fff;
  527. border-radius: 45rpx;
  528. font-size: 24rpx;
  529. display: flex;
  530. align-items: center;
  531. justify-content: center;
  532. border: none;
  533. padding: 0;
  534. line-height: 90rpx;
  535. }
  536. .send-code-btn[disabled] {
  537. background-color: #ccc;
  538. color: #999;
  539. }
  540. .send-code-btn::after {
  541. border: none;
  542. }
  543. .input-field {
  544. width: 100%;
  545. height: 90rpx;
  546. border-radius: 45rpx;
  547. padding: 0 30rpx;
  548. box-sizing: border-box;
  549. background-color: #f5f5f5;
  550. font-size: 28rpx;
  551. }
  552. .form-actions {
  553. display: flex;
  554. justify-content: flex-end;
  555. margin-bottom: 30rpx;
  556. }
  557. .forgot-password {
  558. font-size: 24rpx;
  559. color: #666;
  560. }
  561. .login-btn {
  562. width: 100%;
  563. height: 90rpx;
  564. background-color: #4CAF50;
  565. color: #fff;
  566. border-radius: 45rpx;
  567. font-size: 32rpx;
  568. font-weight: bold;
  569. display: flex;
  570. align-items: center;
  571. justify-content: center;
  572. border: none;
  573. margin-bottom: 30rpx;
  574. }
  575. .bottom-actions {
  576. display: flex;
  577. justify-content: center;
  578. align-items: center;
  579. margin-top: 20rpx;
  580. }
  581. .action-link {
  582. font-size: 26rpx;
  583. color: #666;
  584. margin: 0 20rpx;
  585. }
  586. .register-link {
  587. font-size: 24rpx;
  588. color: #666;
  589. text-align: center;
  590. margin-top: 20rpx;
  591. }
  592. .login-bg {
  593. width: 100%;
  594. max-width: 600rpx;
  595. margin: 20rpx 0 60rpx;
  596. }
  597. .wechat-login-btn {
  598. width: 80%;
  599. height: 90rpx;
  600. background-color: #07C160;
  601. color: #fff;
  602. border-radius: 45rpx;
  603. font-size: 32rpx;
  604. font-weight: bold;
  605. display: flex;
  606. align-items: center;
  607. justify-content: center;
  608. border: none;
  609. margin-bottom: 40rpx;
  610. padding: 0;
  611. margin-top: 20rpx;
  612. }
  613. .wechat-login-btn::after {
  614. border: none;
  615. }
  616. .wechat-icon {
  617. width: 40rpx;
  618. height: 40rpx;
  619. background-color: rgba(255, 255, 255, 0.2);
  620. color: #fff;
  621. border-radius: 20rpx;
  622. font-size: 20rpx;
  623. display: flex;
  624. align-items: center;
  625. justify-content: center;
  626. margin-right: 10rpx;
  627. }
  628. .privacy-agreement {
  629. display: flex;
  630. align-items: center;
  631. justify-content: center;
  632. /* margin-top: auto; */
  633. padding: 40rpx 0;
  634. }
  635. .agreement-text {
  636. font-size: 24rpx;
  637. color: #666;
  638. margin-left: 10rpx;
  639. }
  640. .link {
  641. color: #4CAF50;
  642. }
  643. /* #ifdef H5 */
  644. /* H5特殊样式调整 */
  645. @media screen and (min-width: 768px) {
  646. .form-container {
  647. max-width: 500rpx;
  648. margin: 0 auto;
  649. }
  650. }
  651. /* #endif */
  652. /* #ifdef APP-PLUS || MP-HARMONY */
  653. /* APP和鸿蒙特殊样式调整 */
  654. .login-container {
  655. padding-top: 20rpx;
  656. }
  657. .input-field {
  658. border: 1rpx solid #E0E0E0;
  659. }
  660. .login-btn {
  661. margin-top: 20rpx;
  662. }
  663. /* #endif */
  664. </style>