feat:增加微信小程序登录
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
package com.tiesheng.login.config.zust;
|
||||
|
||||
public class CasLoginDTO {
|
||||
|
||||
private String data;
|
||||
|
||||
/**
|
||||
* 工号
|
||||
*/
|
||||
private String casUser;
|
||||
|
||||
/**
|
||||
* 用户容器
|
||||
*/
|
||||
private String casUserContainerId;
|
||||
|
||||
/**
|
||||
* 用户姓名
|
||||
*/
|
||||
private String casUserCn;
|
||||
|
||||
/**
|
||||
* 用户别名
|
||||
*/
|
||||
private String casUserAlias;
|
||||
/**
|
||||
* 用户所在组
|
||||
*/
|
||||
private String casUserMemberOf;
|
||||
/**
|
||||
* 用户性别
|
||||
*/
|
||||
private String casUserGender;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getCasUser() {
|
||||
return casUser;
|
||||
}
|
||||
|
||||
public void setCasUser(String casUser) {
|
||||
this.casUser = casUser;
|
||||
}
|
||||
|
||||
public String getCasUserContainerId() {
|
||||
return casUserContainerId;
|
||||
}
|
||||
|
||||
public void setCasUserContainerId(String casUserContainerId) {
|
||||
this.casUserContainerId = casUserContainerId;
|
||||
}
|
||||
|
||||
public String getCasUserCn() {
|
||||
return casUserCn;
|
||||
}
|
||||
|
||||
public void setCasUserCn(String casUserCn) {
|
||||
this.casUserCn = casUserCn;
|
||||
}
|
||||
|
||||
public String getCasUserAlias() {
|
||||
return casUserAlias;
|
||||
}
|
||||
|
||||
public void setCasUserAlias(String casUserAlias) {
|
||||
this.casUserAlias = casUserAlias;
|
||||
}
|
||||
|
||||
public String getCasUserMemberOf() {
|
||||
return casUserMemberOf;
|
||||
}
|
||||
|
||||
public void setCasUserMemberOf(String casUserMemberOf) {
|
||||
this.casUserMemberOf = casUserMemberOf;
|
||||
}
|
||||
|
||||
public String getCasUserGender() {
|
||||
return casUserGender;
|
||||
}
|
||||
|
||||
public void setCasUserGender(String casUserGender) {
|
||||
this.casUserGender = casUserGender;
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,14 @@ import com.tiesheng.login.config.token.bean.TokenBean;
|
||||
import com.tiesheng.login.pojos.CodeExtraDTO;
|
||||
import com.tiesheng.login.pojos.DoLoginInfo;
|
||||
import com.tiesheng.login.pojos.UniqueIndexDTO;
|
||||
import com.tiesheng.login.pojos.WxminiLoginVo;
|
||||
import com.tiesheng.login.service.TieshengLoginConfigurer;
|
||||
import com.tiesheng.platform.config.ding.PlatformDingConfig;
|
||||
import com.tiesheng.platform.config.ding.bean.DingJsapiSignature;
|
||||
import com.tiesheng.platform.config.ding.bean.DingUserInfo;
|
||||
import com.tiesheng.platform.config.wxmini.PlatformWxminiConfig;
|
||||
import com.tiesheng.platform.config.wxmp.PlatformWxmpConfig;
|
||||
import com.tiesheng.platform.config.wxmp.bean.WxConfigBean;
|
||||
import com.tiesheng.platform.config.wxmp.bean.WxJsapiSignature;
|
||||
import com.tiesheng.platform.config.wxmp.bean.WxUserInfo;
|
||||
import com.tiesheng.util.config.GlobalConfig;
|
||||
@@ -40,6 +43,8 @@ public class LoginController {
|
||||
@Autowired
|
||||
PlatformDingConfig platformDingConfig;
|
||||
@Autowired
|
||||
PlatformWxminiConfig platformWxminiConfig;
|
||||
@Autowired
|
||||
TieshengLoginConfigurer tieshengLoginConfigurer;
|
||||
@Autowired
|
||||
GlobalConfig globalConfig;
|
||||
@@ -186,5 +191,30 @@ public class LoginController {
|
||||
return ApiResp.respOK(jsapiSignature);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// 小程序相关
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 微信小程序登录
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/wxmini/index/{service}")
|
||||
@TokenIgnore
|
||||
public ApiResp<WxminiLoginVo> wxminiIndex(@PathVariable String service, String code) {
|
||||
String openid = platformWxminiConfig.jscode2session(service, code);
|
||||
WxConfigBean configBean = platformWxminiConfig.getConfigBean(service);
|
||||
TokenBean tokenBean = tieshengLoginConfigurer.doLogin(new DoLoginInfo(configBean.getAppId(),
|
||||
openid, "wxmini", "mini", "{}"));
|
||||
|
||||
WxminiLoginVo loginVo = new WxminiLoginVo();
|
||||
loginVo.setOpenid(openid);
|
||||
if (tokenBean != null) {
|
||||
loginVo.setToken(tokenBean.toToken());
|
||||
}
|
||||
return ApiResp.respOK(loginVo);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.tiesheng.login.pojos;
|
||||
|
||||
public class WxminiLoginVo {
|
||||
|
||||
private String openid;
|
||||
private String token;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getOpenid() {
|
||||
return openid;
|
||||
}
|
||||
|
||||
public void setOpenid(String openid) {
|
||||
this.openid = openid;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.tiesheng.platform.config.wxmini;
|
||||
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.tiesheng.platform.config.wxmp.bean.WxConfigBean;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "platform.wxmini")
|
||||
public class PlatformWxminiConfig {
|
||||
|
||||
private WxConfigBean global;
|
||||
private Map<String, WxConfigBean> configs = MapUtil.newHashMap();
|
||||
|
||||
/**
|
||||
* 获取一个DingConfigBean
|
||||
*
|
||||
* @param service
|
||||
* @return
|
||||
*/
|
||||
public WxConfigBean getConfigBean(String service) {
|
||||
WxConfigBean bean = configs.get(service);
|
||||
if (bean == null) {
|
||||
bean = global;
|
||||
}
|
||||
if (bean == null) {
|
||||
throw new ApiException("该服务未配置微信小程序授权");
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// 业务逻辑
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 通过code获取openid
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String jscode2session(String service, String code) {
|
||||
WxConfigBean configBean = getConfigBean(service);
|
||||
String body = HttpRequest.get("https://api.weixin.qq.com/sns/jscode2session"
|
||||
+ "?appid=" + configBean.getAppId()
|
||||
+ "&secret=" + configBean.getAppSecret()
|
||||
+ "&js_code=" + code + "&grant_type=authorization_code").execute().body();
|
||||
JSONObject object = JSONUtil.parseObj(body);
|
||||
return object.getStr("openid");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取access_token
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getAccessToken(String service) {
|
||||
WxConfigBean configBean = getConfigBean(service);
|
||||
String body = HttpRequest.get("https://api.weixin.qq.com/cgi-bin/token"
|
||||
+ "?grant_type=client_credential&appid=" + configBean.getAppId()
|
||||
+ "&secret=" + configBean.getAppSecret()).execute().body();
|
||||
JSONObject object = JSONUtil.parseObj(body);
|
||||
return object.getStr("access_token");
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public Map<String, WxConfigBean> getConfigs() {
|
||||
return configs;
|
||||
}
|
||||
|
||||
public void setConfigs(Map<String, WxConfigBean> configs) {
|
||||
this.configs = configs;
|
||||
}
|
||||
|
||||
public WxConfigBean getGlobal() {
|
||||
return global;
|
||||
}
|
||||
|
||||
public void setGlobal(WxConfigBean global) {
|
||||
this.global = global;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user