微信公眾號發(fā)送模板消息

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();

? ? ? ? ? ? //要發(fā)送數(shù)據(jù),必須設(shè)置為可發(fā)送數(shù)據(jù)狀態(tài)

? ? ? ? ? ? connection.setDoOutput(true);

? ? ? ? ? ? //獲取輸入流

? ? ? ? ? ? OutputStream os = connection.getOutputStream();

? ? ? ? ? ? //寫出數(shù)據(jù)

? ? ? ? ? ? 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";

? ? //創(chuàng)建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");

? ? ? ? //創(chuàng)建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\":\"請和我聯(lián)系\",\n" +

"? ? ? ? ? ? ? ? ? ? ? \"color\":\"#173177\"\n" +

"? ? ? ? ? ? ? ? ? }\n" +

"? ? ? ? ? }\n" +

"}";

? ? ? ? String result = WeChatUtil.post(url,data);

? ? ? ? System.out.println(result);

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容