feat: 参考v3里面处理接口逻辑调整
This commit is contained in:
@@ -50,6 +50,8 @@ public class SmsServiceImpl implements SmsService {
|
|||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private PropertiesProxy conf;
|
private PropertiesProxy conf;
|
||||||
|
@Inject
|
||||||
|
private ZjnuMessageService zjnuMessageService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送短信消息。
|
* 发送短信消息。
|
||||||
@@ -59,6 +61,9 @@ public class SmsServiceImpl implements SmsService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public MsgPlatformResponse sendSms(MsgPlatformSmsRequest request) {
|
public MsgPlatformResponse sendSms(MsgPlatformSmsRequest request) {
|
||||||
|
if ("zjnu".equals(conf.get("msg.platform.provider", "cug"))) {
|
||||||
|
return zjnuMessageService.send(request, false);
|
||||||
|
}
|
||||||
validateCommonRequest(request);
|
validateCommonRequest(request);
|
||||||
JSONObject payload = buildCommonPayload(request, true);
|
JSONObject payload = buildCommonPayload(request, true);
|
||||||
return doPost(SMS_API_PATH, payload);
|
return doPost(SMS_API_PATH, payload);
|
||||||
@@ -107,6 +112,9 @@ public class SmsServiceImpl implements SmsService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public MsgPlatformResponse sendWechat(MsgPlatformWechatRequest request) {
|
public MsgPlatformResponse sendWechat(MsgPlatformWechatRequest request) {
|
||||||
|
if ("zjnu".equals(conf.get("msg.platform.provider", "cug"))) {
|
||||||
|
return zjnuMessageService.send(request, true);
|
||||||
|
}
|
||||||
validateCommonRequest(request);
|
validateCommonRequest(request);
|
||||||
if (Strings.isBlank(request.getWechatType())) {
|
if (Strings.isBlank(request.getWechatType())) {
|
||||||
request.setWechatType("text");
|
request.setWechatType("text");
|
||||||
@@ -342,6 +350,7 @@ public class SmsServiceImpl implements SmsService {
|
|||||||
* @param request 平台请求参数
|
* @param request 平台请求参数
|
||||||
*/
|
*/
|
||||||
private void fillDefaultSenderInfo(MsgPlatformBaseRequest request) {
|
private void fillDefaultSenderInfo(MsgPlatformBaseRequest request) {
|
||||||
|
if ("zjnu".equals(conf.get("msg.platform.provider", "cug"))) return;
|
||||||
request.setSendUserId(getRequiredConfig("msg.platform.default-send-user-id", "请先配置 msg.platform.default-send-user-id"));
|
request.setSendUserId(getRequiredConfig("msg.platform.default-send-user-id", "请先配置 msg.platform.default-send-user-id"));
|
||||||
request.setSendUserName(getRequiredConfig("msg.platform.default-send-user-name", "请先配置 msg.platform.default-send-user-name"));
|
request.setSendUserName(getRequiredConfig("msg.platform.default-send-user-name", "请先配置 msg.platform.default-send-user-name"));
|
||||||
request.setSendUnitId(conf.get("msg.platform.default-send-unit-id", ""));
|
request.setSendUnitId(conf.get("msg.platform.default-send-unit-id", ""));
|
||||||
|
|||||||
@@ -0,0 +1,165 @@
|
|||||||
|
package com.budwk.app.base.sms.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.crypto.digest.DigestUtil;
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import cn.hutool.http.HttpResponse;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.budwk.app.base.exception.BaseException;
|
||||||
|
import com.budwk.app.base.sms.model.*;
|
||||||
|
import com.budwk.app.sys.models.Sys_user;
|
||||||
|
import com.budwk.app.web.commons.base.Globals;
|
||||||
|
import org.nutz.dao.Cnd;
|
||||||
|
import org.nutz.dao.Dao;
|
||||||
|
import org.nutz.integration.jedis.RedisService;
|
||||||
|
import org.nutz.ioc.impl.PropertiesProxy;
|
||||||
|
import org.nutz.ioc.loader.annotation.Inject;
|
||||||
|
import org.nutz.ioc.loader.annotation.IocBean;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@IocBean
|
||||||
|
public class ZjnuMessageService {
|
||||||
|
@Inject
|
||||||
|
private PropertiesProxy conf;
|
||||||
|
@Inject
|
||||||
|
private RedisService redisService;
|
||||||
|
@Inject
|
||||||
|
private Dao dao;
|
||||||
|
|
||||||
|
public MsgPlatformResponse send(MsgPlatformBaseRequest request, boolean wechat) {
|
||||||
|
if (request == null) throw new BaseException("请求参数不能为空");
|
||||||
|
if (!Globals.sso || !Globals.MyConfig.getBoolean("AppSms", false)
|
||||||
|
|| !conf.getBoolean("msg.platform.enabled", false)) {
|
||||||
|
MsgPlatformResponse result = new MsgPlatformResponse();
|
||||||
|
result.setHttpStatus(200);
|
||||||
|
result.setCode(0);
|
||||||
|
result.setResult(true);
|
||||||
|
result.setMsg("消息发送开关未开启,已跳过实际发送");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
String content = request.getInfo();
|
||||||
|
if (StrUtil.isBlank(content) && request instanceof MsgPlatformWechatRequest) {
|
||||||
|
List<MsgPlatformNewsItem> items = ((MsgPlatformWechatRequest) request).getNewsItems();
|
||||||
|
if (items != null) {
|
||||||
|
StringBuilder text = new StringBuilder();
|
||||||
|
for (MsgPlatformNewsItem item : items) {
|
||||||
|
if (item == null) continue;
|
||||||
|
for (String value : new String[]{item.getTitle(), item.getDescription(), item.getUrl()}) {
|
||||||
|
if (StrUtil.isNotBlank(value)) text.append(value).append('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content = text.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (StrUtil.isBlank(content)) {
|
||||||
|
throw new BaseException("学校消息平台需要消息正文,不能仅使用原通讯平台的模板ID");
|
||||||
|
}
|
||||||
|
List<MsgPlatformRecipient> recipients = recipients(request);
|
||||||
|
if (recipients.isEmpty() || recipients.size() > 500) {
|
||||||
|
throw new BaseException("单次消息接收人数必须为1至500人");
|
||||||
|
}
|
||||||
|
List<Map<String, Object>> receivers = new ArrayList<>();
|
||||||
|
for (MsgPlatformRecipient recipient : recipients) {
|
||||||
|
if (recipient == null) throw new BaseException("接收人不能为空");
|
||||||
|
String account = recipient.getAccount();
|
||||||
|
String name = recipient.getName();
|
||||||
|
// v3消息平台使用学工号;仅提供手机号时从本地人员表解析,不能把手机号冒充学工号。
|
||||||
|
if (StrUtil.isBlank(account) || account.matches("\\d{11}")) {
|
||||||
|
String mobile = StrUtil.blankToDefault(recipient.getMobile(), account);
|
||||||
|
if (StrUtil.isBlank(mobile)) throw new BaseException("Message recipient requires an account or mobile number");
|
||||||
|
List<Sys_user> users = dao.query(Sys_user.class,
|
||||||
|
StrUtil.isBlank(account) ? Cnd.where("mobile", "=", mobile)
|
||||||
|
: Cnd.where("loginname", "=", account).or("mobile", "=", mobile));
|
||||||
|
if (users.size() != 1) throw new BaseException("无法为消息接收人确定唯一学工号");
|
||||||
|
account = users.get(0).getLoginname();
|
||||||
|
name = StrUtil.blankToDefault(name, users.get(0).getUsername());
|
||||||
|
}
|
||||||
|
if (StrUtil.isBlank(account)) throw new BaseException("消息接收人缺少学工号");
|
||||||
|
receivers.add(Map.of("type", "User", "userId", account, "name", StrUtil.nullToEmpty(name)));
|
||||||
|
}
|
||||||
|
Map<String, Object> form = new LinkedHashMap<>();
|
||||||
|
form.put("token", getToken());
|
||||||
|
form.put("msgtitle", conf.get("msg.platform.default-title", "工会通知"));
|
||||||
|
form.put("msgContent", content.replace("<", "<").replace(">", ">"));
|
||||||
|
form.put("msgType", "0");
|
||||||
|
form.put("sendStatus", "1");
|
||||||
|
form.put("sendChannel", wechat ? conf.get("msg.platform.wechat-channel", "IntelligenceMode") : "SMS");
|
||||||
|
form.put("sendMode", "normal");
|
||||||
|
form.put("receivers", JSONUtil.toJsonStr(receivers));
|
||||||
|
form.put("summary", "");
|
||||||
|
form.put("sendTime", StrUtil.nullToEmpty(request.getSendTime()));
|
||||||
|
form.put("isReply", "0");
|
||||||
|
form.put("sendMsgType", "1");
|
||||||
|
try (HttpResponse response = HttpRequest.post(required("msg.platform.send-url"))
|
||||||
|
.form(form).timeout(conf.getInt("msg.platform.read-timeout", 10000)).execute()) {
|
||||||
|
MsgPlatformResponse result = new MsgPlatformResponse();
|
||||||
|
result.setHttpStatus(response.getStatus());
|
||||||
|
result.setRawBody(response.body());
|
||||||
|
if (!response.isOk() || !JSONUtil.isTypeJSONObject(response.body())) {
|
||||||
|
result.setCode(-1);
|
||||||
|
result.setResult(false);
|
||||||
|
result.setMsg("学校消息平台返回异常,HTTP状态码:" + response.getStatus());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
JSONObject body = JSONUtil.parseObj(response.body());
|
||||||
|
Integer code = body.getInt("rspCode");
|
||||||
|
result.setCode(code == null ? -1 : code);
|
||||||
|
result.setResult(Integer.valueOf(0).equals(code));
|
||||||
|
result.setMsg(body.getStr("rspMsg"));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<MsgPlatformRecipient> recipients(MsgPlatformBaseRequest request) {
|
||||||
|
if (StrUtil.isBlank(request.getPersonInfo())) {
|
||||||
|
return request.getRecipients() == null ? List.of() : request.getRecipients();
|
||||||
|
}
|
||||||
|
List<MsgPlatformRecipient> result = new ArrayList<>();
|
||||||
|
for (String line : request.getPersonInfo().split("\\^@\\^", -1)) {
|
||||||
|
String[] fields = line.split("\\|", -1);
|
||||||
|
if (fields.length != 5) throw new BaseException("personInfo 必须包含姓名、学工号、机构ID、机构名称和手机号五段");
|
||||||
|
MsgPlatformRecipient recipient = new MsgPlatformRecipient();
|
||||||
|
recipient.setName(fields[0]);
|
||||||
|
recipient.setAccount(fields[1]);
|
||||||
|
recipient.setMobile(fields[4]);
|
||||||
|
result.add(recipient);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getToken() {
|
||||||
|
String url = required("msg.platform.token-url");
|
||||||
|
String appId = required("msg.platform.app-id");
|
||||||
|
String password = required("msg.platform.app-password");
|
||||||
|
String cacheKey = "zjnu:msg:token:" + DigestUtil.sha256Hex(url + "|" + appId + "|" + password);
|
||||||
|
String cached = redisService.get(cacheKey);
|
||||||
|
if (StrUtil.isNotBlank(cached)) return cached;
|
||||||
|
try (HttpResponse response = HttpRequest.get(url).form(Map.of("appId", appId, "appPassword", password))
|
||||||
|
.timeout(conf.getInt("msg.platform.read-timeout", 10000)).execute()) {
|
||||||
|
if (!response.isOk()) throw new BaseException("学校消息平台Token请求失败");
|
||||||
|
JSONObject body = JSONUtil.parseObj(response.body());
|
||||||
|
if (body.containsKey("rspCode") && !Integer.valueOf(0).equals(body.getInt("rspCode"))) {
|
||||||
|
throw new BaseException("学校消息平台Token返回失败");
|
||||||
|
}
|
||||||
|
Object data = body.get("resultData");
|
||||||
|
JSONObject result = data instanceof CharSequence ? JSONUtil.parseObj(data.toString()) : JSONUtil.parseObj(data);
|
||||||
|
String token = result.getStr("token");
|
||||||
|
if (StrUtil.isBlank(token)) throw new BaseException("学校消息平台未返回Token");
|
||||||
|
redisService.setex(cacheKey, 1200, token);
|
||||||
|
return token;
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
throw new BaseException("获取学校消息平台Token失败,请检查地址和凭证配置");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String required(String key) {
|
||||||
|
String value = conf.get(key, "");
|
||||||
|
if (StrUtil.isBlank(value)) throw new BaseException("请先配置{}", key);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -194,25 +194,56 @@ public class SysLoginController {
|
|||||||
@Ok("re")
|
@Ok("re")
|
||||||
@ApiOperation("企业微信登录回调")
|
@ApiOperation("企业微信登录回调")
|
||||||
public String wxworkOAuth2(@Param("code") String code, @Param("redirect") String redirect, HttpServletRequest request) {
|
public String wxworkOAuth2(@Param("code") String code, @Param("redirect") String redirect, HttpServletRequest request) {
|
||||||
if (StrUtil.isBlank(code)) {
|
Object expectedState = request.getSession().getAttribute("wxwork.oauth.state");
|
||||||
return ">>:/platform/home";
|
request.getSession().removeAttribute("wxwork.oauth.state");
|
||||||
}
|
if (!Globals.sso || !conf.getBoolean("wxwork.enable", false)
|
||||||
String token = redisService.get(RedisConstant.REDIS_KEY_WX_WORK_TOKEN);
|
|| StrUtil.isBlank(code) || expectedState == null
|
||||||
if (StrUtil.isBlank(token)) {
|
|| !expectedState.equals(request.getParameter("state"))) {
|
||||||
return ">>:/platform/home";
|
|
||||||
}
|
|
||||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=ACCESS_TOKEN&code=CODE";
|
|
||||||
url = url.replace("ACCESS_TOKEN", token).replace("CODE", code);
|
|
||||||
String response = HttpUtil.get(url);
|
|
||||||
JSONObject jsonObject = JSONUtil.parseObj(response);
|
|
||||||
if (jsonObject.getInt("errcode") == 0) {
|
|
||||||
String loginName = jsonObject.getStr("userid");
|
|
||||||
sysUserService.checkThirdPlatformLoginName(loginName);
|
|
||||||
Sys_user sysUser = sysUserService.loginByLoginName(loginName);
|
|
||||||
return ">>:" + sysUserService.loginPlus(sysUser, LoginType.QI_YE_WECHAT, request);
|
|
||||||
} else {
|
|
||||||
return ">>:/platform/home/500";
|
return ">>:/platform/home/500";
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
JSONObject userInfo = null;
|
||||||
|
for (int attempt = 0; attempt < 2; attempt++) {
|
||||||
|
String token = getWxWorkToken(attempt > 0);
|
||||||
|
String response = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo",
|
||||||
|
Map.of("access_token", token, "code", code));
|
||||||
|
userInfo = JSONUtil.parseObj(response);
|
||||||
|
Integer error = userInfo.getInt("errcode");
|
||||||
|
if (!Integer.valueOf(40014).equals(error) && !Integer.valueOf(42001).equals(error)
|
||||||
|
&& !Integer.valueOf(40001).equals(error)) break;
|
||||||
|
}
|
||||||
|
if (userInfo != null && Integer.valueOf(0).equals(userInfo.getInt("errcode"))) {
|
||||||
|
String loginName = StrUtil.blankToDefault(userInfo.getStr("UserId"), userInfo.getStr("userid"));
|
||||||
|
if (StrUtil.isBlank(loginName)) return ">>:/platform/home/500";
|
||||||
|
sysUserService.checkThirdPlatformLoginName(loginName);
|
||||||
|
Sys_user sysUser = sysUserService.loginByLoginName(loginName);
|
||||||
|
return ">>:" + sysUserService.loginPlus(sysUser, LoginType.QI_YE_WECHAT, request);
|
||||||
|
}
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
log.warn("Enterprise WeChat authentication failed");
|
||||||
|
}
|
||||||
|
return ">>:/platform/home/500";
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getWxWorkToken(boolean refresh) {
|
||||||
|
String corpId = conf.get("wxwork.corpId", "");
|
||||||
|
String secret = conf.get("wxwork.corpSecret", "");
|
||||||
|
if (StrUtil.isBlank(corpId) || StrUtil.isBlank(secret)) {
|
||||||
|
throw new BaseException("Missing enterprise WeChat configuration");
|
||||||
|
}
|
||||||
|
String key = RedisConstant.REDIS_KEY_WX_WORK_TOKEN + ":"
|
||||||
|
+ cn.hutool.crypto.digest.DigestUtil.sha256Hex(corpId + "|" + secret);
|
||||||
|
if (refresh) redisService.del(key);
|
||||||
|
String token = redisService.get(key);
|
||||||
|
if (StrUtil.isNotBlank(token)) return token;
|
||||||
|
JSONObject result = JSONUtil.parseObj(HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken",
|
||||||
|
Map.of("corpid", corpId, "corpsecret", secret)));
|
||||||
|
token = result.getStr("access_token");
|
||||||
|
if (!Integer.valueOf(0).equals(result.getInt("errcode")) || StrUtil.isBlank(token)) {
|
||||||
|
throw new BaseException("Failed to acquire enterprise WeChat token");
|
||||||
|
}
|
||||||
|
redisService.setex(key, Math.max(1, result.getInt("expires_in", 7200) - 200), token);
|
||||||
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
@At(value = "/platform/wx/oauth2/callback", top = true)
|
@At(value = "/platform/wx/oauth2/callback", top = true)
|
||||||
|
|||||||
@@ -189,25 +189,9 @@ public class WkFailProcessor extends ViewProcessor {
|
|||||||
if (Globals.sso) {
|
if (Globals.sso) {
|
||||||
//1.判断是否为企业微信浏览器并且对接了企业微信
|
//1.判断是否为企业微信浏览器并且对接了企业微信
|
||||||
if (browserPlatform == BrowserPlatform.QI_YE_WEI_XIN && propertiesProxy.getBoolean("wxwork.enable", false)) {
|
if (browserPlatform == BrowserPlatform.QI_YE_WEI_XIN && propertiesProxy.getBoolean("wxwork.enable", false)) {
|
||||||
//进行企业微信授权
|
String state = java.util.UUID.randomUUID().toString();
|
||||||
// String token = getWxWorkToken();
|
request.getSession().setAttribute("wxwork.oauth.state", state);
|
||||||
// if (StrUtil.isNotBlank(token)) {
|
new ServerRedirectView(buildWxWorkOauth2Url(redirect, state)).render(request, response, null);
|
||||||
// //跳转到企业微信授权地址
|
|
||||||
// new ServerRedirectView(buildWxWorkOauth2Url()).render(request, response, null);
|
|
||||||
// } else {
|
|
||||||
// //跳转到错误页 别问我为啥错了 我也不知道 首先去检查配置文件里的参数有没有写对
|
|
||||||
//// new ServerRedirectView(Globals.AppDomain + errorUri).render(request, response, null);
|
|
||||||
// //跳转到cas登录页
|
|
||||||
// String callBackUrl = propertiesProxy.get("cas.client-host-url") + propertiesProxy.get("cas.client-call-back-url") + "?redirect=" + redirect;
|
|
||||||
// String authUrl = propertiesProxy.get("cas.qi-wx-server-login-url") + "?service=" + URLEncoder.encode(callBackUrl, StandardCharsets.UTF_8);
|
|
||||||
// new ServerRedirectView(authUrl).render(request, response, null);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 武汉城市职业学院 跳转到企业微信cas登录页
|
|
||||||
String callBackUrl = propertiesProxy.get("wecom.wecom-client-host-url") + propertiesProxy.get("wecom.wecom-client-call-back-url") + "?redirect=" + redirect;
|
|
||||||
String authUrl = propertiesProxy.get("wecom.wecom-server-login-url") + "?service=" + URLEncoder.encode(callBackUrl, StandardCharsets.UTF_8);
|
|
||||||
log.info("auth:"+authUrl);
|
|
||||||
new ServerRedirectView(authUrl).render(request, response, null);
|
|
||||||
} else if (browserPlatform == BrowserPlatform.WEI_XIN && propertiesProxy.getBoolean("wx.enable", false)) {
|
} else if (browserPlatform == BrowserPlatform.WEI_XIN && propertiesProxy.getBoolean("wx.enable", false)) {
|
||||||
//进行微信公众号授权
|
//进行微信公众号授权
|
||||||
//跳转到微信授权页
|
//跳转到微信授权页
|
||||||
@@ -275,11 +259,12 @@ public class WkFailProcessor extends ViewProcessor {
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private String buildWxWorkOauth2Url() {
|
private String buildWxWorkOauth2Url(String redirect, String state) {
|
||||||
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE&agentid=AGENTID#wechat_redirect";
|
String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&state=STATE&agentid=AGENTID#wechat_redirect";
|
||||||
url = url.replace("CORPID", propertiesProxy.get("wxwork.corpId"));
|
url = url.replace("CORPID", propertiesProxy.get("wxwork.corpId"));
|
||||||
url = url.replace("REDIRECT_URI", URLEncoder.encode(Globals.AppDomain + propertiesProxy.get("wxwork.redirectUri"), StandardCharsets.UTF_8));
|
url = url.replace("REDIRECT_URI", URLEncoder.encode(propertiesProxy.get("cas.client-host-url") + propertiesProxy.get("wxwork.redirectUri") + "?redirect=" + redirect, StandardCharsets.UTF_8));
|
||||||
url = url.replace("AGENTID", propertiesProxy.get("wxwork.agentId"));
|
url = url.replace("AGENTID", propertiesProxy.get("wxwork.agentId"));
|
||||||
|
url = url.replace("STATE", state);
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,24 +25,21 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
@IocBean
|
@IocBean
|
||||||
public class WeAppCloudUtil {
|
public class WeAppCloudUtil {
|
||||||
public static String ENV = "cloud1-1gxxg6xx50b1860e";
|
@Inject
|
||||||
|
private org.nutz.ioc.impl.PropertiesProxy conf;
|
||||||
private static final String APPID = "wxf750f43635f770e0";
|
|
||||||
|
|
||||||
private static final String APP_SECRET = "a54517e19ab700f745f20f27312ab8b2";
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public RedisService redisService;
|
public RedisService redisService;
|
||||||
|
|
||||||
public JSONObject request(CRUD crud, String sql) {
|
public JSONObject request(CRUD crud, String sql) {
|
||||||
String url = crud.url.replace("ACCESS_TOKEN", getAccessToken());
|
String url = crud.url.replace("ACCESS_TOKEN", getAccessToken());
|
||||||
String responseJson = HttpUtil.createPost(url).header("Content-Type", "application/json").body(JSON.toJSONString(Map.of("env", ENV, "query", sql))).execute().body();
|
String responseJson = HttpUtil.createPost(url).header("Content-Type", "application/json").body(JSON.toJSONString(Map.of("env", requiredConfig("weapp.env"), "query", sql))).execute().body();
|
||||||
JSONObject response = JSON.parseObject(responseJson);
|
JSONObject response = JSON.parseObject(responseJson);
|
||||||
if (response.getIntValue("errcode") == 42001) {
|
if (response.getIntValue("errcode") == 42001) {
|
||||||
redisService.del(RedisConstant.REDIS_KEY_WE_APP_ACCESS_TOKEN);
|
redisService.del(tokenCacheKey());
|
||||||
// request(crud, sql);
|
// request(crud, sql);
|
||||||
} else if (response.getIntValue("errcode") == 40001) {
|
} else if (response.getIntValue("errcode") == 40001) {
|
||||||
redisService.del(RedisConstant.REDIS_KEY_WE_APP_ACCESS_TOKEN);
|
redisService.del(tokenCacheKey());
|
||||||
} else if (response.getIntValue("errcode") != 0) {
|
} else if (response.getIntValue("errcode") != 0) {
|
||||||
throw new RuntimeException("小程序云函数接口调用失败,errcode:" + response.getString("errcode") + ",errmsg:" + response.getString("errmsg"));
|
throw new RuntimeException("小程序云函数接口调用失败,errcode:" + response.getString("errcode") + ",errmsg:" + response.getString("errmsg"));
|
||||||
}
|
}
|
||||||
@@ -55,14 +52,17 @@ public class WeAppCloudUtil {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private String getAccessToken() {
|
private String getAccessToken() {
|
||||||
String token = redisService.get(RedisConstant.REDIS_KEY_WE_APP_ACCESS_TOKEN);
|
String token = redisService.get(tokenCacheKey());
|
||||||
if (StrUtil.isNotBlank(token)) {
|
if (StrUtil.isNotBlank(token)) {
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET".replace("APPID", APPID).replace("APPSECRET", APP_SECRET);
|
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET".replace("APPID", requiredConfig("weapp.app-id")).replace("APPSECRET", requiredConfig("weapp.app-secret"));
|
||||||
String responseJson = HttpUtil.createGet(url).execute().body();
|
String responseJson = HttpUtil.createGet(url).execute().body();
|
||||||
TokenResponse tokenResponse = JSON.parseObject(responseJson, TokenResponse.class);
|
TokenResponse tokenResponse = JSON.parseObject(responseJson, TokenResponse.class);
|
||||||
redisService.setex(RedisConstant.REDIS_KEY_WE_APP_ACCESS_TOKEN, 7000, tokenResponse.getAccess_token());
|
if (tokenResponse == null || StrUtil.isBlank(tokenResponse.getAccess_token())) {
|
||||||
|
throw new RuntimeException("Failed to acquire mini-program access token");
|
||||||
|
}
|
||||||
|
redisService.setex(tokenCacheKey(), Math.max(1, tokenResponse.getExpires_in() - 200), tokenResponse.getAccess_token());
|
||||||
return tokenResponse.getAccess_token();
|
return tokenResponse.getAccess_token();
|
||||||
|
|
||||||
/*String result = Http.get(url).getContent();
|
/*String result = Http.get(url).getContent();
|
||||||
@@ -76,6 +76,18 @@ public class WeAppCloudUtil {
|
|||||||
return null;*/
|
return null;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String requiredConfig(String key) {
|
||||||
|
String value = conf.get(key, "");
|
||||||
|
if (StrUtil.isBlank(value)) throw new RuntimeException("Missing configuration: " + key);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String tokenCacheKey() {
|
||||||
|
return RedisConstant.REDIS_KEY_WE_APP_ACCESS_TOKEN + ":"
|
||||||
|
+ cn.hutool.crypto.digest.DigestUtil.sha256Hex(requiredConfig("weapp.app-id")
|
||||||
|
+ "|" + requiredConfig("weapp.app-secret"));
|
||||||
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public enum CRUD {
|
public enum CRUD {
|
||||||
@@ -111,7 +123,7 @@ public class WeAppCloudUtil {
|
|||||||
public String uploadFile(String rootPath, File file) {
|
public String uploadFile(String rootPath, File file) {
|
||||||
String path = rootPath + file.getName();
|
String path = rootPath + file.getName();
|
||||||
String url = "https://api.weixin.qq.com/tcb/uploadfile?access_token=ACCESS_TOKEN".replace("ACCESS_TOKEN", getAccessToken());
|
String url = "https://api.weixin.qq.com/tcb/uploadfile?access_token=ACCESS_TOKEN".replace("ACCESS_TOKEN", getAccessToken());
|
||||||
String responseJson = HttpUtil.createPost(url).header("Content-Type", "application/json").body(JSON.toJSONString(Map.of("env", ENV, "path", path))).execute().body();
|
String responseJson = HttpUtil.createPost(url).header("Content-Type", "application/json").body(JSON.toJSONString(Map.of("env", requiredConfig("weapp.env"), "path", path))).execute().body();
|
||||||
JSONObject jsonObject = JSON.parseObject(responseJson);
|
JSONObject jsonObject = JSON.parseObject(responseJson);
|
||||||
if (jsonObject.getIntValue("errcode") != 0) {
|
if (jsonObject.getIntValue("errcode") != 0) {
|
||||||
throw new RuntimeException(jsonObject.getString("errmsg"));
|
throw new RuntimeException(jsonObject.getString("errmsg"));
|
||||||
@@ -134,7 +146,7 @@ public class WeAppCloudUtil {
|
|||||||
String url = "https://api.weixin.qq.com/tcb/batchdownloadfile?access_token=ACCESS_TOKEN".replace("ACCESS_TOKEN", getAccessToken());
|
String url = "https://api.weixin.qq.com/tcb/batchdownloadfile?access_token=ACCESS_TOKEN".replace("ACCESS_TOKEN", getAccessToken());
|
||||||
|
|
||||||
HashMap<Object, Object> body = new HashMap<>(2) {{
|
HashMap<Object, Object> body = new HashMap<>(2) {{
|
||||||
put("env", ENV);
|
put("env", requiredConfig("weapp.env"));
|
||||||
put("file_list", new ArrayList<>() {{
|
put("file_list", new ArrayList<>() {{
|
||||||
add(new HashMap<>() {{
|
add(new HashMap<>() {{
|
||||||
put("fileid", fileId);
|
put("fileid", fileId);
|
||||||
|
|||||||
@@ -24,10 +24,14 @@
|
|||||||
sdk:
|
sdk:
|
||||||
wxwork:
|
wxwork:
|
||||||
enable: true
|
enable: true
|
||||||
corpId: wwaca81b3016b9a58c
|
corpId: 'ww5386df99fd3b6b0f'
|
||||||
corpSecret: Auc7919Su1Uds5ZErfZybBrt-8FkP5NA
|
corpSecret: 'HpSptE9wKZRq_c_kemAPJc0AfXkNSevpfYCJd1TTgrI'
|
||||||
agentId: 1000131
|
agentId: '1000002'
|
||||||
redirectUri: /platform/wxwork/oauth2/callback
|
redirectUri: /platform/wxwork/oauth2/callback
|
||||||
|
weapp:
|
||||||
|
app-id: 'wxb83d5697d571cd4f'
|
||||||
|
app-secret: '40a4f000c12424f23864a1d7e19eb080'
|
||||||
|
env: 'jsz-zjxu-3gg38x7m6cdd4c0f'
|
||||||
wx:
|
wx:
|
||||||
enable: false
|
enable: false
|
||||||
appID: wx4f65ff5f1b72e99d
|
appID: wx4f65ff5f1b72e99d
|
||||||
@@ -35,10 +39,10 @@ sdk:
|
|||||||
redirectUri: /platform/wx/oauth2/callback
|
redirectUri: /platform/wx/oauth2/callback
|
||||||
cas:
|
cas:
|
||||||
enable: false
|
enable: false
|
||||||
server-url-prefix: https://i.zjnu.njupt.edu.cn/cas
|
server-url-prefix: 'https://authserver.zjnu.edu.cn/authserver'
|
||||||
server-login-url: https://i.zjnu.njupt.edu.cn/cas/login
|
server-login-url: 'https://authserver.zjnu.edu.cn/authserver/login'
|
||||||
qi-wx-server-login-url: httos:/cas.zjnu.whcp.eou.cn/lyuapServer/wechatauz/wxAuthRedirect
|
qi-wx-server-login-url: 'https://authserver.zjnu.edu.cn/authserver/login'
|
||||||
client-host-url: https://zhgh.zjnu.njupt.edu.cn
|
client-host-url: 'http://localhost:8080'
|
||||||
client-call-back-url: /platform/sso/login
|
client-call-back-url: /platform/sso/login
|
||||||
validation-type: cas
|
validation-type: cas
|
||||||
minio:
|
minio:
|
||||||
@@ -61,10 +65,16 @@ custom:
|
|||||||
password: 3848A3C738EFBB2C2E76E0D59B419DACA16A80F3AB0354CD
|
password: 3848A3C738EFBB2C2E76E0D59B419DACA16A80F3AB0354CD
|
||||||
token-url: https://bd.whcp.zjnu.edu.cn/sjjkfwpt/api/bd-api/oauth/token
|
token-url: https://bd.whcp.zjnu.edu.cn/sjjkfwpt/api/bd-api/oauth/token
|
||||||
urls:
|
urls:
|
||||||
teacher: http://mdm.zjnu.edu.cn/do/api/call/lxyxt_jzgjbxx
|
teacher: http://localhost:3000/do/api/call/lxyxt_jzgjbxx
|
||||||
unit: http://mdm.zjnu.edu.cn/do/api/call/lxyxt_dwxx
|
unit: http://localhost:3000/do/api/call/lxyxt_dwxx
|
||||||
msg:
|
msg:
|
||||||
platform:
|
platform:
|
||||||
|
provider: 'zjnu'
|
||||||
|
token-url: 'http://xxt.zjnu.edu.cn/msg/getThirdAPIToken'
|
||||||
|
send-url: 'http://xxt.zjnu.edu.cn/message/sendMessageApi'
|
||||||
|
app-id: 'MSG45363193'
|
||||||
|
app-password: 'OGQxNzVmOTJkZjM5NzJhZDg1MDNiZGM0MDQ5ZTJlNDY'
|
||||||
|
wechat-channel: 'IntelligenceMode'
|
||||||
enabled: false
|
enabled: false
|
||||||
base-url: https://msg.zjnu.cug.edu.cn
|
base-url: https://msg.zjnu.cug.edu.cn
|
||||||
tp-name: nwszl.cug.edu.cn
|
tp-name: nwszl.cug.edu.cn
|
||||||
|
|||||||
@@ -24,10 +24,14 @@
|
|||||||
sdk:
|
sdk:
|
||||||
wxwork:
|
wxwork:
|
||||||
enable: true
|
enable: true
|
||||||
corpId: wwaca81b3016b9a58c
|
corpId: 'ww5386df99fd3b6b0f'
|
||||||
corpSecret: Auc7919Su1Uds5ZErfZybBrt-8FkP5NA-dldZyQuOrM
|
corpSecret: 'HpSptE9wKZRq_c_kemAPJc0AfXkNSevpfYCJd1TTgrI'
|
||||||
agentId: 1000131
|
agentId: '1000002'
|
||||||
redirectUri: /platform/wxwork/oauth2/callback
|
redirectUri: /platform/wxwork/oauth2/callback
|
||||||
|
weapp:
|
||||||
|
app-id: 'wxb83d5697d571cd4f'
|
||||||
|
app-secret: '40a4f000c12424f23864a1d7e19eb080'
|
||||||
|
env: 'jsz-zjxu-3gg38x7m6cdd4c0f'
|
||||||
wx:
|
wx:
|
||||||
enable: false
|
enable: false
|
||||||
appID: wx4f65ff5f1b72e99d
|
appID: wx4f65ff5f1b72e99d
|
||||||
@@ -35,10 +39,10 @@ sdk:
|
|||||||
redirectUri: /platform/wx/oauth2/callback
|
redirectUri: /platform/wx/oauth2/callback
|
||||||
cas:
|
cas:
|
||||||
enable: false
|
enable: false
|
||||||
server-url-prefix: https://i.zjnu.edu.cn/cas
|
server-url-prefix: 'https://authserver.zjnu.edu.cn/authserver'
|
||||||
server-login-url: https://i.zjnu.edu.cn/cas/login
|
server-login-url: 'https://authserver.zjnu.edu.cn/authserver/login'
|
||||||
qi-wx-server-login-url: httos:/cas.whcp.eou.cn/lyuapServer/wechatauz/wxAuthRedirect
|
qi-wx-server-login-url: 'https://authserver.zjnu.edu.cn/authserver/login'
|
||||||
client-host-url: https://zhgh.zjnu.edu.cn
|
client-host-url: 'https://lxy.zjnu.edu.cn'
|
||||||
client-call-back-url: /platform/sso/login
|
client-call-back-url: /platform/sso/login
|
||||||
validation-type: cas
|
validation-type: cas
|
||||||
minio:
|
minio:
|
||||||
@@ -65,6 +69,12 @@ custom:
|
|||||||
unit: http://mdm.zjnu.edu.cn/do/api/call/lxyxt_dwxx
|
unit: http://mdm.zjnu.edu.cn/do/api/call/lxyxt_dwxx
|
||||||
msg:
|
msg:
|
||||||
platform:
|
platform:
|
||||||
|
provider: 'zjnu'
|
||||||
|
token-url: 'http://xxt.zjnu.edu.cn/msg/getThirdAPIToken'
|
||||||
|
send-url: 'http://xxt.zjnu.edu.cn/message/sendMessageApi'
|
||||||
|
app-id: 'MSG45363193'
|
||||||
|
app-password: 'OGQxNzVmOTJkZjM5NzJhZDg1MDNiZGM0MDQ5ZTJlNDY'
|
||||||
|
wechat-channel: 'IntelligenceMode'
|
||||||
enabled: false
|
enabled: false
|
||||||
base-url: https://msg.cug.edu.cn
|
base-url: https://msg.cug.edu.cn
|
||||||
tp-name: nwszl.cug.edu.cn
|
tp-name: nwszl.cug.edu.cn
|
||||||
|
|||||||
@@ -54,6 +54,11 @@ wxwork:
|
|||||||
agentId: '${sdk.wxwork.agentId}'
|
agentId: '${sdk.wxwork.agentId}'
|
||||||
redirectUri: '${sdk.wxwork.redirectUri}'
|
redirectUri: '${sdk.wxwork.redirectUri}'
|
||||||
|
|
||||||
|
weapp:
|
||||||
|
app-id: '${sdk.weapp.app-id}'
|
||||||
|
app-secret: '${sdk.weapp.app-secret}'
|
||||||
|
env: '${sdk.weapp.env}'
|
||||||
|
|
||||||
wx:
|
wx:
|
||||||
enable: '${sdk.wx.enable}'
|
enable: '${sdk.wx.enable}'
|
||||||
appID: '${sdk.wx.appID}'
|
appID: '${sdk.wx.appID}'
|
||||||
@@ -191,6 +196,12 @@ data-center:
|
|||||||
unit: '${custom.data-center.urls.unit}'
|
unit: '${custom.data-center.urls.unit}'
|
||||||
msg:
|
msg:
|
||||||
platform:
|
platform:
|
||||||
|
provider: '${custom.msg.platform.provider}'
|
||||||
|
token-url: '${custom.msg.platform.token-url}'
|
||||||
|
send-url: '${custom.msg.platform.send-url}'
|
||||||
|
app-id: '${custom.msg.platform.app-id}'
|
||||||
|
app-password: '${custom.msg.platform.app-password}'
|
||||||
|
wechat-channel: '${custom.msg.platform.wechat-channel}'
|
||||||
enabled: '${custom.msg.platform.enabled}'
|
enabled: '${custom.msg.platform.enabled}'
|
||||||
# 地大通讯平台基础地址,默认对接正式地址
|
# 地大通讯平台基础地址,默认对接正式地址
|
||||||
base-url: '${custom.msg.platform.base-url}'
|
base-url: '${custom.msg.platform.base-url}'
|
||||||
|
|||||||
Reference in New Issue
Block a user