index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. <view class="input-group">
  21. <input type="password" v-model="password" placeholder="请输入密码" class="input-field" />
  22. </view>
  23. <view class="form-actions">
  24. <text class="forgot-password" @click="navigateToForgetPassword">忘记密码?</text>
  25. </view>
  26. <button type="primary" class="login-btn" @click="handlePhoneLogin">登录</button>
  27. <view class="register-link">
  28. 还没有账号?<text class="link" @click="navigateToRegister">立即注册</text>
  29. </view>
  30. </view>
  31. <!-- #endif -->
  32. <!-- #ifdef MP-WEIXIN -->
  33. <button type="primary" class="wechat-login-btn" bindtap="getUserProfile" @click="getUserProfile()">
  34. <view class="wechat-icon">微</view>
  35. <text>一键登录</text>
  36. </button>
  37. <!-- #endif -->
  38. </view>
  39. <view class="privacy-agreement">
  40. <checkbox :checked="agreed" @click="toggleAgreement"></checkbox>
  41. <text class="agreement-text">
  42. 登录即表示您已同意
  43. <text class="link" @click="navigateToTerms">用户协议</text>
  44. <text class="link" @click="navigateToPrivacy">隐私政策</text>
  45. </text>
  46. </view>
  47. </view>
  48. </template>
  49. <script setup>
  50. import { ref, onMounted } from 'vue'
  51. import {
  52. mpAutoLogin, phoneLogin
  53. } from "@/api/services/connect.js";
  54. import storage from "@/utils/storage.js";
  55. import Foundation from "@/utils/Foundation.js";
  56. // Reactive data
  57. const agreed = ref(false)
  58. const code = ref("")
  59. const nickName = ref("")
  60. const avatarUrl = ref("")
  61. const gender = ref("")
  62. const phoneNumber = ref("")
  63. const password = ref("")
  64. const loading = ref(false)
  65. const logingFlag = ref(false)
  66. const isLogin = ref(0)
  67. // Lifecycle hooks - uni-app specific lifecycle
  68. onMounted(() => {
  69. // // 检查是否已登录
  70. // if (isLoggedIn()) {
  71. // redirectToHome();
  72. // }
  73. // #ifdef MP-WEIXIN
  74. //获取code
  75. uni.login({
  76. success: (res) => {
  77. if (res.errMsg === "login:ok") {
  78. code.value = res.code
  79. } else {
  80. uni.showToast({
  81. title: "系统异常,请联系管理员!"
  82. })
  83. }
  84. },
  85. });
  86. // #endif
  87. })
  88. // Methods
  89. const goBack = () => {
  90. uni.navigateBack();
  91. }
  92. const toggleAgreement = () => {
  93. agreed.value = !agreed.value;
  94. console.log("agreed", agreed.value);
  95. }
  96. const navigateToTerms = () => {
  97. uni.navigateTo({
  98. url: '/pages/privacy/terms'
  99. });
  100. }
  101. const navigateToPrivacy = () => {
  102. uni.navigateTo({
  103. url: '/pages/privacy/index'
  104. });
  105. }
  106. const navigateToForgetPassword = () => {
  107. uni.navigateTo({
  108. url: '/pages/login/forget-password'
  109. });
  110. }
  111. const navigateToRegister = () => {
  112. uni.navigateTo({
  113. url: '/pages/login/register'
  114. });
  115. }
  116. // H5平台手机号密码登录方法
  117. const handlePhoneLogin = () => {
  118. if (!agreed.value) {
  119. uni.showToast({
  120. title: "请同意用户协议和隐私政策",
  121. icon: 'none'
  122. })
  123. return
  124. }
  125. if (!phoneNumber.value || !password.value) {
  126. uni.showToast({
  127. title: "请输入手机号和密码",
  128. icon: 'none'
  129. })
  130. return
  131. }
  132. // 手机号验证
  133. const phone = phoneNumber.value; // 获取用户输入的手机号
  134. const result = Foundation.validatePhoneNumber(phone);
  135. if (!result.valid) {
  136. uni.showToast({
  137. title: result.message,
  138. icon: 'none'
  139. });
  140. return
  141. }
  142. loading.value = true;
  143. uni.showLoading({ title: '登录中...' })
  144. const data = {
  145. username: phoneNumber.value,
  146. password: password.value,
  147. phoneNumber: phoneNumber.value,
  148. }
  149. phoneLogin(data).then(res => {
  150. console.log("res登录", res);
  151. if (res.data.code === 200) {
  152. // 登录成功
  153. storage.setAccessToken(res.data.data.access_token);
  154. storage.setUserInfo(res.data.data.userInfo);
  155. storage.setHasLogin(true);
  156. uni.showToast({
  157. title: "登录成功!",
  158. icon: "success",
  159. });
  160. // 登录成功后返回或跳转到首页
  161. setTimeout(() => {
  162. uni.navigateBack({
  163. delta: 1,
  164. });
  165. }, 1500);
  166. } else {
  167. // 登录失败
  168. uni.showToast({
  169. title: res.data.msg || "登录失败,请检查账号密码",
  170. icon: 'none'
  171. });
  172. }
  173. })
  174. .catch(err => {
  175. uni.showToast({
  176. title: "网络异常,请稍后重试",
  177. icon: 'none'
  178. });
  179. console.error(err);
  180. })
  181. .finally(() => {
  182. uni.hideLoading();
  183. loading.value = false;
  184. });
  185. }
  186. // 微信小程序登录方法
  187. const getUserProfile = () => {
  188. if (!agreed.value) {
  189. uni.showToast({
  190. title: "请同意用户协议和隐私政策",
  191. icon: 'none'
  192. })
  193. return
  194. }
  195. logingFlag.value = true;
  196. // #ifdef MP-WEIXIN
  197. if (code.value) {
  198. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  199. uni.getUserProfile({
  200. desc: "获取你的昵称、头像、地区及性别", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  201. success: (res) => {
  202. console.log("success", res)
  203. nickName.value = res.userInfo.nickName;
  204. avatarUrl.value = res.userInfo.avatarUrl;
  205. gender.value = res.userInfo.gender;
  206. let iv = res.iv;
  207. let encryptedData = res.encryptedData;
  208. let codeVal = code.value;
  209. let avatarUrlVal = avatarUrl.value;
  210. let nickNameVal = nickName.value;
  211. let genderVal = gender.value;
  212. isLogin.value = 2
  213. mpAutoLogin({
  214. encryptedData,
  215. iv,
  216. code: codeVal,
  217. avatarUrl: avatarUrlVal,
  218. nickName: nickNameVal,
  219. gender: genderVal,
  220. }).then((apiRes) => {
  221. console.log("apiRes", apiRes);
  222. storage.setAccessToken(apiRes.data.data.token);
  223. // storage.setRefreshToken(apiRes.data.result.refreshToken);
  224. uni.showToast({
  225. title: "登录成功!",
  226. icon: "none",
  227. });
  228. //存储用户信息
  229. storage.setUserInfo(apiRes.data.data.userInfo);
  230. storage.setHasLogin(true);
  231. // 用户手动授权头像昵称
  232. if (apiRes.data.data.isNewUser) {
  233. // 新用户跳转上传头像昵称界面
  234. uni.navigateTo({
  235. url: `/pages/userInfo/index?openId=${apiRes.data.data.userInfo.openId}`
  236. });
  237. } else {
  238. // 老用户直接跳转首页
  239. // wx.switchTab({
  240. // url: '/pages/user/index'
  241. // });
  242. uni.navigateBack({
  243. delta: 1,
  244. });
  245. }
  246. });
  247. },
  248. fail: (res) => {
  249. console.log("fail", res)
  250. },
  251. });
  252. logingFlag.value = false;
  253. }
  254. // #endif
  255. }
  256. </script>
  257. <style>
  258. .login-container {
  259. min-height: 100vh;
  260. padding: 0 40rpx;
  261. background-color: #fff;
  262. display: flex;
  263. flex-direction: column;
  264. }
  265. .page-header {
  266. display: flex;
  267. justify-content: space-between;
  268. align-items: center;
  269. padding: 40rpx 0;
  270. }
  271. .back-icon {
  272. font-size: 50rpx;
  273. color: #333;
  274. width: 60rpx;
  275. }
  276. .page-title {
  277. font-size: 36rpx;
  278. font-weight: bold;
  279. color: #333;
  280. }
  281. .placeholder {
  282. width: 60rpx;
  283. }
  284. .logo-section {
  285. display: flex;
  286. flex-direction: column;
  287. align-items: center;
  288. margin-bottom: 60rpx;
  289. }
  290. .logo {
  291. width: 150rpx;
  292. height: 150rpx;
  293. margin-bottom: 20rpx;
  294. }
  295. .app-name {
  296. font-size: 48rpx;
  297. font-weight: bold;
  298. color: #4CAF50;
  299. margin-bottom: 10rpx;
  300. }
  301. .app-slogan {
  302. font-size: 28rpx;
  303. color: #666;
  304. }
  305. .login-section {
  306. display: flex;
  307. flex-direction: column;
  308. align-items: center;
  309. padding: 20rpx 0;
  310. position: relative;
  311. width: 100%;
  312. }
  313. /* H5和APP表单样式 */
  314. .form-container {
  315. width: 100%;
  316. margin-bottom: 30rpx;
  317. }
  318. .input-group {
  319. margin-bottom: 30rpx;
  320. width: 100%;
  321. }
  322. .input-field {
  323. width: 100%;
  324. height: 90rpx;
  325. border-radius: 45rpx;
  326. padding: 0 30rpx;
  327. box-sizing: border-box;
  328. background-color: #f5f5f5;
  329. font-size: 28rpx;
  330. }
  331. .form-actions {
  332. display: flex;
  333. justify-content: flex-end;
  334. margin-bottom: 30rpx;
  335. }
  336. .forgot-password {
  337. font-size: 24rpx;
  338. color: #666;
  339. }
  340. .login-btn {
  341. width: 100%;
  342. height: 90rpx;
  343. background-color: #4CAF50;
  344. color: #fff;
  345. border-radius: 45rpx;
  346. font-size: 32rpx;
  347. font-weight: bold;
  348. display: flex;
  349. align-items: center;
  350. justify-content: center;
  351. border: none;
  352. margin-bottom: 30rpx;
  353. }
  354. .register-link {
  355. font-size: 24rpx;
  356. color: #666;
  357. text-align: center;
  358. margin-top: 20rpx;
  359. }
  360. .login-bg {
  361. width: 100%;
  362. max-width: 600rpx;
  363. margin: 20rpx 0 60rpx;
  364. }
  365. .wechat-login-btn {
  366. width: 80%;
  367. height: 90rpx;
  368. background-color: #07C160;
  369. color: #fff;
  370. border-radius: 45rpx;
  371. font-size: 32rpx;
  372. font-weight: bold;
  373. display: flex;
  374. align-items: center;
  375. justify-content: center;
  376. border: none;
  377. margin-bottom: 40rpx;
  378. padding: 0;
  379. margin-top: 20rpx;
  380. }
  381. .wechat-login-btn::after {
  382. border: none;
  383. }
  384. .wechat-icon {
  385. width: 40rpx;
  386. height: 40rpx;
  387. background-color: rgba(255, 255, 255, 0.2);
  388. color: #fff;
  389. border-radius: 20rpx;
  390. font-size: 20rpx;
  391. display: flex;
  392. align-items: center;
  393. justify-content: center;
  394. margin-right: 10rpx;
  395. }
  396. .privacy-agreement {
  397. display: flex;
  398. align-items: center;
  399. justify-content: center;
  400. /* margin-top: auto; */
  401. padding: 40rpx 0;
  402. }
  403. .agreement-text {
  404. font-size: 24rpx;
  405. color: #666;
  406. margin-left: 10rpx;
  407. }
  408. .link {
  409. color: #4CAF50;
  410. }
  411. /* #ifdef H5 */
  412. /* H5特殊样式调整 */
  413. @media screen and (min-width: 768px) {
  414. .form-container {
  415. max-width: 500rpx;
  416. margin: 0 auto;
  417. }
  418. }
  419. /* #endif */
  420. /* #ifdef APP-PLUS || MP-HARMONY */
  421. /* APP和鸿蒙特殊样式调整 */
  422. .login-container {
  423. padding-top: 20rpx;
  424. }
  425. .input-field {
  426. border: 1rpx solid #E0E0E0;
  427. }
  428. .login-btn {
  429. margin-top: 20rpx;
  430. }
  431. /* #endif */
  432. </style>