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