Files
smartDriveEEUniApp/common/check-tenant-id.js
2026-01-31 21:10:40 +08:00

82 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 检查所有后端请求是否包含 tenantId 的工具
*
* 使用方法:
* 1. 在浏览器控制台或 Node.js 环境中运行
* 2. 或者集成到构建流程中
*/
/**
* 检查代码文件中是否使用了 uni.request 且可能缺少 tenantId
*
* 注意:这是一个静态检查工具,实际运行时需要确保:
* 1. 使用统一的 request 封装common/request.js
* 2. 或者手动在每个请求中添加 tenantId
*/
export function checkTenantIdInCode() {
console.log('=== 检查 tenantId 使用情况 ===')
console.log('')
console.log('✅ 已创建统一的请求封装common/request.js')
console.log(' - 自动为所有请求添加 tenantId')
console.log(' - GET 请求tenantId 作为 query 参数')
console.log(' - POST/PUT/DELETE 请求tenantId 作为 body 字段')
console.log('')
console.log('📝 需要迁移的文件(使用 uni.request 的地方):')
console.log(' 1. pages-subpackage/reception/reception.vue')
console.log(' 2. pages-subpackage/customer/customer.vue')
console.log(' 3. uni_modules/uni-id-pages/common/store.js (登出接口)')
console.log('')
console.log('💡 迁移方法:')
console.log(' 将 uni.request 替换为:')
console.log(' import { request, get, post, put, del } from "@/common/request.js"')
console.log('')
console.log(' 示例:')
console.log(' // 旧代码:')
console.log(' uni.request({ url: getApiUrl("/api/xxx"), method: "GET" })')
console.log('')
console.log(' // 新代码:')
console.log(' import { get } from "@/common/request.js"')
console.log(' get("/api/xxx")')
console.log('')
}
/**
* 运行时检查:验证当前请求是否包含 tenantId
* 可以在 uni.request 拦截器中调用
*/
export function validateRequestHasTenantId(requestOptions) {
const { method = 'GET', data = {}, url = '' } = requestOptions
// 检查 tenantId 是否存在
const tenantId = uni.getStorageSync('backend-tenant-id')
if (!tenantId) {
console.warn('[TenantId Check] ⚠️ 未找到 tenantId请先登录')
return false
}
// 检查请求中是否包含 tenantId
if (method.toUpperCase() === 'GET') {
// GET 请求:检查 URL 中是否有 tenantId
if (!url.includes('tenantId=')) {
console.warn('[TenantId Check] ⚠️ GET 请求缺少 tenantId:', url)
return false
}
} else {
// POST/PUT/DELETE 请求:检查 data 中是否有 tenantId
if (!data.tenantId) {
console.warn('[TenantId Check] ⚠️', method, '请求缺少 tenantId:', url)
return false
}
}
console.log('[TenantId Check] ✅ 请求包含 tenantId:', url)
return true
}
// 如果直接运行此文件,执行检查
if (typeof window !== 'undefined' || typeof global !== 'undefined') {
checkTenantIdInCode()
}