| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- <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/logo.png" mode="aspectFit"></image>
- <text class="app-name">农小禹</text>
- <text class="app-slogan">智慧农业,从此开始</text>
- </view>
- <view class="login-section">
- <!-- #ifdef H5 -->
- <view class="form-container">
- <view class="input-group">
- <input type="text" v-model="phoneNumber" placeholder="请输入手机号" class="input-field" />
- </view>
- <view class="input-group">
- <input type="password" v-model="password" placeholder="请输入密码" class="input-field" />
- </view>
- <!-- <view class="form-actions">
- <text class="forgot-password" @click="navigateToForgetPassword">忘记密码?</text>
- </view> -->
- <button type="primary" class="login-btn" @click="handlePhoneLogin">登录</button>
- <view class="register-link">
- 还没有账号?<text class="link" @click="navigateToRegister">立即注册</text>
- </view>
- </view>
- <!-- #endif -->
- <!-- #ifdef MP-WEIXIN -->
- <button type="primary" class="wechat-login-btn" bindtap="getUserProfile" @click="getUserProfile()">
- <view class="wechat-icon">微</view>
- <text>一键登录</text>
- </button>
- <!-- #endif -->
- <!-- #ifdef APP-PLUS -->
- <view class="form-container">
- <view class="input-group">
- <input type="text" v-model="phoneNumber" placeholder="请输入手机号" class="input-field" />
- </view>
- <view class="input-group">
- <input type="password" v-model="password" placeholder="请输入密码" class="input-field" />
- </view>
- <view class="form-actions">
- <text class="forgot-password" @click="navigateToForgetPassword">忘记密码?</text>
- </view>
- <button type="primary" class="login-btn" @click="handlePhoneLogin">登录</button>
- <view class="register-link">
- 还没有账号?<text class="link" @click="navigateToRegister">立即注册</text>
- </view>
- </view>
- <!-- #endif -->
- </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>
- import {
- mpAutoLogin, phoneLogin
- } from "@/api/services/connect.js";
- import storage from "@/utils/storage.js";
- export default {
- data() {
- return {
- // 以下是登录页面测试功能 结束
- agreed: false,
- // 是否展示手机号码授权弹窗,默认第一步不展示,要先获取用户基础信息
- phoneAuthPopup: false,
- // 授权信息展示,商城名称
- // projectName: config.name,
- //微信返回信息,用于揭秘信息,获取sessionkey
- code: "",
- //微信昵称
- nickName: "",
- //微信头像
- avatarUrl: "",
- gender: "",
- // H5登录信息
- phoneNumber: "",
- password: "",
- loading: false
- }
- },
- onLoad() {
- // // 检查是否已登录
- // if (isLoggedIn()) {
- // this.redirectToHome();
- // }
- // #ifdef MP-WEIXIN
- //获取code
- uni.login({
- success: (res) => {
- if (res.errMsg === "login:ok") {
- this.code = res.code
- } else {
- uni.showToast({
- title: "系统异常,请联系管理员!"
- })
- }
- },
- });
- // #endif
- },
- methods: {
- goBack() {
- uni.navigateBack();
- },
- toggleAgreement() {
- this.agreed = !this.agreed;
- console.log("this.agreed", this.agreed);
- },
- navigateToTerms() {
- uni.navigateTo({
- url: '/pages/privacy/terms'
- });
- },
- navigateToPrivacy() {
- uni.navigateTo({
- url: '/pages/privacy/index'
- });
- },
- navigateToForgetPassword() {
- uni.navigateTo({
- url: '/pages/login/forget-password'
- });
- },
- navigateToRegister() {
- uni.navigateTo({
- url: '/pages/login/register'
- });
- },
- showAgreementWarning() {
- uni.showToast({
- title: '请同意用户协议和隐私政策',
- icon: 'none'
- });
- },
- // H5平台手机号密码登录方法
- handlePhoneLogin() {
- if (!this.agreed) {
- uni.showToast({
- title: "请同意用户协议和隐私政策",
- icon: 'none'
- })
- return
- }
- if (!this.phoneNumber || !this.password) {
- uni.showToast({
- title: "请输入手机号和密码",
- icon: 'none'
- })
- return
- }
- // 手机号验证
- if (!/^1\d{10}$/.test(this.phoneNumber)) {
- uni.showToast({
- title: "请输入正确的手机号",
- icon: 'none'
- })
- return
- }
- this.loading = true;
- uni.showLoading({ title: '登录中...' })
- const data = {
- username: this.phoneNumber,
- password: this.password,
- phoneNumber:this.phoneNumber,
- }
- phoneLogin(data).then(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);
- 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();
- this.loading = false;
- });
- },
- // 微信小程序登录方法
- getUserProfile(e) {
- if (!this.agreed) {
- uni.showToast({
- title: "请同意用户协议和隐私政策",
- icon: 'none'
- })
- return
- }
- this.logingFlag = true;
- // #ifdef MP-WEIXIN
- if (this.code) {
- // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
- uni.getUserProfile({
- desc: "获取你的昵称、头像、地区及性别", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
- success: (res) => {
- console.log("success", res)
- this.nickName = res.userInfo.nickName;
- this.avatarUrl = res.userInfo.avatarUrl;
- this.gender = res.userInfo.gender;
- let iv = res.iv;
- let encryptedData = res.encryptedData;
- let code = this.code;
- let avatarUrl = this.avatarUrl;
- let nickName = this.nickName;
- let gender = this.gender;
- this.isLogin = 2
- mpAutoLogin({
- encryptedData,
- iv,
- code,
- avatarUrl,
- nickName,
- gender,
- }).then((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);
- // 用户手动授权头像昵称
- 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)
- },
- });
- this.logingFlag = false;
- }
- // #endif
- },
- redirectToHome() {
- uni.navigateBack();
- }
- }
- }
- </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: 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%;
- }
- .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;
- }
- .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 */
- </style>
|