<template>
|
<scroll-view class="normal-login-container" scroll-y="true">
|
<view class="logo-content align-center justify-center flex">
|
<image style="width: 100rpx;height: 100rpx;" :src="globalConfig.appInfo.logo" mode="widthFix">
|
</image>
|
<text class="title">民航调度系统</text>
|
</view>
|
<view class="login-form-content">
|
<view class="input-item flex align-center">
|
<view class="iconfont icon-user icon"></view>
|
<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
|
</view>
|
<view class="input-item flex align-center">
|
<view class="iconfont icon-password icon"></view>
|
<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
|
</view>
|
<view class="input-item flex align-center captcha-container" v-if="captchaEnabled">
|
<view class="iconfont icon-code icon"></view>
|
<input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
|
<view class="login-code">
|
<image :src="codeUrl" @click="getCode" class="login-code-img" mode="aspectFit"></image>
|
</view>
|
</view>
|
<view class="agreement-checkbox">
|
<checkbox-group @change="onAgreementChange">
|
<label class="checkbox-label">
|
<checkbox :checked="agreedToPolicy" value="agreed" color="#007AFF" class="round-checkbox" style="margin-top: 0;" />
|
<text class="agreement-text">
|
<text class="text-grey1">同意</text>
|
<text @click.stop="handleUserAgrement" class="text-blue agreement-link">《用户协议》</text>
|
<text class="text-grey1">和</text>
|
<text @click.stop="handlePrivacy" class="text-blue agreement-link">《隐私政策》</text>
|
</text>
|
</label>
|
</checkbox-group>
|
</view>
|
|
<view class="action-btn">
|
<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
|
<!-- 微信一键登录按钮(仅在微信小程序环境显示) -->
|
<!-- #ifdef MP-WEIXIN -->
|
<button
|
v-if="isWechat && wechatOpenId"
|
@click="loginByOpenId"
|
class="wechat-login-btn cu-btn block bg-green lg round"
|
style="margin-top: 20rpx;">
|
<text class="cuIcon-wechat" style="margin-right: 10rpx;"></text>
|
手机号码快捷登录
|
</button>
|
<button
|
v-else-if="isWechat"
|
open-type="getPhoneNumber"
|
@getphonenumber="onGetPhoneNumber"
|
class="wechat-login-btn cu-btn block bg-green lg round"
|
style="margin-top: 20rpx;">
|
<text class="cuIcon-wechat" style="margin-right: 10rpx;"></text>
|
手机号码快捷登录
|
</button>
|
<!-- #endif -->
|
</view>
|
</view>
|
</scroll-view>
|
</template>
|
|
<script>
|
import { getCodeImg, loginByOpenId, loginByWechatPhone } from '@/api/login'
|
|
export default {
|
data() {
|
return {
|
codeUrl: "",
|
captchaEnabled: true,
|
// 用户注册开关
|
register: false,
|
// 隐私政策同意状态(默认未选中,需要用户主动勾选)
|
agreedToPolicy: false,
|
globalConfig: getApp().globalData.config,
|
loginForm: {
|
username: "",
|
password: "",
|
code: "",
|
uuid: ''
|
},
|
// 微信一键登录相关
|
isWechat: false, // 是否为微信小程序环境
|
wechatOpenId: '', // 微信OpenID
|
wechatUnionId: '', // 微信UnionID
|
}
|
},
|
created() {
|
this.getCode()
|
this.checkWechatEnv()
|
this.tryAutoLogin()
|
},
|
methods: {
|
// 用户注册
|
handleUserRegister() {
|
this.$tab.redirectTo(`/pages/register`)
|
},
|
// 隐私协议
|
handlePrivacy() {
|
this.$tab.navigateTo('/pages/mine/privacy-policy/index')
|
},
|
// 用户协议
|
handleUserAgrement() {
|
this.$tab.navigateTo('/pages/mine/user-agreement/index')
|
},
|
// 协议同意状态变更
|
onAgreementChange(e) {
|
this.agreedToPolicy = e.detail.value.length > 0
|
},
|
// 获取图形验证码
|
getCode() {
|
getCodeImg().then(res => {
|
this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
|
if (this.captchaEnabled) {
|
this.codeUrl = 'data:image/gif;base64,' + res.img
|
this.loginForm.uuid = res.uuid
|
}
|
})
|
},
|
// 登录方法
|
async handleLogin() {
|
if (!this.agreedToPolicy) {
|
this.$modal.msgError("请先阅读并同意用户协议和隐私政策")
|
return
|
}
|
if (this.loginForm.username === "") {
|
this.$modal.msgError("请输入您的账号")
|
} else if (this.loginForm.password === "") {
|
this.$modal.msgError("请输入您的密码")
|
} else if (this.loginForm.code === "" && this.captchaEnabled) {
|
this.$modal.msgError("请输入验证码")
|
} else {
|
this.$modal.loading("登录中,请耐心等待...")
|
this.pwdLogin()
|
}
|
},
|
// 密码登录
|
async pwdLogin() {
|
this.$store.dispatch('Login', this.loginForm).then(() => {
|
this.$modal.closeLoading()
|
this.loginSuccess()
|
}).catch(() => {
|
if (this.captchaEnabled) {
|
this.getCode()
|
}
|
})
|
},
|
// 登录成功后,处理函数
|
loginSuccess(result) {
|
// 设置用户信息
|
this.$store.dispatch('GetInfo').then(res => {
|
// 触发登录成功事件,启动消息轮询
|
uni.$emit('user-login')
|
this.$tab.reLaunch('/pages/index')
|
})
|
},
|
|
// ==================== 微信一键登录相关方法 ====================
|
|
// 检查是否在微信小程序环境
|
checkWechatEnv() {
|
// #ifdef MP-WEIXIN
|
this.isWechat = true
|
console.log('当前环境:微信小程序')
|
// #endif
|
|
// #ifndef MP-WEIXIN
|
this.isWechat = false
|
console.log('当前环境:非微信小程序')
|
// #endif
|
},
|
|
// 尝试自动登录(检查本地是否有保存的OpenID)
|
tryAutoLogin() {
|
if (!this.isWechat) {
|
return
|
}
|
|
// 从本地存储中获取OpenID和UnionID
|
const savedOpenId = uni.getStorageSync('wechat_openid')
|
const savedUnionId = uni.getStorageSync('wechat_unionid')
|
const autoLogin=false;
|
|
if (savedOpenId && autoLogin) {
|
console.log('检测到已保存的OpenID,尝试自动登录')
|
this.wechatOpenId = savedOpenId
|
this.wechatUnionId = savedUnionId // 可能为null
|
this.loginByOpenId()
|
}
|
},
|
|
// 处理获取手机号的回调
|
onGetPhoneNumber(e) {
|
console.log('获取手机号回调:', e)
|
|
if (!this.agreedToPolicy) {
|
this.$modal.msgError("请先阅读并同意用户协议和隐私政策")
|
return
|
}
|
|
if (e.detail.errMsg === 'getPhoneNumber:ok') {
|
// 用户同意授权
|
const { code } = e.detail
|
|
this.$modal.loading("正在获取手机号...")
|
|
// 先获取微信登录code
|
uni.login({
|
provider: 'weixin',
|
success: (loginRes) => {
|
console.log('微信登录code:', loginRes.code)
|
console.log('手机号code:', code)
|
|
// 调用后端接口,传入loginCode和phoneCode
|
loginByWechatPhone(loginRes.code, code).then(response => {
|
this.$modal.closeLoading()
|
|
if (response.code === 200) {
|
// 登录成功,保存OpenID和UnionID
|
const openId = response.openId
|
const unionId = response.unionId
|
|
uni.setStorageSync('wechat_openid', openId)
|
this.wechatOpenId = openId
|
|
if (unionId) {
|
uni.setStorageSync('wechat_unionid', unionId)
|
this.wechatUnionId = unionId
|
}
|
|
// 保存token到本地存储和Vuex
|
const token = response.token
|
this.$store.commit('SET_TOKEN', token)
|
// 必须调用setToken保存到本地存储
|
const { setToken } = require('@/utils/auth')
|
setToken(token)
|
|
this.loginSuccess()
|
} else {
|
this.$modal.msgError(response.msg || '登录失败')
|
}
|
}).catch(error => {
|
this.$modal.closeLoading()
|
console.error('登录失败:', error)
|
this.$modal.msgError('登录失败,请重试')
|
})
|
},
|
fail: (err) => {
|
this.$modal.closeLoading()
|
console.error('获取微信登录code失败:', err)
|
this.$modal.msgError('获取微信信息失败')
|
}
|
})
|
} else {
|
// 用户拒绝授权
|
this.$modal.msgError('需要您的手机号才能登录')
|
}
|
},
|
|
// 通过OpenID和UnionID登录
|
loginByOpenId() {
|
this.$modal.loading("正在登录...")
|
|
// 同时传入openId和unionId进行双重验证
|
loginByOpenId(this.wechatOpenId, this.wechatUnionId).then(response => {
|
this.$modal.closeLoading()
|
|
if (response.code === 200) {
|
// 登录成功,保存token到本地存储和Vuex
|
const token = response.token
|
this.$store.commit('SET_TOKEN', token)
|
// 必须调用setToken保存到本地存储
|
const { setToken } = require('@/utils/auth')
|
setToken(token)
|
|
this.loginSuccess()
|
} else {
|
// OpenID未绑定或验证失败,需要获取手机号绑定
|
console.log('该OpenID尚未绑定或验证失败,需要获取手机号')
|
this.$modal.closeLoading()
|
this.$modal.msgError(response.msg || '该微信账号尚未绑定,请点击微信一键登录按钮获取手机号授权')
|
}
|
}).catch(error => {
|
this.$modal.closeLoading()
|
console.error('登录失败:', error)
|
this.$modal.msgError('登录失败,请重试')
|
})
|
}
|
}
|
}
|
</script>
|
|
<style lang="scss">
|
page {
|
background-color: #ffffff;
|
}
|
|
.normal-login-container {
|
width: 100%;
|
min-height: 100vh;
|
// 隐藏滚动条但保持滚动功能
|
::-webkit-scrollbar {
|
display: none;
|
width: 0 !important;
|
height: 0 !important;
|
background: transparent;
|
}
|
|
// Firefox滚动条隐藏
|
* {
|
scrollbar-width: none; /* Firefox */
|
}
|
|
// IE/Edge滚动条隐藏
|
* {
|
-ms-overflow-style: none; /* IE 10+ */
|
}
|
|
.logo-content {
|
width: 100%;
|
font-size: 21px;
|
text-align: center;
|
padding-top: 15%;
|
|
image {
|
border-radius: 4px;
|
}
|
|
.title {
|
margin-left: 10px;
|
}
|
}
|
|
.login-form-content {
|
text-align: center;
|
margin: 20px auto;
|
margin-top: 15%;
|
width: 80%;
|
|
.input-item {
|
margin: 20px auto;
|
background-color: #f5f6f7;
|
height: 45px;
|
border-radius: 20px;
|
|
.icon {
|
font-size: 38rpx;
|
margin-left: 10px;
|
}
|
|
.input {
|
margin-left: 20rpx;
|
width: 100%;
|
height: 45px;
|
background: none;
|
}
|
}
|
|
.captcha-container {
|
width: 100%;
|
margin: 20px auto;
|
background-color: #f5f6f7;
|
height: 45px;
|
border-radius: 20px;
|
padding-right: 10rpx;
|
box-sizing: border-box;
|
|
.icon {
|
font-size: 38rpx;
|
margin-left: 10px;
|
}
|
|
.input {
|
margin-left: 20rpx;
|
width: 60%;
|
height: 45px;
|
background: none;
|
}
|
|
.login-code {
|
width: 30%;
|
height: 45px;
|
margin-left: 10rpx;
|
border-left: 1px solid #e0e0e0;
|
display: flex;
|
align-items: center;
|
justify-content: center;
|
|
.login-code-img {
|
max-width: 100%;
|
max-height: 45px;
|
object-fit: contain;
|
cursor: pointer;
|
}
|
}
|
}
|
|
.action-btn {
|
margin: 40rpx 0;
|
|
.login-btn {
|
height: 90rpx;
|
font-size: 32rpx;
|
}
|
|
.wechat-login-btn {
|
height: 90rpx;
|
font-size: 32rpx;
|
display: flex;
|
align-items: center;
|
justify-content: center;
|
}
|
}
|
|
.reg {
|
margin: 20rpx 0;
|
|
.text-grey1 {
|
color: #888;
|
}
|
|
.text-blue {
|
margin: 0 10rpx;
|
color: #007AFF;
|
}
|
}
|
|
.agreement-checkbox {
|
margin: 50rpx 0 30rpx 0;
|
padding: 20rpx;
|
display: flex;
|
justify-content: flex-start;
|
align-items: left;
|
|
checkbox-group {
|
display: flex;
|
align-items: center;
|
}
|
|
.checkbox-label {
|
display: flex;
|
align-items: center;
|
justify-content: flex-start;
|
|
checkbox {
|
margin-right: 15rpx;
|
transform: scale(1.2);
|
flex-shrink: 0;
|
vertical-align: middle;
|
}
|
|
// 圆形复选框样式
|
.round-checkbox {
|
border-radius: 50% !important;
|
}
|
|
// 针对微信小程序的圆形样式
|
::v-deep .uni-checkbox-input,
|
::v-deep .wx-checkbox-input {
|
border-radius: 50% !important;
|
}
|
|
// 针对H5的圆形样式
|
::v-deep input[type="checkbox"] {
|
border-radius: 50% !important;
|
-webkit-appearance: none;
|
appearance: none;
|
width: 36rpx;
|
height: 36rpx;
|
border: 2rpx solid #d1d1d1;
|
background-color: #fff;
|
position: relative;
|
|
&:checked {
|
background-color: #007AFF;
|
border-color: #007AFF;
|
|
&::after {
|
content: '';
|
position: absolute;
|
top: 50%;
|
left: 50%;
|
transform: translate(-50%, -50%);
|
width: 12rpx;
|
height: 12rpx;
|
background-color: #fff;
|
border-radius: 50%;
|
}
|
}
|
}
|
|
.agreement-text {
|
display: inline-flex;
|
align-items: center;
|
flex-wrap: wrap;
|
line-height: 1.5;
|
font-size: 26rpx;
|
text-align: left;
|
justify-content: flex-start;
|
|
.text-grey1 {
|
color: #666;
|
font-size: 26rpx;
|
line-height: 1.5;
|
}
|
|
.text-blue {
|
color: #007AFF;
|
font-size: 26rpx;
|
padding: 8rpx 10rpx;
|
margin: 0 5rpx;
|
display: inline-block;
|
position: relative;
|
z-index: 10;
|
line-height: 1.5;
|
}
|
}
|
}
|
}
|
}
|
}
|
</style>
|