Otsuka-APP/pages/login.vue

380 lines
10 KiB
Vue
Raw Normal View History

2025-08-19 17:07:46 +08:00
<template>
2026-03-16 12:03:48 +08:00
<view class="normal-login-container">
<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">大冢APS Ultra管理平台</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="passwordVisible ? 'text' : 'password'" class="input" placeholder="请输入密码" maxlength="20" />
<view class="password-eye" @click="togglePasswordVisible">
<uni-icons :type="passwordVisible ? 'eye-slash' : 'eye'" size="20" color="#999"></uni-icons>
</view>
2026-03-16 12:03:48 +08:00
</view>
<view class="input-item flex align-center" style="width: 60%;margin: 0px;" 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"></image>
2025-08-19 17:07:46 +08:00
</view>
2026-03-16 12:03:48 +08:00
</view>
<view class="action-btn">
<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
</view>
<view class="reg text-center" v-if="register">
<text class="text-grey1">没有账号</text>
<text @click="handleUserRegister" class="text-blue">立即注册</text>
</view>
2025-08-19 17:07:46 +08:00
</view>
2026-03-16 12:03:48 +08:00
</view>
2025-08-19 17:07:46 +08:00
</template>
<script setup>
2026-03-16 12:03:48 +08:00
import { ref, getCurrentInstance } from "vue"
import { onLoad } from "@dcloudio/uni-app"
2026-03-23 13:07:22 +08:00
import { getToken, setToken } from '@/utils/auth'
import { getCodeImg, autoLogin, autobind } from '@/api/login'
2026-03-16 12:03:48 +08:00
import { useConfigStore, useUserStore } from '@/store'
const { proxy } = getCurrentInstance()
const globalConfig = useConfigStore().config
const codeUrl = ref("")
// 验证码开关
const captchaEnabled = ref(true)
// 用户注册开关
const register = ref(false)
// 密码可见性
const passwordVisible = ref(false)
2026-03-23 13:07:22 +08:00
// 存储从URL获取的weixinId
const weixinId = ref('')
// 防止重复自动登录
let autoLoginExecuted = false
// 标记是否已经清除过URL参数
let urlCleared = false
2026-03-16 12:03:48 +08:00
const loginForm = ref({
2025-08-19 17:07:46 +08:00
username: "admin",
2026-03-23 13:07:22 +08:00
password: "jm@9527",
2025-08-19 17:07:46 +08:00
code: "",
uuid: ""
2026-03-16 12:03:48 +08:00
})
2025-08-19 17:07:46 +08:00
2026-03-16 12:03:48 +08:00
// 切换密码可见性
function togglePasswordVisible() {
passwordVisible.value = !passwordVisible.value
}
// 用户注册
function handleUserRegister() {
2025-08-19 17:07:46 +08:00
proxy.$tab.redirectTo(`/pages/register`)
2026-03-16 12:03:48 +08:00
}
2025-08-19 17:07:46 +08:00
2026-03-16 12:03:48 +08:00
// 获取图形验证码
function getCode() {
2025-08-19 17:07:46 +08:00
getCodeImg().then(res => {
2026-03-16 12:03:48 +08:00
captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
2025-08-19 17:07:46 +08:00
if (captchaEnabled.value) {
2026-03-16 12:03:48 +08:00
codeUrl.value = 'data:image/gif;base64,' + res.img
loginForm.value.uuid = res.uuid
2025-08-19 17:07:46 +08:00
}
})
2026-03-16 12:03:48 +08:00
}
2025-08-19 17:07:46 +08:00
2026-03-16 12:03:48 +08:00
// 登录方法
async function handleLogin() {
2025-08-19 17:07:46 +08:00
if (loginForm.value.username === "") {
2026-03-16 12:03:48 +08:00
proxy.$modal.msgError("请输入账号")
2025-08-19 17:07:46 +08:00
} else if (loginForm.value.password === "") {
2026-03-16 12:03:48 +08:00
proxy.$modal.msgError("请输入密码")
2025-08-19 17:07:46 +08:00
} else if (loginForm.value.code === "" && captchaEnabled.value) {
2026-03-16 12:03:48 +08:00
proxy.$modal.msgError("请输入验证码")
2025-08-19 17:07:46 +08:00
} else {
2026-03-16 12:03:48 +08:00
proxy.$modal.loading("登录中,请耐心等待...")
pwdLogin()
2025-08-19 17:07:46 +08:00
}
2026-03-16 12:03:48 +08:00
}
2025-08-19 17:07:46 +08:00
2026-03-23 13:07:22 +08:00
// 密码登录有微信ID时调用autobind无微信ID时调用普通登录
2026-03-16 12:03:48 +08:00
async function pwdLogin() {
2026-03-23 13:07:22 +08:00
// 如果有微信ID调用绑定接口
if (weixinId.value) {
const params = {
username: loginForm.value.username,
password: loginForm.value.password,
code: loginForm.value.code,
uuid: loginForm.value.uuid,
weixinId: weixinId.value
2026-03-16 12:03:48 +08:00
}
2026-03-23 13:07:22 +08:00
autobind(params).then(res => {
proxy.$modal.closeLoading()
if (res && res.code === 200) {
// 保存token
if (res.data && res.data.token) {
setToken(res.data.token)
}
proxy.$modal.msgSuccess("绑定成功,登录中...")
// 绑定成功后清除URL参数并跳转
removeWeixinIdFromUrl()
setTimeout(() => {
loginSuccess()
}, 1500)
} else {
proxy.$modal.msgError(res.msg || "绑定失败,请重试")
if (captchaEnabled.value) {
getCode()
}
}
}).catch(() => {
proxy.$modal.closeLoading()
if (captchaEnabled.value) {
getCode()
}
})
} else {
// 普通密码登录
useUserStore().login(loginForm.value).then(() => {
proxy.$modal.closeLoading()
loginSuccess()
}).catch(() => {
proxy.$modal.closeLoading()
if (captchaEnabled.value) {
getCode()
}
})
}
2026-03-16 12:03:48 +08:00
}
2025-08-19 17:07:46 +08:00
2026-03-16 12:03:48 +08:00
// 登录成功后,处理函数
2026-03-23 13:07:22 +08:00
function loginSuccess() {
// 设置用户信息并跳转
useUserStore().getInfo().then(() => {
proxy.$tab.reLaunch('/pages/work/index')
}).catch(() => {
// 即使获取用户信息失败,也尝试跳转
2026-03-16 12:03:48 +08:00
proxy.$tab.reLaunch('/pages/work/index')
2025-08-19 17:07:46 +08:00
})
2026-03-16 12:03:48 +08:00
}
2025-08-19 17:07:46 +08:00
2026-03-23 13:07:22 +08:00
// 从URL中获取weixinId参数提取?weixinId=后面所有的内容,包括#
function getWeixinIdFromUrl() {
// #ifdef H5
const currentUrl = window.location.href
// 使用正则表达式匹配 weixinId 参数,提取后面所有内容(包括#和后面的所有字符)
const match = currentUrl.match(/[?&]weixinId=(.*)/)
if (match && match[1]) {
return decodeURIComponent(match[1])
}
return null
// #endif
// #ifndef H5
return null
// #endif
}
// 清除URL中的weixinId参数
function removeWeixinIdFromUrl() {
if (urlCleared) return
// #ifdef H5
try {
const currentUrl = window.location.href
if (currentUrl.includes('weixinId=')) {
const url = new URL(currentUrl)
url.searchParams.delete('weixinId')
window.history.replaceState({}, '', url.toString())
urlCleared = true
}
} catch (e) {
console.error('清除URL参数失败', e)
}
// #endif
}
2026-03-16 12:03:48 +08:00
onLoad(() => {
2025-08-19 17:07:46 +08:00
//#ifdef H5
2026-03-23 13:07:22 +08:00
// 检查是否已登录
const token = getToken()
if (token) {
proxy.$tab.reLaunch('/pages/work/index')
return
2025-08-19 17:07:46 +08:00
}
2026-03-23 13:07:22 +08:00
// 防止重复执行自动登录
if (autoLoginExecuted) {
return
}
// 获取URL中的weixinId
const id = getWeixinIdFromUrl()
if (id) {
// 保存weixinId
weixinId.value = id
autoLoginExecuted = true
// 先清除URL参数防止跳转后再次触发
removeWeixinIdFromUrl()
// 调用自动登录接口
proxy.$modal.loading("微信自动登录中...")
const bodydata = {
weixinId: id
}
autoLogin(bodydata).then(res => {
proxy.$modal.closeLoading()
// 根据返回结果处理
if (res && res.code === 200) {
// 保存token
if (res.data && res.data.token) {
setToken(res.data.token)
}
proxy.$modal.msgSuccess("登录成功")
// 获取用户信息并跳转
useUserStore().getInfo().then(() => {
proxy.$tab.reLaunch('/pages/work/index')
}).catch(() => {
// 即使获取用户信息失败只要有token就尝试跳转
if (getToken()) {
proxy.$tab.reLaunch('/pages/work/index')
} else {
proxy.$modal.msgError("登录异常,请使用账号密码登录")
getCode()
}
})
} else {
proxy.$modal.msgWarning(res?.msg || "请绑定账号后登录")
// 未绑定,显示普通登录界面
getCode()
}
}).catch(error => {
proxy.$modal.closeLoading()
console.error('自动登录错误:', error)
proxy.$modal.msgError(error.message || "自动登录失败,请使用账号密码登录")
// 自动登录失败,显示普通登录界面
getCode()
})
} else {
// 没有微信ID显示普通登录界面
getCode()
}
//#endif
//#ifndef H5
// 非H5环境直接获取验证码
getCode()
2025-08-19 17:07:46 +08:00
//#endif
2026-03-16 12:03:48 +08:00
})
2026-03-23 13:07:22 +08:00
2025-08-19 17:07:46 +08:00
</script>
<style lang="scss" scoped>
2026-03-16 12:03:48 +08:00
page {
2025-08-19 17:07:46 +08:00
background-color: #ffffff;
2026-03-16 12:03:48 +08:00
}
2025-08-19 17:07:46 +08:00
2026-03-16 12:03:48 +08:00
.normal-login-container {
2025-08-19 17:07:46 +08:00
width: 100%;
.logo-content {
2026-03-16 12:03:48 +08:00
width: 100%;
font-size: 21px;
text-align: center;
padding-top: 15%;
image {
border-radius: 4px;
}
.title {
margin-left: 10px;
}
2025-08-19 17:07:46 +08:00
}
.login-form-content {
2026-03-16 12:03:48 +08:00
text-align: center;
margin: 20px auto;
margin-top: 15%;
width: 80%;
2026-03-16 12:03:48 +08:00
.input-item {
margin: 20px auto;
background-color: #f5f6f7;
height: 45px;
border-radius: 20px;
position: relative;
.icon {
font-size: 38rpx;
margin-left: 10px;
color: #999;
}
2026-03-16 12:03:48 +08:00
.input {
width: 100%;
font-size: 14px;
line-height: 20px;
text-align: left;
padding-left: 15px;
2026-03-23 13:07:22 +08:00
padding-right: 45px;
2025-08-19 17:07:46 +08:00
}
2026-03-16 12:03:48 +08:00
.password-eye {
position: absolute;
right: 15px;
top: 50%;
transform: translateY(-50%);
z-index: 10;
display: flex;
align-items: center;
justify-content: center;
width: 30px;
height: 30px;
&::after {
content: '';
position: absolute;
top: -10px;
left: -10px;
right: -10px;
bottom: -10px;
}
2025-08-19 17:07:46 +08:00
}
2026-03-16 12:03:48 +08:00
}
.login-btn {
margin-top: 40px;
height: 45px;
}
.reg {
margin-top: 15px;
}
.xieyi {
color: #333;
margin-top: 20px;
}
.login-code {
height: 38px;
float: right;
.login-code-img {
height: 38px;
position: absolute;
margin-left: 10px;
width: 200rpx;
2025-08-19 17:07:46 +08:00
}
2026-03-16 12:03:48 +08:00
}
2025-08-19 17:07:46 +08:00
}
2026-03-16 12:03:48 +08:00
}
</style>