第一次提交

This commit is contained in:
wyw
2024-08-08 00:31:26 +08:00
commit c202e2b63d
1819 changed files with 221890 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
package com.fastbee.iot.wechat;
/**
* 微信回调相关服务类
* @author fastb
* @date 2024-03-12 15:26
* @version 1.0
*/
public interface WeChatCallbackService {
/**
* 处理微信公众号回调信息
* @param xmlStr xml消息体
* @return java.lang.String
*/
String handleCallback(String xmlStr);
}

View File

@@ -0,0 +1,68 @@
package com.fastbee.iot.wechat;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.wechat.WeChatLoginBody;
import com.fastbee.common.wechat.WeChatLoginResult;
import com.fastbee.iot.wechat.vo.WxBindReqVO;
import com.fastbee.iot.wechat.vo.WxCancelBindReqVO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 微信相关服务类
* @author fastb
* @date 2023-07-31 11:34
*/
public interface WeChatService {
/**
* 移动应用微信登录
* @param weChatLoginBody 微信登录参数
* @return String
*/
WeChatLoginResult mobileLogin(WeChatLoginBody weChatLoginBody, String language);
/**
* 小程序微信登录
* @param weChatLoginBody 微信登录参数
* @return 登录结果
*/
WeChatLoginResult miniLogin(WeChatLoginBody weChatLoginBody, String language);
/**
* 取消所有相关微信绑定
* @param wxCancelBindReqVO 微信解绑传参类型
* @return 结果
*/
AjaxResult cancelBind(WxCancelBindReqVO wxCancelBindReqVO);
/**
* 小程序、移动应用微信绑定
* @param wxBindReqVO 微信绑定传参类型
* @return 结果
*/
AjaxResult bind(WxBindReqVO wxBindReqVO);
/**
* 网站应用获取微信绑定二维码信息
* @return 二维码信息
*/
AjaxResult getWxBindQr(HttpServletRequest httpServletRequest);
/**
* 网站应用内微信扫码绑定回调接口
* @param code 用户凭证
* @param state 时间戳
* @param httpServletRequest 请求信息
* @param httpServletResponse 响应信息
*/
String wxBindCallback(String code, String state, String wxBindId, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);
/**
* 网站应用获取微信绑定结果信息
* @param wxBindMsgId 微信绑定结果信息id
* @return msg
*/
AjaxResult getWxBindMsg(String wxBindMsgId);
}

View File

@@ -0,0 +1,149 @@
package com.fastbee.iot.wechat.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fastbee.common.enums.SocialPlatformType;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.common.utils.uuid.IdUtils;
import com.fastbee.common.utils.wechat.WechatUtils;
import com.fastbee.common.wechat.WeChatAppResult;
import com.fastbee.common.wechat.WeChatUserInfo;
import com.fastbee.iot.domain.SocialPlatform;
import com.fastbee.iot.domain.SocialUser;
import com.fastbee.iot.service.ISocialPlatformService;
import com.fastbee.iot.service.ISocialUserService;
import com.fastbee.iot.wechat.WeChatCallbackService;
import com.fastbee.common.wechat.WxCallBackXmlBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
/**
* @author fastb
* @version 1.0
* @description: 微信回调相关处理器
* @date 2024-03-08 14:36
*/
@Slf4j
@Service
public class WeChatCallbackServiceImpl implements WeChatCallbackService {
@Resource
private ISocialPlatformService socialPlatformService;
@Resource
private ISocialUserService socialUserService;
@Override
public String handleCallback(String xmlStr) {
WxCallBackXmlBO wxCallBackXmlBO;
String resMsg = "";
try {
Map<String, Object> map = XmlUtil.xmlToMap(xmlStr);
wxCallBackXmlBO = JSONObject.parseObject(JSON.toJSONString(map), WxCallBackXmlBO.class);
} catch (Exception e) {
log.error("微信回调传递的xml解析报错{}{}", xmlStr, e);
return "";
}
if (ObjectUtil.isNull(wxCallBackXmlBO) || StringUtils.isEmpty(wxCallBackXmlBO.getMsgType())) {
log.error("微信回调传递的xml中MsgType解析为空{}", xmlStr);
return "";
}
String msgType = wxCallBackXmlBO.getMsgType().toLowerCase();
switch (msgType) {
// 文本搜索关键词匹配
case "text":
break;
// 图片信息 直接转入人工客服
case "image":
break;
//物理地址信息
case "location":
break;
// 事件推送(订阅、取消订阅)
case "event":
resMsg = this.handleEvent(wxCallBackXmlBO);
break;
// 语音识别
case "voice":
break;
default:
break;
}
return resMsg;
}
private String handleEvent(WxCallBackXmlBO wxCallBackXmlBo) {
String resMsg = "";
//事件类型
String eventStr = wxCallBackXmlBo.getEvent();
if (org.apache.commons.lang3.StringUtils.isNotBlank(eventStr)) {
switch (org.apache.commons.lang3.StringUtils.lowerCase(eventStr)) {
// 关键词
case "click":
break;
// 订阅 + 扫描二维码获取的场景值
case "subscribe":
resMsg = this.handleEventSubscribe(wxCallBackXmlBo);
break;
// 取消订阅
case "unsubscribe":
resMsg = this.handleEventUnSubscribe(wxCallBackXmlBo);
break;
// 扫描二维码
case "scan":
break;
default:
break;
}
}
return resMsg;
}
private String handleEventUnSubscribe(WxCallBackXmlBO wxCallBackXmlBo) {
// 解绑微信公众号关注
String openId = wxCallBackXmlBo.getFromUserName();
socialUserService.deleteByOpenIdAndSourceClient(openId, SocialPlatformType.WECHAT_OPEN_PUBLIC_ACCOUNT.getSourceClient());
return "";
}
private String handleEventSubscribe(WxCallBackXmlBO wxCallBackXmlBo) {
// 获取微信公众号appid和secret
String sourceClient = SocialPlatformType.WECHAT_OPEN_PUBLIC_ACCOUNT.getSourceClient();
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(SocialPlatformType.WECHAT_OPEN_PUBLIC_ACCOUNT.getSourceClient());
if (ObjectUtil.isNull(socialPlatform)) {
log.info("请先配置微信开放平台公众号!");
return "";
}
String openId = wxCallBackXmlBo.getFromUserName();
// 公众号获取accessToken信息
WeChatAppResult weChatAppResult = WechatUtils.getAccessToken(socialPlatform.getClientId(), socialPlatform.getSecretKey());
if (ObjectUtil.isNull(weChatAppResult)) {
log.info("公众号获取accessToken失败");
return "";
}
if (StringUtils.isEmpty(weChatAppResult.getAccessToken())) {
log.info("公众号获取accessToken失败:{}", JSON.toJSONString(weChatAppResult));
return "";
}
String accessToken = weChatAppResult.getAccessToken();
WeChatUserInfo weChatUserInfo = WechatUtils.getWeChatPublicAccountUserInfo(accessToken, openId);
if (null != weChatUserInfo.getErrCode()) {
log.info("公众号获取微信用户信息失败:{}", JSON.toJSONString(weChatUserInfo));
return "";
}
SocialUser socialUser = new SocialUser();
socialUser.setStatus("1").setSource(sourceClient).setSourceClient(sourceClient).setOpenId(openId)
.setUnionId(weChatUserInfo.getUnionId()).setAccessToken(accessToken).setAvatar(weChatUserInfo.getHeadImgUrl())
.setNickname(weChatUserInfo.getNickname()).setUuid(IdUtils.randomUUID()).setCreateBy("System");
socialUser.setCreateTime(new Date());
socialUserService.insertSocialUser(socialUser);
// 返回消息
return WechatUtils.responseText(wxCallBackXmlBo, socialPlatform.getRemark());
}
}

View File

@@ -0,0 +1,433 @@
package com.fastbee.iot.wechat.impl;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.core.domain.entity.SysUser;
import com.fastbee.common.core.domain.model.LoginUser;
import com.fastbee.common.core.redis.RedisCache;
import com.fastbee.common.enums.SocialPlatformType;
import com.fastbee.common.enums.VerifyTypeEnum;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.common.utils.bean.BeanUtils;
import com.fastbee.common.utils.uuid.IdUtils;
import com.fastbee.common.utils.wechat.WechatUtils;
import com.fastbee.common.wechat.*;
import com.fastbee.framework.web.service.SysLoginService;
import com.fastbee.iot.domain.SocialPlatform;
import com.fastbee.iot.domain.SocialUser;
import com.fastbee.iot.model.RegisterUserInput;
import com.fastbee.iot.model.RegisterUserOutput;
import com.fastbee.iot.model.login.WeChatLoginQrRes;
import com.fastbee.iot.service.ISocialLoginService;
import com.fastbee.iot.service.ISocialPlatformService;
import com.fastbee.iot.service.ISocialUserService;
import com.fastbee.iot.service.IToolService;
import com.fastbee.iot.wechat.WeChatService;
import com.fastbee.iot.wechat.vo.WxBindReqVO;
import com.fastbee.iot.wechat.vo.WxCancelBindReqVO;
import com.fastbee.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthUser;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import static com.fastbee.common.core.domain.AjaxResult.error;
import static com.fastbee.common.core.domain.AjaxResult.success;
import static com.fastbee.common.utils.SecurityUtils.getLoginUser;
import static com.fastbee.common.utils.SecurityUtils.getUserId;
import static com.fastbee.iot.service.impl.SocialLoginServiceImpl.LOGIN_ERROR_MSG_REDIS_KEY;
import static com.fastbee.iot.service.impl.SocialLoginServiceImpl.WX_BIND_REDIS_KEY;
/**
* 微信登录相关服务类
* @author fastb
* @date 2023-08-14 9:16
*/
@Slf4j
@Service
public class WeChatServiceImpl implements WeChatService {
@Resource
private ISocialUserService socialUserService;
@Resource
private ISysUserService sysUserService;
@Resource
private SysLoginService sysLoginService;
@Resource
private ISocialPlatformService socialPlatformService;
@Resource
private ISocialLoginService socialLoginService;
@Resource
private IToolService toolService;
@Resource
private RedisCache redisCache;
/**
* 移动应用微信登录
* @param weChatLoginBody 微信登录参数
* @return String
*/
@Override
public WeChatLoginResult mobileLogin(WeChatLoginBody weChatLoginBody, String language) {
WeChatLoginResult weChatLoginResult = new WeChatLoginResult();
SocialPlatformType socialPlatformType = SocialPlatformType.WECHAT_OPEN_MOBILE;
// 查询微信平台信息
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(socialPlatformType.getSourceClient());
if (socialPlatform == null) {
throw new ServiceException("请先配置微信开放平台移动应用信息");
}
// 用户凭证code只能消费一次前端调的uni.login有时会消费然后直接就把获取到的信息传过来不会消费的话就这里通过code拿
if (StringUtils.isEmpty(weChatLoginBody.getAccessToken()) || StringUtils.isEmpty(weChatLoginBody.getOpenId()) || StringUtils.isEmpty(weChatLoginBody.getUnionId())) {
WeChatAppResult weChatResult = WechatUtils.getAccessTokenOpenId(weChatLoginBody.getCode(), socialPlatform.getClientId(), socialPlatform.getSecretKey());
if (weChatResult == null || weChatResult.getErrCode() != null) {
throw new ServiceException("用户凭证获取失败,请重新登录!");
}
weChatLoginBody.setAccessToken(weChatResult.getAccessToken()).setRefreshToken(weChatResult.getRefreshToken()).setExpiresIn(weChatResult.getExpiresIn()).setOpenId(weChatResult.getOpenId()).setUnionId(weChatResult.getUnionId()).setScope(weChatResult.getScope());
}
Long bindSysUserId;
// 查询用户第三方信息
SocialUser socialUser = socialUserService.selectOneByOpenIdAndUnionId(weChatLoginBody.getOpenId(), weChatLoginBody.getUnionId());
if (socialUser != null && socialUser.getSysUserId() != null) {
bindSysUserId = socialUser.getSysUserId();
} else {
bindSysUserId = socialUserService.selectSysUserIdByUnionId(weChatLoginBody.getUnionId());
}
Date nowDate = DateUtils.getNowDate();
String uuid = IdUtils.randomUUID();
if (socialUser == null) {
SocialUser addSocialUser = new SocialUser();
addSocialUser.setSysUserId(bindSysUserId).setStatus(bindSysUserId == null ? "0" : "1").setUuid(uuid).setSource(socialPlatform.getPlatform().toUpperCase(Locale.ROOT)).setAccessToken(weChatLoginBody.getAccessToken()).setExpireIn(weChatLoginBody.getExpiresIn())
.setRefreshToken(weChatLoginBody.getRefreshToken()).setOpenId(weChatLoginBody.getOpenId()).setUnionId(weChatLoginBody.getUnionId()).setSourceClient(socialPlatform.getPlatform())
.setCode(weChatLoginBody.getCode());
addSocialUser.setCreateBy("System");
addSocialUser.setCreateTime(nowDate);
// 获取微信用户信息
WeChatUserInfo weChatUserInfo = WechatUtils.getWeChatUserInfo(weChatLoginBody.getAccessToken(), weChatLoginBody.getOpenId());
if (weChatUserInfo != null) {
addSocialUser.setUnionId(weChatUserInfo.getUnionId()).setUsername(weChatUserInfo.getNickname())
.setNickname(weChatUserInfo.getNickname()).setAvatar(weChatUserInfo.getHeadImgUrl()).setGender(weChatUserInfo.getSex());
}
socialUserService.insertSocialUser(addSocialUser);
} else {
SocialUser updateSocialUser = new SocialUser();
BeanUtils.copyProperties(socialUser, updateSocialUser);
updateSocialUser.setUpdateBy("System");
updateSocialUser.setSysUserId(bindSysUserId).setStatus(bindSysUserId == null ? "0" : "1").setAccessToken(weChatLoginBody.getAccessToken())
.setRefreshToken(weChatLoginBody.getRefreshToken()).setUuid(uuid);
socialUserService.updateSocialUser(updateSocialUser);
}
// 没有绑定账号,需要去绑定系统账号,跳转到绑定页面
if (bindSysUserId == null) {
AuthUser authUser = new AuthUser();
authUser.setUuid(uuid);
authUser.setSource(socialPlatform.getPlatform());
String bindId = socialLoginService.genBindId(authUser);
weChatLoginResult.setBindId(bindId);
} else {
SysUser sysUser = sysUserService.selectUserById(bindSysUserId);
if (sysUser == null) {
throw new ServiceException("用户不存在");
}
// if (null != sysUser.getDeptId()) {
// throw new ServiceException("只允许终端用户登录!");
// }
String token = sysLoginService.redirectLogin(sysUser.getUserName(), sysUser.getPassword(), language);
weChatLoginResult.setToken(token);
}
return weChatLoginResult;
}
/**
* 小程序微信登录
* @param weChatLoginBody 微信登录参数
* @return 登录结果
*/
@Override
public WeChatLoginResult miniLogin(WeChatLoginBody weChatLoginBody, String language) {
// 使用微信手机号去登录不绑定微信,没有用户则用手机号自动注册一个登录,密码是手机号
SocialPlatformType socialPlatformType = SocialPlatformType.WECHAT_OPEN_MINI_PROGRAM;
// 查询微信平台信息
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(socialPlatformType.getSourceClient());
if (socialPlatform == null) {
throw new ServiceException("请先配置微信公众平台小程序信息!");
}
if (StringUtils.isEmpty(weChatLoginBody.getCode())) {
throw new ServiceException("用户凭证获取失败,请重新登录!");
}
if (StringUtils.isEmpty(weChatLoginBody.getPhoneCode())) {
throw new ServiceException("用户手机号凭证获取失败,请重新登录!");
}
// 获取用户微信信息
WeChatMiniProgramResult weChatResult = WechatUtils.codeToSession(weChatLoginBody.getCode(), socialPlatform.getClientId(), socialPlatform.getSecretKey());
if (weChatResult == null || StringUtils.isEmpty(weChatResult.getOpenId())) {
throw new ServiceException("用户凭证获取失败,请重新登录!");
}
SocialUser socialUser = socialUserService.selectOneByOpenIdAndUnionId(weChatResult.getOpenId(), weChatResult.getUnionId());
// 先获取token
WeChatAppResult result = WechatUtils.getAccessToken(socialPlatform.getClientId(), socialPlatform.getSecretKey());
if (result == null || StringUtils.isEmpty(result.getAccessToken())) {
throw new ServiceException("获取用户调用凭据失败,请重新登录!");
}
// 根据phoneCode获取用户手机号
WeChatPhoneInfo userPhoneInfo = WechatUtils.getWechatUserPhoneInfo(weChatLoginBody.getPhoneCode(), result.getAccessToken());
if (userPhoneInfo == null) {
throw new ServiceException("获取用户手机号失败,请重新登录!");
}
if (!"0".equals(userPhoneInfo.getErrCode())) {
throw new ServiceException(userPhoneInfo.getErrmsg());
}
String phoneNumber = userPhoneInfo.getPhoneInfo().getPhoneNumber();
SysUser sysUser = sysUserService.selectUserByPhoneNumber(phoneNumber);
String token;
WeChatLoginResult weChatLoginResult = new WeChatLoginResult();
Long bindSysUserId;
if (sysUser == null) {
// 直接用手机号注册一个新用户,密码就是手机号
RegisterUserInput registerUserInput = new RegisterUserInput();
registerUserInput.setUsername(phoneNumber);
registerUserInput.setPhonenumber(phoneNumber);
registerUserInput.setPassword(phoneNumber);
RegisterUserOutput registerUserOutput = toolService.registerNoCaptcha(registerUserInput);
if (StringUtils.isNotEmpty(registerUserOutput.getMsg())) {
throw new ServiceException(registerUserOutput.getMsg());
}
bindSysUserId = registerUserOutput.getSysUserId();
token = sysLoginService.redirectLogin(phoneNumber, registerUserOutput.getEncryptPassword(), language);
} else {
// if (null != sysUser.getDeptId()) {
// throw new ServiceException("只允许终端用户登录!");
// }
bindSysUserId = sysUser.getUserId();
token = sysLoginService.redirectLogin(sysUser.getUserName(), sysUser.getPassword(), language);
}
Long oldBindUserId = socialUserService.selectSysUserIdByUnionId(weChatResult.getUnionId());
// 当前手机号登录用户绑定当前微信信息
if (null == oldBindUserId || oldBindUserId.equals(bindSysUserId)) {
miniCreateOrUpdateSocialUser(socialUser, socialPlatform, weChatLoginBody, weChatResult, result.getAccessToken(), bindSysUserId);
}
weChatLoginResult.setToken(token);
return weChatLoginResult;
}
private void miniCreateOrUpdateSocialUser(SocialUser socialUser, SocialPlatform socialPlatform, WeChatLoginBody weChatLoginBody, WeChatMiniProgramResult weChatResult, String accessToken, Long bindSysUserId) {
Date nowDate = DateUtils.getNowDate();
String uuid = IdUtils.randomUUID();
if (socialUser == null) {
SocialUser insertSocialUser = new SocialUser();
String sourceClient = socialPlatform.getPlatform();
insertSocialUser.setSysUserId(bindSysUserId).setStatus("1").setUuid(uuid).setSource(sourceClient.toUpperCase(Locale.ROOT)).setSourceClient(sourceClient)
.setAccessToken(accessToken).setOpenId(weChatResult.getOpenId()).setUnionId(weChatResult.getUnionId())
.setCode(weChatLoginBody.getCode()).setDelFlag("0");
insertSocialUser.setCreateBy("System");
insertSocialUser.setCreateTime(nowDate);
socialUserService.insertSocialUser(insertSocialUser);
} else {
SocialUser updateSocialUser = new SocialUser();
updateSocialUser.setSocialUserId(socialUser.getSocialUserId());
updateSocialUser.setSysUserId(bindSysUserId).setStatus("1").setCode(weChatLoginBody.getCode());
updateSocialUser.setUpdateBy("System");
socialUserService.updateSocialUser(updateSocialUser);
}
}
/**
* 取消所有相关微信绑定
* @param wxCancelBindReqVO 微信解绑传参类型
* @return 结果
*/
@Override
public AjaxResult cancelBind(WxCancelBindReqVO wxCancelBindReqVO) {
LoginUser loginUser = getLoginUser();
if (loginUser == null || loginUser.getUserId() == null) {
throw new ServiceException("请先登录后重试");
}
// 密码验证
if (VerifyTypeEnum.PASSWORD.getVerifyType().equals(wxCancelBindReqVO.getVerifyType())) {
if (StringUtils.isEmpty(wxCancelBindReqVO.getPassword())) {
throw new ServiceException("请传入用户密码");
}
Boolean validateResult = sysUserService.validatePassword(loginUser.getUser().getPassword(), wxCancelBindReqVO.getPassword());
if (Boolean.FALSE.equals(validateResult)) {
throw new ServiceException("密码错误,请重新输入");
}
}
// 解绑所有微信应用
int cancelBind = socialUserService.cancelBind(loginUser.getUserId(), SocialPlatformType.listWechatPlatform);
return cancelBind >= 1 ? success("解绑成功") : AjaxResult.error("解绑失败");
}
/**
* 小程序、移动应用微信绑定
* @param wxBindReqVO 微信绑定传参类型
* @return 结果
*/
@Override
public AjaxResult bind(WxBindReqVO wxBindReqVO) {
Long sysUserId = getUserId();
if (sysUserId == null) {
throw new ServiceException("请登录后重试");
}
String openId = "";
String unionId = "";
// 区分小程序绑定还是移动应用绑定
if (SocialPlatformType.WECHAT_OPEN_MOBILE.sourceClient.equals(wxBindReqVO.getSourceClient())) {
if (StringUtils.isEmpty(wxBindReqVO.getOpenId()) || StringUtils.isEmpty(wxBindReqVO.getUnionId())) {
throw new ServiceException("请传入微信用户信息");
}
openId = wxBindReqVO.getOpenId();
unionId = wxBindReqVO.getUnionId();
} else if (SocialPlatformType.WECHAT_OPEN_MINI_PROGRAM.sourceClient.equals(wxBindReqVO.getSourceClient())) {
if (StringUtils.isEmpty(wxBindReqVO.getCode())) {
throw new ServiceException("请传入用户凭证");
}
// 查询微信平台信息
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(SocialPlatformType.WECHAT_OPEN_MINI_PROGRAM.sourceClient);
if (socialPlatform == null) {
throw new ServiceException("请先配置微信开放平台小程序信息!");
}
WeChatMiniProgramResult weChatMiniProgramResult = WechatUtils.codeToSession(wxBindReqVO.getCode(), socialPlatform.getClientId(), socialPlatform.getSecretKey());
if (weChatMiniProgramResult == null
|| (StringUtils.isEmpty(weChatMiniProgramResult.getOpenId()) && StringUtils.isEmpty(weChatMiniProgramResult.getUnionId()))) {
throw new ServiceException("获取微信信息失败,请重试!");
}
openId = weChatMiniProgramResult.getOpenId();
unionId = weChatMiniProgramResult.getUnionId();
}
Long bindUserId = socialUserService.selectSysUserIdByUnionId(unionId);
if (bindUserId != null && !bindUserId.equals(sysUserId)) {
throw new ServiceException("您的微信已绑定其他账号,请先使用微信登录解绑后重试!");
}
SocialUser socialUser = socialUserService.selectOneByOpenIdAndUnionId(openId, unionId);
int bindResult;
List<SocialUser> socialUserList = socialUserService.selectBySysUserId(sysUserId);
if (CollectionUtils.isNotEmpty(socialUserList)) {
return success("绑定成功!");
}
if (socialUser != null) {
if (socialUser.getSysUserId() != null && !sysUserId.equals(socialUser.getSysUserId())) {
throw new ServiceException("该微信已绑定其他账号,请先使用微信登录解绑后重试!");
}
SocialUser updateSocialUser = new SocialUser();
updateSocialUser.setSocialUserId(socialUser.getSocialUserId());
updateSocialUser.setSysUserId(sysUserId).setStatus("1");
updateSocialUser.setUpdateBy("System");
bindResult = socialUserService.updateSocialUser(updateSocialUser);
} else {
Date nowDate = DateUtils.getNowDate();
String uuid = IdUtils.randomUUID();
SocialUser insertSocialUser = new SocialUser();
insertSocialUser.setSysUserId(sysUserId).setStatus("1").setUuid(uuid).setSource(wxBindReqVO.getSourceClient().toUpperCase(Locale.ROOT)).setSourceClient(wxBindReqVO.getSourceClient())
.setOpenId(openId).setUnionId(unionId)
.setDelFlag("0");
insertSocialUser.setCreateBy("System");
insertSocialUser.setCreateTime(nowDate);
bindResult = socialUserService.insertSocialUser(insertSocialUser);
}
// 绑定
return bindResult >= 1 ? success("绑定成功!") : AjaxResult.error("绑定失败");
}
@Override
public AjaxResult getWxBindQr(HttpServletRequest httpServletRequest) {
Long sysUserId = getUserId();
if (sysUserId == null) {
throw new ServiceException("请先登录后重试!");
}
WeChatLoginQrRes weChatLoginQrRes = new WeChatLoginQrRes();
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(SocialPlatformType.WECHAT_OPEN_WEB_BIND.sourceClient);
if (socialPlatform == null) {
throw new ServiceException("请先配置微信开放平台网站应用个人中心绑定信息");
}
weChatLoginQrRes.setAppid(socialPlatform.getClientId());
weChatLoginQrRes.setScope("snsapi_login");
weChatLoginQrRes.setState(String.valueOf(System.currentTimeMillis()));
String wxBindId = socialLoginService.genWxBindId(sysUserId);
String url = socialPlatform.getRedirectUri() + wxBindId;
weChatLoginQrRes.setRedirectUri(url);
return success(weChatLoginQrRes);
}
@Override
public String wxBindCallback(String code, String state, String wxBindId, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(SocialPlatformType.WECHAT_OPEN_WEB_BIND.sourceClient);
if (socialPlatform == null) {
String serverName = httpServletRequest.getServerName();
String msgId = socialLoginService.genErrorId("请先配置微信开放平台网站应用个人中心绑定信息");
return "https://" + serverName + "/user/profile?wxBindMsgId=" + msgId;
}
String url = socialPlatform.getRedirectLoginUri();
//获取临时票据 code
log.info("code:{}", code);
if (StringUtils.isEmpty(code)) {
String msgId = socialLoginService.genErrorId("您已取消授权或未获取到授权信息");
return url + msgId;
}
Long sysUserId = redisCache.getCacheObject(WX_BIND_REDIS_KEY + wxBindId);
if (sysUserId == null) {
String msgId = socialLoginService.genErrorId("二维码已失效,请重新点击绑定");
return url + msgId;
}
List<SocialUser> socialUserList = socialUserService.selectBySysUserId(sysUserId);
if (CollectionUtils.isNotEmpty(socialUserList)) {
String msgId = socialLoginService.genErrorId("您的账号已绑定微信,请先解绑");
return url + msgId;
}
// 组装获取accessToken的url
WeChatAppResult weChatAppResult = WechatUtils.getAccessTokenOpenId(code, socialPlatform.getClientId(), socialPlatform.getSecretKey());
if (weChatAppResult == null || StringUtils.isEmpty(weChatAppResult.getAccessToken())
|| StringUtils.isEmpty(weChatAppResult.getOpenId()) || StringUtils.isEmpty(weChatAppResult.getUnionId())) {
String msgId = socialLoginService.genErrorId("获取微信信息失败,请重试");
return url + msgId;
}
Long bindUserId = socialUserService.selectSysUserIdByUnionId(weChatAppResult.getUnionId());
if (bindUserId != null && !bindUserId.equals(sysUserId)) {
String msgId = socialLoginService.genErrorId("您的微信已绑定其他账号,请先使用微信登录解绑后重试!");
return url + msgId;
}
SocialUser socialUser = socialUserService.selectOneByOpenIdAndUnionId(weChatAppResult.getOpenId(), weChatAppResult.getUnionId());
if (socialUser == null) {
socialUser = new SocialUser();
}
WeChatUserInfo weChatUserInfo = WechatUtils.getWeChatUserInfo(weChatAppResult.getAccessToken(), weChatAppResult.getOpenId());
String uuid = IdUtils.randomUUID();
Date nowDate = DateUtils.getNowDate();
socialUser.setUuid(uuid).setAccessToken(weChatAppResult.getAccessToken()).setRefreshToken(weChatAppResult.getRefreshToken())
.setOpenId(weChatUserInfo.getOpenId()).setUnionId(weChatUserInfo.getUnionId()).setSysUserId(sysUserId).setStatus("1")
.setNickname(weChatUserInfo.getNickname()).setUsername(weChatUserInfo.getNickname()).setAvatar(weChatUserInfo.getHeadImgUrl());
// 个人中心绑定这个也做了一个配置,标识设为了 wechat_open_web_bind,但其实都属于网站应用所以这里还是改为使用wechat_open_web这个来记录来源吧,统一为网站来源,方便查询
socialUser.setSource(SocialPlatformType.WECHAT_OPEN_WEB.sourceClient).setSourceClient(SocialPlatformType.WECHAT_OPEN_WEB.sourceClient);
if (socialUser.getCreateTime() == null) {
socialUser.setCreateTime(nowDate);
socialUser.setCreateBy("System");
}
socialUser.setUpdateTime(nowDate);
socialUser.setUpdateBy("System");
int updateResult = socialUser.getSocialUserId() == null ? socialUserService.insertSocialUser(socialUser) : socialUserService.updateSocialUser(socialUser);
String msg = updateResult >= 1 ? "绑定成功" : "绑定失败";
String msgId = socialLoginService.genErrorId(msg);
return url + msgId;
}
@Override
public AjaxResult getWxBindMsg(String wxBindMsgId) {
String errorMsg = redisCache.getCacheObject(LOGIN_ERROR_MSG_REDIS_KEY + wxBindMsgId);
if (StringUtils.isEmpty(errorMsg)) {
return success();
} else if ("绑定成功".equals(errorMsg)) {
return success(errorMsg);
} else {
return error(errorMsg);
}
}
}

View File

@@ -0,0 +1,19 @@
package com.fastbee.iot.wechat.vo;
import lombok.Data;
/**
* @author fastb
* @date 2023-08-30 17:21
*/
@Data
public class WxBindReqVO {
private String sourceClient;
private String openId;
private String unionId;
private String code;
}

View File

@@ -0,0 +1,32 @@
package com.fastbee.iot.wechat.vo;
import lombok.Data;
/**
* 微信解绑传参类
* @author fastb
* @date 2023-08-30 15:10
*/
@Data
public class WxCancelBindReqVO {
/**
* 验证类型
*/
private Integer verifyType;
/**
* 用户密码
*/
private String password;
/**
* 手机号
*/
private String userPhone;
/**
* 短信验证码
*/
private String smsCode;
}