登录调整

This commit is contained in:
weiwei committed 2025-04-25 12:31:49 +08:00
1 parent 6d56a5a8c7
commit 4f480a9927
4 files changed
+66 -111

No files matched your search

+14 -17
View File
@@ -2,18 +2,12 @@
<crud-layout bg="/static/page/blue-bg.png">
<view class="h-20rpx" />
<view class="flex-row items-center">
<image
v-if="state.userInfo?.avatar"
class="w-100rpx h-100rpx rounded-full"
:src="filePath(state.userInfo.sysUser.avatar)"
mode="aspectFill"
/>
<image v-if="userInfo?.avatar" class="w-100rpx h-100rpx rounded-full" :src="userInfo.avatarUrl" mode="aspectFill" />
<image v-else class="w-100rpx h-100rpx rounded-full" src="/static/icon/default-avatar.png" mode="aspectFill" />
<view class="m-l-30rpx" v-if="state.userInfo?.sysUser?.userId">
<view class="m-l-30rpx" v-if="token">
<view class="flex-row items-center">
<text class="sub-text-30">{{ state.userInfo.nickName }}</text>
<text class="sub-text-30">{{ userInfo.nickName }}</text>
</view>
<text class="sub-text-26">ID {{ state.userInfo?.user_id }}</text>
</view>
<view v-else class="flex-row items-center m-l-30rpx" @tap="onTabLogin">
<text class="sub-text-32">请先登录</text>
@@ -31,14 +25,12 @@
</template>
<script setup lang="ts">
import { reactive } from "vue"
import { onLoad } from "@dcloudio/uni-app"
import { ref } from "vue"
import { onLoad, onShow } from "@dcloudio/uni-app"
import { isEmpty } from "radash"
import { filePath } from "@@/utils/file"
const state = reactive<any>({
userInfo: {},
})
const userInfo = ref<any>({})
const token = ref("")
// 获取存储的用户信息
const getUserInfo = () => {
@@ -46,9 +38,11 @@ const getUserInfo = () => {
const userStorage = uni.getStorageSync("user_info")
if (!isEmpty(userStorage) && !isEmpty(tokenStorage)) {
state.userInfo = userStorage
userInfo.value = userStorage
token.value = tokenStorage
} else {
state.userInfo = {}
userInfo.value = {}
token.value = ""
}
}
@@ -67,4 +61,7 @@ const onTabMenu = () => {
onLoad(() => {
getUserInfo()
})
onShow(() => {
getUserInfo()
})
</script>
+22 -75
View File
@@ -1,34 +1,28 @@
<template>
<crud-layout title="登录" bg="/static/page/blue-bg.png">
<view class="card p-x-30rpx p-y-40rpx my-5">
<tm-form ref="applyFormRef" v-model="applyForm" :rules="applyFormRules" direction="vertical">
<tm-form-item name="username" label="用户名" :showBottom="false">
<tm-input v-model="applyForm.username" placeholder="请输入用户名..." />
</tm-form-item>
<view class="h-10rpx" />
<tm-form-item name="password" label="密码" :showBottom="false">
<tm-input password showEye v-model="applyForm.password" placeholder="请输入用户密码..." />
</tm-form-item>
</tm-form>
</view>
<view v-if="!isDevEnv()" class="card p-x-30rpx p-y-40rpx my-5 m-4">
<tm-form ref="applyFormRef" v-model="applyForm" :rules="applyFormRules" direction="vertical">
<tm-form-item name="username" label="用户名" :showBottom="false">
<tm-input v-model="applyForm.username" placeholder="请输入用户名..." />
</tm-form-item>
<view class="h-10rpx" />
<tm-form-item name="password" label="密码" :showBottom="false">
<tm-input password showEye v-model="applyForm.password" placeholder="请输入用户密码..." />
</tm-form-item>
</tm-form>
<tm-button block @tap="doSubmit">登录</tm-button>
</view>
<button
style="background-color: #0779fd; border-radius: 24rpx; color: white; font-size: 32rpx; margin: 20rpx 0; padding: 4rpx 0"
open-type="getUserInfo"
@click="getUserInfo"
>
微信登录测试
</button>
</crud-layout>
<button class="bg-#0779fd rounded-2 c-white text-lg py-2 mx-4 my-1/2" open-type="getUserInfo" @click="getUserInfo">微信登录</button>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue"
import { constant, router } from "@@/index"
import { userApi } from "@@/api"
import request from "@/sheep/utils/request"
import { encryptionBase64 } from "@/sheep/utils/encryption"
import isDevEnv from "@/sheep/utils/isDevEnv"
const tokenKey = constant.storageConstant.TOKEN
const userInfoKey = constant.storageConstant.USERINFO
let applyForm = reactive<any>({
username: "",
@@ -45,9 +39,6 @@ const doSubmit = async () => {
const { isPass } = applyFormRef.value.validate()
if (!isPass) return
const tokenKey = constant.storageConstant.TOKEN
const userInfoKey = constant.storageConstant.USERINFO
uni.removeStorageSync(tokenKey)
uni.showLoading({ title: "登录中...", mask: true })
const res: any = await userApi.login({
@@ -71,40 +62,14 @@ const doSubmit = async () => {
const getUserInfo = () => {
uni.getUserInfo({
success: (res) => {
console.log(res)
// 存储用户信息
uni.setStorageSync("user_info", res.userInfo)
uni.setStorageSync(userInfoKey, res.userInfo)
// 微信登录
login()
},
fail: (res) => {
return uni.showToast({ title: "API调用失败", icon: "error" })
},
})
}
const basicAuth = "Basic " + encryptionBase64(import.meta.env.VITE_OAUTH2_APP_CLIENT)
const mnpLogin = (code: string) => {
const form = {
mobile: `APP-MINI@${code}`,
code: code,
grant_type: "mobile",
scope: "server",
}
return request({
url: "/auth/oauth2/token",
method: "POST",
params: form,
header: {
Authorization: basicAuth, //"mini:mini"
"content-type": "application/x-www-form-urlencoded",
},
custom: {
showLoading: false,
withToken: false,
return uni.showToast({ title: "获取用户信息失败", icon: "error" })
},
})
}
@@ -113,33 +78,15 @@ const mnpLogin = (code: string) => {
const login = async () => {
const result = await uni.login({ provider: "weixin" }).catch((error) => error)
if (result.errMsg === "login:ok") {
const params = {
mobile: "MINI@" + result.code,
code: result.code,
}
console.log("code", result.code)
const res: any = await mnpLogin(result.code)
// const res: any = await request({
// url: "/auth/oauth2/token?grant_type=mobile",
// method: "post",
// data: params,
// header: {
// Authorization: "Basic bWluaTptaW5p",
// "Content-Type": "application/x-www-form-urlencoded",
// },
// })
console.log("res", res)
const res: any = await userApi.mnpLogin(result.code)
if (res.code == 0) {
uni.setStorageSync("token", res.data.access_token)
uni.setStorageSync(tokenKey, res.data)
router.back()
return uni.showToast({ title: "登录成功", icon: "none" })
} else {
return uni.showToast({ title: "获取token失败,登录失败", icon: "error" })
return uni.showToast({ title: "获取token失败", icon: "error" })
}
} else {
uni.showToast({ title: "获取登录code失败", icon: "error" })
+26 -4
View File
@@ -1,8 +1,8 @@
import request from "@/sheep/utils/request"
import { encryption, encryptionBase64 } from "@/sheep/utils/encryption"
// 将 Basic Auth 字符串提取为常量
const BASIC_AUTH = "Basic " + encryptionBase64(import.meta.env.VITE_OAUTH2_APP_CLIENT)
// 加密
const basicAuth = "Basic " + encryptionBase64(import.meta.env.VITE_OAUTH2_APP_CLIENT)
// 定义接口类型
interface LoginParams {
@@ -65,7 +65,7 @@ export default {
// 登录
login: (params: LoginParams) => {
const { username, password, randomStr, code, grant_type, scope } = params
const encPassword = encryption(password, import.meta.env.VITE_PWD_ENC_KEY)
// const encPassword = encryption(password, import.meta.env.VITE_PWD_ENC_KEY)
return request({
url: `/auth/oauth2/token`,
@@ -81,7 +81,7 @@ export default {
data: { password },
header: {
skipToken: true,
Authorization: BASIC_AUTH,
Authorization: basicAuth,
"content-type": "application/x-www-form-urlencoded",
},
custom: {
@@ -98,4 +98,26 @@ export default {
showLoading: false,
},
}),
mnpLogin: (code: string) => {
const form = {
mobile: `APP-MINI@${code}`,
code: code,
grant_type: "mobile",
scope: "server",
}
return request({
url: "/auth/oauth2/token",
method: "POST",
params: form,
header: {
Authorization: basicAuth,
"content-type": "application/x-www-form-urlencoded",
},
custom: {
showLoading: false,
withToken: false,
},
})
},
}
+4 -15
View File
@@ -30,14 +30,6 @@ function showLoading(message: string) {
}
}
// 读取token并处理Authorization请求头
const getAuthHeader = (tokenStorage: any) => {
// if (isDevEnv()) {
return tokenStorage?.access_token ? `Bearer ${tokenStorage.access_token}` : null
// }
// return tokenStorage?.accessToken?.tokenValue ? `Bearer ${tokenStorage.accessToken.tokenValue}` : null
}
// 处理错误响应
function handleErrorResponse(response: any) {
if (!response.config?.custom?.showError) return
@@ -121,13 +113,10 @@ http.interceptors.request.use(async (config) => {
if (needAuth) {
if (!isEmpty(tokenStorage)) {
// token过期刷新检查
// await checkAndRefreshToken(tokenStorage)
// const authHeader = getAuthHeader(tokenStorage)
// if (authHeader) {
config_header.Authorization = uni.getStorageSync("token")
// }
const authHeader = tokenStorage?.access_token ? `Bearer ${tokenStorage.access_token}` : null
if (authHeader) {
config_header.Authorization = authHeader
}
} else {
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" })
router.jump("/pages/test/login")