使用weixin-java-miniapp实现微信小程序登录接口

maven 依赖

        
        <dependency>
            <groupId>com.github.binarywanggroupId>
            <artifactId>weixin-java-miniappartifactId>
            <version>3.6.0version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.10version>
            <scope>providedscope>
        dependency>

code

@Data
public class LoginRequest {
    //用户登录凭证
    String code;

    //原始数据字符串
    String signature;

    //校验用户信息字符串
    String rawData;

    //加密用户数据
    String encryptedData;

    //加密算法的初始向量
    String iv;
}
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import me.chanjar.weixin.common.error.WxErrorException;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/12/10 3:48 PM
 */
public class WxAppLoginService {

    public void login(LoginRequest request) throws WxErrorException {
        final WxMaService wxService = getWxMaService();

        // 获取微信用户session
        WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(request.getCode());
        if (null == session) {
            throw new RuntimeException("login handler error");
        }

        // 解密用户信息
        WxMaUserInfo wxUserInfo = wxService.getUserService().getUserInfo(session.getSessionKey(),
                request.getEncryptedData(), request.getIv());
        if (null == wxUserInfo) {
            throw new RuntimeException("wxUser not exist");
        }
        
        // 解密手机号码信息
        WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxService.getUserService().getPhoneNoInfo(session.getSessionKey(),
                request.getEncryptedData(), request.getIv());
        if (Objects.isNull(wxMaPhoneNumberInfo) || StringUtils.isBlank(wxMaPhoneNumberInfo.getPhoneNumber())) {
            // 解密手机号码信息错误
        }
        
        System.out.printf("============用户登录注册获取微信用户信息===========> openId=%s, username=%s", wxUserInfo.getOpenId(), wxUserInfo.getNickName());
        String unionId = wxUserInfo.getUnionId();
        String openId = wxUserInfo.getOpenId();

    }

    private WxMaService getWxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid("appId");
        config.setSecret("appSecret");
        config.setMsgDataFormat("JSON");
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }
}

实践

请求入参

{
    "code": "071kqVIk1scL4o0ynUJk1YqEIk1kqVIt",
    "encryptedData": "fPjQvxaAYCKJFP3OTHmwLGgAMm8/MZPlCD/qdRKms1T3rij89kHKfSyrxEh68Ay8occT1wUb4F3Cw7lSPacz5ZHpnRUc3w8EUdz/fezCHDdq+Oh4yc3DzChAa77+28+3eCO0ReeB0yiWkfAiP3iLqic5O1d86ZXJbcpDSZa71EnvGJkNFjhhGJJhoDBC7pd/NW+n21RwTHLAwLoYqo+7aqnJSI+po9+42jRvI7EiEpjtb/LlsKOn4NELU4twGOTB3td1rrQ3BOlSFjqMsmDKiZ/deDPnnus4vaD4agDjUH9LGyqmpeLFfeK8mlSVIGk9wEdz1Wk+G53m8RfvkfZmtwiIHuFbWLmaw4Up1Jtt2kz02+G+5/BcGnPK7D8GSiP5Df0W88HsMofbgTo1yhu3fgKhYVEETNFsoxOzOa8O4QBrSiN7pzezxLhALvzxeNkjTeG4wQHnZm+mEW7LsTwgwGVAus9LWleG8mlSfPqSA8u8IOJacz9DWjehVed13GQX",
    "iv": "hF6Oi870Mgxcdhmnt5EZrA==",
    "rawData": {
        "nickName": "morningcat2018",
        "gender": 1,
        "language": "zh_CN",
        "city": "Hangzhou",
        "province": "Zhejiang",
        "country": "China",
        "avatarUrl": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTK3EZ1I2fjXnGPBQSiaYSKRebYoawYXaOwbom50KdXicpQ1IRsd7xKp3e7oucb9scMOibk2uTviavpmibg/132"
    }
}

注意 : code 使用一次后就会失效

你可能感兴趣的:(企业应用)