微信接口测试代码

微信接口测试代码

微信消息接口

package web.platform.mod.test;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
//import com.mall.common.utils.HttpSend;
//import com.mall.common.utils.HttpUtil;

import io.swagger.annotations.ApiOperation;
import web.platform.util.HTTPUtil;
import web.platform.util.HttpsRequest;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestParam;

public class TemplateMessageService 
{

	/**
	 * 获取token
	 * @param appid
	 * @param appSecret
	 * @return
	 */
	public static String getToken(String appid, String appSecret) {
		String url = "https://api.weixin.qq.com/cgi-bin/token";
        String param = "grant_type=client_credential" + "&appid=" + appid + "&secret=" + appSecret;
       
        String accTemp="";
		try {
			HttpsRequest HttpsRequests=new HttpsRequest();
			accTemp =HttpsRequests.httpsGet(url+"?"+param);
			  JSONObject jsonObject = JSONObject.parseObject(accTemp);
		        System.out.println("1--->" + jsonObject);
		      // accessToken = jsonObject.getString("access_token"); // 获取到了access_token,调用接口都要用到的,有时效
		       return jsonObject.getString("access_token");
		} catch (Exception e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
			return "";
		}
	}
	 
	/**
	 * 发送模板消息
	 * @param appid
	 * @param accessToken
	 * @param appSecret
	 * @param realAmount
	 * @param shopName
	 * @param openId
	 * @param msgID
	 */
    public static void sendTemp(String appid, String accessToken, String appSecret, Double realAmount, String shopName, String openId,String msgID) 
    {        
    	HTTPUtil HTTPUtils=new HTTPUtil();
    	 
    	accessToken=getToken( appid,  appSecret);
    	if(accessToken=="")
    	{
    	  return ;    	  
    	}
   	
    	// 封装要发送的json
        Map<String, Object> map = new HashMap();
        map.put("touser", openId);//你要发送给某个用户的openid 前提是已关注该公众号,该openid是对应该公众号的,不是普通的openid
       
        map.put("template_id",msgID );//模板消息id
        map.put("url","https://www.baidu.com");//用户点击模板消息,要跳转的地址
        // 封装miniprogram 跳转小程序用,不跳不要填
        Map<String, String> mapA = new HashMap<>();
        mapA.put("appid", ""); //小程序appid
        mapA.put("pagepath", ""); //小程序页面pagepath
        map.put("miniprogram", mapA);
 
        // 以下就是根据模板消息的格式封装好,我模板的是:问题反馈结果通知  可以和我一样试试
        // 封装first
        Map firstMap = new HashMap();
        firstMap.put("value", "新的消费通知!"); //内容
        firstMap.put("color", "#173177"); //字体颜色
 
        // 封装keyword1 提交的问题
        Map keyword1Map = new HashMap();
        keyword1Map.put("value", shopName);
        keyword1Map.put("color", "#fff");
 
        // 封装keyword2此处也可以是商品名
        Map keyword2Map = new HashMap();
        keyword2Map.put("value", "-");
        keyword2Map.put("color", "#fff");
 
        // 封装keyword2此处可以是商品价格
        Map keyword3Map = new HashMap();
        keyword3Map.put("value", realAmount + "元");
        keyword3Map.put("color", "#fff");
 
        // 封装keyword4
        // 封装remark
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map keyword4Map = new HashMap();
        keyword4Map.put("value", simpleDateFormat.format(date));
        keyword4Map.put("color", "#fff");
 
 
        Map remarkMap = new HashMap();
        remarkMap.put("value", "尊敬的用户,您于" + simpleDateFormat.format(date) + "在" + shopName + "消费了" + realAmount + "元");
        remarkMap.put("color", "#fff");
 
        // 封装data
        Map dataMap = new HashMap();
        dataMap.put("first", firstMap);
        dataMap.put("keyword1", keyword1Map);
        dataMap.put("keyword2", keyword2Map);
        dataMap.put("keyword3", keyword3Map);
        dataMap.put("keyword4", keyword4Map);
        dataMap.put("remark", remarkMap);
 
        map.put("data", dataMap);
        
        String r;
		try {
			r = HTTPUtils.doPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken,JSON.toJSONString(map),"UTF-8");
			System.out.println("-->" + r);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
       // String r = HttpUtil.getJsonData(JSONObject.parseObject(JSON.toJSONString(map)), "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken); //发送模板消息
    }
    
	/**
	 * 添加客服帐号
	 * @param appid
	 * @param appSecret
	 * @param map
	 */
	public static void addkefu(String appid, String appSecret, Map<String, Object> map) 
	{
		HTTPUtil HTTPUtils=new HTTPUtil();
	    String accessToken=getToken( appid,  appSecret);
    	if(accessToken=="")
    	{
    	  return ;    	  
    	}
    	
    	String r;
 		try {
 			r = HTTPUtils.doPost("https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" + accessToken,JSON.toJSONString(map),"UTF-8");
 			System.out.println("-->" + r);
 		} catch (Exception e) {
 			// TODO 自动生成的 catch 块
 			e.printStackTrace();
 		}
	}
	
	/**
	 * 查询客服帐号
	 * @param accessToken
	 * @return 
	 */
	public static String searchkefu(String accessToken) 
	{
    	String r="";
 		try {
 			HttpsRequest HttpsRequests=new HttpsRequest();
			
 			r = HttpsRequests.httpsGet("https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" + accessToken);
 			System.out.println("-->" + r); 			
 		} catch (Exception e) {
 			// TODO 自动生成的 catch 块
 			e.printStackTrace();
 		}
		return r;
	}
	
	/**
	 * 查询客服帐号
	 * @param accessToken
	 * @param appid
	 * @param appSecret
	 * @return
	 */
	public static String searchkefu(String appid, String appSecret) 
	{
		HTTPUtil HTTPUtils=new HTTPUtil();
	    String accessToken=getToken( appid,  appSecret);
    	if(accessToken=="")
    	{
    	  return "获取token失败!";    	  
    	}
		String r="";
 		try {
 			HttpsRequest HttpsRequests=new HttpsRequest();
			
 			r = HttpsRequests.httpsGet("https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" + accessToken);
 			System.out.println("-->" + r); 			
 		} catch (Exception e) {
 			// TODO 自动生成的 catch 块
 			e.printStackTrace();
 		}
		return r;		
	}
	
	/**
	 * 发送客服消息
	 * @param appid
	 * @param appSecret
	 * @param openId
	 */
	public static void sendKfMessege(String appid, String appSecret,String openId) {
		HTTPUtil HTTPUtils=new HTTPUtil();
	    String accessToken=getToken( appid,  appSecret);
    	if(accessToken=="")
    	{
    	  return ;    	  
    	}
    	String searchResult=searchkefu(accessToken);
    	JSONObject ResultObject= JSONObject.parseObject(searchResult);
    	
    	JSONArray ja = ResultObject.getJSONArray("kf_list");
//         for (int i = 0; i < ja.size(); i++) {
//             JSONObject jo = ja.getJSONObject(i);
//             String building_id = jo.getString("kf_account");
//             String kf_nick = jo.getString("kf_nick");
//             String kf_id = jo.getString("kf_id");
//             String kf_headimgurl = jo.getString("kf_headimgurl");
//             
//             
//             //System.out.println(building_id + "building_id>>>>>");
//         }
    	if(ja!=null)
    	{
    		String building_id = ja.getJSONObject(0).getString("kf_account");
    	      String kf_nick = ja.getJSONObject(0).getString("kf_nick");
    	      String kf_id = ja.getJSONObject(0).getString("kf_id");
    	      String kf_headimgurl = ja.getJSONObject(0).getString("kf_headimgurl");
    	}
      
         
      Map<String, Object> map=new HashMap();
		map.put("touser", openId);
		map.put("msgtype", "text");
			Map<String, Object> contentmaps=new HashMap();
			contentmaps.put("content", "百度搜索<a href=\"http://www.baidu.com\" >百度</a>");
		map.put("text", contentmaps);
		
		
      String r;
  		try {
  			r = HTTPUtils.doPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken,JSON.toJSONString(map),"UTF-8");
  			System.out.println("-->" + r);
  		} catch (Exception e) {
  			// TODO 自动生成的 catch 块
  			e.printStackTrace();
  		}
	}
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值