| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- <template>
- <view class="login-container">
- <!-- 暂不登录-->
- <view class="page-header">
- <text class="back-icon" @click="goBack">←</text>
- <text class="page-title">登录</text>
- <text class="placeholder"></text>
- </view>
- <view class="logo-section">
- <image class="logo" src="/static/images/n.png" mode="aspectFit"></image>
- <text class="app-name">农小禹</text>
- <text class="app-slogan">智慧农业,从此开始</text>
- </view>
- <view class="login-section">
- <!-- -->
- <view class="form-container">
- <view class="input-group">
- <input type="text" v-model="phoneNumber" placeholder="请输入手机号" class="input-field" maxlength="11" />
- </view>
-
- <!-- 密码登录 -->
- <view v-if="loginType === 'password'" class="input-group">
- <input type="password" v-model="password" placeholder="请输入密码" class="input-field" />
- </view>
-
- <!-- 验证码登录 -->
- <view v-if="loginType === 'code'" class="input-group verification-group">
- <input type="text" v-model="verificationCode" placeholder="请输入验证码" class="input-field code-input" maxlength="6" />
- <button
- class="send-code-btn"
- :disabled="countdown > 0"
- @click="sendVerificationCode"
- >
- {{ countdown > 0 ? `${countdown}s` : '获取验证码' }}
- </button>
- </view>
-
- <!-- <view class="form-actions">
- <text v-if="loginType === 'password'" class="forgot-password" @click="navigateToForgetPassword">忘记密码?</text>
- </view> -->
- <button class="login-btn" @click="handleLogin">登录</button>
-
- <!-- 底部操作区 -->
- <view class="bottom-actions">
- <text class="action-link" @click="switchLoginType(loginType === 'code' ? 'password' : 'code')">
- {{ loginType === 'code' ? '密码登录' : '验证码登录' }}
- </text>
- <text class="action-link" @click="navigateToRegister">注册账号</text>
- <text class="action-link" @click="navigateToForgetPassword">忘记密码</text>
- </view>
- </view>
-
-
- <!-- <button type="primary" class="wechat-login-btn" bindtap="getUserProfile" @click="getUserProfile()">
- <view class="wechat-icon">微</view>
- <text>一键登录</text>
- </button> -->
-
- </view>
- <view class="privacy-agreement">
- <checkbox :checked="agreed" @click="toggleAgreement"></checkbox>
- <text class="agreement-text">
- 登录即表示您已同意
- <text class="link" @click="navigateToTerms">用户协议</text>
- 和
- <text class="link" @click="navigateToPrivacy">隐私政策</text>
- </text>
- </view>
- </view>
- </template>
- <script setup>
- import { ref, onMounted } from 'vue'
- import {
- mpAutoLogin, phoneLogin, phoneCodeLogin, sendVcode, sendPhoneVcodeNoUnique
- } from "@/api/services/connect.js";
- import { fetchUserFieldList } from "@/api/services/field.js";
- import storage from "@/utils/storage.js";
- import Foundation from "@/utils/Foundation.js";
- // 获取并设置默认地块信息
- const fetchAndSetDefaultField = async () => {
- try {
- const res = await fetchUserFieldList(1, 10);
- console.log("获取地块列表", res);
-
- if (res.data && res.data.code === 200 && res.data.data && res.data.data.list && res.data.data.list.length > 0) {
- const firstField = res.data.data.list[0];
-
- // 保存默认地块到本地存储
- storage.setPlots(JSON.stringify({
- id: firstField.id,
- code: firstField.code,
- name: firstField.name,
- growCrops: firstField.crop,
- managerName: firstField.manager,
- size: firstField.area,
- farmId: firstField.farmId,
- timestamp: Date.now()
- }));
-
- console.log("已设置默认地块:", firstField.name);
- return true;
- } else {
- console.log("暂无可用地块");
- return false;
- }
- } catch (err) {
- console.error("获取地块列表失败", err);
- return false;
- }
- };
- // Reactive data
- const agreed = ref(false)
- const code = ref("")
- const nickName = ref("")
- const avatarUrl = ref("")
- const gender = ref("")
- const phoneNumber = ref("")
- const password = ref("")
- const verificationCode = ref("")
- const loginType = ref("code") // 'password' 或 'code'
- const countdown = ref(0)
- const loading = ref(false)
- const logingFlag = ref(false)
- const isLogin = ref(0)
- let timer = null
- // Lifecycle hooks - uni-app specific lifecycle
- onMounted(() => {
- // // 检查是否已登录
- // if (isLoggedIn()) {
- // redirectToHome();
- // }
- // #ifdef MP-WEIXIN
- //获取code
- uni.login({
- success: (res) => {
- if (res.errMsg === "login:ok") {
- code.value = res.code
- } else {
- uni.showToast({
- title: "系统异常,请联系管理员!"
- })
- }
- },
- });
- // #endif
- })
- // Methods
- const goBack = () => {
- uni.navigateBack();
- }
- const toggleAgreement = () => {
- agreed.value = !agreed.value;
- console.log("agreed", agreed.value);
- }
- const navigateToTerms = () => {
- uni.navigateTo({
- url: '/pages/login/terms'
- });
- }
- const navigateToPrivacy = () => {
- uni.navigateTo({
- url: '/pages/login/privacy'
- })
- }
- const navigateToForgetPassword = () => {
- uni.navigateTo({
- url: '/pages/login/forget-password'
- });
- }
- const navigateToRegister = () => {
- uni.navigateTo({
- url: '/pages/login/register'
- });
- }
- // 切换登录方式
- const switchLoginType = (type) => {
- loginType.value = type;
- // 清空输入
- password.value = "";
- verificationCode.value = "";
- }
- // 发送验证码
- const sendVerificationCode = () => {
- if (!phoneNumber.value) {
- uni.showToast({
- title: "请输入手机号",
- icon: 'none'
- });
- return;
- }
- // 手机号验证
- const result = Foundation.validatePhoneNumber(phoneNumber.value);
- if (!result.valid) {
- uni.showToast({
- title: result.message,
- icon: 'none'
- });
- return;
- }
- // 发送验证码
- uni.showLoading({ title: '发送中...' });
- sendPhoneVcodeNoUnique({ terminal: phoneNumber.value,scene:'LOGIN' })
- .then(res => {
- console.log("发送验证码", res);
- if (res.data.code === 200) {
- uni.showToast({
- title: "验证码已发送",
- icon: 'success'
- });
- // 开始倒计时
- countdown.value = 60;
- timer = setInterval(() => {
- countdown.value--;
- if (countdown.value <= 0) {
- clearInterval(timer);
- }
- }, 1000);
- } else {
- uni.showToast({
- title: res.data.msg || "发送失败",
- icon: 'none'
- });
- }
- })
- .catch(err => {
- console.error(err);
- uni.showToast({
- title: "网络异常,请稍后重试",
- icon: 'none'
- });
- })
- .finally(() => {
- uni.hideLoading();
- });
- }
- // 统一登录处理
- const handleLogin = () => {
- if (loginType.value === 'password') {
- handlePhoneLogin();
- } else {
- handleCodeLogin();
- }
- }
- // 验证码登录
- const handleCodeLogin = () => {
- if (!agreed.value) {
- uni.showToast({
- title: "请同意用户协议和隐私政策",
- icon: 'none'
- });
- return;
- }
- if (!phoneNumber.value || !verificationCode.value) {
- uni.showToast({
- title: "请输入手机号和验证码",
- icon: 'none'
- });
- return;
- }
- // 手机号验证
- const result = Foundation.validatePhoneNumber(phoneNumber.value);
- if (!result.valid) {
- uni.showToast({
- title: result.message,
- icon: 'none'
- });
- return;
- }
- loading.value = true;
- uni.showLoading({ title: '登录中...' });
-
- const data = {
- phonenumber: phoneNumber.value,
- vcode: verificationCode.value
- };
- phoneCodeLogin(data).then(async res => {
- console.log("验证码登录", res);
- if (res.data.code === 200) {
- storage.setAccessToken(res.data.data.access_token);
- storage.setUserInfo(res.data.data.userInfo);
- storage.setHasLogin(true);
- await fetchAndSetDefaultField();
- uni.showToast({
- title: "登录成功!",
- icon: "success",
- });
- setTimeout(() => {
- uni.navigateBack({
- delta: 1,
- });
- }, 1500);
- } else {
- uni.showToast({
- title: res.data.msg || "登录失败,请检查验证码",
- icon: 'none'
- });
- }
- })
- .catch(err => {
- uni.showToast({
- title: "网络异常,请稍后重试",
- icon: 'none'
- });
- console.error(err);
- })
- .finally(() => {
- uni.hideLoading();
- loading.value = false;
- });
- }
- // H5平台手机号密码登录方法
- const handlePhoneLogin = () => {
- if (!agreed.value) {
- uni.showToast({
- title: "请同意用户协议和隐私政策",
- icon: 'none'
- })
- return
- }
- if (!phoneNumber.value || !password.value) {
- uni.showToast({
- title: "请输入手机号和密码",
- icon: 'none'
- })
- return
- }
- // 手机号验证
- const phone = phoneNumber.value; // 获取用户输入的手机号
- const result = Foundation.validatePhoneNumber(phone);
- if (!result.valid) {
- uni.showToast({
- title: result.message,
- icon: 'none'
- });
- return
- }
- loading.value = true;
- uni.showLoading({ title: '登录中...' })
- const data = {
- username: phoneNumber.value,
- password: password.value,
- phoneNumber: phoneNumber.value,
- }
- phoneLogin(data).then(async res => {
- console.log("res登录", res);
- if (res.data.code === 200) {
- // 登录成功
- storage.setAccessToken(res.data.data.access_token);
- storage.setUserInfo(res.data.data.userInfo);
- storage.setHasLogin(true);
- // 获取并设置默认地块信息
- await fetchAndSetDefaultField();
- uni.showToast({
- title: "登录成功!",
- icon: "success",
- });
- // 登录成功后返回或跳转到首页
- setTimeout(() => {
- uni.navigateBack({
- delta: 1,
- });
- }, 1500);
- } else {
- // 登录失败
- uni.showToast({
- title: res.data.msg || "登录失败,请检查账号密码",
- icon: 'none'
- });
- }
- })
- .catch(err => {
- uni.showToast({
- title: "网络异常,请稍后重试",
- icon: 'none'
- });
- console.error(err);
- })
- .finally(() => {
- uni.hideLoading();
- loading.value = false;
- });
- }
- // 微信小程序登录方法
- const getUserProfile = () => {
- if (!agreed.value) {
- uni.showToast({
- title: "请同意用户协议和隐私政策",
- icon: 'none'
- })
- return
- }
- logingFlag.value = true;
- // #ifdef MP-WEIXIN
- if (code.value) {
- // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
- uni.getUserProfile({
- desc: "获取你的昵称、头像、地区及性别", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
- success: (res) => {
- console.log("success", res)
- nickName.value = res.userInfo.nickName;
- avatarUrl.value = res.userInfo.avatarUrl;
- gender.value = res.userInfo.gender;
- let iv = res.iv;
- let encryptedData = res.encryptedData;
- let codeVal = code.value;
- let avatarUrlVal = avatarUrl.value;
- let nickNameVal = nickName.value;
- let genderVal = gender.value;
- isLogin.value = 2
- mpAutoLogin({
- encryptedData,
- iv,
- code: codeVal,
- avatarUrl: avatarUrlVal,
- nickName: nickNameVal,
- gender: genderVal,
- }).then(async (apiRes) => {
- console.log("apiRes", apiRes);
- storage.setAccessToken(apiRes.data.data.token);
- // storage.setRefreshToken(apiRes.data.result.refreshToken);
- uni.showToast({
- title: "登录成功!",
- icon: "none",
- });
- //存储用户信息
- storage.setUserInfo(apiRes.data.data.userInfo);
- storage.setHasLogin(true);
-
- // 获取并设置默认地块信息
- await fetchAndSetDefaultField();
-
- // 用户手动授权头像昵称
- if (apiRes.data.data.isNewUser) {
- // 新用户跳转上传头像昵称界面
- uni.navigateTo({
- url: `/pages/userInfo/index?openId=${apiRes.data.data.userInfo.openId}`
- });
- } else {
- // 老用户直接跳转首页
- // wx.switchTab({
- // url: '/pages/user/index'
- // });
- uni.navigateBack({
- delta: 1,
- });
- }
- });
- },
- fail: (res) => {
- console.log("fail", res)
- },
- });
- logingFlag.value = false;
- }
- // #endif
- }
- </script>
- <style>
- .login-container {
- min-height: 100vh;
- padding: 0 40rpx;
- background-color: #fff;
- display: flex;
- flex-direction: column;
- }
- .page-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding-top: calc(15rpx + constant(safe-area-inset-top));
- padding-top: calc(15rpx + env(safe-area-inset-top));
- /* padding: 40rpx 0; */
- }
- .back-icon {
- font-size: 50rpx;
- color: #333;
- width: 60rpx;
- }
- .page-title {
- font-size: 36rpx;
- font-weight: bold;
- color: #333;
- }
- .placeholder {
- width: 60rpx;
- }
- .logo-section {
- display: flex;
- flex-direction: column;
- align-items: center;
- margin-bottom: 60rpx;
- }
- .logo {
- width: 150rpx;
- height: 150rpx;
- margin-bottom: 20rpx;
- }
- .app-name {
- font-size: 48rpx;
- font-weight: bold;
- color: #4CAF50;
- margin-bottom: 10rpx;
- }
- .app-slogan {
- font-size: 28rpx;
- color: #666;
- }
- .login-section {
- display: flex;
- flex-direction: column;
- align-items: center;
- padding: 20rpx 0;
- position: relative;
- width: 100%;
- }
- /* H5和APP表单样式 */
- .form-container {
- width: 100%;
- margin-bottom: 30rpx;
- }
- .input-group {
- margin-bottom: 30rpx;
- width: 100%;
- }
- .verification-group {
- display: flex;
- align-items: center;
- gap: 20rpx;
- }
- .code-input {
- flex: 1;
- }
- .send-code-btn {
- width: 200rpx;
- height: 90rpx;
- background-color: #4CAF50;
- color: #fff;
- border-radius: 45rpx;
- font-size: 24rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- border: none;
- padding: 0;
- line-height: 90rpx;
- }
- .send-code-btn[disabled] {
- background-color: #ccc;
- color: #999;
- }
- .send-code-btn::after {
- border: none;
- }
- .input-field {
- width: 100%;
- height: 90rpx;
- border-radius: 45rpx;
- padding: 0 30rpx;
- box-sizing: border-box;
- background-color: #f5f5f5;
- font-size: 28rpx;
- }
- .form-actions {
- display: flex;
- justify-content: flex-end;
- margin-bottom: 30rpx;
- }
- .forgot-password {
- font-size: 24rpx;
- color: #666;
- }
- .login-btn {
- width: 100%;
- height: 90rpx;
- background-color: #4CAF50;
- color: #fff;
- border-radius: 45rpx;
- font-size: 32rpx;
- font-weight: bold;
- display: flex;
- align-items: center;
- justify-content: center;
- border: none;
- margin-bottom: 30rpx;
- }
- .bottom-actions {
- display: flex;
- justify-content: center;
- align-items: center;
- margin-top: 20rpx;
- }
- .action-link {
- font-size: 26rpx;
- color: #666;
- margin: 0 20rpx;
- }
- .register-link {
- font-size: 24rpx;
- color: #666;
- text-align: center;
- margin-top: 20rpx;
- }
- .login-bg {
- width: 100%;
- max-width: 600rpx;
- margin: 20rpx 0 60rpx;
- }
- .wechat-login-btn {
- width: 80%;
- height: 90rpx;
- background-color: #07C160;
- color: #fff;
- border-radius: 45rpx;
- font-size: 32rpx;
- font-weight: bold;
- display: flex;
- align-items: center;
- justify-content: center;
- border: none;
- margin-bottom: 40rpx;
- padding: 0;
- margin-top: 20rpx;
- }
- .wechat-login-btn::after {
- border: none;
- }
- .wechat-icon {
- width: 40rpx;
- height: 40rpx;
- background-color: rgba(255, 255, 255, 0.2);
- color: #fff;
- border-radius: 20rpx;
- font-size: 20rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- margin-right: 10rpx;
- }
- .privacy-agreement {
- display: flex;
- align-items: center;
- justify-content: center;
- /* margin-top: auto; */
- padding: 40rpx 0;
- }
- .agreement-text {
- font-size: 24rpx;
- color: #666;
- margin-left: 10rpx;
- }
- .link {
- color: #4CAF50;
- }
- /* #ifdef H5 */
- /* H5特殊样式调整 */
- @media screen and (min-width: 768px) {
- .form-container {
- max-width: 500rpx;
- margin: 0 auto;
- }
- }
- /* #endif */
- /* #ifdef APP-PLUS || MP-HARMONY */
- /* APP和鸿蒙特殊样式调整 */
- .login-container {
- padding-top: 20rpx;
- }
- .input-field {
- border: 1rpx solid #E0E0E0;
- }
- .login-btn {
- margin-top: 20rpx;
- }
- /* #endif */
- </style>
|