微信用户免密免验证码登录

/**
 * 微信用户免密免验证码登录
 *
 * @param loginBody
 * @return
 */
public Map wxCodeLogin(LoginWeixinBody loginBody) {
    TMiniProgramInfo info = new TMiniProgramInfo();
    info.setType(EnumMiniProgramType.STUDYANDTEST.getCode());
    List infos = miniProgramInfoService.selectTMiniProgramInfoList(info);
    if (infos == null || infos.size() == 0){
        throw new CustomException("微信小程序登录异常,请联系管理员");
    }
    info = infos.get(0);
    //与微信服务端交换获取用户openId
    String appId = info.getAppId();
    String appSecret = info.getAppSecret();
    WeixinLoginRespBean weixinLoginRespBean = weixinHttpUtils.login(appId, appSecret, loginBody.getCode());
    //查询社矫对象与微信openId对应关系(一对一)
    TCorrectArchiveWx one = new TCorrectArchiveWx();
    one.setOpenId(weixinLoginRespBean.getOpenId());
    List list = correctArchiveWxMapper.selectTCorrectArchiveWxList(one);
    if (list == null || list.size() == 0) {
        throw new CustomException("用户绑定关系已失效,请重新绑定");
    } else {
        one = list.get(0);
    }

    CorrectArchiveVO archiveVO = tCorrectArchiveService.selectCorrectArchiveVOByPkId(one.getaPk());
    String loginPersonType = "";
    String mobilePhone = "";
    if (archiveVO != null) {
        mobilePhone = archiveVO.getMobilePhone();
        loginPersonType = EnumUserType.ARCHIVE.getCode();
    } else {
        AsyncManager.me()
                .execute(AsyncFactory.recordLogininfor(null, null, "",
                        null, null, EnumUserType.ARCHIVE.getCode(),
                        loginBody.getTenant(), mobilePhone, "", Constants.LOGIN_FAIL, "微信号未查到人员"));
        throw new CustomException("微信号未查到人员");
    }

    if (StringUtils.isBlank(loginPersonType)) {
        throw new CustomException("微信登录类型获取异常");
    }
    LoginUser loginUser = new LoginUser();
    loginUser.setTenant(loginBody.getTenant());
    loginUser.setArchive(archiveVO);
    updateTCorrectArchiveLog(archiveVO);

    // 生成token
    String token = tokenService.createToken(loginUser);

    // 矫正人员登录移除其他token
    SysLogininfor loginInfor = new SysLogininfor();
    loginInfor.setUserPk(loginUser.getLoginUserId());
    loginInfor.setUserType(loginPersonType);
    SysLogininfor sysLogininfor = sysLogininforService.selectLogininforObj(loginInfor);
    if (sysLogininfor != null) {
        String userKey = tokenService.getTokenKey(sysLogininfor.getToken());
        redisCache.deleteObject(userKey);
    }
    // 先清除再添加
    AsyncManager.me()
            .execute(AsyncFactory.recordLogininfor(null, null,
                    loginUser.getToken(), loginUser.getDeptPk(),
                    loginUser.getLoginUserId(), loginPersonType, loginBody.getTenant(), mobilePhone,
                    nickName(loginUser), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));

    Map param = new HashMap(2);
    param.put(Constants.TOKEN, token);
    param.put(Constants.USER_TYPE, loginPersonType);
    return param;
}
import com.alibaba.fastjson.JSON;
import com.cdkj.common.exception.CustomException;
import com.cdkj.common.utils.CDHttpUtil;
import com.cdkj.common.utils.weixin.vo.WeixinLoginRespBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;


/**
 * 微信小程序工具类
 *
 * @author HeHuan
 */
@Component
public class WeixinHttpUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(WeixinHttpUtils.class);

    private static final String URL = "https://api.weixin.qq.com/";

    /**
     * 登录小程序平台获取openId
     * 

* appid string 是 小程序 appId * secret string 是 小程序 appSecret * js_code string 是 登录时获取的 code * grant_type string 是 授权类型,此处只需填写 authorization_code */ public WeixinLoginRespBean login(String appId, String appSecret, String code) { try { String res = CDHttpUtil.httpSendGet(getLoginUrl(appId, appSecret, code), new HashMap<>(0)); WeixinLoginRespBean bean = JSON.parseObject(res, WeixinLoginRespBean.class); if (bean == null || bean.getErrCode() != null) { throw new CustomException("获取微信用户信息失败"); } return bean; } catch (CustomException e) { throw e; } catch (Exception e) { throw new CustomException("请求返回信息转换异常"); } } public String getLoginUrl(String appId, String appSecret, String code) { StringBuffer sb = new StringBuffer(); sb.append(URL).append("sns/jscode2session?"); sb.append("appid=").append(appId); sb.append("&secret=").append(appSecret); sb.append("&js_code=").append(code); sb.append("&grant_type=authorization_code"); return sb.toString(); } }

你可能感兴趣的:(java,微信)