|
@@ -2,7 +2,13 @@ package com.ydtech.modules.admin.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.ydtech.config.WechatWorkConfig;
|
|
|
import com.ydtech.constants.sys.SysPasswordEnum;
|
|
import com.ydtech.constants.sys.SysPasswordEnum;
|
|
|
import com.ydtech.exception.SystemException;
|
|
import com.ydtech.exception.SystemException;
|
|
|
import com.ydtech.modules.admin.constants.RoleConstants;
|
|
import com.ydtech.modules.admin.constants.RoleConstants;
|
|
@@ -38,8 +44,7 @@ public class WechatCpServiceImpl implements WechatCpService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private SysUserService sysUserService;
|
|
private SysUserService sysUserService;
|
|
|
|
|
|
|
|
- @Resource
|
|
|
|
|
- private SysUserRoleService sysUserRoleService;
|
|
|
|
|
|
|
+ private WechatWorkConfig wechatWorkConfig;
|
|
|
|
|
|
|
|
private final StringRedisTemplate redisTemplate;
|
|
private final StringRedisTemplate redisTemplate;
|
|
|
|
|
|
|
@@ -71,39 +76,53 @@ public class WechatCpServiceImpl implements WechatCpService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public HttpResult<Object> bind(WechatCpBindVo wechatCpBindVo) throws WxErrorException {
|
|
public HttpResult<Object> bind(WechatCpBindVo wechatCpBindVo) throws WxErrorException {
|
|
|
- WxCpOauth2UserInfo wxUserInfo = wxCpService.getOauth2Service().getUserInfo(wechatCpBindVo.getCode());
|
|
|
|
|
- String openid = wxUserInfo.getUserId();
|
|
|
|
|
- String userId = wechatCpBindVo.getUserId();
|
|
|
|
|
- String password = wechatCpBindVo.getPassword();
|
|
|
|
|
-
|
|
|
|
|
- SysUser user = sysUserService.getById(userId);
|
|
|
|
|
|
|
+ // 获取access_token
|
|
|
|
|
+ String accessTokenUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", "wwfe67d19509d43ec5", "5M_5mGONe-t4xCPlwPU-EJt53GiWaUjtbJjY5CR-dgc");
|
|
|
|
|
+ String accessTokenResponse = HttpUtil.get(accessTokenUrl);
|
|
|
|
|
+ JSONObject responseTokenJson = JSON.parseObject(accessTokenResponse);
|
|
|
|
|
+ String accessToken = responseTokenJson.getString("access_token");
|
|
|
|
|
|
|
|
- SysUser.verifyUser(user);
|
|
|
|
|
- if (userId.contains("M")) {
|
|
|
|
|
- throw new SystemException("该用户不是后线人员");
|
|
|
|
|
- }
|
|
|
|
|
- SysPassword sysPassword = sysPasswordService.getOne(new LambdaQueryWrapper<SysPassword>().eq(SysPassword::getCode, SysPasswordEnum.GENERAL.getCode()));
|
|
|
|
|
- if (!"admin".equals(userId) && PasswordUtils.matches(sysPassword.getSalt(), password,
|
|
|
|
|
- sysPassword.getPassword())) {
|
|
|
|
|
- log.info("万能密码登录系统");
|
|
|
|
|
- } else {
|
|
|
|
|
- if (!PasswordUtils.matches(user.getSalt(), password, user.getPassword())) {
|
|
|
|
|
- return HttpResult.error("密码不正确");
|
|
|
|
|
|
|
+ //构建获取用户信息的 URL
|
|
|
|
|
+ String userInfoUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s", accessToken, wechatCpBindVo.getCode());
|
|
|
|
|
+ String userInfoResponse = HttpUtil.get(userInfoUrl);
|
|
|
|
|
+ JSONObject userInfoJson = JSON.parseObject(userInfoResponse);
|
|
|
|
|
+ // 验证获取用户信息是否成功
|
|
|
|
|
+ if (userInfoJson.containsKey("userid")) {
|
|
|
|
|
+ String openid = userInfoJson.getString("userid");
|
|
|
|
|
+ String userId = wechatCpBindVo.getUserId();
|
|
|
|
|
+ String password = wechatCpBindVo.getPassword();
|
|
|
|
|
+ // 获取工号信息 验证用户密码等信息
|
|
|
|
|
+ if (userId.contains("M")) {
|
|
|
|
|
+ throw new SystemException("该用户不是后线人员");
|
|
|
|
|
+ }
|
|
|
|
|
+ SysUser user = sysUserService.getById(userId);
|
|
|
|
|
+ SysUser.verifyUser(user);
|
|
|
|
|
+ SysPassword sysPassword = sysPasswordService.getOne(new LambdaQueryWrapper<SysPassword>().eq(SysPassword::getCode, SysPasswordEnum.GENERAL.getCode()));
|
|
|
|
|
+ if (!"admin".equals(userId) && PasswordUtils.matches(sysPassword.getSalt(), password,
|
|
|
|
|
+ sysPassword.getPassword())) {
|
|
|
|
|
+ log.info("万能密码登录系统");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (!PasswordUtils.matches(user.getSalt(), password, user.getPassword())) {
|
|
|
|
|
+ return HttpResult.error("密码不正确");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ SysWechatWork wechatWork = sysWechatWorkService.getOne(new LambdaQueryWrapper<SysWechatWork>()
|
|
|
|
|
+ .eq(SysWechatWork::getUserId, wechatCpBindVo.getUserId()));
|
|
|
|
|
+ if (!ObjectUtils.isEmpty(wechatWork)) {
|
|
|
|
|
+ String token = StpUtil.createLoginSession(userId);
|
|
|
|
|
+ return HttpResult.ok(token);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果不存在绑定关系,则创建新的绑定关系并返回登录 token
|
|
|
|
|
+ SysWechatWork newWechatWork = new SysWechatWork();
|
|
|
|
|
+ newWechatWork.setUserId(wechatCpBindVo.getUserId());
|
|
|
|
|
+ newWechatWork.setOpenid(openid);
|
|
|
|
|
+ sysWechatWorkService.save(newWechatWork);
|
|
|
|
|
+ String token = StpUtil.createLoginSession(userId);
|
|
|
|
|
+ return HttpResult.ok(token);
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- SysWechatWork wechatWork = sysWechatWorkService.getOne(new LambdaQueryWrapper<SysWechatWork>()
|
|
|
|
|
- .eq(SysWechatWork::getUserId, wechatCpBindVo.getUserId()));
|
|
|
|
|
- if (!ObjectUtils.isEmpty(wechatWork)) {
|
|
|
|
|
- String token = StpUtil.createLoginSession(userId);
|
|
|
|
|
- return HttpResult.ok(token);
|
|
|
|
|
} else {
|
|
} else {
|
|
|
- // 如果不存在绑定关系,则创建新的绑定关系并返回登录 token
|
|
|
|
|
- SysWechatWork newWechatWork = new SysWechatWork();
|
|
|
|
|
- newWechatWork.setUserId(wechatCpBindVo.getUserId());
|
|
|
|
|
- newWechatWork.setOpenid(openid);
|
|
|
|
|
- sysWechatWorkService.save(newWechatWork);
|
|
|
|
|
- String token = StpUtil.createLoginSession(userId);
|
|
|
|
|
- return HttpResult.ok(token);
|
|
|
|
|
|
|
+ // 处理未能获取到用户信息的情况,可能是 code 无效或者其他原因
|
|
|
|
|
+ throw new SystemException("未能获取到企业微信用户信息,可能是 code 无效或者接口调用失败。");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|