Files
labelmain/components/label/store/auth.ts
2026-03-16 15:54:57 +08:00

415 lines
11 KiB
TypeScript

import dayjs from "dayjs"
import { create } from "zustand"
import { persist } from "zustand/middleware"
import { storage } from "../store"
import { ALL_CODE } from "../utils/constants"
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",
}
)
)
export interface Option {
label: string
value: any
}
interface UserStoreProps {
userOpts: Option[]
treeData: any[]
adminOpts: Option[]
needUpdate: boolean
setUserOpts: (opts: Option[]) => any
setTreeData: (opts: any[]) => any
setAdminOpts: (opts: Option[]) => any
setFlag: (f: boolean) => void
}
export const useAllUserStore = create<UserStoreProps>((set) => ({
userOpts: [],
treeData: [],
adminOpts: [],
needUpdate: true,
setUserOpts: (opts: Option[]) =>
set((state: any) => ({ ...state, userOpts: opts })),
setTreeData: (opts: any[]) =>
set((state: any) => ({ ...state, treeData: opts })),
setAdminOpts: (opts: Option[]) =>
set((state: any) => ({ ...state, adminOpts: opts })),
setFlag: (flag) => set((state: any) => ({ ...state, needUpdate: flag })),
}))
/* ******************* Project *********************** */
type paramsType = "all" | "collect" | "dynamic"
export const initAllParams = {
page_number: 1,
page_size: 15,
status: ALL_CODE,
}
export const initCollectParams = {
page_number: 1,
page_size: 15,
status: ALL_CODE,
is_collect: 1,
}
export const initDynamicParams = {
page_number: 1,
page_size: 15,
status: ALL_CODE,
is_dynamic: 1,
}
export const useProjectStore = create((set) => ({
allParams: initAllParams,
allTotal: 0,
collectParams: initCollectParams,
collectTotal: 0,
dynamicParams: initDynamicParams,
dynamicTotal: 0,
setSearchParams: (params: any, type: paramsType) =>
set((state: any) => {
let obj = { ...state }
if (type === "all") {
obj.allParams = params
} else if (type === "collect") {
obj.collectParams = params
} else if (type === "dynamic") {
obj.dynamicParams = params
}
return obj
}),
setTotal: (total: number, type: paramsType) =>
set((state: any) => {
let obj = { ...state }
if (type === "all") {
obj.allTotal = total
} else if (type === "collect") {
obj.collectTotal = total
} else if (type === "dynamic") {
obj.dynamicTotal = total
}
return obj
}),
resetParams: (type: paramsType) =>
set((state: any) => {
let obj = { ...state }
if (type === "all") {
obj.allParams = initAllParams
} else if (type === "collect") {
obj.collectParams = initCollectParams
} else if (type === "dynamic") {
obj.dynamicParams = initDynamicParams
}
return obj
}),
}))
/* ******************* Task *********************** */
interface TaskStore {
params: any
total: number
setParams: (params: any) => void
resetParams: () => void
setPagination: (page: number, size: number) => void
setTotal: (total: number) => void
}
export const initTaskParams = {
page_number: 1,
page_size: 20,
}
export const useOwnTaskStore = create<TaskStore>((set) => ({
params: initTaskParams,
total: 0,
setParams: (params: any) => set((state: any) => ({ ...state, params })),
resetParams: () =>
set((state: any) => ({ ...state, params: initTaskParams })),
setPagination: (p, s) =>
set((state: any) => ({
...state,
params: { ...state.params, page_number: p, page_size: s },
})),
setTotal: (total: number) => set((state: any) => ({ ...state, total })),
}))
export const useAllTaskStore = create<TaskStore>((set) => ({
params: initTaskParams,
total: 0,
setParams: (params: any) => set((state: any) => ({ ...state, params })),
resetParams: () =>
set((state: any) => ({ ...state, params: initTaskParams })),
setPagination: (p, s) =>
set((state: any) => ({
...state,
params: { ...state.params, page_number: p, page_size: s },
})),
setTotal: (total: number) => set((state: any) => ({ ...state, total })),
}))
/* ******************* Scheme *********************** */
// 处理标注方案数据结构
const handlePlanData = (data: any) => {
const id = data.length
let fieldsObj: any = {}
const list: any = []
data.forEach((item: any, index: number) => {
const { label_class, color, label_type, radius, sub_attributes_describe } =
item
const singleClass: any = {
[`class${index}-label_class`]: label_class,
[`class${index}-color`]: color.join("_"),
[`class${index}-label_type`]: label_type,
[`class${index}-radius`]: radius,
}
const sub_count = sub_attributes_describe.length
const sub_attr: string[] = []
sub_attributes_describe.forEach((subItem: any, subIndex: number) => {
const { chinese_name, select_mode, round_id, isrequired, optional_item } =
subItem
sub_attr.push(`sub${subIndex}`)
singleClass[`class${index}-sub${subIndex}-sub_attributes`] = chinese_name
singleClass[`class${index}-sub${subIndex}-select_mode`] = select_mode
singleClass[`class${index}-sub${subIndex}-round_id`] = round_id
singleClass[`class${index}-sub${subIndex}-isrequired`] = isrequired
singleClass[`class${index}-sub${subIndex}-optional_item`] = optional_item
})
if (sub_count === 0)
list.push({ name: `class${index}`, sub_attr: ["sub0"], sub_count })
else list.push({ name: `class${index}`, sub_attr, sub_count })
fieldsObj = { ...fieldsObj, ...singleClass }
})
return { id, fieldsObj, list }
}
// scheme Store
export const useSchemeStore = create((set) => ({
// 首页搜索
searchParams: {
current_page: 1,
page_size: 20,
},
total: 0,
setSearchParams: (params: any) =>
set((state: any) => ({ ...state, searchParams: params })),
setTotal: (total: number) =>
set((state: any) => ({ ...state, total: total })),
// 方案版本数据
nowVersionPlan: [],
nowVersionFormData: {},
nowVersionFormId: 0,
nowVersionFormLabel: [
{ name: `class${0}`, sub_attr: ["sub0"], sub_count: 0 },
],
selectPlan: (obj: any, data: any) => {
const { id, fieldsObj, list } = handlePlanData(data)
set((state: any) => ({
...state,
nowVersionPlan: data,
nowVersionFormId: id,
nowVersionFormData: { ...fieldsObj, ...obj },
nowVersionFormLabel: list.length
? list
: [{ name: `class${0}`, sub_attr: ["sub0"], sub_count: 0 }],
}))
},
resetPlan: () =>
set((state: any) => ({
...state,
nowVersionPlan: [],
nowVersionFormData: {},
nowVersionFormId: 0,
nowVersionFormLabel: [
{ name: `class${0}`, sub_attr: ["sub0"], sub_count: 0 },
],
})),
}))
/* ******************* 标注页面返回跳转 *********************** */
// 返回跳转
export const useBackUrlStore = create(
persist<any>(
(set) => ({
backUrl: null,
backTitle: null,
setBackProps: (url: string, title?: string) =>
set({ backUrl: url, backTitle: title || null }),
}),
{
name: "back_url",
}
)
)
/* ******************* 数据统计 *********************** */
interface WorkLoadDataProps {
all: any[]
order_by_group_name: any[]
order_by_project_name: any[]
order_by_project_type: any[]
order_by_user_name: any[]
}
type dimensionType =
| "all"
| "order_by_group_name"
| "order_by_project_name"
| "order_by_project_type"
| "order_by_user_name"
interface WorkloadStore {
labelData: WorkLoadDataProps
reviewData: WorkLoadDataProps
review2Data: WorkLoadDataProps
filters: {
label: [any, any]
review1: [any, any]
review2: [any, any]
}
dimensions: {
label: dimensionType
review1: dimensionType
review2: dimensionType
}
setLabelData: (data: WorkLoadDataProps) => void
setReviewData: (data: WorkLoadDataProps) => void
setReview2Data: (data: WorkLoadDataProps) => void
setFilters: (key: "label" | "review1" | "review2", data: [any, any]) => void
setDimensions: (
key: "label" | "review1" | "review2",
data: dimensionType
) => void
}
export const dimensionOpts = [
{ label: "全部", value: "all" },
{ label: "用户名", value: "order_by_user_name" },
{ label: "用户组", value: "order_by_group_name" },
{ label: "项目名称", value: "order_by_project_name" },
{ label: "项目类型", value: "order_by_project_type" },
]
export const useWorkloadStore = create(
persist<WorkloadStore>(
(set) => ({
labelData: {
all: [],
order_by_group_name: [],
order_by_project_name: [],
order_by_project_type: [],
order_by_user_name: [],
},
reviewData: {
all: [],
order_by_group_name: [],
order_by_project_name: [],
order_by_project_type: [],
order_by_user_name: [],
},
review2Data: {
all: [],
order_by_group_name: [],
order_by_project_name: [],
order_by_project_type: [],
order_by_user_name: [],
},
filters: {
label: [dayjs().subtract(1, "day"), dayjs().subtract(1, "day")],
review1: [dayjs().subtract(1, "day"), dayjs().subtract(1, "day")],
review2: [dayjs().subtract(1, "day"), dayjs().subtract(1, "day")],
},
dimensions: {
label: "all",
review1: "all",
review2: "all",
},
setLabelData: (data) => set((state) => ({ ...state, labelData: data })),
setReviewData: (data) => set((state) => ({ ...state, reviewData: data })),
setReview2Data: (data) =>
set((state) => ({ ...state, review2Data: data })),
setFilters: (key, data) =>
set((state) => ({
...state,
filters: { ...state.filters, [key]: data },
})),
setDimensions: (key, data) =>
set((state) => ({
...state,
dimensions: { ...state.dimensions, [key]: data },
})),
}),
{
name: "workload",
storage: storage,
}
)
)
interface TaskDataStore {
taskData: any[]
filters: {
page_size: number
project_type?: string[]
project_id?: number[]
label_status?: number[]
current_uid?: number[]
label_user?: number[]
review_user1?: number[]
review_user2?: number[]
}
totalSize: any
total: number | null
setTaskData: (data: any[]) => void
setFilters: (data: any) => void
setTotal: (n: number) => void
setTotalSize: (data: any) => void
}
export const useTaskDataStore = create(
persist<TaskDataStore>(
(set) => ({
taskData: [],
filters: { page_size: 1000 },
total: null,
totalSize: {},
setTaskData: (data) => set((state) => ({ ...state, taskData: data })),
setFilters: (data) => set((state) => ({ ...state, filters: data })),
setTotal: (num) => set((state) => ({ ...state, total: num })),
setTotalSize: (data) => set((state) => ({ ...state, totalSize: data })),
}),
{
name: "task-list",
storage: storage,
}
)
)