admin接入数据库
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import supa from '@/components/supadb/aksupainstance.uts'
|
||||
|
||||
/**
|
||||
* 分销配置模型 (与 ak_distribution_config 表对齐)
|
||||
* 分销配置模型
|
||||
*/
|
||||
export type DistributionConfig = {
|
||||
id?: string
|
||||
// 分销模式
|
||||
is_enabled: boolean
|
||||
extract_type: string
|
||||
bind_type: string
|
||||
@@ -15,8 +14,6 @@ export type DistributionConfig = {
|
||||
is_area_manager: boolean
|
||||
is_agent_apply: boolean
|
||||
is_commission_window: boolean
|
||||
|
||||
// 返佣设置
|
||||
is_self_brokerage: boolean
|
||||
is_member_brokerage: boolean
|
||||
brokerage_type: string
|
||||
@@ -26,17 +23,13 @@ export type DistributionConfig = {
|
||||
store_brokerage_ratio: number
|
||||
store_brokerage_two_ratio: number
|
||||
extract_frozen_time: number
|
||||
|
||||
// 提现设置
|
||||
user_extract_min_price: number
|
||||
extract_bank_list: string
|
||||
extract_type_list: string[]
|
||||
wechat_extract_type: string
|
||||
alipay_extract_type: string
|
||||
user_extract_fee: number
|
||||
|
||||
updated_at?: string
|
||||
updated_by?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,6 +51,52 @@ export type Promoter = {
|
||||
unwithdrawnAmount: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 事业部模型
|
||||
*/
|
||||
export type Division = {
|
||||
uid: string
|
||||
name: string
|
||||
invite_code: string
|
||||
commission_ratio: number
|
||||
is_enabled: boolean
|
||||
end_time: string | null
|
||||
created_at: string
|
||||
agentCount: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理商模型
|
||||
*/
|
||||
export type Agent = {
|
||||
uid: string
|
||||
name: string
|
||||
division_uid: string
|
||||
division_name: string
|
||||
commission_ratio: number
|
||||
is_enabled: boolean
|
||||
end_time: string | null
|
||||
created_at: string
|
||||
staffCount: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理商申请模型
|
||||
*/
|
||||
export type AgentApply = {
|
||||
id: string
|
||||
uid: string
|
||||
name: string
|
||||
phone: string
|
||||
dept_uid: string
|
||||
dept_name: string
|
||||
proof_images: string[] | null
|
||||
status: string
|
||||
refusal_reason: string | null
|
||||
time: string
|
||||
invite_code: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取分销全局配置
|
||||
*/
|
||||
@@ -89,11 +128,7 @@ export async function saveDistributionConfig(config: DistributionConfig): Promis
|
||||
})
|
||||
.execute()
|
||||
|
||||
if (error != null) {
|
||||
console.error('保存分销配置失败:', error)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return error == null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,8 +143,6 @@ export type DistributionLevel = {
|
||||
task_total: number
|
||||
task_finish: number
|
||||
is_visible: boolean
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,30 +155,18 @@ export async function getDistributionLevelList(): Promise<DistributionLevel[]> {
|
||||
.order('level', { ascending: true })
|
||||
.execute()
|
||||
|
||||
if (error != null) {
|
||||
console.error('获取分销等级列表失败:', error)
|
||||
return [] as DistributionLevel[]
|
||||
}
|
||||
return data as DistributionLevel[]
|
||||
return (data ?? []) as DistributionLevel[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存/更新分销等级
|
||||
* 保存分销等级
|
||||
*/
|
||||
export async function saveDistributionLevel(level: DistributionLevel): Promise<boolean> {
|
||||
const { error } = await supa
|
||||
.from('ak_distribution_level')
|
||||
.upsert({
|
||||
...level,
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.upsert(level)
|
||||
.execute()
|
||||
|
||||
if (error != null) {
|
||||
console.error('保存分销等级失败:', error)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return error == null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,16 +178,11 @@ export async function deleteDistributionLevel(id: string): Promise<boolean> {
|
||||
.delete()
|
||||
.eq('id', id)
|
||||
.execute()
|
||||
|
||||
if (error != null) {
|
||||
console.error('删除分销等级失败:', error)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return error == null
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推广员列表
|
||||
* 推广员列表参数
|
||||
*/
|
||||
export type PromoterListParams = {
|
||||
search?: string | null
|
||||
@@ -177,185 +193,111 @@ export type PromoterListParams = {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推广员列表(聚合统计)
|
||||
* 获取推广员列表
|
||||
*/
|
||||
export async function getPromoterList(params?: PromoterListParams): Promise<Promoter[]> {
|
||||
const payload = {
|
||||
const { data, error } = await supa.rpc('rpc_admin_get_promoter_list', {
|
||||
p_search: params?.search ?? null,
|
||||
p_page: params?.page ?? 1,
|
||||
p_page_size: params?.pageSize ?? 20,
|
||||
p_start_time: params?.startTime ?? null,
|
||||
p_end_time: params?.endTime ?? null
|
||||
} as any
|
||||
} as any)
|
||||
|
||||
const { data, error } = await supa
|
||||
.rpc('rpc_admin_get_promoter_list', payload as any)
|
||||
|
||||
if (error != null) {
|
||||
console.error('获取推广员列表失败:', error)
|
||||
return [] as Promoter[]
|
||||
}
|
||||
return (data ?? []) as Promoter[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 事业部模型
|
||||
*/
|
||||
export type DistributionDivision = {
|
||||
id?: string
|
||||
merchant_id?: string
|
||||
uid: string
|
||||
name: string
|
||||
invite_code: string
|
||||
ratio: number
|
||||
agent_count: number
|
||||
end_time: string | null
|
||||
status: boolean
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
// 关联字段
|
||||
nickname?: string
|
||||
avatar_url?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理商模型
|
||||
*/
|
||||
export type DistributionAgent = {
|
||||
id?: string
|
||||
merchant_id?: string
|
||||
uid: string
|
||||
division_id: string
|
||||
name: string
|
||||
status: boolean
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
// 关联字段
|
||||
nickname?: string
|
||||
avatar_url?: string
|
||||
division_name?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取事业部列表
|
||||
*/
|
||||
export async function fetchDivisions(query?: { search?: string, page?: number, pageSize?: number }): Promise<{ total: number, items: DistributionDivision[] }> {
|
||||
let q = supa.from('ak_distribution_divisions').select('*, ak_users!uid(username, avatar_url)', { count: 'exact' })
|
||||
|
||||
if (query?.search != null && query.search !== '') {
|
||||
q = q.or(`name.ilike.%${query.search}%,ak_users.username.ilike.%${query.search}%`)
|
||||
}
|
||||
|
||||
const p = query?.page ?? 1
|
||||
const ps = query?.pageSize ?? 20
|
||||
const from = (p - 1) * ps
|
||||
const to = from + ps - 1
|
||||
|
||||
const { data, error, count } = await q
|
||||
.order('created_at', { ascending: false })
|
||||
.range(from, to)
|
||||
.execute()
|
||||
|
||||
if (error != null) {
|
||||
console.error('获取事业部列表失败:', error)
|
||||
return { total: 0, items: [] as DistributionDivision[] }
|
||||
}
|
||||
|
||||
const items = (data ?? []).map((item: any): DistributionDivision => {
|
||||
return {
|
||||
...item,
|
||||
nickname: item.ak_users?.username,
|
||||
avatar_url: item.ak_users?.avatar_url
|
||||
} as DistributionDivision
|
||||
})
|
||||
|
||||
return { total: count ?? 0, items }
|
||||
export async function getDivisionList(search: string | null, page: number, pageSize: number): Promise<Division[]> {
|
||||
const { data, error } = await supa.rpc('rpc_admin_get_division_list', {
|
||||
p_search: search,
|
||||
p_page: page,
|
||||
p_page_size: pageSize
|
||||
} as any)
|
||||
return (data ?? []) as Division[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存事业部(新增/更新)
|
||||
* 保存事业部
|
||||
*/
|
||||
export async function saveDivision(division: DistributionDivision): Promise<boolean> {
|
||||
const session = supa.getSession()
|
||||
const mid = session?.user?.getString('id')
|
||||
if (mid == null) return false
|
||||
|
||||
const { error } = await supa
|
||||
.from('ak_distribution_divisions')
|
||||
.upsert({
|
||||
...division,
|
||||
merchant_id: mid,
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.execute()
|
||||
|
||||
export async function saveDivision(division: any): Promise<boolean> {
|
||||
const { error } = await supa.rpc('rpc_admin_save_division', {
|
||||
p_uid: division.uid,
|
||||
p_name: division.name,
|
||||
p_invite_code: division.invite_code,
|
||||
p_commission_ratio: division.commission_ratio,
|
||||
p_is_enabled: division.is_enabled,
|
||||
p_end_time: division.end_time
|
||||
} as any)
|
||||
return error == null
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除事业部
|
||||
*/
|
||||
export async function deleteDivision(id: string): Promise<boolean> {
|
||||
const { error } = await supa.from('ak_distribution_divisions').delete().eq('id', id).execute()
|
||||
return error == null
|
||||
export async function deleteDivision(uid: string): Promise<boolean> {
|
||||
const { data, error } = await supa.rpc('rpc_admin_delete_division', { p_uid: uid } as any)
|
||||
return error == null && data === true
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取代理商列表
|
||||
*/
|
||||
export async function fetchAgents(query?: { search?: string, divisionId?: string, page?: number, pageSize?: number }): Promise<{ total: number, items: DistributionAgent[] }> {
|
||||
let q = supa.from('ak_distribution_agents').select('*, ak_users!uid(username, avatar_url), ak_distribution_divisions!division_id(name)', { count: 'exact' })
|
||||
|
||||
if (query?.search != null && query.search !== '') {
|
||||
q = q.or(`name.ilike.%${query.search}%,ak_users.username.ilike.%${query.search}%`)
|
||||
}
|
||||
if (query?.divisionId != null) {
|
||||
q = q.eq('division_id', query.divisionId)
|
||||
}
|
||||
|
||||
const p = query?.page ?? 1
|
||||
const ps = query?.pageSize ?? 20
|
||||
const from = (p - 1) * ps
|
||||
const to = from + ps - 1
|
||||
|
||||
const { data, error, count } = await q
|
||||
.order('created_at', { ascending: false })
|
||||
.range(from, to)
|
||||
.execute()
|
||||
|
||||
if (error != null) {
|
||||
console.error('获取代理商列表失败:', error)
|
||||
return { total: 0, items: [] as DistributionAgent[] }
|
||||
}
|
||||
|
||||
const items = (data ?? []).map((item: any): DistributionAgent => {
|
||||
return {
|
||||
...item,
|
||||
nickname: item.ak_users?.username,
|
||||
avatar_url: item.ak_users?.avatar_url,
|
||||
division_name: item.ak_distribution_divisions?.name
|
||||
} as DistributionAgent
|
||||
})
|
||||
|
||||
return { total: count ?? 0, items }
|
||||
export async function getAgentList(search: string | null, page: number, pageSize: number): Promise<Agent[]> {
|
||||
const { data, error } = await supa.rpc('rpc_admin_get_agent_list', {
|
||||
p_search: search,
|
||||
p_page: page,
|
||||
p_page_size: pageSize
|
||||
} as any)
|
||||
return (data ?? []) as Agent[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存代理商
|
||||
*/
|
||||
export async function saveAgent(agent: DistributionAgent): Promise<boolean> {
|
||||
const session = supa.getSession()
|
||||
const mid = session?.user?.getString('id')
|
||||
if (mid == null) return false
|
||||
|
||||
const { error } = await supa
|
||||
.from('ak_distribution_agents')
|
||||
.upsert({
|
||||
...agent,
|
||||
merchant_id: mid,
|
||||
updated_at: new Date().toISOString()
|
||||
})
|
||||
.execute()
|
||||
|
||||
export async function saveAgent(agent: any): Promise<boolean> {
|
||||
const { error } = await supa.rpc('rpc_admin_save_agent', {
|
||||
p_uid: agent.uid,
|
||||
p_division_uid: agent.division_uid,
|
||||
p_name: agent.name,
|
||||
p_commission_ratio: agent.commission_ratio,
|
||||
p_is_enabled: agent.is_enabled,
|
||||
p_end_time: agent.end_time
|
||||
} as any)
|
||||
return error == null
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除代理商
|
||||
*/
|
||||
export async function deleteAgent(uid: string): Promise<boolean> {
|
||||
const { data, error } = await supa.rpc('rpc_admin_delete_agent', { p_uid: uid } as any)
|
||||
return error == null && data === true
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取代理商申请列表
|
||||
*/
|
||||
export async function getAgentApplyList(status: string, search: string | null, page: number, pageSize: number): Promise<AgentApply[]> {
|
||||
const { data, error } = await supa.rpc('rpc_admin_get_agent_apply_list', {
|
||||
p_status: status,
|
||||
p_search: search,
|
||||
p_page: page,
|
||||
p_page_size: pageSize
|
||||
} as any)
|
||||
return (data ?? []) as AgentApply[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 审核代理商申请
|
||||
*/
|
||||
export async function processAgentApply(id: string, status: string, reason: string | null): Promise<boolean> {
|
||||
const { data, error } = await supa.rpc('rpc_admin_process_agent_apply', {
|
||||
p_id: id,
|
||||
p_status: status,
|
||||
p_refusal_reason: reason
|
||||
} as any)
|
||||
return error == null && data === true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user