Merge branch 'open_gj' into 'master'

添加测试小程序code

See merge request !53
This commit is contained in:
2021-07-08 15:30:46 +08:00
5 changed files with 75 additions and 1 deletions
@@ -217,6 +217,23 @@ public class UserController {
String username = StringUtils.trimToNull(model.getUsername());
String pass = StringUtils.trimToNull(model.getPass());
String weiXinId = CookieUtils.getCookieValue(request,"we_chat_open_id");
if(StringUtils.isBlank(weiXinId) && StringUtils.isNotBlank(model.getCode())){
System.out.println("小程序code:"+model.getCode());
WeChatAuthServiceImpl.WeChatWebToken token = weChatAuthService.getWeChatWebToken(model.getCode(), model.getAppId(), model.getAppSecret());
if(token != null) {
weiXinId = token.getOpenid();
} else {
weiXinId = CookieUtils.getCookieValue(request,"we_chat_open_id");
}
if(weiXinId == null) {
weiXinId = CookieUtils.getCookieValue(request,"we_chat_open_id");
}
if(weiXinId != null && !weiXinId.equals("")){
CookieUtils.setCookie("we_chat_open_id", weiXinId, "/", 60 * 60 * 24 *7, response);
}
System.out.println("小程序微信OPEN_ID:" + weiXinId);
}
//测试时可以先去掉
if (StringUtils.isBlank(username) || StringUtils.isBlank(pass) || StringUtils.isBlank(weiXinId)){
resultModel.setMsg(Message.PARAM_NOT_NULL);
@@ -16,6 +16,9 @@ public class UserModel extends BaseModel {
private String pass;
private String code;
private List<String> citys;
private String appId;//²âÊÔ
private String appSecret;//²âÊÔ
public List<String> getCitys() {
return citys;
@@ -80,4 +83,20 @@ public class UserModel extends BaseModel {
public void setCode(String code) {
this.code = code;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
}
@@ -77,7 +77,8 @@
url : "../user/enterpriseLogin",
data : {
"username" : username,
"pass":pass
"pass":pass,
"code":"${code!''}"
},
success : function(result) {
var re = $.parseJSON(result);
@@ -24,4 +24,7 @@ public interface WeChatAuthService {
*/
WeChatAuthServiceImpl.WeChatWebToken getWeChatWebToken(String code);
WeChatAuthServiceImpl.WeChatWebToken getWeChatWebToken(String code, String appId, String appSecret);
}
@@ -108,6 +108,40 @@ public class WeChatAuthServiceImpl implements WeChatAuthService{
}
public WeChatWebToken getWeChatWebToken(String code, String appId, String appSecret){
if(code == null){
return null;
}
HttpRequest request = new HttpRequest();
Map<String, String> params = new HashMap<String, String>();
params.put("appid", appId);
params.put("grant_type", "authorization_code");
params.put("secret", appSecret);
params.put("code", code);
try {
JSONObject json = request.getJsonContent(WE_CHAT_API_HOST + "/sns/oauth2/access_token",params ,"UTF-8");
if(json.containsKey("access_token")){
WeChatWebToken token = new WeChatWebToken();
token.accessToken = json.getString("access_token");
token.expiresIn = json.getInt("expires_in");
token.refreshToken= json.getString("refresh_token");
token.openid = json.getString("openid");
token.scope = json.getString("scope");
return token;
}else{
logger.error("Web Access Token 获取失败:" + json.toString());
}
} catch (HttpRequestException e) {
logger.error("Web Access Token 获取失败:", e);
}
return null;
}
public static class WeChatToken implements Serializable{