fix(login): login change
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
"use client"
|
||||
|
||||
import { useUserStore } from "@/app/store/user"
|
||||
import { usePermissionStore } from "@/components/label/store/auth"
|
||||
import { usePathname } from "next/navigation"
|
||||
import { useEffect } from "react"
|
||||
|
||||
export function InfoCheck() {
|
||||
const pathname = usePathname()
|
||||
|
||||
const user_info = useUserStore.getState().user_info
|
||||
const user_id = usePermissionStore.getState().user_id
|
||||
|
||||
useEffect(() => {
|
||||
if (pathname.startsWith("/login")) {
|
||||
return
|
||||
}
|
||||
if (!user_info.account) {
|
||||
if (!user_id) {
|
||||
// tauri 打包后, 使用router.push跳转路由不执行,会导致白屏
|
||||
window.location.href = "/login"
|
||||
}
|
||||
}, [pathname, user_info.account])
|
||||
}, [pathname, user_id])
|
||||
return <></>
|
||||
}
|
||||
|
||||
38
components/label/store/auth.ts
Normal file
38
components/label/store/auth.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { create } from "zustand"
|
||||
import { persist } from "zustand/middleware"
|
||||
|
||||
const initUserInfo = {
|
||||
user_id: null,
|
||||
user_name: null,
|
||||
user_password: null,
|
||||
detailInfo: null,
|
||||
}
|
||||
|
||||
export const usePermissionStore = create(
|
||||
persist<any>(
|
||||
(set) => ({
|
||||
user_id: null,
|
||||
user_name: null,
|
||||
user_password: null,
|
||||
detailInfo: null,
|
||||
setUserPermissionInfo: ({ user_id, user_name, detailInfo }: any) =>
|
||||
set({
|
||||
user_id,
|
||||
user_name,
|
||||
detailInfo,
|
||||
user_password: usePermissionStore.getState().user_password,
|
||||
}),
|
||||
setUserPassword: (user_password: string) =>
|
||||
set((state: any) => {
|
||||
return {
|
||||
...state,
|
||||
user_password,
|
||||
}
|
||||
}),
|
||||
logout: () => set(initUserInfo),
|
||||
}),
|
||||
{
|
||||
name: "label_permission",
|
||||
}
|
||||
)
|
||||
)
|
||||
@@ -4,13 +4,13 @@ import { Login } from "./types"
|
||||
|
||||
const BASE_PATH =
|
||||
process.env.NEXT_PUBLIC_ENV === "production"
|
||||
? "https://basis.soft.cowarobot.com"
|
||||
: "https://basis.softtest.cowarobot.com"
|
||||
? "https://label.soft.cowarobot.com"
|
||||
: "https://label.softtest.cowarobot.com"
|
||||
|
||||
// 获取验证码
|
||||
export const getVerifyCode = (params: { phone: string }) => {
|
||||
return httpFetch<ResultData<{}>>({
|
||||
url: BASE_PATH + `/api/v1/basis/login/get_verify_code`,
|
||||
url: BASE_PATH + `/api/v1/label_server/login/get_verify_code`,
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
})
|
||||
@@ -18,10 +18,10 @@ export const getVerifyCode = (params: { phone: string }) => {
|
||||
|
||||
// 账户登录
|
||||
export const passwdLogin = (params: Login.ReqPasswdLogin) => {
|
||||
return httpFetch<ResultData<Login.ResLogin>>({
|
||||
url: BASE_PATH + `/api/v1/basis/login/passwd`,
|
||||
return httpFetch<Login.ResLabelLogin>({
|
||||
url: BASE_PATH + `/api/v1/label_server/user/login`,
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
body: JSON.stringify({ ...params, name: params.account || params.phone }),
|
||||
isLogin: true,
|
||||
})
|
||||
}
|
||||
@@ -29,7 +29,7 @@ export const passwdLogin = (params: Login.ReqPasswdLogin) => {
|
||||
// 验证码登录
|
||||
export const verificationLogin = (params: Login.ReqVerificationLogin) => {
|
||||
return httpFetch<ResultData<Login.ResLogin>>({
|
||||
url: BASE_PATH + `/api/v1/basis/login/verification`,
|
||||
url: BASE_PATH + `/api/v1/label_server/login/verification`,
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
isLogin: true,
|
||||
@@ -39,7 +39,7 @@ export const verificationLogin = (params: Login.ReqVerificationLogin) => {
|
||||
// 飞书登录
|
||||
export const fetchFeishuLogin = (params: Login.ReqFeishuLogin) => {
|
||||
return httpFetch<ResultData<Login.ResLogin>>({
|
||||
url: BASE_PATH + `/api/v1/basis/login/feishu`,
|
||||
url: BASE_PATH + `/api/v1/label_server/login/feishu`,
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
isLogin: true,
|
||||
@@ -49,7 +49,7 @@ export const fetchFeishuLogin = (params: Login.ReqFeishuLogin) => {
|
||||
// 刷新token
|
||||
export const refreshKey = (params: { token: string }) => {
|
||||
return httpFetch<ResultData<Login.ResRefreshKey>>({
|
||||
url: BASE_PATH + `/api/v1/basis/key/refresh`,
|
||||
url: BASE_PATH + `/api/v1/label_server/user/refresh_token`,
|
||||
method: "POST",
|
||||
body: JSON.stringify(params),
|
||||
isRefresh: true,
|
||||
|
||||
@@ -58,6 +58,19 @@ export namespace Login {
|
||||
refresh_token: string
|
||||
}
|
||||
|
||||
export interface ResLabelLogin {
|
||||
all_projects: Record<string, Array<number>>
|
||||
base_city: string
|
||||
collect_projects: Array<number>
|
||||
group_name: string
|
||||
name: string
|
||||
permissions: Record<string, Record<string, [boolean, any, string]>>
|
||||
refresh_token: string
|
||||
token: string
|
||||
uid: number
|
||||
work_status: number
|
||||
}
|
||||
|
||||
export interface ResRefreshKey {
|
||||
access: string
|
||||
refresh: string
|
||||
|
||||
@@ -33,6 +33,7 @@ import { DigitalIcon } from "./resource/icons/digital"
|
||||
import { useLoginStore } from "./store"
|
||||
import tabClasses from "./Tab.module.css"
|
||||
// import { useSearchParams } from "next/navigation"
|
||||
import { usePermissionStore } from "../label/store/auth"
|
||||
import Feishu from "./feishu/auto-login"
|
||||
import DarkBgImage from "./resource/images/dark-login-bg.png"
|
||||
import LightBgImage from "./resource/images/light-login-bg.png"
|
||||
@@ -176,6 +177,8 @@ export default function LoginPage({ useUserStore }: { useUserStore: any }) {
|
||||
|
||||
const { setUserInfo, refreshToken } = useUserStore()
|
||||
|
||||
const { setUserPermissionInfo } = usePermissionStore()
|
||||
|
||||
// 表单提交处理
|
||||
const handleAccountSubmit = accountForm.onSubmit(
|
||||
async (values) => {
|
||||
@@ -198,11 +201,15 @@ export default function LoginPage({ useUserStore }: { useUserStore: any }) {
|
||||
}
|
||||
try {
|
||||
const res = await passwdLogin(params)
|
||||
const info = res.message
|
||||
setUserInfo(info.tenant, info.user_info, info.rbac)
|
||||
const { uid, name } = res
|
||||
setUserPermissionInfo({
|
||||
user_id: uid,
|
||||
user_name: name,
|
||||
detailInfo: res,
|
||||
})
|
||||
refreshToken({
|
||||
access_token: info.access_token || "",
|
||||
refresh_token: info.refresh_token || "",
|
||||
access_token: res.token || "",
|
||||
refresh_token: res.refresh_token || "",
|
||||
})
|
||||
window.location.href = `${process.env.NEXT_PUBLIC_BASE_PATH}/`
|
||||
} catch (error) {
|
||||
@@ -396,18 +403,18 @@ export default function LoginPage({ useUserStore }: { useUserStore: any }) {
|
||||
className={tabClasses.tab}>
|
||||
账户登录
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab
|
||||
{/* <Tabs.Tab
|
||||
value="phone"
|
||||
ref={setControlRef("phone")}
|
||||
className={""}>
|
||||
手机号登录
|
||||
</Tabs.Tab>
|
||||
<Tabs.Tab
|
||||
</Tabs.Tab> */}
|
||||
{/* <Tabs.Tab
|
||||
value="feishu"
|
||||
ref={setControlRef("feishu")}
|
||||
className={tabClasses.tab}>
|
||||
飞书扫码
|
||||
</Tabs.Tab>
|
||||
</Tabs.Tab> */}
|
||||
<FloatingIndicator
|
||||
target={activeTab ? controlsRefs[activeTab] : null}
|
||||
parent={rootRef}
|
||||
|
||||
@@ -7,9 +7,10 @@ import { cookies } from "next/headers"
|
||||
export const handleLoginData = async (props: {
|
||||
clientIP: string
|
||||
fingerprint: string
|
||||
data: ResultData<Login.ResLogin>
|
||||
data: Login.ResLabelLogin
|
||||
}) => {
|
||||
const { clientIP, fingerprint, data } = props
|
||||
|
||||
const sessionKey = {
|
||||
clientIP,
|
||||
fingerprint: fingerprint,
|
||||
@@ -30,9 +31,9 @@ export const handleLoginData = async (props: {
|
||||
JSON.stringify({
|
||||
clientIP,
|
||||
fingerprint: fingerprint,
|
||||
user_name: data?.message?.user_info?.user_name,
|
||||
access_token: data.message.access_token,
|
||||
refresh_token: data.message.refresh_token,
|
||||
user_name: data?.name,
|
||||
access_token: data.token,
|
||||
refresh_token: data.refresh_token,
|
||||
}),
|
||||
"EX",
|
||||
172800
|
||||
@@ -47,11 +48,9 @@ export const handleLoginData = async (props: {
|
||||
})
|
||||
return {
|
||||
...data,
|
||||
message: {
|
||||
...data.message,
|
||||
access_token: "",
|
||||
refresh_token: "",
|
||||
},
|
||||
token: "",
|
||||
access_token: "",
|
||||
refresh_token: "",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user