|
| 1 | +package me.chanjar.weixin.open.api.impl; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.api.WxMaService; |
| 4 | +import cn.binarywang.wx.miniapp.api.WxMaUserService; |
| 5 | +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
| 6 | +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; |
| 7 | +import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; |
| 8 | +import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; |
| 9 | +import com.google.common.base.Joiner; |
| 10 | +import me.chanjar.weixin.common.exception.WxErrorException; |
| 11 | +import me.chanjar.weixin.open.api.WxOpenComponentService; |
| 12 | +import org.apache.commons.codec.digest.DigestUtils; |
| 13 | + |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Charming |
| 19 | + */ |
| 20 | +class WxOpenMaUserServiceImpl implements WxMaUserService { |
| 21 | + private static final String COMPONENT_JSCODE_TO_SESSION_URL = "https://api.weixin.qq.com/sns/component/jscode2session"; |
| 22 | + private WxOpenComponentService wxOpenComponentService; |
| 23 | + private WxMaService wxMaService; |
| 24 | + |
| 25 | + public WxOpenMaUserServiceImpl(WxOpenComponentService wxOpenComponentService, WxMaService wxMaService) { |
| 26 | + this.wxOpenComponentService = wxOpenComponentService; |
| 27 | + this.wxMaService = wxMaService; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * 第三方平台开发者的服务器使用登录凭证 code 以及 |
| 32 | + * 第三方平台的 component_access_token |
| 33 | + * 获取 session_key 和 openid。 |
| 34 | + * 其中 session_key 是对用户数据进行加密签名的密钥。 |
| 35 | + * 为了自身应用安全,session_key 不应该在网络上传输。 |
| 36 | + * 文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1492585163_FtTNA&token=&lang=zh_CN |
| 37 | + * |
| 38 | + * @param jsCode 登录时获取的 code |
| 39 | + * @return session_key 和 openid |
| 40 | + * @throws WxErrorException 发生错误时 |
| 41 | + */ |
| 42 | + @Override |
| 43 | + public WxMaJscode2SessionResult getSessionInfo(String jsCode) throws WxErrorException { |
| 44 | + Map<String, String> params = new HashMap<>(5); |
| 45 | + params.put("appid", wxMaService.getWxMaConfig().getAppid()); |
| 46 | + params.put("js_code", jsCode); |
| 47 | + params.put("grant_type", "authorization_code"); |
| 48 | + params.put("component_appid", wxOpenComponentService.getWxOpenConfigStorage().getComponentAppId()); |
| 49 | + params.put("component_access_token", wxOpenComponentService.getComponentAccessToken(false)); |
| 50 | + |
| 51 | + String result = this.wxMaService.get(COMPONENT_JSCODE_TO_SESSION_URL, Joiner.on("&").withKeyValueSeparator("=").join(params)); |
| 52 | + return WxMaJscode2SessionResult.fromJson(result); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public WxMaUserInfo getUserInfo(String sessionKey, String encryptedData, String ivStr) { |
| 57 | + return WxMaUserInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedData, String ivStr) { |
| 62 | + return WxMaPhoneNumberInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr)); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean checkUserInfo(String sessionKey, String rawData, String signature) { |
| 67 | + final String generatedSignature = DigestUtils.sha1Hex(rawData + sessionKey); |
| 68 | + return generatedSignature.equals(signature); |
| 69 | + } |
| 70 | +} |
0 commit comments