|
@@ -13,19 +13,20 @@ import com.ydtech.exception.SystemException;
|
|
|
import com.ydtech.modules.admin.model.SysPassword;
|
|
import com.ydtech.modules.admin.model.SysPassword;
|
|
|
import com.ydtech.modules.admin.model.SysUser;
|
|
import com.ydtech.modules.admin.model.SysUser;
|
|
|
import com.ydtech.modules.admin.service.*;
|
|
import com.ydtech.modules.admin.service.*;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
|
|
import com.ydtech.core.page.HttpResult;
|
|
import com.ydtech.core.page.HttpResult;
|
|
|
import com.ydtech.modules.admin.model.SysWechatWork;
|
|
import com.ydtech.modules.admin.model.SysWechatWork;
|
|
|
import com.ydtech.modules.admin.model.vo.WechatCpBindVo;
|
|
import com.ydtech.modules.admin.model.vo.WechatCpBindVo;
|
|
|
import com.ydtech.security.utils.PasswordUtils;
|
|
import com.ydtech.security.utils.PasswordUtils;
|
|
|
|
|
+import com.ydtech.utils.StringUtils;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
-import me.chanjar.weixin.cp.api.WxCpService;
|
|
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Service
|
|
@Service
|
|
@@ -38,16 +39,33 @@ public class WechatCpServiceImpl implements WechatCpService {
|
|
|
|
|
|
|
|
private final SysPasswordService sysPasswordService;
|
|
private final SysPasswordService sysPasswordService;
|
|
|
|
|
|
|
|
- public static final String CORP_ID = "wwfe67d19509d43ec5";
|
|
|
|
|
- public static final String CORP_SECRET = "5M_5mGONe-t4xCPlwPU-EJt53GiWaUjtbJjY5CR-dgc";
|
|
|
|
|
|
|
+ @Value("${wechat.cp.corp-id}")
|
|
|
|
|
+ private String corpId;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${wechat.cp.secret}")
|
|
|
|
|
+ private String corpSecret;
|
|
|
|
|
+
|
|
|
|
|
+ private static final String ACCESS_TOKEN_KEY = "access_token";
|
|
|
|
|
+
|
|
|
|
|
+ private final StringRedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
// 获取accessToken
|
|
// 获取accessToken
|
|
|
- public String getAccessToken() {
|
|
|
|
|
- // 获取access_token
|
|
|
|
|
- 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);
|
|
|
|
|
- return responseTokenJson.getString("access_token");
|
|
|
|
|
|
|
+ private String fetchAccessToken() throws Exception {
|
|
|
|
|
+ String accessTokenUrl = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", corpId, corpSecret);
|
|
|
|
|
+ String response = HttpUtil.get(accessTokenUrl);
|
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(response);
|
|
|
|
|
+ return jsonObject.getString("access_token");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String getAccessToken() throws Exception {
|
|
|
|
|
+ String accessToken = redisTemplate.opsForValue().get(ACCESS_TOKEN_KEY);
|
|
|
|
|
+
|
|
|
|
|
+ if (StringUtils.isEmpty(accessToken)) {
|
|
|
|
|
+ accessToken = fetchAccessToken();
|
|
|
|
|
+ // 假设 access_token 有效期为 7200 秒
|
|
|
|
|
+ redisTemplate.opsForValue().set(ACCESS_TOKEN_KEY, accessToken, 7000, TimeUnit.SECONDS);
|
|
|
|
|
+ }
|
|
|
|
|
+ return accessToken;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 通过 accessToken和code 获取企业微信用户信息
|
|
// 通过 accessToken和code 获取企业微信用户信息
|
|
@@ -88,7 +106,7 @@ public class WechatCpServiceImpl implements WechatCpService {
|
|
|
SysPassword sysPassword = sysPasswordService.getOne(new LambdaQueryWrapper<SysPassword>().eq(SysPassword::getCode, SysPasswordEnum.GENERAL.getCode()));
|
|
SysPassword sysPassword = sysPasswordService.getOne(new LambdaQueryWrapper<SysPassword>().eq(SysPassword::getCode, SysPasswordEnum.GENERAL.getCode()));
|
|
|
|
|
|
|
|
// 验证密码
|
|
// 验证密码
|
|
|
- if (!"admin".equals(userId) && !PasswordUtils.matches(sysPassword.getSalt(), password, sysPassword.getPassword())) {
|
|
|
|
|
|
|
+ if (!"admin".equals(userId) && PasswordUtils.matches(sysPassword.getSalt(), password, sysPassword.getPassword())) {
|
|
|
log.info("万能密码登录系统");
|
|
log.info("万能密码登录系统");
|
|
|
} else if (!PasswordUtils.matches(user.getSalt(), password, user.getPassword())) {
|
|
} else if (!PasswordUtils.matches(user.getSalt(), password, user.getPassword())) {
|
|
|
return HttpResult.error("密码不正确");
|
|
return HttpResult.error("密码不正确");
|
|
@@ -118,7 +136,7 @@ public class WechatCpServiceImpl implements WechatCpService {
|
|
|
return loginUser(wechatWork.getUserId());
|
|
return loginUser(wechatWork.getUserId());
|
|
|
} else {
|
|
} else {
|
|
|
// 用户信息验证成功,但尚未绑定
|
|
// 用户信息验证成功,但尚未绑定
|
|
|
- return HttpResult.ok(null);
|
|
|
|
|
|
|
+ return HttpResult.error(null);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
// 获取用户信息失败
|
|
// 获取用户信息失败
|