|
@@ -0,0 +1,155 @@
|
|
|
+package com.care.client.passport;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.care.client.config.PlatformConfig;
|
|
|
+import com.care.client.vo.MemberInitParams;
|
|
|
+import com.care.common.cache.RedisUtil;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.text.MessageFormat;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class WxPassportService extends AbstractPassportService {
|
|
|
+
|
|
|
+ /* @Resource
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PlatformConfig platformConfig;
|
|
|
+
|
|
|
+ Gson gson = new Gson();
|
|
|
+
|
|
|
+ public ResultData login(MemberInitParams params) {
|
|
|
+ WxConfig wxConfig = platformConfig.wxConfig();
|
|
|
+ // 解析codeUrl
|
|
|
+ String codeUrl = MessageFormat.format(wxConfig.getCodeUrl(), wxConfig.getAppId(), wxConfig.getSecret(), params.getCode());
|
|
|
+ // 通过codeUrl获取openid
|
|
|
+ JSONObject jsonObject = this.httpGet(codeUrl);
|
|
|
+ log.warn("-----jsonObject:[{}]", jsonObject);
|
|
|
+
|
|
|
+ String openid = jsonObject.getString("openid");
|
|
|
+ String sessionKey = jsonObject.getString("session_key"); // TODO 暂时用不到
|
|
|
+ log.warn("-----openid:[{}], session_key:[{}]", openid, sessionKey);
|
|
|
+
|
|
|
+ // TODO 测试阶段先注释掉
|
|
|
+ params.setOpenid(openid);
|
|
|
+ return cache(params);
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject httpGet(String url) {
|
|
|
+ HttpResponse response = HttpRequest.get(url).send();
|
|
|
+ String json = response.charset("utf-8").bodyText();
|
|
|
+ JSONObject jsonObject = JSON.parseObject(json);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String accessToken() {
|
|
|
+ WxConfig wxConfig = platformConfig.wxConfig();
|
|
|
+ String access_token = (String) redisUtil.hget(RedisKeyConstant.WX_ACCESS_TOKEN, wxConfig.getAppId());
|
|
|
+ if (access_token != null) {
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
+ return this.flushWxToken();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String flushWxToken() {
|
|
|
+ WxConfig wxConfig = platformConfig.wxConfig();
|
|
|
+ // 解析codeUrl
|
|
|
+ String tokenUrl = MessageFormat.format(wxConfig.getTokenUrl(), wxConfig.getAppId(), wxConfig.getSecret());
|
|
|
+ // 通过codeUrl获取openid
|
|
|
+ JSONObject jsonObject = this.httpGet(tokenUrl);
|
|
|
+ String access_token = jsonObject.getString("access_token");
|
|
|
+ redisUtil.hset(RedisKeyConstant.WX_ACCESS_TOKEN, wxConfig.getAppId(), access_token, RedisKeyConstant.WX_ACCESS_TOKEN_TIME);
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject httpPost(String url, String content) {
|
|
|
+ HttpRequest httpRequest = HttpRequest.post(url).bodyText(content, "application/json", "utf-8");
|
|
|
+ HttpResponse res = httpRequest.send();
|
|
|
+ String json = res.charset("utf-8").bodyText();
|
|
|
+ JSONObject jsonObject = JSON.parseObject(json);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int msgSecCheck(String content){
|
|
|
+ WxConfig wxConfig = platformConfig.wxConfig();
|
|
|
+ int result = 0;
|
|
|
+ String url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + this.accessToken();
|
|
|
+ Map<String ,String> map = Maps.newHashMap();
|
|
|
+ map.put("content",content);
|
|
|
+ String resBody = cn.hutool.http.HttpRequest.post(url).header(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8").body(gson.toJson(map)).execute().body();
|
|
|
+ if (StrUtil.isNotEmpty(resBody)){
|
|
|
+ JsonObject object = gson.fromJson(resBody, JsonObject.class);
|
|
|
+ JsonElement ele = object.get("errcode");
|
|
|
+ if (ele != null){
|
|
|
+ if (ele.getAsInt() == 0 ){
|
|
|
+ result = 1;
|
|
|
+ }else if(ele.getAsInt() == 87014){
|
|
|
+ result = -1;
|
|
|
+ }else{
|
|
|
+ result = 9999;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public boolean msgCheck(Object object) {
|
|
|
+ boolean flag = true;
|
|
|
+ try{
|
|
|
+ StringBuffer text = new StringBuffer();
|
|
|
+ Field[] fields = object.getClass().getDeclaredFields();
|
|
|
+ for (Field field : fields){
|
|
|
+ field.setAccessible( true );
|
|
|
+ if (field.isAnnotationPresent(MsgSecAnnotation.class)){
|
|
|
+ if (null != field.get(object)){
|
|
|
+ String value = (String)field.get(object);
|
|
|
+ if (StrUtil.isNotBlank(value)){
|
|
|
+ text.append(value).append("|");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotEmpty(text.toString())){
|
|
|
+ int secCheck = this.msgSecCheck(text.toString());
|
|
|
+ if (-1 == secCheck){
|
|
|
+ flag = false;
|
|
|
+ }else{
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }catch (Exception e){
|
|
|
+
|
|
|
+ }
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String code = "0117jbby1dPIob0szIdy1Xidby17jbby";
|
|
|
+ String appid = "wxfaea3a9cf3b92ded";
|
|
|
+ String secret = "36bd50d9a65c2950c00d85338fc76c25";
|
|
|
+ String url = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code";
|
|
|
+ String codeUrl = MessageFormat.format(url, appid, secret, code);
|
|
|
+ // 通过codeUrl获取openid
|
|
|
+ HttpResponse response = HttpRequest.get(codeUrl).send();
|
|
|
+ String json = response.charset("utf-8").bodyText();
|
|
|
+ JSONObject jsonObject = JSON.parseObject(json);
|
|
|
+
|
|
|
+ log.warn("-----jsonObject:[{}], ", jsonObject);
|
|
|
+ String openid = jsonObject.getString("openid");
|
|
|
+ String sessionKey = jsonObject.getString("session_key"); // TODO 暂时用不到
|
|
|
+ log.warn("-----openid:[{}], session_key:[{}]", openid, sessionKey);
|
|
|
+ }*/
|
|
|
+}
|