81 lines
2.5 KiB
81 lines
2.5 KiB
4 years ago
|
/*
|
||
|
* Copyright (C), 2018-2021
|
||
|
* Project: starter
|
||
|
* FileName: UserNameTransferRequest
|
||
|
* Author: Louis
|
||
|
* Date: 2021/7/16 9:20
|
||
|
*/
|
||
|
package com.fr.plugin.dingtalksyn.request;
|
||
|
|
||
|
import com.fanruan.api.decision.user.UserKit;
|
||
|
import com.fanruan.api.util.StringKit;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
import com.fr.general.IOUtils;
|
||
|
import com.fr.json.JSONObject;
|
||
|
|
||
|
import javax.servlet.ServletInputStream;
|
||
|
import javax.servlet.http.HttpServletRequest;
|
||
|
import javax.servlet.http.HttpServletRequestWrapper;
|
||
|
import java.io.BufferedReader;
|
||
|
import java.io.ByteArrayInputStream;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* <Function Description><br>
|
||
|
* <UserNameTransferRequest>
|
||
|
*
|
||
|
* @author fr.open
|
||
|
* @since 1.0.0
|
||
|
*/
|
||
|
public class ModifyBodyRequestWrapper extends HttpServletRequestWrapper {
|
||
|
private byte[] data;
|
||
|
private String bodyContent;
|
||
|
|
||
|
public ModifyBodyRequestWrapper(HttpServletRequest request) {
|
||
|
super(request);
|
||
|
this.bodyContent = modifyRequestBody();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 如登陆输入手机号,则替换为用户名
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
private String modifyRequestBody() {
|
||
|
try {
|
||
|
String orgContent = IOUtils.inputStream2String(this.getRequest().getInputStream());
|
||
|
JSONObject bodyJo = new JSONObject(orgContent);
|
||
|
if (!bodyJo.has("username")) {
|
||
|
return orgContent;
|
||
|
}
|
||
|
String username = bodyJo.getString("username");
|
||
|
if (StringKit.isBlank(username)) {
|
||
|
return orgContent;
|
||
|
}
|
||
|
if (!UserKit.existUsername(username)) {
|
||
|
List<String> usernameList = UserService.getInstance().getUserNamesFromMobile(username);
|
||
|
if (!usernameList.isEmpty() && StringKit.isNotBlank(usernameList.get(0))) {
|
||
|
bodyJo.put("username", usernameList.get(0));
|
||
|
}
|
||
|
}
|
||
|
return bodyJo.encode();
|
||
|
} catch (Exception e) {
|
||
|
return StringKit.EMPTY;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public ServletInputStream getInputStream() {
|
||
|
try {
|
||
|
return new DelegatingServletInputStream(new ByteArrayInputStream(this.bodyContent.getBytes()));
|
||
|
} catch (Exception e) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public BufferedReader getReader() {
|
||
|
return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(this.bodyContent.getBytes())));
|
||
|
}
|
||
|
}
|