feat:增加微信小程序登录
This commit is contained in:
@@ -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