From e9a9dcebfb754d39270945261e43736c798f4c47 Mon Sep 17 00:00:00 2001
From: sgj <1442489573@qq.com>
Date: 星期三, 15 四月 2026 15:29:33 +0800
Subject: [PATCH] 清除微信相关测试代码

---
 fzzy-igdss-core/src/main/java/com/fzzy/igds/utils/WxUtil.java |  181 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 181 insertions(+), 0 deletions(-)

diff --git a/fzzy-igdss-core/src/main/java/com/fzzy/igds/utils/WxUtil.java b/fzzy-igdss-core/src/main/java/com/fzzy/igds/utils/WxUtil.java
new file mode 100644
index 0000000..2dbc4f5
--- /dev/null
+++ b/fzzy-igdss-core/src/main/java/com/fzzy/igds/utils/WxUtil.java
@@ -0,0 +1,181 @@
+package com.fzzy.igds.utils;
+
+import com.alibaba.fastjson.JSONObject;
+import com.fzzy.igds.data.ConfigData;
+import com.fzzy.igds.data.Template;
+import com.fzzy.igds.data.TemplateParam;
+import com.ruoyi.common.core.redis.RedisCache;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+import javax.annotation.Resource;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+
+@Slf4j
+@Component
+public class WxUtil {
+
+    /**
+     * 鍏紬鍙疯皟鐢ㄥ叏灞�鎺ュ彛鐨勬爣璇咥CCESS_TOKEN缂撳瓨key
+     */
+    public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
+
+    /**
+     * 妯℃澘娑堟伅鍙戦�乽rl
+     */
+    public static final String TEMPLATE_URL =
+            "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
+
+    /**
+     * 鑾峰彇access_token鐨勬帴鍙e湴鍧�(GET , 2000娆�/澶�)
+     */
+    public static final String ACCESS_TOKEN_URL =
+            "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Autowired
+    private ConfigData configData;
+
+    @Resource
+    private RedisCache redisCache;
+
+
+
+    /**
+     * 鍙戦�佹ā鏉挎秷鎭�
+     * @param openid    鐢ㄦ埛openid
+     * @param warnId    璀﹀憡id
+     * @param companyId 缁勭粐id
+     * @param title     璀﹀憡鏍囬
+     * @param keyword1  璀﹀憡瀵硅薄
+     * @param keyword2  璀﹀憡绫诲瀷
+     * @param keyword3  璀﹀憡鏃堕棿
+     * @param keyword4  璀﹀憡鍐呭
+     * @param remark    璀﹀憡璇︽儏
+     * @return
+     */
+    public boolean sendMessage(String openid, String warnId, String companyId, String title,
+                               String keyword1, String keyword2, String keyword3,
+                               String keyword4,String remark, String userName){
+
+        Template tem = new Template();
+        tem.setTemplate_id(configData.getWxTempId());
+        tem.setTopcolor("#FF0000");
+
+        tem.setTouser(openid);
+        tem.setUrl(configData.getWxServeUrl() + "/wx/view-gateway?state=warn-detail1&id=" + warnId + "&companyId=" + companyId + "&userName=" + userName);
+
+        Map<String, TemplateParam> params = new HashMap<>();
+        params.put("first",new TemplateParam(title,"#173177"));
+        //璀﹀憡瀵硅薄
+        params.put("keyword1",new TemplateParam( keyword1, "#173177"));
+        //璀﹀憡绫诲瀷
+        params.put("keyword2",new TemplateParam( keyword2, "#173177"));
+        //璀﹀憡鏃堕棿
+        params.put("keyword3",new TemplateParam( keyword3, "#173177"));
+        //璀﹀憡鍐呭
+        params.put("keyword4",new TemplateParam( keyword4, "#173177"));
+        params.put("remark",new TemplateParam( remark, "#173177"));
+
+        tem.setData(params);
+        log.debug("------寰俊閫氱煡寮�濮嬫帹閫�------");
+        //鎺ㄩ�佹秷鎭�
+        return sendTemplateMsg(tem);
+    }
+
+
+
+    /**
+     * 鎺ㄩ�佹ā鏉挎秷鎭�
+     *
+     * @param template
+     * @return
+     */
+    public boolean sendTemplateMsg(Template template) {
+        //鑾峰彇AccessToken
+        String accessToken = getAccessToken();
+        if(StringUtils.isEmpty(accessToken)){
+            log.debug("------鑾峰彇AccessToken澶辫触------");
+            return false;
+        }
+
+        boolean flag = false;
+        String url = WxUtil.TEMPLATE_URL.replace("ACCESS_TOKEN",accessToken);
+        String str = JSONObject.toJSONString(template);
+        JSONObject jsonObject = doPost(url, str);
+        if (jsonObject != null) {
+            int errorCode = jsonObject.getIntValue("errcode");
+            String errorMessage = jsonObject.getString("errmsg");
+            if (errorCode == 0) {
+                log.debug("鎺ㄩ�佸井淇℃秷鎭垚鍔燂細" + str);
+                flag = true;
+            } else {
+                log.info("妯℃澘娑堟伅鍙戦�佸け璐},{}", errorCode, errorMessage);
+                log.error("ACCESS_TOKEN:" + accessToken);
+                flag = false;
+            }
+        }
+
+        log.debug("鎺ㄩ�佸井淇℃秷鎭け璐ワ紒" );
+        return flag;
+    }
+
+    /**
+     * 鑾峰彇鍏ㄥ眬AccessToken
+     *
+     * @return
+     */
+    public String getAccessToken() {
+
+        //鍏堜粠缂撳瓨鑾峰彇锛岃嫢涓嶅瓨鍦ㄥ垯閫氳繃鎺ュ彛鑾峰彇
+        String accessToken = (String)redisCache.getCacheObject(WxUtil.ACCESS_TOKEN);
+        if(StringUtils.isNotEmpty(accessToken)){
+            return accessToken;
+        }
+
+        //璇锋眰鑾峰彇AccessToken鐨勫井淇℃帴鍙�
+        String resp = doGet(WxUtil.ACCESS_TOKEN_URL.replace("APPID",
+                configData.getWxAppId()).replace("APPSECRET", configData.getWxSecret()));
+        JSONObject json = JSONObject.parseObject(resp);
+        if (json != null) {
+            //鑾峰彇accessToken
+            accessToken = json.getString("access_token");
+            //鑾峰彇accessToken鏈夋晥鏃堕暱锛屼负7200s
+            int expiresIn = json.getIntValue("expires_in");
+            //灏哸ccessToken瀛樺叆缂撳瓨锛屾湁鏁堟椂闀夸负expiresIn锛屽嵆2灏忔椂
+            redisCache.setCacheObject(WxUtil.ACCESS_TOKEN, accessToken,expiresIn, TimeUnit.SECONDS);
+            return accessToken;
+        }
+        log.error("鑾峰彇access_token澶辫触");
+        return null;
+    }
+    /**
+     * post璇锋眰
+     * @param url
+     * @param data
+     * @return
+     */
+    public JSONObject doPost(String url, String data){
+        ResponseEntity<JSONObject> jsonObject = restTemplate.postForEntity(url, data, JSONObject.class);
+        return jsonObject.getBody();
+    }
+
+    /**
+     * get璇锋眰
+     *
+     * @param url
+     * @return
+     */
+    public String doGet(String url) {
+        return restTemplate.getForObject(url, String.class);
+    }
+}

--
Gitblit v1.9.3