111 lines
3.7 KiB
TypeScript
111 lines
3.7 KiB
TypeScript
/** 采集插件读取的站点根地址(与 OMS / WMS 约定一致) */
|
||
export const RAPID_PACKAGE_HOST_COOKIE = 'RAPID_PACKAGE_HOST'
|
||
/** 插件确认页默认仓库:JSON `{ id, name }` */
|
||
export const RAPID_PACKAGE_WAREHOUSE_COOKIE = 'RAPID_PACKAGE_WAREHOUSE'
|
||
/** 管理端代采客户:JSON `{ id, name }` */
|
||
export const RAPID_PACKAGE_CUSTOMER_COOKIE = 'RAPID_PACKAGE_CUSTOMER'
|
||
|
||
function writeCookie(name: string, value: string): void {
|
||
if (typeof document === 'undefined') return
|
||
const protocol = window.location.protocol
|
||
const isHttps = protocol === 'https:'
|
||
const parts = [
|
||
`${encodeURIComponent(name)}=${encodeURIComponent(value)}`,
|
||
'path=/',
|
||
`expires=${new Date(Date.now() + 999 * 864e5).toUTCString()}`,
|
||
]
|
||
if (isHttps) {
|
||
parts.push('Secure', 'SameSite=None')
|
||
}
|
||
document.cookie = parts.join('; ')
|
||
}
|
||
|
||
function readJsonCookie(name: string): { id: number; name: string } | null {
|
||
if (typeof window === 'undefined') return null
|
||
try {
|
||
const fromLs = window.localStorage?.getItem(name)
|
||
const raw =
|
||
fromLs ||
|
||
document.cookie
|
||
.split(';')
|
||
.map((s) => s.trim())
|
||
.find((s) => s.startsWith(`${name}=`))
|
||
?.slice(name.length + 1)
|
||
if (!raw) return null
|
||
const parsed = JSON.parse(decodeURIComponent(raw))
|
||
const id = Number(parsed?.id)
|
||
if (!id) return null
|
||
return { id, name: String(parsed?.name || id) }
|
||
} catch {
|
||
return null
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 写入 RAPID_PACKAGE_HOST,供浏览器扩展识别当前 OMS 域名。
|
||
*/
|
||
export function setRapidPackageHostCookie(): void {
|
||
if (typeof window === 'undefined') return
|
||
const url = `${window.location.protocol}//${window.location.host}`
|
||
try {
|
||
writeCookie(RAPID_PACKAGE_HOST_COOKIE, url)
|
||
} catch (e) {
|
||
console.warn('[rapidPackagePluginHost] cookie set failed', e)
|
||
}
|
||
try {
|
||
window.localStorage?.setItem(RAPID_PACKAGE_HOST_COOKIE, url)
|
||
} catch {
|
||
/* 隐私模式等 */
|
||
}
|
||
}
|
||
|
||
/** 写入当前选用的仓库,供确认页读取 */
|
||
export function setRapidPackageWarehouseCookie(warehouse: { id: number; name: string } | null): void {
|
||
if (typeof window === 'undefined') return
|
||
if (!warehouse?.id) {
|
||
try {
|
||
writeCookie(RAPID_PACKAGE_WAREHOUSE_COOKIE, '')
|
||
window.localStorage?.removeItem(RAPID_PACKAGE_WAREHOUSE_COOKIE)
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
return
|
||
}
|
||
const raw = JSON.stringify({ id: warehouse.id, name: warehouse.name || String(warehouse.id) })
|
||
try {
|
||
writeCookie(RAPID_PACKAGE_WAREHOUSE_COOKIE, raw)
|
||
window.localStorage?.setItem(RAPID_PACKAGE_WAREHOUSE_COOKIE, raw)
|
||
} catch (e) {
|
||
console.warn('[rapidPackagePluginHost] warehouse cookie set failed', e)
|
||
}
|
||
}
|
||
|
||
export function readRapidPackageWarehouseCookie(): { id: number; name: string } | null {
|
||
return readJsonCookie(RAPID_PACKAGE_WAREHOUSE_COOKIE)
|
||
}
|
||
|
||
/** 管理端写入当前代采客户,供插件草稿/查重携带 customerId */
|
||
export function setRapidPackageCustomerCookie(customer: { id: number; name: string } | null): void {
|
||
if (typeof window === 'undefined') return
|
||
if (!customer?.id) {
|
||
try {
|
||
writeCookie(RAPID_PACKAGE_CUSTOMER_COOKIE, '')
|
||
window.localStorage?.removeItem(RAPID_PACKAGE_CUSTOMER_COOKIE)
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
return
|
||
}
|
||
const raw = JSON.stringify({ id: customer.id, name: customer.name || String(customer.id) })
|
||
try {
|
||
writeCookie(RAPID_PACKAGE_CUSTOMER_COOKIE, raw)
|
||
window.localStorage?.setItem(RAPID_PACKAGE_CUSTOMER_COOKIE, raw)
|
||
} catch (e) {
|
||
console.warn('[rapidPackagePluginHost] customer cookie set failed', e)
|
||
}
|
||
}
|
||
|
||
export function readRapidPackageCustomerCookie(): { id: number; name: string } | null {
|
||
return readJsonCookie(RAPID_PACKAGE_CUSTOMER_COOKIE)
|
||
}
|