diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java index ba80db7..7e787cd 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java @@ -8,6 +8,8 @@ import com.lyms.talkonlineweb.domain.*; import com.lyms.talkonlineweb.result.BaseResponse; import com.lyms.talkonlineweb.service.*; import com.lyms.talkonlineweb.service.impl.PushedartlogsInfoServiceImpl; +import com.lyms.talkonlineweb.util.StringUtil; +import com.lyms.talkonlineweb.util.WeiXinUtil; import lombok.extern.log4j.Log4j2; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -53,6 +55,10 @@ public class ArticleController { @Autowired private LymsPushMessagesService lymsPushMessagesService; + + @Autowired + private LymsPatientService lymsPatientService; + /** * 上传文件 * @@ -385,4 +391,34 @@ public class ArticleController { return baseResponse; } + /** + * 登录小程序后获取小程序关联公众号的OpenId + * + * @return + */ + @GetMapping("getGZOpenId") + public BaseResponse getGZOpenId(LymsPatient patient){ + BaseResponse baseResponse=new BaseResponse(); + try { + if(StringUtil.isNotEmpty(patient.getCode())&&null!=patient.getId()){ + String openid = WeiXinUtil.getWxGzOpenId(patient.getCode()); + if(StringUtil.isNotEmpty(openid)){ + //更新到登录患者的gzopenid + boolean b = lymsPatientService.updateById(patient); + if(b){ + baseResponse.setErrormsg("成功"); + return baseResponse; + } + } + } + } catch (Exception e) { + baseResponse.setErrorcode(1); + baseResponse.setErrormsg("失败"); + e.printStackTrace(); + } + baseResponse.setErrorcode(1); + baseResponse.setErrormsg("失败"); + return baseResponse; + } + } diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPatient.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPatient.java index 39822bd..dde9607 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPatient.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPatient.java @@ -108,7 +108,7 @@ public class LymsPatient implements Serializable { @TableField(exist = false) private static final long serialVersionUID = 1L; -// 小程序上传的code +// 小程序公众号上传的code @TableField(exist = false) private String code; diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java index bb8b830..fd88466 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java @@ -1,6 +1,7 @@ package com.lyms.talkonlineweb.task; import com.alibaba.fastjson.JSONObject; +import com.lyms.talkonlineweb.util.Constant; import com.lyms.talkonlineweb.util.HttpUtil; import com.lyms.talkonlineweb.util.StringUtil; import com.lyms.talkonlineweb.util.WeiXinUtil; @@ -17,8 +18,6 @@ import javax.servlet.http.HttpServlet; */ @Component public class AccessTokenServlet extends HttpServlet implements Runnable { - private final String appId ="wxd3c36244d006cb90";//公众号appid - private final String appSecret ="fc80b5dd03a581a088adcd2c65a7e10a";//公众号AppSecret public static String accessToken = null; /** @@ -59,8 +58,8 @@ public class AccessTokenServlet extends HttpServlet implements Runnable { // 授予形式 String grant_type = "client_credential"; // 接口地址拼接参数 - String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + appId - + "&secret=" + appSecret; + String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + Constant.GZ_APP_ID + + "&secret=" + Constant.GZ_SECRET; String tokenJsonStr = WeiXinUtil.repeatDoGetPost(getTokenApi, "GET", null); if(StringUtil.isEmpty(tokenJsonStr)){ System.out.println("获取TOKEN :失败,repeatDoGetPost返回值为空>>>>>>>"); diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java index 187c70f..a24206e 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java @@ -45,5 +45,13 @@ public class Constant { * 小程序的 app secret */ public static final String WX_SECRET = "005ab68859ca2504b7217dac4c903cd2"; + /** + * 公众号的appid + */ + public static final String GZ_APP_ID ="wxd3c36244d006cb90"; + /** + * 公众号AppSecret + */ + public static final String GZ_SECRET ="fc80b5dd03a581a088adcd2c65a7e10a"; } diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java index a6ce300..95d80a3 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java @@ -96,7 +96,7 @@ public class HttpUtil { } /** - * 调用接口 post + * 调用接口 getOrpost * 微信公众号 * @param apiPath */ diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java index 36d4dd1..4ed2178 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java @@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject; import com.lyms.talkonlineweb.result.BaseResponse; import lombok.extern.log4j.Log4j2; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import java.util.HashMap; import java.util.Map; @@ -81,6 +82,38 @@ public class WeiXinUtil { } /** + * 获取微信用户的公众号openid + * + * @param code 登录时获取的code + * @return 微信方获取openid + */ + public static String getWxGzOpenId(String code) + { + if (StringUtils.isEmpty(code)) { + log.info("code:null"); + return null; + } + try { + // 接口地址 + String reqUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + Constant.GZ_APP_ID + "&secret=" + Constant.GZ_SECRET + "&js_code=" + code + "&grant_type=authorization_code"; + log.info(reqUrl); + String result = repeatDoGetPost(reqUrl, "GET", null); + log.info("result :" + result); + if (StringUtils.isEmpty(result)) { + log.info("取微信用户的公众号openid : 请求返回null..." ); + return null; + } + Map resultMap = JSON.parseObject(result, HashMap.class); + if (resultMap.containsKey(OPEN_ID_KEY)) { + return resultMap.get(OPEN_ID_KEY).toString(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** * 网络超时-重新发送3次 */ public static String repeatDoGetPost(String sendMsgApi,String type,Map paramMap) {