cn.herodotus.engine.access.wxapp.processor.WxappAccessHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of access-sdk-wxapp Show documentation
Show all versions of access-sdk-wxapp Show documentation
基于 Spring Authorization Server 的 微信小程序外部接入基础核心组件模块
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2020-2030 郑庚伟 ZHENGGENGWEI (码匠君), Licensed under the AGPL License
*
* This file is part of Herodotus Engine.
*
* Herodotus Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Herodotus Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package cn.herodotus.engine.access.wxapp.processor;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.herodotus.engine.access.core.definition.AccessHandler;
import cn.herodotus.engine.access.core.definition.AccessResponse;
import cn.herodotus.engine.access.core.definition.AccessUserDetails;
import cn.herodotus.engine.access.core.exception.AccessIdentityVerificationFailedException;
import cn.herodotus.engine.access.core.exception.AccessPreProcessFailedException;
import cn.herodotus.engine.assistant.definition.constants.SymbolConstants;
import cn.herodotus.engine.assistant.definition.domain.oauth2.AccessPrincipal;
import cn.herodotus.engine.assistant.core.enums.AccountType;
import org.apache.commons.lang3.ObjectUtils;
/**
* Description: 微信小程序接入处理器
*
* @author : gengwei.zheng
* @date : 2022/1/26 10:56
*/
public class WxappAccessHandler implements AccessHandler {
private final WxappProcessor wxappProcessor;
public WxappAccessHandler(WxappProcessor wxappProcessor) {
this.wxappProcessor = wxappProcessor;
}
@Override
public AccessResponse preProcess(String core, String... params) {
WxMaJscode2SessionResult wxMaSession = wxappProcessor.login(core, params[0]);
if (ObjectUtils.isNotEmpty(wxMaSession)) {
AccessResponse accessResponse = new AccessResponse();
accessResponse.setSession(wxMaSession);
return accessResponse;
}
throw new AccessPreProcessFailedException("Wxapp login failed");
}
@Override
public AccessUserDetails loadUserDetails(String source, AccessPrincipal accessPrincipal) {
WxMaUserInfo wxMaUserInfo = wxappProcessor.getUserInfo(accessPrincipal.getAppId(), accessPrincipal.getSessionKey(), accessPrincipal.getEncryptedData(), accessPrincipal.getIv());
if (ObjectUtils.isNotEmpty(wxMaUserInfo)) {
return convertWxMaUserInfoToAccessUserDetails(wxMaUserInfo, accessPrincipal);
}
throw new AccessIdentityVerificationFailedException("Can not find the userinfo from Wechat!");
}
private AccessUserDetails convertWxMaUserInfoToAccessUserDetails(WxMaUserInfo wxMaUserInfo, AccessPrincipal accessPrincipal) {
AccessUserDetails accessUserDetails = new AccessUserDetails();
accessUserDetails.setUuid(accessPrincipal.getOpenId());
accessUserDetails.setUsername(wxMaUserInfo.getNickName());
accessUserDetails.setNickname(wxMaUserInfo.getNickName());
accessUserDetails.setAvatar(wxMaUserInfo.getAvatarUrl());
accessUserDetails.setLocation(wxMaUserInfo.getCountry() + SymbolConstants.FORWARD_SLASH + wxMaUserInfo.getProvince() + SymbolConstants.FORWARD_SLASH + wxMaUserInfo.getCity());
accessUserDetails.setSource(AccountType.WXAPP.name());
accessUserDetails.setOpenId(accessPrincipal.getOpenId());
accessUserDetails.setUnionId(accessPrincipal.getUnionId());
accessUserDetails.setAppId(wxMaUserInfo.getWatermark().getAppid());
accessUserDetails.setPhoneNumber(wxMaUserInfo.getWatermark().getAppid());
return accessUserDetails;
}
}