🎨 #2506 【小程序】补充完善获取用户encryptKey接口的参数

This commit is contained in:
huazai
2022-01-17 13:49:23 +08:00
committed by GitHub
parent 0601e8fb17
commit 1dc58f0021
4 changed files with 91 additions and 11 deletions

View File

@@ -8,20 +8,36 @@ import me.chanjar.weixin.common.error.WxErrorException;
* 【小程序-服务端-网络】网络相关接口.
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
* </pre>
*
* @author <a href="https://github.com/chutian0124">chutian0124</a>
*/
public interface WxMaInternetService {
/**
*
*
* <pre>
* 获取用户encryptKey。 会获取用户最近3次的key每个key的存活时间为3600s。
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
* 接口地址POST https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256
* @param openid 用户的openid
* @param signature 用sessionkey对空字符串签名得到的结果
* @param sigMethod 签名方法,只支持 hmac_sha256
* </pre>
*
* @return {@link WxMaInternetResponse}
* @throws WxErrorException
*/
WxMaInternetResponse getUserEncryptKey() throws WxErrorException;
WxMaInternetResponse getUserEncryptKey(String openid, String signature, String sigMethod) throws WxErrorException;
/**
* <pre>
* 获取用户encryptKey。 会获取用户最近3次的key每个key的存活时间为3600s。
* 文档地址https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/internet/internet.getUserEncryptKey.html
* 接口地址POST https://api.weixin.qq.com/wxa/business/getuserencryptkey?access_token=ACCESS_TOKEN&openid=OPENID&signature=SIGNATURE&sig_method=hmac_sha256
* @param openid 用户的openid
* @param sessionKey 用户的sessionKey
* </pre>
*
* @return {@link WxMaInternetResponse}
* @throws WxErrorException
*/
WxMaInternetResponse getUserEncryptKey(String openid, String sessionKey) throws WxErrorException;
}

View File

@@ -9,9 +9,12 @@ import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import org.jetbrains.annotations.NotNull;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
*
* 服务端网络相关接口
*
* @author <a href="https://github.com/chutian0124">chutian0124</a>
@@ -21,9 +24,39 @@ import me.chanjar.weixin.common.error.WxErrorException;
public class WxMaInternetServiceImpl implements WxMaInternetService {
private final WxMaService wxMaService;
private String sha256(String data, String sessionKey) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(sessionKey.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString().toUpperCase();
}
@Override
public WxMaInternetResponse getUserEncryptKey() throws WxErrorException {
String responseContent = this.wxMaService.post(WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY, "");
public WxMaInternetResponse getUserEncryptKey(String openid, String signature, String sigMethod) throws WxErrorException {
String url = WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY + "?openid=" + openid + "&signature=" + signature + "&sig_method=" + sigMethod;
return getWxMaInternetResponse(url);
}
@Override
public WxMaInternetResponse getUserEncryptKey(String openid, String sessionKey) throws WxErrorException {
String signature = null;
try {
signature = sha256("", sessionKey);
} catch (Exception e) {
throw new WxErrorException("签名错误");
}
String url = WxMaApiUrlConstants.Internet.GET_USER_ENCRYPT_KEY + "?sig_method=hmac_sha256&openid=" + openid + "&signature=" + signature;
return getWxMaInternetResponse(url);
}
@NotNull
private WxMaInternetResponse getWxMaInternetResponse(String url) throws WxErrorException {
String responseContent = this.wxMaService.post(url, "");
WxMaInternetResponse response = WxMaGsonBuilder.create().fromJson(responseContent, WxMaInternetResponse.class);
if (response.getErrcode() == -1) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));