bonus-material-app/src/pages/login/index.vue

132 lines
3.8 KiB
Vue
Raw Normal View History

2024-11-18 09:05:38 +08:00
<template>
<!-- 登录页面 -->
2025-04-28 09:06:26 +08:00
<view class="login" v-if="origin.indexOf('ticket') == -1">
2024-11-18 09:05:38 +08:00
<uni-forms :modelValue="loginForm" label-position="top" class="login-form">
<h1>用户登录</h1>
2024-11-19 09:30:15 +08:00
<uni-forms-item required label="用户名">
2024-11-18 18:28:06 +08:00
<uni-easyinput v-model="loginForm.username" placeholder="请输入用户名" />
2024-11-18 09:05:38 +08:00
</uni-forms-item>
<uni-forms-item required label="密码">
<uni-easyinput
2024-11-18 18:28:06 +08:00
type="password"
2024-11-18 09:05:38 +08:00
placeholder="请输入密码"
2024-11-18 18:28:06 +08:00
v-model="loginForm.password"
/>
2024-11-18 09:05:38 +08:00
</uni-forms-item>
<view class="login-btn" @tap="onHandleLogin">登录</view>
</uni-forms>
</view>
</template>
<script setup>
2024-11-20 10:18:19 +08:00
import { onMounted, reactive, ref } from 'vue'
2025-04-28 09:06:26 +08:00
import { appLoginAPI, getUserInfoAPI, iwsLoginAPI } from '@/services/index.js'
2024-11-18 18:28:06 +08:00
import { useMemberStore } from '@/stores'
2024-11-20 10:38:18 +08:00
const memberStore = useMemberStore()
2025-04-28 09:06:26 +08:00
const origin = window.location.href
2024-11-18 09:05:38 +08:00
2024-11-18 18:28:06 +08:00
// 登录参数
2024-11-18 09:05:38 +08:00
const loginForm = reactive({
2024-11-19 09:30:15 +08:00
username: uni.getStorageSync('username'),
password: uni.getStorageSync('password'),
2024-11-18 18:28:06 +08:00
loginType: 'USERNAME_PASSWORD',
code: '',
phoneUuid: '',
uuid: '',
verificationCode: '',
2024-11-18 09:05:38 +08:00
})
2024-11-19 09:30:15 +08:00
2025-04-28 09:06:26 +08:00
onMounted(async () => {
console.log('🚀 ~ onMounted ~ origin:', origin)
// uni.showToast({
// title: origin,
// icon: 'none',
// duration: 5000,
// })
if (origin.indexOf('ticket') != -1) {
try {
uni.showLoading({ title: '登录中' })
const { data: result } = await iwsLoginAPI({
ticket: origin.split('ticket=')[1].split('#/')[0],
sysType: 1,
})
// 1. 获取 token 并存储
memberStore.setToken(result.access_token)
// 2 . 获取用户信息并存储
const res = await getUserInfoAPI()
memberStore.setUserInfo(res.user)
uni.showToast({ title: '登录成功!', icon: 'none' })
uni.setStorageSync('username', loginForm.username)
uni.setStorageSync('password', loginForm.password)
setTimeout(() => {
uni.switchTab({
url: '/pages/index/index',
})
}, 500)
} catch (error) {
console.log(error)
uni.hideLoading()
showToast('登录失败')
}
}
})
2024-11-18 18:28:06 +08:00
// 登录按钮
const onHandleLogin = async () => {
const res = await appLoginAPI(loginForm)
if (res.code === 200) {
2024-11-19 09:30:15 +08:00
// 1. 获取 token 并存储
2024-11-18 18:28:06 +08:00
memberStore.setToken(res.data.access_token)
2024-11-19 09:30:15 +08:00
// 2 . 获取用户信息并存储
2024-11-18 18:28:06 +08:00
const result = await getUserInfoAPI()
memberStore.setUserInfo(result.user)
uni.showToast({ title: '登录成功!', icon: 'none' })
2024-11-19 09:30:15 +08:00
uni.setStorageSync('username', loginForm.username)
uni.setStorageSync('password', loginForm.password)
2024-11-18 18:28:06 +08:00
setTimeout(() => {
uni.switchTab({
url: '/pages/index/index',
})
}, 500)
}
2024-11-18 09:05:38 +08:00
}
</script>
<style lang="scss" scoped>
.login {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
2024-11-19 09:30:15 +08:00
background-color: #dcf4ff;
2024-11-18 09:05:38 +08:00
.login-form {
width: 90%;
padding: 15rpx;
2024-11-19 09:30:15 +08:00
border: 1px solid #eee;
2024-11-18 09:05:38 +08:00
background-color: #fff;
border-radius: 16rpx;
2024-11-19 09:30:15 +08:00
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
2024-11-18 09:05:38 +08:00
h1 {
text-align: center;
padding: 30rpx 0;
font-size: 32rpx;
font-weight: bold;
}
.login-btn {
padding: 18rpx 0;
text-align: center;
background-color: #1989fa;
color: #fff;
border-radius: 10rpx;
}
}
}
</style>