Commit 803c2371ea17b9c9068974ab67cc074fa2143cce

Authored by shiyang
1 parent 0e5e1cc8e8
Exists in master

获取公众号Openid逻辑

Showing 6 changed files with 82 additions and 6 deletions

talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/ArticleController.java View file @ 803c237
... ... @@ -8,6 +8,8 @@
8 8 import com.lyms.talkonlineweb.result.BaseResponse;
9 9 import com.lyms.talkonlineweb.service.*;
10 10 import com.lyms.talkonlineweb.service.impl.PushedartlogsInfoServiceImpl;
  11 +import com.lyms.talkonlineweb.util.StringUtil;
  12 +import com.lyms.talkonlineweb.util.WeiXinUtil;
11 13 import lombok.extern.log4j.Log4j2;
12 14 import org.apache.commons.io.IOUtils;
13 15 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -53,6 +55,10 @@
53 55  
54 56 @Autowired
55 57 private LymsPushMessagesService lymsPushMessagesService;
  58 +
  59 + @Autowired
  60 + private LymsPatientService lymsPatientService;
  61 +
56 62 /**
57 63 * 上传文件
58 64 *
... ... @@ -382,6 +388,36 @@
382 388 baseResponse.setErrormsg("失败");
383 389 e.printStackTrace();
384 390 }
  391 + return baseResponse;
  392 + }
  393 +
  394 + /**
  395 + * 登录小程序后获取小程序关联公众号的OpenId
  396 + *
  397 + * @return
  398 + */
  399 + @GetMapping("getGZOpenId")
  400 + public BaseResponse getGZOpenId(LymsPatient patient){
  401 + BaseResponse baseResponse=new BaseResponse();
  402 + try {
  403 + if(StringUtil.isNotEmpty(patient.getCode())&&null!=patient.getId()){
  404 + String openid = WeiXinUtil.getWxGzOpenId(patient.getCode());
  405 + if(StringUtil.isNotEmpty(openid)){
  406 + //更新到登录患者的gzopenid
  407 + boolean b = lymsPatientService.updateById(patient);
  408 + if(b){
  409 + baseResponse.setErrormsg("成功");
  410 + return baseResponse;
  411 + }
  412 + }
  413 + }
  414 + } catch (Exception e) {
  415 + baseResponse.setErrorcode(1);
  416 + baseResponse.setErrormsg("失败");
  417 + e.printStackTrace();
  418 + }
  419 + baseResponse.setErrorcode(1);
  420 + baseResponse.setErrormsg("失败");
385 421 return baseResponse;
386 422 }
387 423  
talkonlineweb/src/main/java/com/lyms/talkonlineweb/domain/LymsPatient.java View file @ 803c237
... ... @@ -108,7 +108,7 @@
108 108  
109 109 @TableField(exist = false)
110 110 private static final long serialVersionUID = 1L;
111   -// 小程序上传的code
  111 +// 小程序公众号上传的code
112 112 @TableField(exist = false)
113 113 private String code;
114 114  
talkonlineweb/src/main/java/com/lyms/talkonlineweb/task/AccessTokenServlet.java View file @ 803c237
1 1 package com.lyms.talkonlineweb.task;
2 2  
3 3 import com.alibaba.fastjson.JSONObject;
  4 +import com.lyms.talkonlineweb.util.Constant;
4 5 import com.lyms.talkonlineweb.util.HttpUtil;
5 6 import com.lyms.talkonlineweb.util.StringUtil;
6 7 import com.lyms.talkonlineweb.util.WeiXinUtil;
... ... @@ -17,8 +18,6 @@
17 18 */
18 19 @Component
19 20 public class AccessTokenServlet extends HttpServlet implements Runnable {
20   - private final String appId ="wxd3c36244d006cb90";//公众号appid
21   - private final String appSecret ="fc80b5dd03a581a088adcd2c65a7e10a";//公众号AppSecret
22 21 public static String accessToken = null;
23 22  
24 23 /**
... ... @@ -59,8 +58,8 @@
59 58 // 授予形式
60 59 String grant_type = "client_credential";
61 60 // 接口地址拼接参数
62   - String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + appId
63   - + "&secret=" + appSecret;
  61 + String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + Constant.GZ_APP_ID
  62 + + "&secret=" + Constant.GZ_SECRET;
64 63 String tokenJsonStr = WeiXinUtil.repeatDoGetPost(getTokenApi, "GET", null);
65 64 if(StringUtil.isEmpty(tokenJsonStr)){
66 65 System.out.println("获取TOKEN :失败,repeatDoGetPost返回值为空>>>>>>>");
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/Constant.java View file @ 803c237
... ... @@ -45,6 +45,14 @@
45 45 * 小程序的 app secret
46 46 */
47 47 public static final String WX_SECRET = "005ab68859ca2504b7217dac4c903cd2";
  48 + /**
  49 + * 公众号的appid
  50 + */
  51 + public static final String GZ_APP_ID ="wxd3c36244d006cb90";
48 52  
  53 + /**
  54 + * 公众号AppSecret
  55 + */
  56 + public static final String GZ_SECRET ="fc80b5dd03a581a088adcd2c65a7e10a";
49 57 }
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HttpUtil.java View file @ 803c237
... ... @@ -96,7 +96,7 @@
96 96 }
97 97  
98 98 /**
99   - * 调用接口 post
  99 + * 调用接口 getOrpost
100 100 * 微信公众号
101 101 * @param apiPath
102 102 */
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/WeiXinUtil.java View file @ 803c237
... ... @@ -5,6 +5,7 @@
5 5 import com.lyms.talkonlineweb.result.BaseResponse;
6 6 import lombok.extern.log4j.Log4j2;
7 7 import org.springframework.util.CollectionUtils;
  8 +import org.springframework.util.StringUtils;
8 9  
9 10 import java.util.HashMap;
10 11 import java.util.Map;
... ... @@ -78,6 +79,38 @@
78 79 Map<String,Object> resultMap = JSON.parseObject(result, HashMap.class);
79 80 return Integer.parseInt(resultMap.get("errcode").toString());
80 81  
  82 + }
  83 +
  84 + /**
  85 + * 获取微信用户的公众号openid
  86 + *
  87 + * @param code 登录时获取的code
  88 + * @return 微信方获取openid
  89 + */
  90 + public static String getWxGzOpenId(String code)
  91 + {
  92 + if (StringUtils.isEmpty(code)) {
  93 + log.info("code:null");
  94 + return null;
  95 + }
  96 + try {
  97 + // 接口地址
  98 + 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";
  99 + log.info(reqUrl);
  100 + String result = repeatDoGetPost(reqUrl, "GET", null);
  101 + log.info("result :" + result);
  102 + if (StringUtils.isEmpty(result)) {
  103 + log.info("取微信用户的公众号openid : 请求返回null..." );
  104 + return null;
  105 + }
  106 + Map<String, Object> resultMap = JSON.parseObject(result, HashMap.class);
  107 + if (resultMap.containsKey(OPEN_ID_KEY)) {
  108 + return resultMap.get(OPEN_ID_KEY).toString();
  109 + }
  110 + } catch (Exception e) {
  111 + e.printStackTrace();
  112 + }
  113 + return null;
81 114 }
82 115  
83 116 /**