|
@@ -39,6 +39,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.http.HttpStatus;
|
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
|
+import org.jetbrains.annotations.Nullable;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
@@ -518,77 +519,20 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
|
|
|
@Override
|
|
|
public void sendWechatMessage(String openId, TEquipment equipment, TOrder order, String companyType) {
|
|
|
- String url = "";
|
|
|
String accessToken = "";
|
|
|
try {
|
|
|
if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
|
|
|
accessToken = redisTemplate.opsForValue().get(SZ_ACCESS_TOKEN_KEY);
|
|
|
+ log.info("申泽的accessToken:{}", accessToken);
|
|
|
} else {
|
|
|
accessToken = redisTemplate.opsForValue().get(SC_ACCESS_TOKEN_KEY);
|
|
|
+ log.info("七云的accessToken:{}", accessToken);
|
|
|
}
|
|
|
} 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="
|
|
|
- + HuifuConstant.SZ_WX_SUB_APP_ID + "&secret=" + HuifuConstant.SZ_WX_APP_SECRET;
|
|
|
- } else {
|
|
|
- url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
|
|
|
- + HuifuConstant.SC_WX_SUB_APP_ID + "&secret=" + HuifuConstant.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);
|
|
|
}
|
|
|
// 推送消息
|
|
|
// 模板参数
|
|
@@ -624,6 +568,23 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
} else {
|
|
|
sendBody.put("template_id", HuifuConstant.SC_TEMPLATE_ID);
|
|
|
}
|
|
|
+ String messageCode = sendMessage(accessToken, restTemplate, sendBody);
|
|
|
+ if (StringUtils.isNotEmpty(messageCode) && messageCode.equals("40001")) {
|
|
|
+ // 获取到新的accessToken
|
|
|
+ accessToken = getAccessToken(companyType);
|
|
|
+ messageCode = sendMessage(accessToken, restTemplate, sendBody);
|
|
|
+ log.info("微信推送结果:{}","messageCode : " + messageCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送微信消息
|
|
|
+ * @param accessToken
|
|
|
+ * @param restTemplate
|
|
|
+ * @param sendBody
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String sendMessage(String accessToken, RestTemplate restTemplate, Map<String, Object> sendBody) {
|
|
|
String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
|
|
|
ResponseEntity<String> forEntity = restTemplate.postForEntity(sendUrl, sendBody, String.class);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(forEntity.getBody());
|
|
@@ -631,6 +592,77 @@ public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> impleme
|
|
|
String msgId = jsonObject.getString("msgid");
|
|
|
System.out.println("messageCode : " + messageCode + ", msgId: " +msgId);
|
|
|
log.info("微信推送结果:{}","messageCode : " + messageCode + ", msgId: " +msgId);
|
|
|
+ return messageCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取accessToken
|
|
|
+ * @param companyType
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getAccessToken(String companyType) {
|
|
|
+ String accessToken = "";
|
|
|
+ String url = "";
|
|
|
+ if(StringUtils.isEmpty(companyType) || companyType.equals("0")) {
|
|
|
+ url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
|
|
|
+ + HuifuConstant.SZ_WX_SUB_APP_ID + "&secret=" + HuifuConstant.SZ_WX_APP_SECRET;
|
|
|
+ } else {
|
|
|
+ url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
|
|
|
+ + HuifuConstant.SC_WX_SUB_APP_ID + "&secret=" + HuifuConstant.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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
}
|
|
|
|
|
|
public void processRefund(Map<String, Object> bodyMap) {
|