|
|
@@ -3,8 +3,6 @@ package com.ydtech.modules.admin.service.impl;
|
|
|
|
|
|
import cn.dev33.satoken.session.SaSession;
|
|
|
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;
|
|
|
@@ -12,10 +10,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.ydtech.config.WechatWorkConfig;
|
|
|
import com.ydtech.constants.sys.SysPasswordEnum;
|
|
|
import com.ydtech.exception.SystemException;
|
|
|
-import com.ydtech.modules.admin.constants.RoleConstants;
|
|
|
import com.ydtech.modules.admin.model.SysPassword;
|
|
|
import com.ydtech.modules.admin.model.SysUser;
|
|
|
-import com.ydtech.modules.admin.model.SysUserRole;
|
|
|
import com.ydtech.modules.admin.service.*;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.ydtech.core.page.HttpResult;
|
|
|
@@ -26,7 +22,6 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import me.chanjar.weixin.cp.api.WxCpService;
|
|
|
-import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -38,94 +33,113 @@ import javax.annotation.Resource;
|
|
|
public class WechatCpServiceImpl implements WechatCpService {
|
|
|
|
|
|
private final SysWechatWorkService sysWechatWorkService;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private WxCpService wxCpService;
|
|
|
-
|
|
|
@Resource
|
|
|
private SysUserService sysUserService;
|
|
|
|
|
|
- private WechatWorkConfig wechatWorkConfig;
|
|
|
-
|
|
|
- private final StringRedisTemplate redisTemplate;
|
|
|
-
|
|
|
private final SysPasswordService sysPasswordService;
|
|
|
|
|
|
+ public static final String CORP_ID = "wwfe67d19509d43ec5";
|
|
|
+ public static final String CORP_SECRET = "5M_5mGONe-t4xCPlwPU-EJt53GiWaUjtbJjY5CR-dgc";
|
|
|
|
|
|
-// @Override
|
|
|
-// public String getAccessToken() {
|
|
|
-// WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
|
|
|
-// config.setCorpId(CORP_ID);
|
|
|
-// config.setCorpSecret(CORP_SECRET);
|
|
|
-// config.setAgentId(AGENT_ID);
|
|
|
-//
|
|
|
-// WxCpService wxCpService = new WxCpServiceImpl();
|
|
|
-// wxCpService.setWxCpConfigStorage(config);
|
|
|
-//
|
|
|
-// try {
|
|
|
-// // 从redis里获取access_token
|
|
|
-// String accessToken = redisTemplate.opsForValue().get("access_token:" + userId);
|
|
|
-// if (!exists) {
|
|
|
-// String accessToken = wxCpService.getAccessToken();
|
|
|
-// redisTemplate.opsForValue().set("access_token", accessToken);
|
|
|
-// }
|
|
|
-// } catch (WxErrorException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// return "获取access_token失败" + e.getMessage();
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
- @Override
|
|
|
- public HttpResult<Object> bind(WechatCpBindVo wechatCpBindVo) throws WxErrorException {
|
|
|
+ // 获取accessToken
|
|
|
+ public String getAccessToken() {
|
|
|
// 获取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 accessTokenUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", CORP_ID, CORP_SECRET);
|
|
|
String accessTokenResponse = HttpUtil.get(accessTokenUrl);
|
|
|
JSONObject responseTokenJson = JSON.parseObject(accessTokenResponse);
|
|
|
- String accessToken = responseTokenJson.getString("access_token");
|
|
|
+ return responseTokenJson.getString("access_token");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 通过 accessToken和code 获取企业微信用户信息
|
|
|
+ private JSONObject getUserInfo(String code) {
|
|
|
+ try {
|
|
|
+ String userInfoUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s", getAccessToken(), code);
|
|
|
+ String userInfoResponse = HttpUtil.get(userInfoUrl);
|
|
|
+ return JSON.parseObject(userInfoResponse);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new SystemException("500", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据openid获取绑定用户信息
|
|
|
+ private SysWechatWork getWechatWorkByOpenid(String openid) {
|
|
|
+ return sysWechatWorkService.getOne(new LambdaQueryWrapper<SysWechatWork>().eq(SysWechatWork::getOpenid, openid));
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpResult<Object> loginUser(String userId) {
|
|
|
+ SysUser user = sysUserService.getById(userId);
|
|
|
+ String token = StpUtil.createLoginSession(userId);
|
|
|
+ StpUtil.getSessionByLoginId(userId).set(SaSession.USER, user);
|
|
|
+ return HttpResult.ok(token);
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpResult<Object> handleNewUserBinding(WechatCpBindVo wechatCpBindVo, String openid) {
|
|
|
+ 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("密码不正确");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存绑定关系并返回 token
|
|
|
+ SysWechatWork newWechatWork = new SysWechatWork();
|
|
|
+ newWechatWork.setUserId(userId);
|
|
|
+ newWechatWork.setOpenid(openid);
|
|
|
+ sysWechatWorkService.save(newWechatWork);
|
|
|
+
|
|
|
+ return loginUser(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public HttpResult<Object> verifyBind(WechatCpBindVo wechatCpBindVo) {
|
|
|
+ // 获取企业微信用户信息
|
|
|
+ JSONObject userInfoJson = getUserInfo(wechatCpBindVo.getCode());
|
|
|
+ System.out.println(userInfoJson);
|
|
|
|
|
|
- //构建获取用户信息的 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);
|
|
|
- StpUtil.getSessionByLoginId(user.getId()).set(SaSession.USER, user);
|
|
|
- return HttpResult.ok(token);
|
|
|
+ SysWechatWork wechatWork = getWechatWorkByOpenid(openid);
|
|
|
+
|
|
|
+ if (wechatWork != null) {
|
|
|
+ // 已绑定用户直接返回token
|
|
|
+ return loginUser(wechatWork.getUserId());
|
|
|
} else {
|
|
|
- // 如果不存在绑定关系,则创建新的绑定关系并返回登录 token
|
|
|
- SysWechatWork newWechatWork = new SysWechatWork();
|
|
|
- newWechatWork.setUserId(wechatCpBindVo.getUserId());
|
|
|
- newWechatWork.setOpenid(openid);
|
|
|
- sysWechatWorkService.save(newWechatWork);
|
|
|
- String token = StpUtil.createLoginSession(userId);
|
|
|
- StpUtil.getSessionByLoginId(user.getId()).set(SaSession.USER, user);
|
|
|
- return HttpResult.ok(token);
|
|
|
+ // 用户信息验证成功,但尚未绑定
|
|
|
+ return HttpResult.ok(null);
|
|
|
}
|
|
|
} else {
|
|
|
- // 处理未能获取到用户信息的情况,可能是 code 无效或者其他原因
|
|
|
- throw new SystemException("未能获取到企业微信用户信息,可能是 code 无效或者接口调用失败。");
|
|
|
+ // 获取用户信息失败
|
|
|
+ throw new SystemException("未能获取到企业微信用户信息");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpResult<Object> bind(WechatCpBindVo wechatCpBindVo) throws WxErrorException {
|
|
|
+ // 获取企业微信用户信息
|
|
|
+ JSONObject userInfoJson = getUserInfo(wechatCpBindVo.getCode());
|
|
|
+ // 验证获取用户信息是否成功
|
|
|
+ if (userInfoJson.containsKey("userid")) {
|
|
|
+ String openid = userInfoJson.getString("userid");
|
|
|
+ // 处理新的用户绑定
|
|
|
+ return handleNewUserBinding(wechatCpBindVo, openid);
|
|
|
+ } else {
|
|
|
+ // 获取用户信息失败
|
|
|
+ throw new SystemException("未能获取到企业微信用户信息");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|