package com.example.swaggerDemo.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.swaggerDemo.entity.ArticleEntity;
import com.example.swaggerDemo.entity.SysUserEntity;
import com.example.swaggerDemo.service.ArticleService;
import com.example.swaggerDemo.service.UserService;
import com.example.swaggerDemo.util.MyHttpUtils;
import com.example.swaggerDemo.wxpush.xstream;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.util.*;
@Api("微信图文推送")
@RestController
@RequestMapping("/articlewx")
public class ArticlewxController {
@Autowired
private ArticleService Service;
@Autowired
private UserService SysUserDao;
/**
* 获取token
* @param appId
* @param appSecret
* @return
*/
public static String getAccessToken(String appId, String appSecret) {
StringBuffer userInfoUrl =
new StringBuffer("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential")
.append("&appid=").append(appId)
.append("&secret=").append(appSecret);
String userInfoStr = MyHttpUtils.doPost(userInfoUrl.toString(), "", Charset.forName("UTF-8").toString());
Map<String, String> map = null;
return userInfoStr;
}
@ApiOperation(value="",notes="")
@ResponseBody
@RequestMapping(value="/findwx",method= RequestMethod.POST)
public void SendQYMessageimg(@RequestParam("id") String id)
{
String Url="http://****/view/addetails.html?id="+id;
String mp = getAccessToken("wxd4aff866a1642b28","3baceeec4066458afe6850bb689ec9c6");
JSONObject rq = JSON.parseObject(mp);
String accessToken= rq.get("access_token").toString();
String strurl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
List<SysUserEntity> dto = null;
try {
ArticleEntity art = Service.findid(id);
dto = SysUserDao.findall();
;
String Title=art.getTitle();
String Description=art.getPretext();
String PicUrl="http://*****"+ art.getPreimage();
for (SysUserEntity q: dto)
{
String responeJsonStr = "{";
responeJsonStr += "\"touser\": \"" + q.getOpenid() + "\",";
responeJsonStr += "\"msgtype\": \"news\",";
responeJsonStr += "\"agentid\": \"wxd4aff8****\",";
responeJsonStr += "\"news\": {";
responeJsonStr += "\"articles\": [{";
responeJsonStr += " \"title\": \"" + Title + "\",";
responeJsonStr += " \"description\": \" " + Description + "\",";
responeJsonStr += " \"url\": \"" + Url + "\",";
responeJsonStr += " \"picurl\": \"" + PicUrl + "\"";
responeJsonStr += "}]";
responeJsonStr += "},";
responeJsonStr += "\"safe\":\"0\"";
responeJsonStr += "}";
xstream.connectWeiXinInterface(strurl,responeJsonStr);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
-------------------------------------------------------------------------------------------
package com.example.swaggerDemo.wxpush;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class xstream {
public static void connectWeiXinInterface(String action,String json){
URL url;
try {
url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
OutputStream os = http.getOutputStream();
os.write(json.getBytes("UTF-8"));// 传入参数
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String result = new String(jsonBytes, "UTF-8");
System.out.println("请求返回结果:"+result);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}