Java Code Examples for cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult#getSessionKey()

The following examples show how to use cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult#getSessionKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MiniAppIntegrationAuthenticator.java    From cola-cloud with MIT License 6 votes vote down vote up
@Override
public SysUserAuthentication authenticate(IntegrationAuthentication integrationAuthentication) {
    WxMaJscode2SessionResult session = null;
    String password = integrationAuthentication.getAuthParameter("password");
    try {
        session = this.wxMaService.getUserService().getSessionInfo(password);
        WechatMiniAppToken wechatToken = new WechatMiniAppToken(session.getOpenid(), session.getUnionid(), session.getSessionKey());
        // 加密算法的初始向量
        wechatToken.setIv(integrationAuthentication.getAuthParameter("iv"));
        // 用户的加密数据
        wechatToken.setEncryptedData(integrationAuthentication.getAuthParameter("encryptedData"));
    } catch (WxErrorException e) {
        throw new InternalAuthenticationServiceException("获取微信小程序用户信息失败",e);
    }
    String openId = session.getOpenid();
    SysUserAuthentication sysUserAuthentication = sysUserClient.findUserBySocial(UcClientConstant.SOCIAL_TYPE_WECHAT_MINIAP, openId);
    if(sysUserAuthentication != null){
        sysUserAuthentication.setPassword(passwordEncoder.encode(password));
    }
    return sysUserAuthentication;
}
 
Example 2
Source File: WxSessionService.java    From spring-microservice-exam with MIT License 5 votes vote down vote up
/**
 * 根据code获取WxSession
 *
 * @param code code
 * @return WxSession
 * @author tangyi
 * @date 2019/07/05 20:37:02
 */
public WxSession getSession(String code) {
    WxSession session = null;
    try {
        WxMaJscode2SessionResult result = wxMaService.getUserService().getSessionInfo(code);
        session = new WxSession(result.getOpenid(), result.getSessionKey());
        log.info("Get wx session success,openId: {}, sessionKey: {}", session.getOpenId(), session.getSessionKey());
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return session;
}
 
Example 3
Source File: WxSessionService.java    From spring-microservice-exam with MIT License 5 votes vote down vote up
/**
 * 根据code获取WxSession
 *
 * @param code code
 * @return WxSession
 * @author tangyi
 * @date 2019/07/06 14:01:13
 */
public WxSession code2Session(String code) {
    WxSession session = null;
    try {
        WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(code);
        session = new WxSession(result.getOpenid(), result.getSessionKey());
        log.info("Get wx session success,openId: {}, sessionKey: {}", session.getOpenId(), session.getSessionKey());
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return session;
}
 
Example 4
Source File: AuthController.java    From supplierShop with MIT License 4 votes vote down vote up
/**
 * 小程序登陆,颁发token,暂时模拟openid
 *
 * @return token字符串
 */
@PostMapping("/oauth/access_token")
@ApiOperation(value = "获取token",notes = "获取token")
public R loginReturnToken(@Validated @RequestBody LoginVO loginVO) {
    Boolean isProduct = false; //true 开启真实小程序环境
    String openid = "orIMY4xGhMmipwFZoSL1vOhUNFZ0";
    WxMaUserInfo userInfo = null;
    try{
        if(isProduct){
            WxMaJscode2SessionResult session = wxService.getUserService()
                    .getSessionInfo(loginVO.getCode());
            //log.info(session.getSessionKey());
            //log.info(session.getOpenid());
            //TODO 可以增加自己的逻辑,关联业务相关数据

            String sessionKey = session.getSessionKey();
            openid = session.getOpenid();
            // 解密用户信息
            userInfo = wxService.getUserService()
                    .getUserInfo(sessionKey, loginVO.getEncrypted_data(), loginVO.getIv());

        }

        StoreMember member = memberService.login(openid);
        User user = null;
        if(member == null){
            //新用户插入数据
            StoreMember newMember = new StoreMember();
            newMember.setOpenid(userInfo.getOpenId());
            newMember.setNickname(userInfo.getNickName());
            newMember.setHeadimg(userInfo.getAvatarUrl());
            newMember.setSex(userInfo.getGender());
            newMember.setCreateTime(new Date());
            memberService.save(newMember);
            user = User.builder()
                    .id(newMember.getId().intValue())
                    .username(newMember.getNickname())
                    .build();
        }else{
            user = User.builder()
                    .id(member.getId().intValue())
                    .username(member.getNickname())
                    .build();
        }


        String token = operator.generateToken(user);
        HashMap<String,String> map = new HashMap<>();
        map.put("access_token",token);
        return R.success(map);
    } catch (WxErrorException e) {
        log.error(e.getMessage());
        return R.error(4000,e.getMessage());
    }


}
 
Example 5
Source File: WxMaUserController.java    From sdb-mall with Apache License 2.0 4 votes vote down vote up
/**
 * 登陆接口
 */
@PostMapping("/login")
public R login(HttpServletRequest request, @RequestBody MaLoginForm maLoginForm) {

    if (StringUtils.isBlank(maLoginForm.getCode())) {
        return R.error("empty jscode");
    }

    try {
        WxMaJscode2SessionResult session = this.wxService.getUserService().getSessionInfo(maLoginForm.getCode());

        String sessionKey = session.getSessionKey();
        String openId = session.getOpenid();

        // 用户信息校验
        if (!this.wxService.getUserService().checkUserInfo(sessionKey, maLoginForm.getRawData(), maLoginForm.getSignature())) {
            return R.error("user check failed");
        }

        User user = new User();
        user.setMaOpenId(openId);

        User dbUser = userService.findFirstByModel(user);
        if (dbUser == null) {
            // 解密用户信息
            WxMaUserInfo userInfo = this.wxService.getUserService().getUserInfo(sessionKey, maLoginForm.getEncryptedData(), maLoginForm.getIv());
            dbUser = userService.addMaUser(userInfo);
            if (dbUser.getUserId() == null) {
                return R.error(ResultEnum.MA_LOGIN_ERROR);
            }
        }

        //生成token
        String token = jwtUtils.generateToken(dbUser.getUserId());

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        return R.ok(map);
    } catch (WxErrorException e) {
        this.logger.error(e.getMessage(), e);
        return R.error(e.toString());
    }
}