164 lines
3.9 KiB
Vue
164 lines
3.9 KiB
Vue
<template>
|
||
<!-- 空白loading页 -->
|
||
<view class="loading-container">
|
||
<view class="loading-text">正在登录中,请稍候...</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
import {
|
||
login
|
||
} from '@/api/login.js'
|
||
|
||
export default {
|
||
onLoad(options) {
|
||
// 页面加载 → 自动执行流程
|
||
this.getToken()
|
||
this.autoLogin(options)
|
||
},
|
||
|
||
methods: {
|
||
getToken() {
|
||
|
||
return new Promise((resolve, reject) => {
|
||
uni.request({
|
||
url: '/epoint-web-cs/rest/oauth2/token',
|
||
method: 'POST',
|
||
header: {
|
||
'Content-Type': 'application/x-www-form-urlencoded',
|
||
},
|
||
data: {
|
||
client_id: '316f983b-2b93-4c57-b912-c00e05aceed2', // 你接口需要的参数名
|
||
client_secret: 'cd2cf239-1506-47bb-9b63-507b6b759a06',
|
||
grant_type: 'password',
|
||
platform: 'mobile',
|
||
username: '马贤宝',
|
||
password: 'Epoint@123',
|
||
// 其他需要的参数继续加
|
||
},
|
||
success: (res) => {
|
||
const data = res.data
|
||
console.log(data, 'data')
|
||
|
||
// 接口返回成功判断(根据接口标准结构)
|
||
if (data.status.code === 1) {
|
||
resolve(data.custom) // 返回用户信息:name, phone, idCard
|
||
} else {
|
||
reject(new Error(data.status.text || '获取用户信息失败'))
|
||
}
|
||
}
|
||
})
|
||
})
|
||
},
|
||
/**
|
||
* 自动登录流程
|
||
*/
|
||
async autoLogin(options) {
|
||
try {
|
||
// 1. 从URL获取token
|
||
const token = options.token
|
||
if (!token) {
|
||
uni.showToast({
|
||
title: '未获取到登录凭证',
|
||
icon: 'none'
|
||
})
|
||
return
|
||
}
|
||
|
||
// 2. 使用token 请求【获取用户信息接口】
|
||
const userInfo = await this.getUserInfo(token)
|
||
|
||
|
||
// 4. 执行你系统的登录逻辑(保存信息)
|
||
this.doLocalLogin(userInfo)
|
||
|
||
// 5. 登录成功 → 跳首页
|
||
// uni.switchTab({
|
||
// url: '/pages/home/home' // 你的首页路径
|
||
// })
|
||
|
||
} catch (err) {
|
||
console.error('登录失败:', err)
|
||
uni.showToast({
|
||
title: err.message || '登录失败,请重试',
|
||
icon: 'none',
|
||
duration: 3000
|
||
})
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 请求接口:获取用户信息(你给的正式接口)
|
||
* @param {string} token
|
||
*/
|
||
getUserInfo(token) {
|
||
return new Promise((resolve, reject) => {
|
||
let url = '/epoint-web-cs/rest/wechatloginrest/getuserinfo'
|
||
|
||
// if (process.env.NODE_ENV === 'development') {
|
||
// // 开发环境接口地址
|
||
// url = 'epoint-web-cs/rest/wechatloginrest/getuserinfo'
|
||
// } else if (process.env.NODE_ENV === 'production') {
|
||
// // 正式环境接口地址
|
||
// url = 'epoint-web/rest/wechatloginrest/getuserinfo'
|
||
// }
|
||
|
||
uni.request({
|
||
url: url,
|
||
method: 'POST',
|
||
header: {
|
||
'Content-Type': 'application/x-www-form-urlencoded',
|
||
'authorization': 'Bearer ' + token // 按接口要求携带token
|
||
},
|
||
success: (res) => {
|
||
const data = res.data
|
||
|
||
// 接口返回成功判断(根据接口标准结构)
|
||
if (data.status.code === 1) {
|
||
resolve(data.custom) // 返回用户信息:name, phone, idCard
|
||
} else {
|
||
reject(new Error(data.status.text || '获取用户信息失败'))
|
||
}
|
||
}
|
||
})
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 你系统本地登录:缓存token + 用户信息
|
||
*/
|
||
doLocalLogin(userInfo) {
|
||
login({
|
||
// username: userInfo.idnumber,
|
||
username: userInfo.mobile,
|
||
|
||
grant_type: "third"
|
||
})
|
||
.then(data => {
|
||
this.$u.func.login(data, '/pages/home/home')
|
||
})
|
||
.catch(err => {
|
||
this.$u.func.showToast({
|
||
title: '用户名或密码错误'
|
||
})
|
||
})
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style scoped>
|
||
.loading-container {
|
||
width: 100%;
|
||
height: 100vh;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
background: #fff;
|
||
}
|
||
|
||
.loading-text {
|
||
font-size: 28rpx;
|
||
color: #666;
|
||
}
|
||
</style> |