pojo类
package com.xiguaLab.ssm.pojo;/*
*
* Created by lee on 2018/11/19 21:46
*/
public class AccessToken {
private StringaccessToken;
private long expireTime;
public StringgetAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public AccessToken(String accessToken, String expireIn) {
this.accessToken = accessToken;
expireTime =System.currentTimeMillis()+Integer.parseInt(expireIn)*1000;
}
public boolean isExpired(){
return System.currentTimeMillis()>expireTime;
}
}
Controller
package com.xiguaLab.ssm.controllers;/*
*
* Created by lee on 2018/11/19 21:13
*/
import org.springframework.stereotype.Controller;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
@Controller
public class WeChatUtil {
/* private static final String GET_TOKEN_URL ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
private static final String APPID = "wxdb924e25df03d333";
private static final String APPSECRET = "dd2839e44e3976c3f777752a51d6430d";*/
public static Stringget(String url){
try {
URL urlObj =new URL(url);
URLConnection connection = urlObj.openConnection();
InputStream is = connection.getInputStream();
byte[] b =new byte[1024];
int len;
StringBuilder sb =new StringBuilder();
while((len = is.read(b))!=-1){
sb.append(new String(b,0,len));
}
return sb.toString();
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Stringpost(String url,String data){
try {
URL urlObj =new URL(url);
URLConnection connection = urlObj.openConnection();
//要发送数据,必须设置为可发送数据状态
connection.setDoOutput(true);
//获取输入流
OutputStream os = connection.getOutputStream();
//写出数据
os.write(data.getBytes());
InputStream is = connection.getInputStream();
byte[] b=new byte[1024];
int len;
StringBuilder sb =new StringBuilder();
while((len=is.read(b))!=-1){
sb.append(new String(b,0,len));
}
return sb.toString();
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Service
package com.xiguaLab.ssm.service.ServiceImpl;/*
*
* Created by lee on 2018/11/19 21:24
*/
import com.xiguaLab.ssm.controllers.WeChatUtil;
import com.xiguaLab.ssm.pojo.AccessToken;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
@Service
@PropertySource(value={"classpath:resources/wechat.proprerties"})
public class WeChatServiceImpl {
private static final StringGET_TOKEN_URL ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
/*@Value("$APPID")
private static final String APPID = "wxdb924e25df03d333";
@Value("APPSECRET")
private static final String APPSECRET = "dd2839e44e3976c3f777752a51d6430d";*/
private static final StringAPPID ="wxdb924e25df03d333";
private static final StringAPPSECRET ="dd2839e44e3976c3f777752a51d6430d";
//创建token对象并存起来
private static AccessTokenat;
private static void getToken(){
String url =GET_TOKEN_URL.replace("APPID",APPID).replace("APPSECRET",APPSECRET);
String tokenStr =WeChatUtil.get(url);
JSONObject jsonObject = JSONObject.fromObject(tokenStr);
String token = jsonObject.getString("access_token");
String expireIn = jsonObject.getString("expires_in");
//创建token对象
at =new AccessToken(token,expireIn);
}
/*
向外暴露的获取token的方法
*/
public static StringgetAccessToken(){
if(at==null||at.isExpired()){
getToken();
}
return at.getAccessToken();
}
}
主方法
package com.xiguaLab.ssm.controllers;
import com.xiguaLab.ssm.service.ServiceImpl.WeChatServiceImpl;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.rmi.CORBA.Util;
@Controller
public class TestController {
@RequestMapping("/home")
public StringshowPageTest(){return "/login_page";}
@Test
public void testToken(){
System.out.println(WeChatServiceImpl.getAccessToken());
}
@Test
public void sendTemplateMessage(){
String at = WeChatServiceImpl.getAccessToken();
String url ="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+at;
String data ="{\n" +
" \"touser\":\"oPCxu1QhD-0Zoeo5b9BbgAoPLdY4\",\n" +
" \"template_id\":\"iHBqmG372SjljeeoXQPBqh9WfFD_CGwe6mmsfVB3Jr4\", \n" +
" \"data\":{\n" +
" \"first\": {\n" +
" \"value\":\"您有新的反馈信息啦\",\n" +
" \"color\":\"#173177\"\n" +
" },\n" +
" \"company\":{\n" +
" \"value\":\"巧克力\",\n" +
" \"color\":\"#173177\"\n" +
" },\n" +
" \"time\": {\n" +
" \"value\":\"2018年12月11日12:00\",\n" +
" \"color\":\"#173177\"\n" +
" },\n" +
" \"result\": {\n" +
" \"value\":\"面试通过\",\n" +
" \"color\":\"#173177\"\n" +
" },\n" +
" \"remark\":{\n" +
" \"value\":\"请和我联系\",\n" +
" \"color\":\"#173177\"\n" +
" }\n" +
" }\n" +
"}";
String result = WeChatUtil.post(url,data);
System.out.println(result);
}
}
网友评论