feat:login.vue改为调用PSI登录接口/api/login/255,使用PSI签名+multipart
This commit is contained in:
parent
bec528613c
commit
c2d9c0ed3d
@ -149,6 +149,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { psiLogin } from '@/utils/api.js'
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@ -218,7 +219,7 @@ export default {
|
||||
},
|
||||
|
||||
// 登录处理
|
||||
handleLogin() {
|
||||
async handleLogin() {
|
||||
if (!this.canLogin) {
|
||||
uni.showToast({
|
||||
title: '请填写完整信息并同意隐私协议',
|
||||
@ -239,70 +240,49 @@ export default {
|
||||
mask: true
|
||||
})
|
||||
|
||||
// 构建登录参数(form-data格式)
|
||||
const loginData = {
|
||||
clientId: 'cec96a240989d1c6bcd55f86fca702b7',
|
||||
phoneNumber: this.formData.account,
|
||||
password: this.formData.password
|
||||
}
|
||||
// 调用PSI系统登录接口
|
||||
try {
|
||||
const data = await psiLogin(this.formData.account, this.formData.password)
|
||||
uni.hideLoading()
|
||||
|
||||
// 调用登录API
|
||||
uni.request({
|
||||
url: 'https://api.buzhiyushu.cn/auth/interFaceLogin',
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Authorization': 'Basic ZWxhc3RpYzo1bVJESVVnNTJWQzBmcDE0bnctRg=='
|
||||
},
|
||||
data: loginData,
|
||||
success: (res) => {
|
||||
uni.hideLoading()
|
||||
const response = res.data
|
||||
// 兼容多种响应格式:{ token }, { access_token }, 或直接返回字符串
|
||||
const token = data.token || data.access_token || (typeof data === 'string' ? data : '')
|
||||
if (token) {
|
||||
// 登录成功,保存数据
|
||||
uni.setStorageSync('token', token)
|
||||
uni.setStorageSync('phoneNumber', data.phoneNumber || this.formData.account)
|
||||
uni.setStorageSync('nickName', data.nickName || data.username || data.realname || '')
|
||||
uni.setStorageSync('userId', data.id || data.userId || '')
|
||||
uni.setStorageSync('lastSubmitTime', Date.now())
|
||||
uni.setStorageSync('agreedPrivacy', true)
|
||||
|
||||
// 新接口返回结构:{ code: 200, msg: "操作成功", data: { access_token, userId, ... } }
|
||||
if (response && response.code === 200 && response.data && response.data.access_token) {
|
||||
const data = response.data
|
||||
// 登录成功,保存数据
|
||||
uni.setStorageSync('token', data.access_token)
|
||||
uni.setStorageSync('openId', data.openid || '')
|
||||
uni.setStorageSync('userId', data.userId || '')
|
||||
uni.setStorageSync('phoneNumber', data.phoneNumber || this.formData.account)
|
||||
uni.setStorageSync('nickName', data.nickName || '')
|
||||
uni.setStorageSync('lastSubmitTime', Date.now())
|
||||
uni.setStorageSync('agreedPrivacy', true)
|
||||
uni.showToast({
|
||||
title: '登录成功',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
// 调用商户记录接口
|
||||
if (data.userId) {
|
||||
uni.request({
|
||||
url: `https://go.order.service.buzhiyushu.cn/api/user/insertRecbusinessByUserId?userId=${data.userId}&sort=1`,
|
||||
method: 'GET'
|
||||
})
|
||||
}
|
||||
|
||||
uni.showToast({
|
||||
title: '登录成功',
|
||||
icon: 'success'
|
||||
// 跳转到功能入口页面
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({
|
||||
url: '/pages/entry/entry'
|
||||
})
|
||||
|
||||
// 跳转到功能入口页面
|
||||
setTimeout(() => {
|
||||
uni.redirectTo({
|
||||
url: '/pages/entry/entry'
|
||||
})
|
||||
}, 1500)
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: response.msg || response.message || '登录失败,请检查账号密码',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
uni.hideLoading()
|
||||
uni.showToast({ title: '网络请求失败', icon: 'none' })
|
||||
}, 1500)
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: data.msg || data.message || '登录失败,请检查账号密码',
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
})
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
uni.hideLoading()
|
||||
const errMsg = e.message || String(e)
|
||||
uni.showToast({
|
||||
title: errMsg,
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 保存账号密码到本地
|
||||
|
||||
67
utils/api.js
67
utils/api.js
@ -141,10 +141,74 @@ function generateSimpleSignedUrl(baseUrl, params = {}) {
|
||||
*/
|
||||
function getAuthToken() {
|
||||
const token = uni.getStorageSync('token')
|
||||
console.log('【getAuthToken】读取到token:', token ? (token.substring(0, 30) + '...长度=' + token.length) : '(空)')
|
||||
return token || ''
|
||||
}
|
||||
|
||||
/**
|
||||
* PSI系统登录(role:255管理员登录)
|
||||
* 使用PSI签名机制,通过multipart/form-data提交到PSI登录接口
|
||||
*/
|
||||
export function psiLogin(username, password) {
|
||||
const timestamp = Math.floor(Date.now() / 1000).toString()
|
||||
const params = {
|
||||
about_id: '0',
|
||||
app_key: APP_KEY,
|
||||
client_id: CLIENT_ID,
|
||||
password: password,
|
||||
sign_method: SIGN_METHOD,
|
||||
timestamp: timestamp,
|
||||
type: '1',
|
||||
username: username
|
||||
}
|
||||
|
||||
// 计算签名
|
||||
const sign = calculateSign(params)
|
||||
|
||||
// 构建multipart/form-data body(按key排序与签名一致)
|
||||
const boundary = '----WebKitFormBoundary' + Math.random().toString(36).substring(2, 14)
|
||||
let body = ''
|
||||
const keys = Object.keys(params)
|
||||
sortKeys(keys)
|
||||
for (const key of keys) {
|
||||
body += '--' + boundary + '\r\n'
|
||||
body += 'Content-Disposition: form-data; name="' + key + '"\r\n\r\n'
|
||||
body += params[key] + '\r\n'
|
||||
}
|
||||
body += '--' + boundary + '\r\n'
|
||||
body += 'Content-Disposition: form-data; name="sign"\r\n\r\n'
|
||||
body += sign + '\r\n'
|
||||
body += '--' + boundary + '--'
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: BASE_URL + '/api/login/255',
|
||||
method: 'POST',
|
||||
header: {
|
||||
'Content-Type': 'multipart/form-data; boundary=' + boundary
|
||||
},
|
||||
data: body,
|
||||
success: (res) => {
|
||||
console.log('【PSI登录】响应状态码:', res.statusCode)
|
||||
console.log('【PSI登录】响应数据:', JSON.stringify(res.data))
|
||||
if (res.statusCode === 200 && res.data) {
|
||||
if (res.data.code === 0) {
|
||||
resolve(res.data.data || res.data)
|
||||
} else {
|
||||
reject(new Error(res.data.msg || res.data.message || '登录失败'))
|
||||
}
|
||||
} else {
|
||||
const errMsg = (res.data && (res.data.msg || res.data.message || res.data.error)) || ('请求失败: ' + res.statusCode)
|
||||
reject(new Error(errMsg))
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('【PSI登录】请求失败:', JSON.stringify(err))
|
||||
reject(new Error('网络请求失败'))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 带错误提示的PSI API请求(401时弹窗跳转登录页)
|
||||
*/
|
||||
@ -268,6 +332,7 @@ export function searchBookByIsbn(isbn) {
|
||||
export default {
|
||||
getWarehouseList,
|
||||
getLocationList,
|
||||
psiLogin,
|
||||
searchBookByIsbn,
|
||||
generateSignedUrl
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user