|
@@ -37,6 +37,7 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLDecoder;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.text.ParseException;
|
|
@@ -67,15 +68,19 @@ public class WeChatController {
|
|
|
* 调用的第一个接口,获取微信公众号CODE,获取openid
|
|
|
*/
|
|
|
@GetMapping("/getFirst1")
|
|
|
- public void getFirst1(){
|
|
|
+ public void getFirst1() throws UnsupportedEncodingException {
|
|
|
String http = "https://";
|
|
|
String code = request.getParameter("code");
|
|
|
String userAgent = request.getHeader("User-Agent");
|
|
|
|
|
|
+ System.out.println("userAgent: "+userAgent);
|
|
|
+
|
|
|
if(userAgent.contains("MicroMessenger")){ //wx
|
|
|
request.getSession().setAttribute("type","wx");
|
|
|
if(StringUtils.isBlank(code)){
|
|
|
- String url = URLDecoder.decode(http+constant.call_back_domain+"/mobile/#/pages"+request.getRequestURI());
|
|
|
+ String temp = http+constant.call_back_domain+"/prod-api/service-iot"+request.getRequestURI();
|
|
|
+// String temp = "https://manager.usky.cn/prod-api/service-iot/weChat/getFirst1";
|
|
|
+ String url = URLDecoder.decode(temp, "UTF-8");
|
|
|
String sendUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+constant.WE_CHAT_APP_ID+"&redirect_uri="+url+"&response_type=code&scope=snsapi_base&state=abc123#wechat_redirect";
|
|
|
try{
|
|
|
System.out.println("sendUrl: "+sendUrl);
|
|
@@ -107,6 +112,7 @@ public class WeChatController {
|
|
|
}
|
|
|
|
|
|
}else{ //app
|
|
|
+ System.out.println("code: app +++++ "+code);
|
|
|
request.getSession().setAttribute("type","app");
|
|
|
request.getSession().setAttribute("app_token","");
|
|
|
try{
|
|
@@ -122,7 +128,7 @@ public class WeChatController {
|
|
|
* 跳转首页权限
|
|
|
*/
|
|
|
@GetMapping("/getPageAuthorization")
|
|
|
- public void getPageAuthorization(){
|
|
|
+ public void getPageAuthorization() throws UnsupportedEncodingException {
|
|
|
String http = "https://";
|
|
|
String code = request.getParameter("code");
|
|
|
String userAgent = request.getHeader("User-Agent");
|
|
@@ -149,9 +155,30 @@ public class WeChatController {
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
- if((null == request.getSession().getAttribute("openid")) && (StringUtils.isBlank(request.getSession().getAttribute("openid").toString()))){
|
|
|
+ Object obj = request.getSession().getAttribute("openid");
|
|
|
+ if((obj != null) && (StringUtils.isNotBlank(obj.toString()))){
|
|
|
+ try{
|
|
|
+ LambdaQueryWrapper<MceMbuser> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(MceMbuser::getOpenid,request.getSession().getAttribute("openid"));
|
|
|
+ MceMbuser one = mceMbuserService.getOne(queryWrapper);
|
|
|
+ if(one != null){
|
|
|
+ request.getSession().setAttribute("phone",one.getPhone());
|
|
|
+ String sendUrl = "https://manager.usky.cn/mobile/#/pages/index?op="+one.getOpenid();
|
|
|
+ response.sendRedirect(sendUrl);
|
|
|
+ }else{
|
|
|
+ request.getSession().removeAttribute("openid");
|
|
|
+ request.getSession().removeAttribute("phone");
|
|
|
+
|
|
|
+ response.sendRedirect("https://manager.usky.cn/mobile/#/pages/login");
|
|
|
+ }
|
|
|
+ }catch (IOException e){
|
|
|
+ throw new BusinessException(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }else{
|
|
|
if(StringUtils.isBlank(code)){
|
|
|
- String url = URLDecoder.decode(http+constant.call_back_domain+request.getRequestURI());
|
|
|
+ String temp = http+constant.call_back_domain+"/prod-api/service-iot"+request.getRequestURI();
|
|
|
+ String url = URLDecoder.decode(temp, "UTF-8");
|
|
|
String sendUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+constant.WE_CHAT_APP_ID+"&redirect_uri="+url+"&response_type=code&scope=snsapi_base&state=abc123#wechat_redirect";
|
|
|
try{
|
|
|
System.out.println("sendUrl: "+sendUrl);
|
|
@@ -188,24 +215,6 @@ public class WeChatController {
|
|
|
throw new BusinessException(e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
- }else{
|
|
|
- try{
|
|
|
- LambdaQueryWrapper<MceMbuser> queryWrapper = Wrappers.lambdaQuery();
|
|
|
- queryWrapper.eq(MceMbuser::getOpenid,request.getSession().getAttribute("openid"));
|
|
|
- MceMbuser one = mceMbuserService.getOne(queryWrapper);
|
|
|
- if(one != null){
|
|
|
- request.getSession().setAttribute("phone",one.getPhone());
|
|
|
- String sendUrl = "https://manager.usky.cn/mobile/#/pages/index?op="+one.getOpenid();
|
|
|
- response.sendRedirect(sendUrl);
|
|
|
- }else{
|
|
|
- request.getSession().removeAttribute("openid");
|
|
|
- request.getSession().removeAttribute("phone");
|
|
|
-
|
|
|
- response.sendRedirect("https://manager.usky.cn/mobile/#/pages/login");
|
|
|
- }
|
|
|
- }catch (IOException e){
|
|
|
- throw new BusinessException(e.getMessage());
|
|
|
- }
|
|
|
|
|
|
}
|
|
|
}
|
|
@@ -269,40 +278,6 @@ public class WeChatController {
|
|
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * Description:[获取微信公众号的Access_Token]
|
|
|
- *
|
|
|
- * @return JSONResult
|
|
|
- * @date 2019-05-19
|
|
|
- * @author huazai
|
|
|
- */
|
|
|
- @GetMapping("/getWeChatAccessToken")
|
|
|
- public String getWeChatAccessToken() {
|
|
|
- try {
|
|
|
- // 微信公众号官方获取AccessToken
|
|
|
- RestTemplate restTemplate = new RestTemplate();
|
|
|
- String requestParams = String.format(constant.WE_CHAT_ACCESS_TOKEN_URL, constant.WE_CHAT_APP_ID, constant.WE_CHAT_SECRET);
|
|
|
- ResponseEntity<String> responseEntity = restTemplate.getForEntity(requestParams, String.class);
|
|
|
-
|
|
|
- String accessToken = JSONObject.parseObject(responseEntity.getBody()).getString("access_token");
|
|
|
- log.info("we_chat_access_token: " + accessToken);
|
|
|
-
|
|
|
- return accessToken;
|
|
|
- } catch (Exception e) {
|
|
|
- log.info("异常信息:{}", e);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- public static long getTimeDifference(String oldTime,String newTime) throws ParseException {
|
|
|
-
|
|
|
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- long NTime =df.parse(newTime).getTime();
|
|
|
- //从对象中拿到时间
|
|
|
- long OTime = df.parse(oldTime).getTime();
|
|
|
- long diff=(NTime-OTime)/1000/60;
|
|
|
- return diff;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 微信消息提醒
|
|
@@ -310,63 +285,8 @@ public class WeChatController {
|
|
|
*/
|
|
|
@PostMapping("sendWeChatMessage")
|
|
|
public JSONObject sendWeChatMessage(@RequestBody SendWeChatMessageRequestVO requestVO) {
|
|
|
+ return mceMbuserService.sendWeChatMessage(requestVO);
|
|
|
|
|
|
- String infoType = requestVO.getInfoType();
|
|
|
- String infoTitle = requestVO.getInfoTitle();
|
|
|
- String infoContent = requestVO.getInfoContent();
|
|
|
- Integer infoId = requestVO.getInfoId();
|
|
|
- String openId = requestVO.getOpenId();
|
|
|
-
|
|
|
- //access_token时效校验,判断获取access_token获取时间是否超过有效时间,超过就调用更新,保证一直有效
|
|
|
- if(!redisHelper.hasKey("access_key")){
|
|
|
- redisHelper.set("access_time",LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
- redisHelper.set("access_key",this.getWeChatAccessToken());
|
|
|
- }else{
|
|
|
- try{
|
|
|
- String access_time = redisHelper.get("access_time").toString();
|
|
|
- String now_time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
- long i = getTimeDifference(access_time,now_time);
|
|
|
- if(i > 115){ //大于115分钟
|
|
|
- redisHelper.set("access_time",LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
|
- redisHelper.set("access_key",this.getWeChatAccessToken());
|
|
|
- }
|
|
|
- }catch (Exception e){
|
|
|
- throw new BusinessException(e.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- String token = redisHelper.get("access_key").toString();
|
|
|
-
|
|
|
- TemplateMsgEntityVO messageVo=new TemplateMsgEntityVO();
|
|
|
- messageVo.setTTitle(infoTitle);
|
|
|
- messageVo.setTKeyword1("测试1");
|
|
|
- messageVo.setTKeyword2("测试2");
|
|
|
- messageVo.setTKeyword3("测试3");
|
|
|
- messageVo.setTRemark(infoContent);
|
|
|
- messageVo.setTUrl(String.format(constant.WE_CHAT_CUSTOMER_CALL_URL,infoId));
|
|
|
- messageVo.setTemplateId(constant.WE_CHAT_TEMPLATE_ID);
|
|
|
-
|
|
|
- String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
|
|
|
- Map<String,Object> content=new HashMap<>();
|
|
|
- Map<String, TemplateData> data = new HashMap<>();
|
|
|
- data.put("first",new TemplateData(messageVo.getTTitle(),"#44b549"));
|
|
|
- data.put("keyword1",new TemplateData(messageVo.getTKeyword1(),"#173177"));
|
|
|
- data.put("keyword2",new TemplateData(messageVo.getTKeyword2(),"#173177"));
|
|
|
- data.put("keyword3",new TemplateData(messageVo.getTKeyword3(),"#173177"));
|
|
|
- data.put("remark",new TemplateData(messageVo.getTRemark(),"#173177"));
|
|
|
-
|
|
|
- content.put("touser",openId);
|
|
|
- content.put("url",messageVo.getTUrl());
|
|
|
- content.put("template_id",messageVo.getTemplateId());
|
|
|
- content.put("data",data);
|
|
|
- String resp = HttpUtil.post(requestUrl, JSONUtil.parseObj(content).toString());
|
|
|
- System.out.println(content.toString());
|
|
|
- System.out.println(JSONUtil.parseObj(content));
|
|
|
- JSONObject result = JSONObject.parseObject(resp);
|
|
|
- System.out.println("发送消息:" + resp);
|
|
|
- return result;
|
|
|
}
|
|
|
|
|
|
}
|