feat(web): userStore 暴露所属公司,盘点相关 API 支持按公司传参
多设备协同盘点需要前端明确知道「在盘哪个公司」: - userStore 从 JWT claim company_name 解出 companyName 并暴露,登录、 刷新 token、登出时同步维护。后端 get_current_company_filter() 读的 也是这个 claim,两侧口径一致。 - 新增 getStocktakeCompanies(),对应盘点页的公司下拉接口。 - 盘点 API 增加公司维度:scanStockByBarcode 加可选 company; getDraftMergedList / getAllStocktakeItems 加 company_name; updateStocktakeQuantity 加第二个参数。 公司标识一律走 query string 而非 body:后端 get_current_company_filter() 只读 request.args,POST 请求 body 里的 company_name 会被直接忽略。
This commit is contained in:
@ -20,11 +20,12 @@ export function getStockList(params: { page?: number; pageSize?: number; keyword
|
||||
}
|
||||
|
||||
// 扫码精确匹配库存(盘库/出库/借库通用,替代模糊搜索漏匹配问题)
|
||||
export function scanStockByBarcode(barcode: string) {
|
||||
// company: 跨域/超管场景下需显式指定公司;不传则由后端按 JWT 里的公司隔离
|
||||
export function scanStockByBarcode(barcode: string, company?: string) {
|
||||
return request({
|
||||
url: '/v1/inbound/stock/scan',
|
||||
method: 'get',
|
||||
params: { barcode }
|
||||
params: company ? { barcode, company_name: company } : { barcode }
|
||||
})
|
||||
}
|
||||
|
||||
@ -89,6 +90,7 @@ export function getDraftMergedList(params: {
|
||||
status_filter?: string
|
||||
page?: number
|
||||
pageSize?: number
|
||||
company_name?: string
|
||||
}) {
|
||||
return request({
|
||||
url: '/v1/inbound/stock/draft/merged-list',
|
||||
@ -97,8 +99,22 @@ export function getDraftMergedList(params: {
|
||||
})
|
||||
}
|
||||
|
||||
// 盘点页的公司下拉选项(超管拿到全部公司,普通用户只拿到本公司)
|
||||
export function getStocktakeCompanies() {
|
||||
return request({
|
||||
url: '/v1/inbound/stock/stocktake/companies',
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
// 获取应盘物资清单(盘点基数)
|
||||
export function getAllStocktakeItems(params?: { keyword?: string; session_id?: string }) {
|
||||
export function getAllStocktakeItems(params?: {
|
||||
keyword?: string
|
||||
session_id?: string
|
||||
page?: number
|
||||
pageSize?: number
|
||||
company_name?: string
|
||||
}) {
|
||||
return request({
|
||||
url: '/v1/inbound/stock/stocktake/all-items',
|
||||
method: 'get',
|
||||
@ -107,10 +123,15 @@ export function getAllStocktakeItems(params?: { keyword?: string; session_id?: s
|
||||
}
|
||||
|
||||
// 更新盘点实盘数(手动修改)
|
||||
export function updateStocktakeQuantity(data: { stock_id: number; source_table: string; quantity: number; session_id?: string }) {
|
||||
// company 走 query string(后端 get_current_company_filter 只读 request.args,不读 body)
|
||||
export function updateStocktakeQuantity(
|
||||
data: { stock_id: number; source_table: string; quantity: number; session_id?: string },
|
||||
company?: string
|
||||
) {
|
||||
return request({
|
||||
url: '/v1/inbound/stock/stocktake/update-quantity',
|
||||
method: 'post',
|
||||
params: company ? { company_name: company } : undefined,
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
@ -30,6 +30,14 @@ function getTokenExp(token: string): number | null {
|
||||
return decoded?.exp ? decoded.exp * 1000 : null // 转换为毫秒
|
||||
}
|
||||
|
||||
// 从 JWT 读取用户所属公司。
|
||||
// 多租户隔离的依据:后端 get_current_company_filter() 读的也是这个 claim,
|
||||
// 前端拿它作为「公司选择器」的默认值(普通用户锁定本公司)。
|
||||
function readCompanyFromToken(token: string): string {
|
||||
const decoded = parseJWT(token)
|
||||
return decoded?.company_name || ''
|
||||
}
|
||||
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
// 1. State: 初始化时优先从 localStorage 获取,防止刷新丢失
|
||||
const token = ref(localStorage.getItem('access_token') || localStorage.getItem('token') || '')
|
||||
@ -37,6 +45,8 @@ export const useUserStore = defineStore('user', () => {
|
||||
const role = ref(localStorage.getItem('role') || '')
|
||||
const username = ref(localStorage.getItem('username') || '')
|
||||
const permissions = ref<string[]>(JSON.parse(localStorage.getItem('permissions') || '[]'))
|
||||
// 所属公司(取自 JWT claim company_name);空字符串表示未绑定公司
|
||||
const companyName = ref(readCompanyFromToken(localStorage.getItem('access_token') || localStorage.getItem('token') || ''))
|
||||
|
||||
// 兼容旧版 localStorage key
|
||||
if (localStorage.getItem('token') && !localStorage.getItem('access_token')) {
|
||||
@ -66,6 +76,7 @@ export const useUserStore = defineStore('user', () => {
|
||||
// 更新 Pinia 状态 (内存)
|
||||
token.value = data.access_token
|
||||
refreshToken.value = data.refresh_token || ''
|
||||
companyName.value = readCompanyFromToken(data.access_token)
|
||||
|
||||
// 处理用户信息 (确保后端返回结构中有 user 字段)
|
||||
if (data.user) {
|
||||
@ -114,6 +125,7 @@ export const useUserStore = defineStore('user', () => {
|
||||
// 更新 Access Token(无感刷新时调用)
|
||||
const setToken = (newToken: string) => {
|
||||
token.value = newToken
|
||||
companyName.value = readCompanyFromToken(newToken)
|
||||
localStorage.setItem('access_token', newToken)
|
||||
|
||||
// [Dify] Token 刷新后,重新初始化 Dify 以更新用户会话
|
||||
@ -130,6 +142,7 @@ export const useUserStore = defineStore('user', () => {
|
||||
role.value = ''
|
||||
username.value = ''
|
||||
permissions.value = []
|
||||
companyName.value = ''
|
||||
|
||||
// 2. 清空 LocalStorage (硬盘)
|
||||
localStorage.removeItem('access_token')
|
||||
@ -198,6 +211,7 @@ export const useUserStore = defineStore('user', () => {
|
||||
refreshToken,
|
||||
role,
|
||||
username,
|
||||
companyName,
|
||||
permissions,
|
||||
handleLogin,
|
||||
setToken,
|
||||
|
||||
Reference in New Issue
Block a user