index.vue 11 KB

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