index.vue 11 KB

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