给登录接口增加发送订阅消息的功能
This commit is contained in:
@@ -7,15 +7,19 @@ import com.jsh.erp.datasource.entities.PlatformConfigExample;
|
||||
import com.jsh.erp.datasource.mappers.PlatformConfigMapper;
|
||||
import com.jsh.erp.datasource.mappers.PlatformConfigMapperEx;
|
||||
import com.jsh.erp.exception.JshException;
|
||||
import com.jsh.erp.utils.HttpClient;
|
||||
import com.jsh.erp.utils.PageUtils;
|
||||
import com.jsh.erp.utils.StringUtil;
|
||||
import com.jsh.erp.utils.Tools;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -25,6 +29,9 @@ public class PlatformConfigService {
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
@Resource
|
||||
private PlatformConfigMapper platformConfigMapper;
|
||||
|
||||
@@ -182,4 +189,76 @@ public class PlatformConfigService {
|
||||
}
|
||||
return platformConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信token信息
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public String getAccessToken() throws Exception {
|
||||
String accessToken = "";
|
||||
if(redisService.getCacheObject("weixinToken")==null) {
|
||||
//1-获取token
|
||||
String weixinToken = getPlatformConfigByKey("weixinUrl").getPlatformValue() + BusinessConstants.WEIXIN_TOKEN;
|
||||
String weixinAppid = getPlatformConfigByKey("weixinAppid").getPlatformValue();
|
||||
String weixinSecret = getPlatformConfigByKey("weixinSecret").getPlatformValue();
|
||||
String url = weixinToken + "?grant_type=client_credential&appid=" + weixinAppid + "&secret=" + weixinSecret;
|
||||
JSONObject jsonObject = HttpClient.httpGet(url);
|
||||
logger.info("获取到微信token信息:{}", jsonObject);
|
||||
if (jsonObject != null) {
|
||||
accessToken = jsonObject.getString("access_token");
|
||||
Long expiresIn = jsonObject.getLong("expires_in") - 10;
|
||||
if (StringUtil.isNotEmpty(accessToken)) {
|
||||
//存redis
|
||||
redisService.storageKeyWithTime("weixinToken", accessToken, expiresIn);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
accessToken = redisService.getCacheObject("weixinToken");
|
||||
}
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送微信订阅消息(该方法将在一个单独的线程中执行)
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@Async
|
||||
public void sendSubscribeMessage(String accessToken, String weixinUrl, String platformName, String templateId, String page, String openId) throws Exception {
|
||||
String weixinMessageSend = weixinUrl + BusinessConstants.WEIXIN_MESSAGE_SEND;
|
||||
String url = weixinMessageSend + "?access_token=" + accessToken;
|
||||
JSONObject paramObj = new JSONObject();
|
||||
paramObj.put("template_id", templateId);
|
||||
if (StringUtil.isNotEmpty(page)) {
|
||||
paramObj.put("page", page);
|
||||
}
|
||||
paramObj.put("touser", openId);
|
||||
JSONObject dataObj = new JSONObject();
|
||||
//登录状态
|
||||
JSONObject phraseObj = new JSONObject();
|
||||
//登陆方式
|
||||
JSONObject thing2Obj = new JSONObject();
|
||||
//登陆应用
|
||||
JSONObject thing3Obj = new JSONObject();
|
||||
//登录时间
|
||||
JSONObject time4Obj = new JSONObject();
|
||||
//备注
|
||||
JSONObject thing5Obj = new JSONObject();
|
||||
phraseObj.put("value", "已登录");
|
||||
thing2Obj.put("value", "账号登录");
|
||||
thing3Obj.put("value", platformName);
|
||||
time4Obj.put("value", Tools.getCenternTime(new Date()));
|
||||
thing5Obj.put("value", "欢迎" + BusinessConstants.DEFAULT_MANAGER + "登录!");
|
||||
dataObj.put("phrase1", phraseObj);
|
||||
dataObj.put("thing2", thing2Obj);
|
||||
dataObj.put("thing3", thing3Obj);
|
||||
dataObj.put("time4", time4Obj);
|
||||
dataObj.put("thing5", thing5Obj);
|
||||
paramObj.put("data", dataObj);
|
||||
paramObj.put("miniprogram_state", "formal");
|
||||
paramObj.put("lang", "zh_CN");
|
||||
String param = paramObj.toJSONString();
|
||||
HttpClient.httpPost(url, param);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -376,6 +376,18 @@ public class UserService {
|
||||
pwdSimple = true;
|
||||
}
|
||||
user.setPassword(null);
|
||||
if(BusinessConstants.DEFAULT_MANAGER.equals(user.getLoginName())) {
|
||||
//如果是管理员,则发送订阅消息
|
||||
//1-获取token
|
||||
String accessToken = platformConfigService.getAccessToken();
|
||||
//2-发送订阅消息
|
||||
String templateId = platformConfigService.getPlatformConfigByKey("login_temp_id").getPlatformValue();
|
||||
String weixinUrl = platformConfigService.getPlatformConfigByKey("weixinUrl").getPlatformValue();
|
||||
String platformName = platformConfigService.getPlatformConfigByKey("platform_name").getPlatformValue();
|
||||
if(StringUtil.isNotEmpty(accessToken) && StringUtil.isNotEmpty(user.getWeixinOpenId()) && StringUtil.isNotEmpty(templateId)) {
|
||||
platformConfigService.sendSubscribeMessage(accessToken, weixinUrl, platformName, templateId, null, user.getWeixinOpenId());
|
||||
}
|
||||
}
|
||||
redisService.storageObjectBySession(token,"clientIp", Tools.getLocalIp(request));
|
||||
logService.insertLogWithUserId(user.getId(), user.getTenantId(), "用户",
|
||||
new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_LOGIN).append(user.getLoginName()).toString(),
|
||||
@@ -913,10 +925,10 @@ public class UserService {
|
||||
}
|
||||
|
||||
public User getUserByWeixinCode(String weixinCode) throws Exception {
|
||||
String weixinUrl = platformConfigService.getPlatformConfigByKey("weixinUrl").getPlatformValue();
|
||||
String weixinLogin = platformConfigService.getPlatformConfigByKey("weixinUrl").getPlatformValue() + BusinessConstants.WEIXIN_LOGIN;
|
||||
String weixinAppid = platformConfigService.getPlatformConfigByKey("weixinAppid").getPlatformValue();
|
||||
String weixinSecret = platformConfigService.getPlatformConfigByKey("weixinSecret").getPlatformValue();
|
||||
String url = weixinUrl + "?appid=" + weixinAppid + "&secret=" + weixinSecret + "&js_code=" + weixinCode
|
||||
String url = weixinLogin + "?appid=" + weixinAppid + "&secret=" + weixinSecret + "&js_code=" + weixinCode
|
||||
+ "&grant_type=authorization_code";
|
||||
JSONObject jsonObject = HttpClient.httpGet(url);
|
||||
if(jsonObject!=null) {
|
||||
|
||||
Reference in New Issue
Block a user