Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信开放平台:增加 WxMaUserService 的实现 #578

Merged
merged 11 commits into from
May 8, 2018
Merged
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package me.chanjar.weixin.open.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import com.google.common.base.Joiner;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.open.api.WxOpenComponentService;

import java.util.HashMap;
import java.util.Map;

/**
* @author <a href="https://github.com/007gzs">007</a>
*/
/* package */ class WxOpenMaServiceImpl extends WxMaServiceImpl {
private WxOpenComponentService wxOpenComponentService;
private WxMaConfig wxMaConfig;
private String appId;
private WxMaUserService wxMaUserService;

public WxOpenMaServiceImpl(WxOpenComponentService wxOpenComponentService, String appId, WxMaConfig wxMaConfig) {
this.wxOpenComponentService = wxOpenComponentService;
this.appId = appId;
this.wxMaConfig = wxMaConfig;
initHttp();
this.wxMaUserService = new WxOpenMaUserServiceImpl(wxOpenComponentService, this);
}

@Override
public WxMaUserService getUserService() {
return this.wxMaUserService;
}

@Override
Expand All @@ -38,5 +42,4 @@ public WxMaConfig getWxMaConfig() {
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
return wxOpenComponentService.getAuthorizerAccessToken(appId, forceRefresh);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package me.chanjar.weixin.open.api.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import com.google.common.base.Joiner;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.open.api.WxOpenComponentService;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
* @author Charming
*/
class WxOpenMaUserServiceImpl implements WxMaUserService {
private static final String COMPONENT_JSCODE_TO_SESSION_URL = "https://api.weixin.qq.com/sns/component/jscode2session";
private WxOpenComponentService wxOpenComponentService;
private WxMaService wxMaService;

public WxOpenMaUserServiceImpl(WxOpenComponentService wxOpenComponentService, WxMaService wxMaService) {
this.wxOpenComponentService = wxOpenComponentService;
this.wxMaService = wxMaService;
}

/**
* 第三方平台开发者的服务器使用登录凭证 code 以及
* 第三方平台的 component_access_token
* 获取 session_key 和 openid。
* 其中 session_key 是对用户数据进行加密签名的密钥。
* 为了自身应用安全,session_key 不应该在网络上传输。
* 文档:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1492585163_FtTNA&token=&lang=zh_CN
*
* @param jsCode 登录时获取的 code
* @return session_key 和 openid
* @throws WxErrorException 发生错误时
*/
@Override
public WxMaJscode2SessionResult getSessionInfo(String jsCode) throws WxErrorException {
Map<String, String> params = new HashMap<>(5);
params.put("appid", wxMaService.getWxMaConfig().getAppid());
params.put("js_code", jsCode);
params.put("grant_type", "authorization_code");
params.put("component_appid", wxOpenComponentService.getWxOpenConfigStorage().getComponentAppId());
params.put("component_access_token", wxOpenComponentService.getComponentAccessToken(false));

String result = this.wxMaService.get(COMPONENT_JSCODE_TO_SESSION_URL, Joiner.on("&").withKeyValueSeparator("=").join(params));
return WxMaJscode2SessionResult.fromJson(result);
}

@Override
public WxMaUserInfo getUserInfo(String sessionKey, String encryptedData, String ivStr) {
return WxMaUserInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));
}

@Override
public WxMaPhoneNumberInfo getPhoneNoInfo(String sessionKey, String encryptedData, String ivStr) {
return WxMaPhoneNumberInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));
}

@Override
public boolean checkUserInfo(String sessionKey, String rawData, String signature) {
final String generatedSignature = DigestUtils.sha1Hex(rawData + sessionKey);
return generatedSignature.equals(signature);
}
}