New file |
| | |
| | | package com.panzhihua.service_community.util; |
| | | |
| | | import cn.hutool.http.HttpUtil; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.panzhihua.common.model.vos.SanShuoMessageVO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | public class WXMessageUtil { |
| | | private static final String APP_ID="wx0cef797390444b75"; |
| | | private static final String SEND_MESSAGE_URL="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; |
| | | private static final String TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; |
| | | private static final String SECRET="c7ea9aaa7e391a487e8a5b9ba61045d1"; |
| | | private static final String TEMPLATE_ID="iy8r0YbGlvmysbjGcbEj0yx-kftKBjC-HrXlSeNBrE0"; |
| | | public static String queryToken(){ |
| | | String tokenUrl=TOKEN_URL+"&appid="+APP_ID+"&secret="+SECRET; |
| | | String result= HttpUtil.get(tokenUrl); |
| | | cn.hutool.json.JSONObject obj = JSONUtil.parseObj(result); |
| | | String token = obj.get("access_token").toString(); |
| | | return token; |
| | | } |
| | | |
| | | public static String sendStatusMessage(SanShuoMessageVO vo){ |
| | | String token = queryToken(); |
| | | if (StringUtils.isNotEmpty(token)){ |
| | | //发送消息 |
| | | String url=SEND_MESSAGE_URL+token; |
| | | Map<String,Object> param=new HashMap<>(); |
| | | param.put("touser", vo.getTouser()); |
| | | param.put("template_id", TEMPLATE_ID); |
| | | Map<String,Object> data=new HashMap<>(); |
| | | data.put("thing1", formData(vo.getThing1())); |
| | | data.put("thing5", formData(vo.getThing5())); |
| | | data.put("thing2", formData(vo.getThing2())); |
| | | data.put("time3", formData(vo.getTime3())); |
| | | param.put("data", data); |
| | | String paramMap = JSONUtil.toJsonStr(param); |
| | | log.info("===============推送参数:"+paramMap); |
| | | String result = HttpUtil.post(url, paramMap); |
| | | log.info("状态推送结果"+result); |
| | | return result; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static Map<String,Object> formData(String param){ |
| | | Map<String,Object> data=new HashMap<>(); |
| | | data.put("value",param ); |
| | | return data; |
| | | } |
| | | |
| | | } |