|
|
@@ -0,0 +1,118 @@
|
|
|
+package com.chuanghai.ihotel.component;
|
|
|
+
|
|
|
+import com.chuanghai.ihotel.common.exception.BizCodeEnume;
|
|
|
+import com.chuanghai.ihotel.common.exception.RRException;
|
|
|
+import com.chuanghai.ihotel.config.WeixiaoConfig;
|
|
|
+import com.chuanghai.ihotel.constant.RedisKey;
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: codingliang
|
|
|
+ * @Description: 微校相关组件
|
|
|
+ * @Date: 2022-08-23 14:19
|
|
|
+ * @Version: V1.0
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class WeiXiaoComponent {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WeixiaoConfig weixiaoConfig;
|
|
|
+ @Autowired
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取token
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getApplicationToken() {
|
|
|
+ String token = stringRedisTemplate.opsForValue().get(RedisKey.WEI_XIAO_SERVICE_TOKEN);
|
|
|
+ if (StringUtils.hasText(token)) {
|
|
|
+ return token;
|
|
|
+ } else {
|
|
|
+ Map<String, Object> jsonMap = doGetApplicationToken();
|
|
|
+ String accessToken = jsonMap.get("access_token").toString();
|
|
|
+ String expiresIn = jsonMap.get("expires_in").toString();
|
|
|
+
|
|
|
+ stringRedisTemplate.opsForValue().set(RedisKey.WEI_XIAO_SERVICE_TOKEN, accessToken, Long.valueOf(expiresIn), TimeUnit.SECONDS);
|
|
|
+
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送通知
|
|
|
+ * @param cards 卡号集合
|
|
|
+ * @param digest 摘要
|
|
|
+ * @param title 通知标题
|
|
|
+ * @param content 通知内容
|
|
|
+ * @param customs 自定义参数,长度为2,分别为提示文案和通知跳转链接
|
|
|
+ */
|
|
|
+ public void sendNotice(List<String> cards, String digest, String title, String content, List<String> customs) {
|
|
|
+ String uri = "https://open.wecard.qq.com/cgi-bin/notice/send?access_token=" + getApplicationToken();
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+
|
|
|
+ try {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ params.put("cards", mapper.writeValueAsString(cards));
|
|
|
+ params.put("title", title);
|
|
|
+ params.put("content", content);
|
|
|
+ params.put("sender", "智慧公寓");
|
|
|
+ params.put("digest", digest);
|
|
|
+ if (customs != null && customs.size() == 2) {
|
|
|
+ params.put("customs", mapper.writeValueAsString(customs));
|
|
|
+ }
|
|
|
+
|
|
|
+ RestTemplate client = new RestTemplate();
|
|
|
+ ResponseEntity<String> tokenResponse = client.postForEntity(uri, params, String.class);
|
|
|
+ String body = tokenResponse.getBody();
|
|
|
+ log.info("微校发送通知返回结果【{}】", body);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("微校发送通知错误【{}】", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取token
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String, Object> doGetApplicationToken() {
|
|
|
+ String uri = "https://open.wecard.qq.com/cgi-bin/oauth2/token";
|
|
|
+
|
|
|
+ // 构建请求参数
|
|
|
+ Map<String, String> tokenParams = new HashMap<>();
|
|
|
+ tokenParams.put("app_key", weixiaoConfig.getAppKey());
|
|
|
+ tokenParams.put("app_secret", weixiaoConfig.getAppSecret());
|
|
|
+ tokenParams.put("grant_type", "client_credentials");
|
|
|
+ tokenParams.put("scope", "base");
|
|
|
+ tokenParams.put("ocode", weixiaoConfig.getOcode());
|
|
|
+
|
|
|
+ try {
|
|
|
+ RestTemplate client = new RestTemplate();
|
|
|
+ ResponseEntity<String> tokenResponse = client.postForEntity(uri, tokenParams, String.class);
|
|
|
+ String body = tokenResponse.getBody();
|
|
|
+ log.info("微校请求token返回结果【{}】", body);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ Map<String, Object> jsonMap = mapper.readValue(body, new TypeReference<Map<String, Object>>() {});
|
|
|
+
|
|
|
+
|
|
|
+ return jsonMap;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("微校请求token【{}】", e.getMessage());
|
|
|
+ throw new RRException(BizCodeEnume.THIRD_PARTY_SERVICE_CALL_FAILED, "微校请求token异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|