index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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: 40rpx 0;
  460. }
  461. .back-icon {
  462. font-size: 50rpx;
  463. color: #333;
  464. width: 60rpx;
  465. }
  466. .page-title {
  467. font-size: 36rpx;
  468. font-weight: bold;
  469. color: #333;
  470. }
  471. .placeholder {
  472. width: 60rpx;
  473. }
  474. .logo-section {
  475. display: flex;
  476. flex-direction: column;
  477. align-items: center;
  478. margin-bottom: 60rpx;
  479. }
  480. .logo {
  481. width: 150rpx;
  482. height: 150rpx;
  483. margin-bottom: 20rpx;
  484. }
  485. .app-name {
  486. font-size: 48rpx;
  487. font-weight: bold;
  488. color: #4CAF50;
  489. margin-bottom: 10rpx;
  490. }
  491. .app-slogan {
  492. font-size: 28rpx;
  493. color: #666;
  494. }
  495. .login-section {
  496. display: flex;
  497. flex-direction: column;
  498. align-items: center;
  499. padding: 20rpx 0;
  500. position: relative;
  501. width: 100%;
  502. }
  503. /* H5和APP表单样式 */
  504. .form-container {
  505. width: 100%;
  506. margin-bottom: 30rpx;
  507. }
  508. .input-group {
  509. margin-bottom: 30rpx;
  510. width: 100%;
  511. }
  512. .verification-group {
  513. display: flex;
  514. align-items: center;
  515. gap: 20rpx;
  516. }
  517. .code-input {
  518. flex: 1;
  519. }
  520. .send-code-btn {
  521. width: 200rpx;
  522. height: 90rpx;
  523. background-color: #4CAF50;
  524. color: #fff;
  525. border-radius: 45rpx;
  526. font-size: 24rpx;
  527. display: flex;
  528. align-items: center;
  529. justify-content: center;
  530. border: none;
  531. padding: 0;
  532. line-height: 90rpx;
  533. }
  534. .send-code-btn[disabled] {
  535. background-color: #ccc;
  536. color: #999;
  537. }
  538. .send-code-btn::after {
  539. border: none;
  540. }
  541. .input-field {
  542. width: 100%;
  543. height: 90rpx;
  544. border-radius: 45rpx;
  545. padding: 0 30rpx;
  546. box-sizing: border-box;
  547. background-color: #f5f5f5;
  548. font-size: 28rpx;
  549. }
  550. .form-actions {
  551. display: flex;
  552. justify-content: flex-end;
  553. margin-bottom: 30rpx;
  554. }
  555. .forgot-password {
  556. font-size: 24rpx;
  557. color: #666;
  558. }
  559. .login-btn {
  560. width: 100%;
  561. height: 90rpx;
  562. background-color: #4CAF50;
  563. color: #fff;
  564. border-radius: 45rpx;
  565. font-size: 32rpx;
  566. font-weight: bold;
  567. display: flex;
  568. align-items: center;
  569. justify-content: center;
  570. border: none;
  571. margin-bottom: 30rpx;
  572. }
  573. .bottom-actions {
  574. display: flex;
  575. justify-content: center;
  576. align-items: center;
  577. margin-top: 20rpx;
  578. }
  579. .action-link {
  580. font-size: 26rpx;
  581. color: #666;
  582. margin: 0 20rpx;
  583. }
  584. .register-link {
  585. font-size: 24rpx;
  586. color: #666;
  587. text-align: center;
  588. margin-top: 20rpx;
  589. }
  590. .login-bg {
  591. width: 100%;
  592. max-width: 600rpx;
  593. margin: 20rpx 0 60rpx;
  594. }
  595. .wechat-login-btn {
  596. width: 80%;
  597. height: 90rpx;
  598. background-color: #07C160;
  599. color: #fff;
  600. border-radius: 45rpx;
  601. font-size: 32rpx;
  602. font-weight: bold;
  603. display: flex;
  604. align-items: center;
  605. justify-content: center;
  606. border: none;
  607. margin-bottom: 40rpx;
  608. padding: 0;
  609. margin-top: 20rpx;
  610. }
  611. .wechat-login-btn::after {
  612. border: none;
  613. }
  614. .wechat-icon {
  615. width: 40rpx;
  616. height: 40rpx;
  617. background-color: rgba(255, 255, 255, 0.2);
  618. color: #fff;
  619. border-radius: 20rpx;
  620. font-size: 20rpx;
  621. display: flex;
  622. align-items: center;
  623. justify-content: center;
  624. margin-right: 10rpx;
  625. }
  626. .privacy-agreement {
  627. display: flex;
  628. align-items: center;
  629. justify-content: center;
  630. /* margin-top: auto; */
  631. padding: 40rpx 0;
  632. }
  633. .agreement-text {
  634. font-size: 24rpx;
  635. color: #666;
  636. margin-left: 10rpx;
  637. }
  638. .link {
  639. color: #4CAF50;
  640. }
  641. /* #ifdef H5 */
  642. /* H5特殊样式调整 */
  643. @media screen and (min-width: 768px) {
  644. .form-container {
  645. max-width: 500rpx;
  646. margin: 0 auto;
  647. }
  648. }
  649. /* #endif */
  650. /* #ifdef APP-PLUS || MP-HARMONY */
  651. /* APP和鸿蒙特殊样式调整 */
  652. .login-container {
  653. padding-top: 20rpx;
  654. }
  655. .input-field {
  656. border: 1rpx solid #E0E0E0;
  657. }
  658. .login-btn {
  659. margin-top: 20rpx;
  660. }
  661. /* #endif */
  662. </style>