|
@@ -3,6 +3,7 @@ package com.szwl.service.impl;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.szwl.constant.ConfigConsts;
|
|
|
import com.szwl.model.bean.WeChatTemplateMsg;
|
|
|
+import com.szwl.model.entity.TAdmin;
|
|
|
import com.szwl.model.entity.TWechat;
|
|
|
import com.szwl.mapper.TWechatMapper;
|
|
|
import com.szwl.service.TAdminService;
|
|
@@ -16,6 +17,7 @@ import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
@@ -43,7 +45,7 @@ public class TWechatServiceImpl extends ServiceImpl<TWechatMapper, TWechat> impl
|
|
|
@Autowired
|
|
|
TAdminService adminService;
|
|
|
|
|
|
- @Autowired
|
|
|
+ @Resource
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
private static final String SZ_ACCESS_TOKEN_KEY = "sz_wechat_access_token";
|
|
@@ -102,6 +104,82 @@ public class TWechatServiceImpl extends ServiceImpl<TWechatMapper, TWechat> impl
|
|
|
sendWechatMessage(openId, sendMag, companyType);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getAccessToken(String companyType) {
|
|
|
+ String url= "";
|
|
|
+ String accessToken = "";
|
|
|
+ if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
|
|
|
+ url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
|
|
|
+ + ConfigConsts.SZ_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SZ_WX_APP_SECRET;
|
|
|
+ TAdmin admin = adminService.getById(2738L);
|
|
|
+ admin.setApiKey(accessToken);
|
|
|
+ adminService.updateById(admin);
|
|
|
+ } else {
|
|
|
+ url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
|
|
|
+ + ConfigConsts.SC_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SC_WX_APP_SECRET;
|
|
|
+ TAdmin admin = adminService.getById(2739L);
|
|
|
+ admin.setApiKey(accessToken);
|
|
|
+ adminService.updateById(admin);
|
|
|
+ }
|
|
|
+ PrintWriter out = null;
|
|
|
+ BufferedReader in = null;
|
|
|
+ String line;
|
|
|
+ StringBuffer stringBuffer = new StringBuffer();
|
|
|
+ try {
|
|
|
+ URL realUrl = new URL(url);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ URLConnection conn = realUrl.openConnection();
|
|
|
+
|
|
|
+ // 设置通用的请求属性 设置请求格式
|
|
|
+ //设置返回类型
|
|
|
+ conn.setRequestProperty("contentType", "text/plain");
|
|
|
+ //设置请求类型
|
|
|
+ conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
|
|
|
+ //设置超时时间
|
|
|
+ conn.setConnectTimeout(1000);
|
|
|
+ conn.setReadTimeout(1000);
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.connect();
|
|
|
+ // 获取URLConnection对象对应的输出流
|
|
|
+ out = new PrintWriter(conn.getOutputStream());
|
|
|
+ // flush输出流的缓冲
|
|
|
+ out.flush();
|
|
|
+ // 定义BufferedReader输入流来读取URL的响应 设置接收格式
|
|
|
+ in = new BufferedReader(
|
|
|
+ new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ stringBuffer.append(line);
|
|
|
+ }
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(stringBuffer.toString());
|
|
|
+ log.info("获取token:{}",jsonObject.toString());
|
|
|
+ accessToken = jsonObject.getString("access_token");
|
|
|
+ if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
|
|
|
+ TAdmin admin = adminService.getById(2738L);
|
|
|
+ admin.setApiKey(accessToken);
|
|
|
+ adminService.updateById(admin);
|
|
|
+ } else {
|
|
|
+ TAdmin admin = adminService.getById(2739L);
|
|
|
+ admin.setApiKey(accessToken);
|
|
|
+ adminService.updateById(admin);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ //使用finally块来关闭输出流、输入流
|
|
|
+ try {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (IOException ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取accessToken推送消息
|
|
|
* @param openId
|
|
@@ -114,73 +192,17 @@ public class TWechatServiceImpl extends ServiceImpl<TWechatMapper, TWechat> impl
|
|
|
String accessToken = "";
|
|
|
try {
|
|
|
if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
|
|
|
- accessToken = redisTemplate.opsForValue().get(SZ_ACCESS_TOKEN_KEY);
|
|
|
+ TAdmin admin = adminService.getById(2738L);
|
|
|
+ accessToken = admin.getApiKey();
|
|
|
} else {
|
|
|
- accessToken = redisTemplate.opsForValue().get(SC_ACCESS_TOKEN_KEY);
|
|
|
+ TAdmin admin = adminService.getById(2739L);
|
|
|
+ accessToken = admin.getApiKey();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
if(StringUtils.isEmpty(accessToken)) {
|
|
|
- if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
|
|
|
- url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
|
|
|
- + ConfigConsts.SZ_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SZ_WX_APP_SECRET;
|
|
|
- } else {
|
|
|
- url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
|
|
|
- + ConfigConsts.SC_WX_SUB_APP_ID + "&secret=" + ConfigConsts.SC_WX_APP_SECRET;
|
|
|
- }
|
|
|
- PrintWriter out = null;
|
|
|
- BufferedReader in = null;
|
|
|
- String line;
|
|
|
- StringBuffer stringBuffer = new StringBuffer();
|
|
|
- try {
|
|
|
- URL realUrl = new URL(url);
|
|
|
- // 打开和URL之间的连接
|
|
|
- URLConnection conn = realUrl.openConnection();
|
|
|
-
|
|
|
- // 设置通用的请求属性 设置请求格式
|
|
|
- //设置返回类型
|
|
|
- conn.setRequestProperty("contentType", "text/plain");
|
|
|
- //设置请求类型
|
|
|
- conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
|
|
|
- //设置超时时间
|
|
|
- conn.setConnectTimeout(1000);
|
|
|
- conn.setReadTimeout(1000);
|
|
|
- conn.setDoOutput(true);
|
|
|
- conn.connect();
|
|
|
- // 获取URLConnection对象对应的输出流
|
|
|
- out = new PrintWriter(conn.getOutputStream());
|
|
|
- // flush输出流的缓冲
|
|
|
- out.flush();
|
|
|
- // 定义BufferedReader输入流来读取URL的响应 设置接收格式
|
|
|
- in = new BufferedReader(
|
|
|
- new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
- while ((line = in.readLine()) != null) {
|
|
|
- stringBuffer.append(line);
|
|
|
- }
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(stringBuffer.toString());
|
|
|
- log.info("获取token:{}",jsonObject.toString());
|
|
|
- accessToken = jsonObject.getString("access_token");
|
|
|
- if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
|
|
|
- redisTemplate.opsForValue().set(SZ_ACCESS_TOKEN_KEY, accessToken, EXPIRATION_TIME, TimeUnit.SECONDS);
|
|
|
- } else {
|
|
|
- redisTemplate.opsForValue().set(SZ_ACCESS_TOKEN_KEY, accessToken, EXPIRATION_TIME, TimeUnit.SECONDS);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- //使用finally块来关闭输出流、输入流
|
|
|
- try {
|
|
|
- if (out != null) {
|
|
|
- out.close();
|
|
|
- }
|
|
|
- if (in != null) {
|
|
|
- in.close();
|
|
|
- }
|
|
|
- } catch (IOException ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
+ accessToken = getAccessToken(companyType);
|
|
|
}
|
|
|
// 推送消息
|
|
|
RestTemplate restTemplate = new RestTemplate();
|