11 changed files with 845 additions and 1 deletions
@ -0,0 +1,99 @@ |
|||
<!DOCTYPE html> |
|||
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"> |
|||
<head> |
|||
<th:block th:include="zdxtInclude :: header('登陆记录')"/> |
|||
</head> |
|||
<body class="gray-bg"> |
|||
<div class="container-div"> |
|||
<div class="row"> |
|||
<div class="col-sm-12 search-collapse"> |
|||
<form id="formId"> |
|||
<div class="select-list"> |
|||
<ul> |
|||
<li> |
|||
<p>昵称:</p> |
|||
<input type="text" name="nickname"/> |
|||
</li> |
|||
<li> |
|||
<a class="btn btn-success btn-rounded btn-sm" onclick="$.table.search()"><i |
|||
class="fa fa-search"></i> 搜索</a> |
|||
<a class="btn btn-reset btn-rounded btn-sm" onclick="reset()"><i |
|||
class="fa fa-refresh"></i> 重置</a> |
|||
</li> |
|||
</ul> |
|||
</div> |
|||
</form> |
|||
</div> |
|||
|
|||
<div class="btn-group-sm" id="toolbar" role="group"> |
|||
|
|||
</div> |
|||
<div class="col-sm-12 select-table table-striped"> |
|||
<table id="bootstrap-table"></table> |
|||
</div> |
|||
</div> |
|||
</div> |
|||
<th:block th:include="zdxtInclude :: footer"/> |
|||
<script th:inline="javascript"> |
|||
var editFlag = [[${@permission.hasPermi('project:user:edit')}]]; |
|||
var removeFlag = [[${@permission.hasPermi('project:user:remove')}]]; |
|||
var detailFlag = [[${@permission.hasPermi('project:user:detail')}]]; |
|||
var prefix = ctx + "small/wechat"; |
|||
|
|||
$(function () { |
|||
var options = { |
|||
url: prefix + "/list", |
|||
createUrl: prefix + "/add/{}", |
|||
updateUrl: prefix + "/edit/{id}", |
|||
removeUrl: prefix + "/remove", |
|||
exportUrl: prefix + "/export", |
|||
detailUrl: prefix + "/detail/{guid}", |
|||
modalName: "主页", |
|||
columns: [{ |
|||
checkbox: true |
|||
}, |
|||
{ |
|||
field: 'guid', |
|||
title: 'id' |
|||
}, |
|||
{ |
|||
field: 'nickname', |
|||
title: '昵称' |
|||
}, |
|||
{ |
|||
field: 'gender', |
|||
title: '性别' |
|||
}, |
|||
{ |
|||
field: 'city', |
|||
title: '城市' |
|||
}, |
|||
{ |
|||
field: 'creator', |
|||
title: '创建人' |
|||
}, |
|||
{ |
|||
field: 'createTime', |
|||
title: '创建时间' |
|||
}, |
|||
{ |
|||
title: '操作', |
|||
align: 'center', |
|||
formatter: function (value, row, index) { |
|||
var actions = []; |
|||
//actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.guid + '\')"><i class="fa fa-edit"></i>编辑</a> '); |
|||
return actions.join(''); |
|||
} |
|||
}] |
|||
}; |
|||
$.table.init(options); |
|||
}); |
|||
|
|||
|
|||
function reset() { |
|||
$("[name='nickname']").val(""); |
|||
$.table.search() |
|||
} |
|||
</script> |
|||
</body> |
|||
</html> |
|||
@ -0,0 +1,63 @@ |
|||
package com.zdxt.knowledge.controller; |
|||
|
|||
import com.zdxt.common.controller.BaseController; |
|||
import com.zdxt.common.util.TableDataInfo; |
|||
import com.zdxt.knowledge.domain.WechatLoginRequest; |
|||
import com.zdxt.knowledge.domain.WechatUser; |
|||
import com.zdxt.knowledge.service.WechatService; |
|||
import io.swagger.annotations.ApiOperation; |
|||
import org.apache.shiro.authz.annotation.RequiresPermissions; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.validation.annotation.Validated; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
|
|||
/** |
|||
* 【请填写功能名称】Controller |
|||
* |
|||
* @author ruoyi |
|||
* @date 2023-01-23 |
|||
*/ |
|||
@Controller |
|||
@RequestMapping("/small/wechat") |
|||
public class WechatLoginController extends BaseController { |
|||
|
|||
@Autowired |
|||
private WechatService wechatService; |
|||
|
|||
private String prefix = "project/user"; |
|||
|
|||
@RequiresPermissions("project:user:view") |
|||
@GetMapping("/view") |
|||
public String view() { |
|||
return prefix + "/view"; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 查询 |
|||
*/ |
|||
@RequiresPermissions("project:user:list") |
|||
@PostMapping("/list") |
|||
@ResponseBody |
|||
public TableDataInfo list(WechatUser wechatUser) { |
|||
startPage(); |
|||
List<WechatUser> list = wechatService.selectList(wechatUser); |
|||
return getDataTable(list); |
|||
} |
|||
|
|||
/** |
|||
* 登入 |
|||
*/ |
|||
@ApiOperation(value = "登入接口", httpMethod = "POST") |
|||
@PostMapping("/login") |
|||
@ResponseBody |
|||
public Map<String, Object> login(@Validated @RequestBody WechatLoginRequest loginRequest) throws Exception { |
|||
Map<String, Object> userInfoMap = wechatService.getUserInfoMap(loginRequest); |
|||
return userInfoMap; |
|||
} |
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
package com.zdxt.knowledge.domain; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
public class RawDataDO implements Serializable { |
|||
private String nickName; |
|||
private String avatarUrl; |
|||
private Integer gender; |
|||
private String city; |
|||
private String country; |
|||
private String province; |
|||
|
|||
public String getNickName() { |
|||
return nickName; |
|||
} |
|||
|
|||
public void setNickName(String nickName) { |
|||
this.nickName = nickName; |
|||
} |
|||
|
|||
public String getAvatarUrl() { |
|||
return avatarUrl; |
|||
} |
|||
|
|||
public void setAvatarUrl(String avatarUrl) { |
|||
this.avatarUrl = avatarUrl; |
|||
} |
|||
|
|||
public Integer getGender() { |
|||
return gender; |
|||
} |
|||
|
|||
public void setGender(Integer gender) { |
|||
this.gender = gender; |
|||
} |
|||
|
|||
public String getCity() { |
|||
return city; |
|||
} |
|||
|
|||
public void setCity(String city) { |
|||
this.city = city; |
|||
} |
|||
|
|||
public String getCountry() { |
|||
return country; |
|||
} |
|||
|
|||
public void setCountry(String country) { |
|||
this.country = country; |
|||
} |
|||
|
|||
public String getProvince() { |
|||
return province; |
|||
} |
|||
|
|||
public void setProvince(String province) { |
|||
this.province = province; |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
package com.zdxt.knowledge.domain; |
|||
|
|||
import io.swagger.annotations.ApiModelProperty; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
public class WechatLoginRequest implements Serializable { |
|||
|
|||
@ApiModelProperty(value = "微信code", required = true) |
|||
private String code; |
|||
|
|||
@ApiModelProperty(value = "用户非敏感字段") |
|||
private String rawData; |
|||
|
|||
@ApiModelProperty(value = "签名") |
|||
private String signature; |
|||
|
|||
@ApiModelProperty(value = "用户敏感字段") |
|||
private String encryptedData; |
|||
|
|||
@ApiModelProperty(value = "解密向量") |
|||
private String iv; |
|||
|
|||
public String getCode() { |
|||
return code; |
|||
} |
|||
|
|||
public void setCode(String code) { |
|||
this.code = code; |
|||
} |
|||
|
|||
public String getRawData() { |
|||
return rawData; |
|||
} |
|||
|
|||
public void setRawData(String rawData) { |
|||
this.rawData = rawData; |
|||
} |
|||
|
|||
public String getSignature() { |
|||
return signature; |
|||
} |
|||
|
|||
public void setSignature(String signature) { |
|||
this.signature = signature; |
|||
} |
|||
|
|||
public String getEncryptedData() { |
|||
return encryptedData; |
|||
} |
|||
|
|||
public void setEncryptedData(String encryptedData) { |
|||
this.encryptedData = encryptedData; |
|||
} |
|||
|
|||
public String getIv() { |
|||
return iv; |
|||
} |
|||
|
|||
public void setIv(String iv) { |
|||
this.iv = iv; |
|||
} |
|||
} |
|||
@ -0,0 +1,138 @@ |
|||
package com.zdxt.knowledge.domain; |
|||
|
|||
import java.io.Serializable; |
|||
import java.util.Date; |
|||
|
|||
public class WechatUser implements Serializable { |
|||
private static final long serialVersionUID = 1L; |
|||
|
|||
private String guid; |
|||
|
|||
private String token; |
|||
|
|||
private String nickname; |
|||
|
|||
private String avatarUrl; |
|||
|
|||
private Integer gender; |
|||
|
|||
private String country; |
|||
|
|||
private String province; |
|||
|
|||
private String city; |
|||
|
|||
private String mobile; |
|||
|
|||
private String openId; |
|||
|
|||
private String unionId; |
|||
|
|||
private String creator; |
|||
|
|||
private Date createTime; |
|||
|
|||
public String getGuid() { |
|||
return guid; |
|||
} |
|||
|
|||
public void setGuid(String guid) { |
|||
this.guid = guid; |
|||
} |
|||
|
|||
public String getToken() { |
|||
return token; |
|||
} |
|||
|
|||
public void setToken(String token) { |
|||
this.token = token; |
|||
} |
|||
|
|||
public String getNickname() { |
|||
return nickname; |
|||
} |
|||
|
|||
public void setNickname(String nickname) { |
|||
this.nickname = nickname; |
|||
} |
|||
|
|||
public String getAvatarUrl() { |
|||
return avatarUrl; |
|||
} |
|||
|
|||
public void setAvatarUrl(String avatarUrl) { |
|||
this.avatarUrl = avatarUrl; |
|||
} |
|||
|
|||
public Integer getGender() { |
|||
return gender; |
|||
} |
|||
|
|||
public void setGender(Integer gender) { |
|||
this.gender = gender; |
|||
} |
|||
|
|||
public String getCountry() { |
|||
return country; |
|||
} |
|||
|
|||
public void setCountry(String country) { |
|||
this.country = country; |
|||
} |
|||
|
|||
public String getProvince() { |
|||
return province; |
|||
} |
|||
|
|||
public void setProvince(String province) { |
|||
this.province = province; |
|||
} |
|||
|
|||
public String getCity() { |
|||
return city; |
|||
} |
|||
|
|||
public void setCity(String city) { |
|||
this.city = city; |
|||
} |
|||
|
|||
public String getMobile() { |
|||
return mobile; |
|||
} |
|||
|
|||
public void setMobile(String mobile) { |
|||
this.mobile = mobile; |
|||
} |
|||
|
|||
public String getOpenId() { |
|||
return openId; |
|||
} |
|||
|
|||
public void setOpenId(String openId) { |
|||
this.openId = openId; |
|||
} |
|||
|
|||
public String getUnionId() { |
|||
return unionId; |
|||
} |
|||
|
|||
public void setUnionId(String unionId) { |
|||
this.unionId = unionId; |
|||
} |
|||
|
|||
public String getCreator() { |
|||
return creator; |
|||
} |
|||
|
|||
public void setCreator(String creator) { |
|||
this.creator = creator; |
|||
} |
|||
|
|||
public Date getCreateTime() { |
|||
return createTime; |
|||
} |
|||
|
|||
public void setCreateTime(Date createTime) { |
|||
this.createTime = createTime; |
|||
} |
|||
} |
|||
@ -0,0 +1,23 @@ |
|||
package com.zdxt.knowledge.mapper; |
|||
|
|||
import com.zdxt.knowledge.domain.WechatUser; |
|||
|
|||
import java.util.List; |
|||
|
|||
/** |
|||
* Mapper接口 |
|||
* |
|||
* @author ruoyi |
|||
* @date 2023-12-26 |
|||
*/ |
|||
public interface WechatLoginMapper { |
|||
|
|||
public List<WechatUser> selectList(WechatUser wechatUser); |
|||
|
|||
public WechatUser getByOpenId(String openId); |
|||
|
|||
public int insertUser(WechatUser wechatUser); |
|||
|
|||
public int updateByOpenId(WechatUser wechatUser); |
|||
|
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
package com.zdxt.knowledge.service; |
|||
|
|||
import com.zdxt.knowledge.domain.WechatLoginRequest; |
|||
import com.zdxt.knowledge.domain.WechatUser; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* Service接口 |
|||
* |
|||
* @author ruoyi |
|||
* @date 2023-01-23 |
|||
*/ |
|||
public interface WechatService { |
|||
|
|||
public List<WechatUser> selectList(WechatUser wechatUser); |
|||
|
|||
/** |
|||
* 登陆 |
|||
* |
|||
* @param loginRequest |
|||
* @return 集合 |
|||
*/ |
|||
Map<String, Object> getUserInfoMap(WechatLoginRequest loginRequest) throws Exception; |
|||
|
|||
} |
|||
@ -0,0 +1,198 @@ |
|||
package com.zdxt.knowledge.service.impl; |
|||
|
|||
import cn.hutool.core.lang.Assert; |
|||
import cn.hutool.core.util.StrUtil; |
|||
import cn.hutool.http.HttpRequest; |
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.zdxt.common.util.StringUtils; |
|||
import com.zdxt.knowledge.domain.RawDataDO; |
|||
import com.zdxt.knowledge.domain.WechatLoginRequest; |
|||
import com.zdxt.knowledge.domain.WechatUser; |
|||
import com.zdxt.knowledge.mapper.WechatLoginMapper; |
|||
import com.zdxt.knowledge.service.WechatService; |
|||
import com.zdxt.knowledge.utils.HttpClientUtils; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.bouncycastle.jce.provider.BouncyCastleProvider; |
|||
import org.bouncycastle.util.encoders.Base64; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import javax.crypto.Cipher; |
|||
import javax.crypto.spec.IvParameterSpec; |
|||
import javax.crypto.spec.SecretKeySpec; |
|||
import java.security.AlgorithmParameters; |
|||
import java.security.Security; |
|||
import java.util.Arrays; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* Service业务层处理 |
|||
* |
|||
* @author ruoyi |
|||
* @date 2023-01-23 |
|||
*/ |
|||
@Service |
|||
@Slf4j |
|||
public class WechatServiceImpl implements WechatService { |
|||
|
|||
@Autowired |
|||
private WechatLoginMapper wechatLoginMapper; |
|||
|
|||
private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session"; |
|||
private static final String APPID = "wx93ec514a5c456507"; |
|||
private static final String SECRET = "f5a3f295dbee3cc1c2f73b9a324c2eb2"; |
|||
private static final String GRANT_TYPE = "authorization_code"; |
|||
|
|||
|
|||
@Override |
|||
public List<WechatUser> selectList(WechatUser wechatUser) { |
|||
return wechatLoginMapper.selectList(wechatUser); |
|||
} |
|||
|
|||
@Override |
|||
public Map<String, Object> getUserInfoMap(WechatLoginRequest loginRequest) throws Exception { |
|||
Map<String, Object> userInfoMap = new HashMap<>(); |
|||
log.info("Start get SessionKey,loginRequest的数据为:" + JSONObject.toJSONString(loginRequest)); |
|||
JSONObject sessionKeyOpenId = getSessionKeyOrOpenId(loginRequest.getCode()); |
|||
Assert.isTrue(sessionKeyOpenId != null, "获取sessionKeyOpenId出错"); |
|||
|
|||
// 获取openId && sessionKey
|
|||
String openId = sessionKeyOpenId.getString("openid"); |
|||
Assert.isTrue(openId != null, "获取openId出错"); |
|||
String sessionKey = sessionKeyOpenId.getString("session_key"); |
|||
WechatUser insertOrUpdateDO = buildWechatUserAuthInfoDO(loginRequest, openId, sessionKey); |
|||
|
|||
// 根据code保存openId和sessionKey
|
|||
JSONObject sessionObj = new JSONObject(); |
|||
sessionObj.put("openId", openId); |
|||
sessionObj.put("sessionKey", sessionKey); |
|||
// 这里的set方法使用Redis,key自行替换,10表示10天
|
|||
// stringJedisClientTem.set(WechatRedisPrefixConstant.USER_OPPEN_ID_AND_SESSION_KEY_PREFIX + loginRequest.getCode(),
|
|||
// sessionObj.toJSONString(), 10, TimeUnit.DAYS);
|
|||
|
|||
// 根据openid查询用户
|
|||
WechatUser user = wechatLoginMapper.getByOpenId(openId); |
|||
|
|||
if (user == null) { |
|||
// 用户不存在,insert用户,这里加了个分布式锁,防止insert重复用户
|
|||
// if (setLock(WechatRedisPrefixConstant.INSERT_USER_DISTRIBUTED_LOCK_PREFIX + openId, "1", 10)) {
|
|||
// // 用户入库
|
|||
// insertOrUpdateDO.setToken(getToken());
|
|||
// wechatLoginMapper.insertUser(insertOrUpdateDO);
|
|||
// userInfoMap.put("token", insertOrUpdateDO.getToken());
|
|||
// }
|
|||
insertOrUpdateDO.setGuid(StringUtils.getUUID()); |
|||
insertOrUpdateDO.setCreator("SYSTEM"); |
|||
insertOrUpdateDO.setToken(getToken(insertOrUpdateDO.getGuid(),insertOrUpdateDO.getNickname())); |
|||
wechatLoginMapper.insertUser(insertOrUpdateDO); |
|||
userInfoMap.put("token", insertOrUpdateDO.getToken()); |
|||
} else { |
|||
userInfoMap.put("token", user.getToken()); |
|||
// 已存在,做已存在的处理,如更新用户的头像,昵称等,根据openID更新
|
|||
wechatLoginMapper.updateByOpenId(insertOrUpdateDO); |
|||
} |
|||
|
|||
return userInfoMap; |
|||
} |
|||
|
|||
private JSONObject getSessionKeyOrOpenId(String code) throws Exception { |
|||
|
|||
// String apiUrl = StrUtil.format("https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code", APPID, SECRET, code);
|
|||
// String body = HttpRequest.get(apiUrl).execute().body();
|
|||
|
|||
Map<String, String> requestUrlParam = new HashMap<>(); |
|||
// 小程序appId
|
|||
requestUrlParam.put("appid", APPID); |
|||
// 小程序secret
|
|||
requestUrlParam.put("secret", SECRET); |
|||
// 小程序端返回的code
|
|||
requestUrlParam.put("js_code", code); |
|||
// 默认参数
|
|||
requestUrlParam.put("grant_type", GRANT_TYPE); |
|||
|
|||
// 发送post请求读取调用微信接口获取openid用户唯一标识
|
|||
String result = HttpClientUtils.doPost(REQUEST_URL, requestUrlParam); |
|||
return JSON.parseObject(result); |
|||
} |
|||
|
|||
private WechatUser buildWechatUserAuthInfoDO(WechatLoginRequest loginRequest, String sessionKey, String openId) { |
|||
WechatUser wechatUserDO = new WechatUser(); |
|||
wechatUserDO.setOpenId(openId); |
|||
|
|||
if (loginRequest.getRawData() != null) { |
|||
RawDataDO rawDataDO = JSON.parseObject(loginRequest.getRawData(), RawDataDO.class); |
|||
wechatUserDO.setNickname(rawDataDO.getNickName()); |
|||
wechatUserDO.setAvatarUrl(rawDataDO.getAvatarUrl()); |
|||
wechatUserDO.setGender(rawDataDO.getGender()); |
|||
wechatUserDO.setCity(rawDataDO.getCity()); |
|||
wechatUserDO.setCountry(rawDataDO.getCountry()); |
|||
wechatUserDO.setProvince(rawDataDO.getProvince()); |
|||
} |
|||
|
|||
// 解密加密信息,获取unionID
|
|||
if (loginRequest.getEncryptedData() != null) { |
|||
JSONObject encryptedData = getEncryptedData(loginRequest.getEncryptedData(), sessionKey, loginRequest.getIv()); |
|||
if (encryptedData != null) { |
|||
String unionId = encryptedData.getString("unionId"); |
|||
String phone = encryptedData.getString("phoneNumber"); |
|||
wechatUserDO.setUnionId(unionId); |
|||
} |
|||
} |
|||
|
|||
return wechatUserDO; |
|||
} |
|||
|
|||
private JSONObject getEncryptedData(String encryptedData, String sessionkey, String iv) { |
|||
// 被加密的数据
|
|||
byte[] dataByte = Base64.decode(encryptedData); |
|||
// 加密秘钥
|
|||
byte[] keyByte = Base64.decode(sessionkey); |
|||
// 偏移量
|
|||
byte[] ivByte = Base64.decode(iv); |
|||
try { |
|||
// 如果密钥不足16位,那么就补足.这个if中的内容很重要
|
|||
int base = 16; |
|||
if (keyByte.length % base != 0) { |
|||
int groups = keyByte.length / base + 1; |
|||
byte[] temp = new byte[groups * base]; |
|||
Arrays.fill(temp, (byte) 0); |
|||
System.arraycopy(keyByte, 0, temp, 0, keyByte.length); |
|||
keyByte = temp; |
|||
} |
|||
// 初始化
|
|||
Security.addProvider(new BouncyCastleProvider()); |
|||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); |
|||
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES"); |
|||
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES"); |
|||
parameters.init(new IvParameterSpec(ivByte)); |
|||
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
|
|||
byte[] resultByte = cipher.doFinal(dataByte); |
|||
if (null != resultByte && resultByte.length > 0) { |
|||
String result = new String(resultByte, "UTF-8"); |
|||
return JSONObject.parseObject(result); |
|||
} |
|||
} catch (Exception e) { |
|||
log.error("解密加密信息报错", e.getMessage()); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
// private boolean setLock(String key, String value, long expire) throws Exception {
|
|||
// boolean result = stringJedisClientTem.setNx(key, value, expire, TimeUnit.SECONDS);
|
|||
// return result;
|
|||
// }
|
|||
|
|||
private String getToken(String guid, String nickname) throws Exception { |
|||
// 写入用户信息
|
|||
HashMap<String, Object> tokenMap = new HashMap<>(); |
|||
tokenMap.put("userName",nickname); |
|||
tokenMap.put("id",guid); |
|||
|
|||
// 生成token
|
|||
//String token = JWTUtil.createToken(tokenMap,tokenKey.getBytes());
|
|||
return StringUtils.getUUID(); |
|||
} |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
package com.zdxt.knowledge.utils; |
|||
|
|||
import org.apache.http.NameValuePair; |
|||
import org.apache.http.client.config.RequestConfig; |
|||
import org.apache.http.client.entity.UrlEncodedFormEntity; |
|||
import org.apache.http.client.methods.CloseableHttpResponse; |
|||
import org.apache.http.client.methods.HttpPost; |
|||
import org.apache.http.impl.client.CloseableHttpClient; |
|||
import org.apache.http.impl.client.HttpClients; |
|||
import org.apache.http.message.BasicNameValuePair; |
|||
import org.apache.http.util.EntityUtils; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
public class HttpClientUtils { |
|||
|
|||
final static int TIMEOUT = 1000; |
|||
|
|||
final static int TIMEOUT_MSEC = 5 * 1000; |
|||
|
|||
public static String doPost(String url, Map<String, String> paramMap) throws IOException { |
|||
// 创建Httpclient对象
|
|||
CloseableHttpClient httpClient = HttpClients.createDefault(); |
|||
CloseableHttpResponse response = null; |
|||
String resultString = ""; |
|||
|
|||
try { |
|||
// 创建Http Post请求
|
|||
HttpPost httpPost = new HttpPost(url); |
|||
|
|||
// 创建参数列表
|
|||
if (paramMap != null) { |
|||
List<NameValuePair> paramList = new ArrayList<>(); |
|||
for (Map.Entry<String, String> param : paramMap.entrySet()) { |
|||
paramList.add(new BasicNameValuePair(param.getKey(), param.getValue())); |
|||
} |
|||
// 模拟表单
|
|||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); |
|||
httpPost.setEntity(entity); |
|||
} |
|||
|
|||
httpPost.setConfig(builderRequestConfig()); |
|||
|
|||
// 执行http请求
|
|||
response = httpClient.execute(httpPost); |
|||
|
|||
resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); |
|||
} catch (Exception e) { |
|||
throw e; |
|||
} finally { |
|||
try { |
|||
response.close(); |
|||
} catch (IOException e) { |
|||
throw e; |
|||
} |
|||
} |
|||
|
|||
return resultString; |
|||
} |
|||
|
|||
private static RequestConfig builderRequestConfig() { |
|||
return RequestConfig.custom() |
|||
.setConnectTimeout(TIMEOUT_MSEC) |
|||
.setConnectionRequestTimeout(TIMEOUT_MSEC) |
|||
.setSocketTimeout(TIMEOUT_MSEC).build(); |
|||
} |
|||
} |
|||
@ -0,0 +1,104 @@ |
|||
<?xml version="1.0" encoding="UTF-8" ?> |
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
|||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
<mapper namespace="com.zdxt.knowledge.mapper.WechatLoginMapper"> |
|||
|
|||
<resultMap type="com.zdxt.knowledge.domain.WechatUser" id="WechatUserResult"> |
|||
<result property="guid" column="guid"/> |
|||
<result property="token" column="token"/> |
|||
<result property="nickname" column="nickname"/> |
|||
<result property="avatarUrl" column="avatar_url"/> |
|||
<result property="gender" column="gender"/> |
|||
<result property="country" column="country"/> |
|||
<result property="province" column="province"/> |
|||
<result property="city" column="city"/> |
|||
<result property="mobile" column="mobile"/> |
|||
<result property="openId" column="open_id"/> |
|||
<result property="unionId" column="union_id"/> |
|||
<result property="creator" column="creator"/> |
|||
<result property="createTime" column="create_time"/> |
|||
</resultMap> |
|||
|
|||
<sql id="selectWechatUserVo"> |
|||
select guid, |
|||
token, |
|||
nickname, |
|||
avatar_url, |
|||
gender, |
|||
country, |
|||
province, |
|||
city, |
|||
mobile, |
|||
creator, |
|||
create_time, |
|||
open_id, |
|||
union_id |
|||
from wechat_user |
|||
</sql> |
|||
|
|||
<select id="selectList" parameterType="WechatUser" resultMap="WechatUserResult"> |
|||
<include refid="selectWechatUserVo"/> |
|||
<where> |
|||
<if test="nickname != null and nickname != ''">and `nickname` like concat('%', #{nickname}, '%')</if> |
|||
<if test="mobile != null and mobile != ''">and mobile = #{mobile}</if> |
|||
</where> |
|||
order by create_time ASC |
|||
</select> |
|||
|
|||
<select id="getByOpenId" parameterType="String" resultMap="WechatUserResult"> |
|||
<include refid="selectWechatUserVo"/> |
|||
where open_id = #{openId} |
|||
</select> |
|||
|
|||
<insert id="insertUser" parameterType="WechatUser"> |
|||
insert into wechat_user |
|||
<trim prefix="(" suffix=")" suffixOverrides=","> |
|||
<if test="guid != null ">guid,</if> |
|||
<if test="token != null and token != ''">`token`,</if> |
|||
<if test="nickname != null and nickname != ''">nickname,</if> |
|||
<if test="avatarUrl != null and avatarUrl != ''">avatar_url,</if> |
|||
<if test="gender != null and gender != ''">gender,</if> |
|||
<if test="country != null and country != ''">country,</if> |
|||
<if test="province != null and province != ''">province,</if> |
|||
<if test="city != null and city != ''">`city`,</if> |
|||
<if test="mobile != null and mobile != ''">mobile,</if> |
|||
<if test="creator != null and creator != ''">creator,</if> |
|||
<if test="createTime != null ">create_time,</if> |
|||
<if test="openId != null and openId != ''">open_id,</if> |
|||
<if test="unionId != null and unionId != ''">union_id,</if> |
|||
</trim> |
|||
<trim prefix="values (" suffix=")" suffixOverrides=","> |
|||
<if test="guid != null ">#{guid},</if> |
|||
<if test="token != null and token != ''">#{token},</if> |
|||
<if test="nickname != null and nickname != ''">#{nickname},</if> |
|||
<if test="avatarUrl != null and avatarUrl != ''">#{avatarUrl},</if> |
|||
<if test="gender != null and gender != ''">#{gender},</if> |
|||
<if test="country != null and country != ''">#{country},</if> |
|||
<if test="province != null and province != ''">#{province},</if> |
|||
<if test="city != null and city != ''">#{city},</if> |
|||
<if test="mobile != null and mobile != ''">#{mobile},</if> |
|||
<if test="creator != null and creator != ''">#{creator},</if> |
|||
<if test="createTime != null ">#{createTime},</if> |
|||
<if test="openId != null and openId != ''">#{openId},</if> |
|||
<if test="unionId != null and unionId != ''">#{unionId},</if> |
|||
</trim> |
|||
</insert> |
|||
|
|||
<update id="updateByOpenId" parameterType="WechatUser"> |
|||
update wechat_user |
|||
<trim prefix="SET" suffixOverrides=","> |
|||
<if test="token != null and token != ''">token = #{token},</if> |
|||
<if test="nickname != null and nickname != ''">nickname = #{nickname},</if> |
|||
<if test="avatarUrl != null and avatarUrl != ''">avatar_url = #{avatarUrl},</if> |
|||
<if test="gender != null and gender != ''">gender = #{gender},</if> |
|||
<if test="country != null and country != ''">country = #{country},</if> |
|||
<if test="province != null and province != ''">province = #{province},</if> |
|||
<if test="city != null and city != ''">city = #{city},</if> |
|||
<if test="mobile != null and mobile != ''">`mobile` = #{mobile},</if> |
|||
<if test="openId != null and openId != ''">open_id = #{openId},</if> |
|||
<if test="unionId != null and unionId != ''">union_id = #{unionId},</if> |
|||
</trim> |
|||
where guid = #{guid} |
|||
</update> |
|||
|
|||
</mapper> |
|||
Loading…
Reference in new issue