@@ -148,12 +148,36 @@ export class EncryptionHandler {
148148 return this . decryptAES ( encryptedData , aesKey )
149149 }
150150
151- static encryptRequestData ( data , config ) {
151+ static encryptRequestData ( data , config , encryptFields = [ ] ) {
152152 if ( ! config . enabled || ! config . encryptRequest || ! data ) return data
153153 if ( isFormData ( data ) || isBlob ( data ) ) {
154154 logger . warn ( 'FormData/Blob 类型数据不进行加密' )
155155 return data
156156 }
157+
158+ // 1. 部分字段加密
159+ if ( Array . isArray ( encryptFields ) && encryptFields . length > 0 && typeof data === 'object' ) {
160+ const newData = { ...data }
161+ encryptFields . forEach ( ( field ) => {
162+ if ( Object . prototype . hasOwnProperty . call ( newData , field ) ) {
163+ const value = newData [ field ]
164+ const jsonValue = typeof value === 'string' ? value : JSON . stringify ( value )
165+
166+ if ( config . mode === 'AES' ) {
167+ newData [ field ] = this . encryptAES ( jsonValue , config . aesKey )
168+ } else if ( config . mode === 'RSA' ) {
169+ newData [ field ] = this . encryptRSA ( jsonValue , config . rsaPublicKey )
170+ } else if ( config . mode === 'HYBRID' ) {
171+ // 混合加密部分字段:返回对象结构 { encrypted: '...', key: '...' }
172+ const { encryptedKey, encryptedData } = this . encryptHybrid ( jsonValue , config . aesKey , config . rsaPublicKey )
173+ newData [ field ] = { encrypted : encryptedData , key : encryptedKey }
174+ }
175+ }
176+ } )
177+ return newData
178+ }
179+
180+ // 2. 全量加密
157181 const jsonData = typeof data === 'string' ? data : JSON . stringify ( data )
158182 try {
159183 switch ( config . mode ) {
@@ -237,21 +261,50 @@ class HttpClient {
237261 // 添加默认请求拦截器
238262 this . useRequestInterceptor ( ( config ) => {
239263 // 1. 处理加密
240- if ( encryptionConfig . enabled && config . encrypt !== false && ( config . payload || config . body ) ) {
241- const dataToEncrypt = config . payload || config . body
242- try {
243- const encrypted = EncryptionHandler . encryptRequestData ( dataToEncrypt , encryptionConfig )
244- if ( encrypted && encrypted !== dataToEncrypt ) {
245- config . payload = encrypted
246- // 确保 payload 生效,清除 body
247- if ( config . body ) config . body = undefined
248- config . headers = {
249- ...config . headers ,
250- [ encryptionConfig . encryptionHeader ] : encryptionConfig . mode ,
264+ if ( encryptionConfig . enabled && config . encrypt !== false ) {
265+ // A. 处理 Body 加密 (POST/PUT/PATCH 等)
266+ if ( config . payload || config . body ) {
267+ const dataToEncrypt = config . payload || config . body
268+ try {
269+ const encrypted = EncryptionHandler . encryptRequestData (
270+ dataToEncrypt ,
271+ encryptionConfig ,
272+ config . encryptFields
273+ )
274+ if ( encrypted && encrypted !== dataToEncrypt ) {
275+ config . payload = encrypted
276+ // 确保 payload 生效,清除 body
277+ if ( config . body ) config . body = undefined
278+
279+ // 如果是全量加密(没有指定 partial fields),添加 header
280+ if ( ! config . encryptFields || config . encryptFields . length === 0 ) {
281+ config . headers = {
282+ ...config . headers ,
283+ [ encryptionConfig . encryptionHeader ] : encryptionConfig . mode ,
284+ }
285+ }
251286 }
287+ } catch ( error ) {
288+ logger . error ( 'Body 加密失败:' , error )
289+ }
290+ }
291+
292+ // B. 处理 Params 加密 (GET/DELETE 等)
293+ if ( config . params && Object . keys ( config . params ) . length > 0 ) {
294+ try {
295+ // 复用 encryptRequestData 逻辑(支持全量和部分)
296+ const encryptedParams = EncryptionHandler . encryptRequestData (
297+ config . params ,
298+ encryptionConfig ,
299+ config . encryptFields
300+ )
301+ if ( encryptedParams && encryptedParams !== config . params ) {
302+ config . params = encryptedParams
303+ // 注意:Fetch/Axios 处理 params 对象时,如果变成 { encrypted: '...' } 形式,会自动序列化为 ?encrypted=...
304+ }
305+ } catch ( error ) {
306+ logger . error ( 'Params 加密失败:' , error )
252307 }
253- } catch ( error ) {
254- logger . error ( '请求加密失败:' , error )
255308 }
256309 }
257310
0 commit comments