From 8e31667fb6288bb5098f8b9183774a7a5d71e706 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期一, 08 十一月 2021 14:41:08 +0800 Subject: [PATCH] 企业微信:小程序openId和企业userId相互转化 --- src/main/java/com/hx/util/corp/entity/OpenIdAUserId.java | 62 +++++++++++++++++++++++++++++++ src/main/java/com/hx/util/corp/CorpMpUtil.java | 50 ++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/hx/util/corp/CorpMpUtil.java b/src/main/java/com/hx/util/corp/CorpMpUtil.java index d27faee..a10a8dd 100644 --- a/src/main/java/com/hx/util/corp/CorpMpUtil.java +++ b/src/main/java/com/hx/util/corp/CorpMpUtil.java @@ -2,6 +2,7 @@ import com.alibaba.fastjson.JSONObject; import com.hx.util.HttpMethodUtil; +import com.hx.util.corp.entity.OpenIdAUserId; import com.hx.util.corp.entity.WeiXinInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,7 +20,54 @@ private static Logger logger = LoggerFactory.getLogger(CorpMpUtil.class.getName()); /**閾炬帴-鑾峰彇搴旂敤accessToken*/ - public static final String URL_GET_USER_INFO= "https://qyapi.weixin.qq.com/cgi-bin/user/get"; + public static final String URL_GET_USER_INFO = "https://qyapi.weixin.qq.com/cgi-bin/user/get"; + + /**openid杞瑄serid*/ + public static final String URL_OPENID_TO_USERID = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_userid"; + + /**userid杞琽penid*/ + public static final String URL_USERID_TO_OPENID = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid"; + + /**鑾峰彇灏忕▼搴忛摼鎺�*/ + public static final String URL_USERID_TO_APPLET = "https://api.weixin.qq.com/wxa/generate_urllink"; + + + /** + * openId鑾峰彇userId + * @param openId 鐢ㄦ埛openId + * @return 杩斿洖 + */ + public static OpenIdAUserId openIdToUserId(String openId, String token) { + Map<String,Object> map=new HashMap<>(); + map.put("access_token",token); + + JSONObject data = new JSONObject(); + data.put("openid",openId); + + String datas = HttpMethodUtil.HttpURLUtilJson(URL_OPENID_TO_USERID, data.toString(), map, null, "GET"); + OpenIdAUserId openIdAUserId = JSONObject.parseObject(datas, OpenIdAUserId.class); + return openIdAUserId; + } + + + /** + * userId鑾峰彇openId + * @param userid 浼佷笟鍛樺伐id + * @return 杩斿洖 + */ + public static OpenIdAUserId userIdToOpenId(String userid, String token) { + Map<String,Object> map=new HashMap<>(); + map.put("access_token",token); + + JSONObject data = new JSONObject(); + data.put("userid",userid); + + String datas = HttpMethodUtil.HttpURLUtilJson(URL_USERID_TO_OPENID, data.toString(), map, null, "GET"); + OpenIdAUserId openIdAUserId = JSONObject.parseObject(datas, OpenIdAUserId.class); + return openIdAUserId; + } + + /** * 鑾峰彇浼佷笟寰俊鍛樺伐淇℃伅 diff --git a/src/main/java/com/hx/util/corp/entity/OpenIdAUserId.java b/src/main/java/com/hx/util/corp/entity/OpenIdAUserId.java new file mode 100644 index 0000000..07cc69b --- /dev/null +++ b/src/main/java/com/hx/util/corp/entity/OpenIdAUserId.java @@ -0,0 +1,62 @@ +package com.hx.util.corp.entity; + +/**openId杞瑄serid鎴栬�卽serid杞琽penId鎺ユ敹瀹炰綋 + * + */ +public class OpenIdAUserId { + + /**杩斿洖鐮侊紝0琛ㄧず鎴愬姛*/ + private Integer errcode; + /**瀵硅繑鍥炵爜鐨勬枃鏈弿杩板唴瀹�*/ + private String errmsg; + /**浼佷笟鍛樺伐userid*/ + private String userid; + /**鐢ㄦ埛openid*/ + private String openid; + + + /**鐘舵�佺爜-鎴愬姛*/ + public static final int CODE_SUCCESS = 0; + + public Integer getErrcode() { + return errcode; + } + + public void setErrcode(Integer errcode) { + this.errcode = errcode; + } + + public String getErrmsg() { + return errmsg; + } + + public void setErrmsg(String errmsg) { + this.errmsg = errmsg; + } + + public String getUserid() { + return userid; + } + + public void setUserid(String userid) { + this.userid = userid; + } + + public String getOpenid() { + return openid; + } + + public void setOpenid(String openid) { + this.openid = openid; + } + + @Override + public String toString() { + return "OpenIdAUserId{" + + "errcode=" + errcode + + ", errmsg='" + errmsg + '\'' + + ", userid='" + userid + '\'' + + ", openid='" + openid + '\'' + + '}'; + } +} -- Gitblit v1.8.0