package com.winsun.utils;

import net.sf.ehcache.Element;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class WxInterfacesUtil {

	@Autowired
	@Qualifier("redisStringTemplate")
	private RedisTemplate redisTemplate;

	/**
	 * 获取凭证
	 * @param grant_type 获取access_token填写client_credential
	 * @param appid 第三方用户唯一凭证
	 * @param secret 第三方用户唯一凭证密钥，既appsecret
	 * @return json  token:获取到的凭证  expires_in:凭证有效时间，单位：秒
	 */
	public String getToken(String grant_type,String appid,String secret){
		String result = null;
		Element ele = null;//tokenCache.get("token_cache");
		if(ele!=null){
			result = (String)ele.getValue();
		}else{
			String url="https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+appid+"&secret="+secret;
			
			String res = HTTPSClient.sendGet(url);
			JSONObject jobj = JSONObject.fromObject(res);
			if(jobj.has("access_token")){
				result = jobj.getString("access_token");
			}
			ele = new Element("token_cache",result);
			//两小时内有效
			redisTemplate.opsForValue().set("token_cache", ele, 120,TimeUnit.MINUTES);
			//tokenCache.put(ele);
		}
		
		return result;
	}
	
	/**
	 * 获取jsapi_ticket
	 * @param accessToken 获取jsapi_ticket需要填写access_token
	 * @return String jsapi_ticket
	 */
	public static String  getJsapiTicket(String accessToken){
		String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi";
		String ticketStr = HTTPSClient.sendGet(url);
		JSONObject obj = JSONObject.fromObject(ticketStr) ;
		// 获取jsapi_ticket
		String ticket = (String) obj.get("ticket");
		return ticket;
			
	}
	
	
	/**
	 * 发送文本消息接口
	 * @param access_token 凭证
	 * @param openId 目标用户
	 * @param text 文本
	 * @return json
	 */
	public static String sendGroupMessage_text(String access_token,String openId,String text){
	//	String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token;
	//	String url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token="+access_token;
		String url="https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=63jVygxyIkK6oUCeM2YuzZHYLuzrkhSf6eDwmdd2HkXvIxnbfYIzQNMnt7deEWugUUobmyxNrZZa4gEMx1G0wtEJFqofT1sA3yvm9ZVctGPEG0B9DO0L8c3j3cnCC5XtZYRhAEATSL";
		JSONObject obj = new JSONObject();
		
				String openList=getOpenIdList(access_token);
		JSONObject opnejson=JSONObject.fromObject(openList);
		JSONObject opneIdsjson=(JSONObject) opnejson.get("data");
		
		obj.put("touser", "o22lhwRZP2zbXff7UHH3J8oqH8A0");
		
		obj.put("msgtype", "text");
		Map map1=new HashMap();
		map1.put("content","30+70");		
		obj.put("text",map1);
	
		String result =HTTPSClient.sendPost(url, obj.toString());

		return result;
	}
	
	/*
	 * 获取openId 列表 
	 * */
	public static String getOpenIdList(String access_token){
		String url="https://api.weixin.qq.com/cgi-bin/user/get?access_token="+access_token;
		String	result=HTTPSClient.sendGet(url);
		return  result;
	}
	
	public Map<String,Object> getJspaiSign(String url) {
		Map<String,Object> jsonObject = new HashMap<>();
		// 时间戳
		long timeStamp = System.currentTimeMillis();
		// 随机字符串
		String nonceStr = Integer.toString(((int) ((Math.random() + 1) * 1000000)));
		// 凭证
		String accessToken = getToken("client_credential",WxConfig.APPID,WxConfig.APPSECRET);
		// 票据
		String ticket = getJsapiTicket(accessToken);
		String signature = "";

		String params = "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "&timestamp=" + timeStamp + "&url=" + url;

		try {
			// 获取签名
			MessageDigest crypt = MessageDigest.getInstance("SHA-1");
			crypt.reset();
			crypt.update(params.getBytes("UTF-8"));
			signature = byteToHex(crypt.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		jsonObject.put("timestamp", timeStamp);
		jsonObject.put("nonceStr", nonceStr);
		jsonObject.put("signature", signature);

		return jsonObject;
	}
	
	public static String byteToHex(final byte[] hash) {
		Formatter formatter = new Formatter();
		for (byte b : hash) {
			formatter.format("%02x", b);
		}
		String result = formatter.toString();
		formatter.close();
		return result;
	}

	/*@param title 消息标题
	 * @param description 消息描述
	 * @param url 点击链接跳转地址
	 * @param picurl 图片地址
	 * @return*/
	/**
	 * 发送图文消息接口
	 * @param access_token
	 * @param openId
	 * @param news JSONArray 最多支持10条图文消息
	 * [
	 *   {"title": "Happy Day","description": "Is Really A Happy Day","url": "URL","picurl": "PIC_URL"}
     * ]
	 *
	 */
	public static String sendGroupMessage_news(String access_token,String openId,JSONArray news){
		String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token;
		            
		JSONObject obj = new JSONObject();
		obj.put("group", "网迅");
		obj.put("msgtype", "news");
		
		JSONObject newsObj = new JSONObject();
		newsObj.put("articles", news);
		obj.put("news", newsObj);
		String result = HTTPSClient.sendPost(url, obj.toString());
		return result;
	}
	
	
	
	/**
	 * 发送文本消息接口
	 * @param access_token 凭证
	 * @param openId 目标用户
	 * @param text 文本
	 * @return json
	 */
	public static String sendMessage_text(String access_token,String openId,String text){
		String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=63jVygxyIkK6oUCeM2YuzZHYLuzrkhSf6eDwmdd2HkXvIxnbfYIzQNMnt7deEWugUUobmyxNrZZa4gEMx1G0wtEJFqofT1sA3yvm9ZVctGPEG0B9DO0L8c3j3cnCC5XtZYRhAEATSL";
		
		JSONObject obj = new JSONObject();
		obj.put("touser", "o22lhwRZP2zbXff7UHH3J8oqH8A0");
		obj.put("msgtype", "text");
		
		JSONObject txtObj = new JSONObject();
		txtObj.put("content", "30+70");
		obj.put("text", txtObj);
		String result = HTTPSClient.sendPosts(url, obj.toString());
		
		return result;
	}
	
	/**
	 * 发送链接消息接口
	 * @param access_token 凭证
	 * @param openId 目标用户
	 * @param title 链接标题
	 * @param link_url  链接地址
	 * @return json
	 */
	public static String sendMessage_link(String access_token,String openId,String title,String link_url){
		String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token;
		
		JSONObject obj = new JSONObject();
		obj.put("touser", openId);
		obj.put("msgtype", "link");
		
		JSONObject linkObj = new JSONObject();
		linkObj.put("title", title);
		linkObj.put("url", link_url);
		obj.put("link", linkObj);
		String result = HTTPSClient.sendPost(url, obj.toString());
		
		return result;
	}

	/*@param title 消息标题
	 * @param description 消息描述
	 * @param url 点击链接跳转地址
	 * @param picurl 图片地址
	 * @return*/
	/**
	 * 发送图文消息接口
	 * @param access_token
	 * @param openId
	 * @param news JSONArray 最多支持10条图文消息
	 * [
	 *   {"title": "Happy Day","description": "Is Really A Happy Day","url": "URL","picurl": "PIC_URL"}
     * ]
	 *
	 */
	public static String sendMessage_news(String access_token,String openId,JSONArray news){
		String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token;
		
		JSONObject obj = new JSONObject();
		obj.put("touser", openId);
		obj.put("msgtype", "news");
		
		JSONObject newsObj = new JSONObject();
		newsObj.put("articles", news);
		obj.put("news", newsObj);
		String result = HTTPSClient.sendPost(url, obj.toString());
		return result;
	}
	
	public static String create_menu(String access_token,String openId,JSONArray news){
		String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token;
		
		JSONObject obj = new JSONObject();
		obj.put("touser", openId);
		obj.put("msgtype", "news");
		
		JSONObject newsObj = new JSONObject();
		newsObj.put("articles", news);
		obj.put("news", newsObj);
		String result = HTTPSClient.sendPost(url, obj.toString());
		return result;
	}
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 获取文本消息XML
	 * @param toUser 接收方帐号（收到的OpenID）
	 * @param fromUser 公众号易信ID（参照“接收消息”中的ToUserName）
	 * @param createTime 消息创建时间 （整型）
	 * @param msgType text
	 * @param content 回复的消息内容（换行：在content中能够换行，易信客户端就支持换行显示）
	 * @return
	 */
	public static String return_text(String toUser,String fromUser,Integer createTime,String msgType,String content){
		String xml = 
				"<xml>"
				+"<ToUserName><![CDATA["+toUser+"]]></ToUserName>"
				+"<FromUserName><![CDATA["+fromUser+"]]></FromUserName>"
				+"<CreateTime>"+createTime+"</CreateTime>"
				+"<MsgType><![CDATA["+msgType+"]]></MsgType>"
				+"<Content><![CDATA["+content+"]]></Content>"
				+"</xml>";
		return xml;
	}
	
	/**
	 * 获取回复图文消息xml
	 * @param toUser 接收方帐号（收到的OpenID）
	 * @param fromUser 公众号易信ID（参照“接收消息”中的ToUserName）
	 * @param createTime 消息创建时间 （整型）
	 * @param msgType news
	 * @param items List<String[]>  [0]:图文消息标题     [1]:图文消息描述          [2]:图片链接         [3]:点击图文消息跳转链接
	 * @return
	 */
	public static String return_picUrl(String toUser, String fromUser, Integer createTime, String msgType, List<String[]> items) {
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		sb.append("<ToUserName><![CDATA[" + toUser + "]]></ToUserName>");
		sb.append("<FromUserName><![CDATA[" + fromUser + "]]></FromUserName>");
		sb.append("<CreateTime>" + createTime + "</CreateTime>");
		sb.append("<MsgType><![CDATA[news]]></MsgType>");
		sb.append("<ArticleCount>" + items.size() + "</ArticleCount>");
		sb.append("<Articles>");
		for(Object[] obj : items) {
			sb.append("<item>");
			sb.append("<Title><![CDATA[" + obj[0] + "]]></Title>");
			sb.append("<Description><![CDATA[" + obj[1] + "]]></Description>");
			sb.append("<PicUrl><![CDATA[" + obj[2] + "]]></PicUrl>");
			sb.append("<Url><![CDATA[" + obj[3] + "]]></Url>");
			sb.append("</item>");
		}
		sb.append("</Articles>");
		sb.append("</xml>");
		return sb.toString();
	}
	
	
	/**
	 * 终端登记图文页数组
	 * @param openId
	 * @return
	 */
	public static String[] zddj_page(String openId) {
		/*String[] str = new String[]{"终端登记", "终端登记", "https://nos.163.com/yixinpublic/pr_8-jqipwwv8polh-seiahlw==_1423981193_6571103", "http://120.24.88.216/xyjl/yixinuser/the-sales-registration.jsp?openId=" + openId};
		return str;*/
		return commonPage("http://wenjun.pub/xyjl/yx/bind/sales-bind!toReg.action?openId=" + openId, "终端登记", "点击进入终端登记界面", "https://nos.163.com/yixinpublic/pr_8-jqipwwv8polh-seiahlw==_1423981193_6571103");
	}
	
	/**
	 * 店员销量TOP5榜单图文页数组
	 * @param openId
	 * @return
	 */
	public static String[] terTop_page(String openId){
		String[] str = new String[]{"终端TOP榜","终端TOP榜","https://nos.163.com/yixinpublic/pr_mzw1uyt7l0sqiloodepj2g==_1437708277_48269407","http://120.24.88.216/xyjl/yx/report/mobile/mobile-report!toTerTopPage.action?openId="+openId};
		return str;
	}
	
	/**
	 * TOP5bangdan图文页数组
	 * @param openId
	 * @return
	 */
	public static String[] top_page(String openId){
		String[] str = new String[]{"奖励TOP榜","TOP5榜单","https://nos.163.com/yixinpublic/pr_9xmo9qrwhykzv03wwc9icq==_1435911065_45762356","http://120.24.88.216/xyjl/yx/report/mobile/mobile-report!toTopPage.action?openId="+openId};
		return str;
	}
	
	
	/**
	 * 终端登记明细查询图文页数组
	 * @param openId
	 * @return
	 */
	public static String[] djmxcx_page(String openId){
		String[] str = new String[]{"奖励查询","终端奖励明细查询","https://nos.163.com/yixinpublic/pr_6lpschhp5dhfxtbrcz288w==_1423981001_6569803","http://120.24.88.216/xyjl/yx/report/mobile/mobile-report!toFrontlineStaffSecondPage.action?openId="+openId};
		return str;
	}
	

	/**
	 * 登记查询图文页数组
	 * @param openId
	 * @return
	 */
	public static String[] djcx_page(String openId){
		String[] str = new String[]{"奖励查询","终端销售奖励查询","https://nos.163.com/yixinpublic/pr_r8oamwtnvnfblpuyffsbsw==_1425439255_10135230","http://120.24.88.216/xyjl/yx/report/mobile/mobile-report!toFrontlineStaffFirstPage.action?openId="+openId};
		return str;
	}
	
	
	/**
	 * 投诉建议进度查询
	 * @param openId
	 * @return
	 */
	public static String[] tsjy_page(String openId) {
		/*String[] str = new String[]{"进度查询","进度查询","https://nos.163.com/yixinpublic/pr_p71ybejuqqctefcsseli_w==_1423980900_6267434","http://120.24.88.216/xyjl/yx/appeal/appeal!toPage.action?openId="+openId};
		return str;*/
		return commonPage("http://wenjun.pub/xyjl/yx/appeal/appeal!toPage.action?openId=" + openId, "在线申诉", "在线申诉", "https://nos.163.com/yixinpublic/pr_p71ybejuqqctefcsseli_w==_1423980900_6267434");
	}
	
	/**
	 * 反馈/建议
	 * @param openId
	 * @return
	 */
	public static String[] ts_page(String openId){
		/*String[] str = new String[]{"投诉建议","投诉进度查询","https://nos.163.com/yixinpublic/pr_p71ybejuqqctefcsseli_w==_1423980900_6267434","http://120.24.88.216/xyjl/yx/appeal/appeal!toAppealPage.action?openId="+openId};
		return str;*/
		return commonPage("http://wenjun.pub/xyjl/yx/appeal/appeal!toAppealPage.action?openId=" + openId, "在线申诉", "在线申诉", "https://nos.163.com/yixinpublic/pr_p71ybejuqqctefcsseli_w==_1423980900_6267434");
	}
	
	
	/**
	 * 手机绑定图文页数组
	 * @param openId
	 * @return
	 */
	public static String[] bdsj_page(String openId) {
		/*String[] str = new String[]{"绑定手机", "绑定手机", "https://nos.163.com/yixinpublic/pr_9egota4nyl_dmoducr7csq==_1423980707_6568520", "http://120.24.88.216/xyjl/yixinuser/bind.jsp?openId=" + openId};
		return str;*/
		return commonPage("http://wenjun.pub/xyjl/yx/bind/sales-bind!toBindPage.action?openId=" + openId, "绑定手机", "营业员手机号与易信号绑定", "https://nos.163.com/yixinpublic/pr_6ketettmxd5-s-b0qjmqdq==_1424945149_8625212");
	}
	

	/**
	 * 用户个人信息界面
	 * @param openId
	 * @return
	 */
	public static String[] userInfoPage(String openId) {
		return commonPage("http://wenjun.pub/xyjl/yx/system/sales!salesInfo.action?openId=" + openId, "个人信息", "营业员个人信息", "https://nos.163.com/yixinpublic/pr_6byhe_qrvu8cc85i8t3dcw==_1425288345_10376617");
	}
	
	public static String[] terminalAwardListPage(String openId) {
		return commonPage("http://wenjun.pub/xyjl/yx/report/mobile/mobile-report!toTerList.action?openId=" + openId, "终端奖励", "终端的奖励信息查询", "https://nos.163.com/yixinpublic/pr_4pnt4kmgl002jqn3xyr34q==_1425439428_9798017");
	}
	
	/**
	 * 系统介绍页面
	 * @param openId
	 * @return
	 */
	public static String[] info_page(String openId) {
		String url = "http://wenjun.pub/xyjl/info.jsp";
		return commonPage(url, "一线销售终端登记及奖励系统", "", "https://nos.163.com/yixinpublic/pr_9egota4nyl_dmoducr7csq==_1423980707_6568520");
	}
	
	/**
	 * 通用的xml图文数组
	 * @param url
	 * @param title
	 * @param context
	 * @param pic
	 * @return
	 */
	public static String[] commonPage(String url, String title, String context, String pic) {
		String[] str = new String[]{title, context, pic, url};
		return str;
	}
	
	public static String sendMsg(String sendNumber,String msg){
		String url = "http://120.24.88.216:8080/PearSmsCenter/SendValidateSms";
		
		try {
			url+="?sendNumber="+sendNumber;
			url+="&msg="+URLEncoder.encode(URLEncoder.encode(msg,"utf-8"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String result="";
		result = HTTPSClient.sendGet(url);
		
		return result;
	}
	
	/**
	 * 通过code换取access_token
	 * @param code
	 * @return
	 */
	public static String getAccess_tokenByCode(String code){
	
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WxConfig.APPID+"&secret="+WxConfig.APPSECRET+"&code="+code+"&grant_type=authorization_code";
		String result="";
		result = HTTPSClient.sendGet(url);
		System.out.println(url);
		return result;
	}
	
	/**
	 * 刷新access_token
	 * @return
	 */
	public static String refresh_token(String refresh_token){
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WxConfig.APPID+"&grant_type=refresh_token&refresh_token="+refresh_token;
	//	String url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wxfc18f5186b729d15&grant_type=refresh_token&refresh_token="+refresh_token; 
		String result="";
		result = HTTPSClient.sendPost(url,"");
		return result;
	}
	
	
	
	/*public static void main(String[] args) {
	//	String url="https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+appid+"&secret="+secret;b
		String token = WxInterfacesUtil.getToken("client_credential", "wx42f959d029fed0ea", "4d7a334db8f7a414f3665c18cbc187d8");
		JSONObject obj = new JSONObject();
		obj.put("title", "公告测试");
		obj.put("description", "这是一条系统测试公告，请勿回复!");
		obj.put("url", "http://120.24.88.216:7033/xyjl/info.jsp");
		obj.put("picurl", "http://120.24.88.216:7033/xyjl/images/login/bg1.jpg");
		JSONArray arr = new JSONArray();
		arr.add(obj);
		String str = WxInterfacesUtil.sendGroupMessage_news(token, null, arr);
		
		System.out.println(str);
	
		//WxInterfacesUtil.sendMsg("13542107342", "test");
		System.out.println("token=="+token);
	
	
	}
	*/
	
	/**
	 * 生成二维码
	 * @param type  1:临时二维码 2：永久二维码
	 * @param sceneType 1：整形场景值  2：字符串场景值
	 * @throws IOException 
	 * @throws ParseException 
	 */
	public static String createCode(int type, int sceneType, String scene, String token) throws ParseException, IOException{
		String url = "";
		if(type == 1){
			url = WxConfig.TEMPORARY_CODE_URL ;
		}
		else{
			url = WxConfig.PERMANENT_CODE_URL;
		}
		
		url += token;
		
		String params = getSendStr(type, sceneType, scene);
	//	String respStr = HTTPSClient.sendPosts(url,params);
		String respStr = HttpService.sendPost(url,params);
		Map map = JSONObject.fromObject(respStr);
		String ticket = (String)map.get("ticket");
		
		String codeUrl = WxConfig.TICKET_CODE_URL  + URLEncoder.encode(ticket,"UTF-8");
		return codeUrl;
	}
	
	
	/**
	 * 网页授权获取用户信息
	 * @param code 用于获取openid和access_token 
	 *  这个access_token与基础access_token不同，专门用与获取用户信息 
	 * @throws UnsupportedEncodingException 
	 */
	public static String getUserInfo(String code) throws UnsupportedEncodingException{
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+ WxConfig.APPID +"&secret="+ WxConfig.APPSECRET +"&code="+code+"&grant_type=authorization_code";
		String result = HTTPSClient.sendGet(url);
		JSONObject obj = JSONObject.fromObject(result);
		String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+ obj.get("access_token") +"&openid="+ obj.get("openid") +"&lang=zh_CN";
		String userInfo = HTTPSClient.sendGet(infoUrl);
		JSONObject userObj = JSONObject.fromObject(userInfo);

		return userInfo.toString();
	
	}
	
	/**
	 * 网页授权根据open_id和特殊access_token获取用户信息
	 *  这个access_token与基础access_token不同，专门用与获取用户信息 
	 * @throws UnsupportedEncodingException 
	 */
	public static JSONObject getWxInfo(String open_id, String access_token) throws UnsupportedEncodingException{
		String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+ access_token +"&openid="+ open_id +"&lang=zh_CN";
		String userInfo = HTTPSClient.sendGet(infoUrl);
		// 解决微信发送的中文信息乱码
		userInfo = new String(userInfo.getBytes("ISO-8859-1"), "UTF-8");
		JSONObject userObj = JSONObject.fromObject(userInfo);

		return userObj;
	
	}
	
	/**
	 * 获取凭证
	 * @return
	 */
	public static String getAccessToken(){
		String result = HTTPSClient.sendGet(WxConfig.ACCESS_TOKEN_URL);
		JSONObject json = JSONObject.fromObject(result);
		
		return (String)json.get("access_token");
	}	

	/**
	 * 获取生成二维码所需的post
	 * @param type  1:临时二维码  2：永久二维码
	 * @param sceneType 1：整形场景值  2：字符串场景值
	 */
	public static String getSendStr(int type,int sceneType,String scene){
		String params = "";
		
		if(type == 1 && sceneType == 1){
			params = "{\"expire_seconds\":604800,\"action_name\":\"QR_SCENE\",\"action_info\":"
					+ "{\"scene\":{\"scene_id\":"+ scene +"}}}";
		}
		else if(type == 2 && sceneType == 1){
			params = "{\"action_name\":\"QR_LIMIT_STR_SCENE\",\"action_info\":"
					+ "{\"scene\":{\"scene_str\":\""+ scene +"\"}}}";
		}
		else if(type == 2 && sceneType ==2){
			params = "{\"action_name\":\"QR_LIMIT_STR_SCENE\",\"action_info\":"
					+ "{\"scene\":{\"scene_str\":\""+ scene +"\"}}}";
		}
		
		return params;
	}
	
	/**
	 * 发送审核结果通知
	 * @return
	 */
	public static String sendAppealResult(String access_token,String openid,String title,String appealResult,String replyContet,String detailUrl){
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
		String message = "{"
							+"\"touser\": \""+openid+"\", "
							+"\"template_id\": \"TJO_y05LF4-u8dyCMhnu3AsAplRxpFNrG_-plnCEdUA\", "
							+"\"url\": \""+ detailUrl +"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\"您的申诉已处理完毕！\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ title +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\""+ appealResult +"\","
											+"\"color\":\"#173177\""
										+"}, "		
									+"\"remark\": {"
											+"\"value\":\""+ replyContet +"\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		String result = HTTPSClient.sendPosts(url, message);
		
		return result;
	}
		
	/**
	 * 发送业务办理邀请的模板信息
	 * @return
	 */
	public static String sendBusinessTransactionInvitation(String access_token,String openid,String salemanName,String clickUrl){
		
		SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		String date = sf.format(new Date());
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
		String message = "{"
							+"\"touser\": \""+openid+"\", "
							+"\"template_id\": \"QUJQiH14xgHa98zonF1s9oNu7_xXLsJHIh9n0cJN67Q\", "
							+"\"url\": \""+ clickUrl +"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\"尊敬的客户，你正在向业务办理人员"+ salemanName +"发起业务办理！\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ date +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\"线下销售\","
											+"\"color\":\"#173177\""
										+"}, "
											
									+"\"keyword3\": {"
											+"\"value\":\""+ salemanName +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"remark\": {"
											+"\"value\":\"请点击进行业务办理！\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		String result = HTTPSClient.sendPosts(url, message);
		
		return result;
	}
	
	/**
	 * 发送解绑通知
	 * @return
	 */
	public static String sendUnbindInfo(String access_token,String openid,String account){
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
		String message = "{"
							+"\"touser\": \""+openid+"\", "
							+"\"template_id\": \"12Q_7dxz4GHA2UTJo_2PHZNhprPmTQPKKEiNjErNNJI\", "
							+"\"url\": \"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\"您好，你的微信与飞Young系统的账号解除绑定！\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ account +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\"解绑后，你的微信进入飞Young合伙人公众号菜单需要登录\","
											+"\"color\":\"#173177\""
										+"}, "
											
									
									+"\"remark\": {"
											+"\"value\":\"如该解绑动作不是本人操作，请通知管理员！\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		String result = HTTPSClient.sendPosts(url, message);
		System.out.println(result);
		
		return result;
	}
	
	/**
	 * 发送佣金结算通知
	 * @return
	 */
	public static String sendBonusInfo(String access_token,String openid,String account){
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
		String message = "{"
							+"\"touser\": \""+openid+"\", "
							+"\"template_id\": \"12Q_7dxz4GHA2UTJo_2PHZNhprPmTQPKKEiNjErNNJI\", "
							+"\"url\": \"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\"您好，你的微信与飞Young系统的账号解除绑定！\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ account +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\"解绑后，你的微信进入飞Young合伙人公众号菜单需要登录\","
											+"\"color\":\"#173177\""
										+"}, "
											
									
									+"\"remark\": {"
											+"\"value\":\"如该解绑动作不是本人操作，请通知管理员！\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		String result = HTTPSClient.sendPosts(url, message);
		System.out.println(result);
		
		return result;
	}
	
	/**
	 * 发送申诉结果通知
	 * @return
	 */
	public static String sendAppealNotice(String access_token,String openid,String content, String time, String auditResult){
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
		String message = "{"
							+"\"touser\": \""+openid+"\", "
							+"\"template_id\": \"TJO_y05LF4-u8dyCMhnu3AsAplRxpFNrG_-plnCEdUA\", "
							+"\"url\": \"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\"您好，您在"+ time +"提交的申请已经审核了！\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ content +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\""+ auditResult +"\","
											+"\"color\":\"#173177\""
										+"}, "
											
									
									+"\"remark\": {"
											+"\"value\":\"\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		
		String result = HTTPSClient.sendPosts(url, message);
		System.out.println(result);
		
		return result;
	}
	
	/**
	 * 发货通知
	 * @return
	 */
	public static String sendDeliveryNotice(Map<String, String> map){
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + map.get("accessToken");
		String message = "{"
							+"\"touser\": \""+map.get("openId")+"\", "
							+"\"template_id\": \"Z_DhP04JVUUFbGJfqvVb7q15xGXT9rXIl9RwPxAyjg0\", "
							+"\"url\": \"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\""+ map.get("first") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ map.get("keyword1") +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\""+ map.get("keyword2") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									+"\"keyword3\": {"
											+"\"value\":\""+  map.get("keyword3") +"\","
											+"\"color\":\"#173177\""
										+"}, "
											
									+"\"keyword4\": {"
											+"\"value\":\""+  map.get("keyword4") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									+"\"keyword5\": {"
											+"\"value\":\""+  map.get("keyword5") +"\","
											+"\"color\":\"#173177\""
										+"}, "
						
									
									+"\"remark\": {"
											+"\"value\":\""+  map.get("remark") +"\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		
		String result = HTTPSClient.sendPosts(url, message);
		System.out.println(result);
		
		return result;
	}
	
	/**
	 * 下单通知
	 * @return
	 */
	public static String sendOrderNotice(Map<String, String> map){
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + map.get("accessToken");
		String message = "{"
							+"\"touser\": \""+map.get("openId")+"\", "
							+"\"template_id\": \"kL1_WJAh2qLyjc8c1AD92uevEe3DLGX3MtCna6dWuL0\", "
							+"\"url\": \"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\""+ map.get("first") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ map.get("keyword1") +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\""+ map.get("keyword2") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									+"\"keyword3\": {"
											+"\"value\":\""+  map.get("keyword3") +"\","
											+"\"color\":\"#173177\""
										+"}, "
											
									+"\"keyword4\": {"
											+"\"value\":\""+  map.get("keyword4") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									+"\"keyword5\": {"
											+"\"value\":\""+  map.get("keyword5") +"\","
											+"\"color\":\"#173177\""
										+"}, "
						
									
									+"\"remark\": {"
											+"\"value\":\"\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		
		String result = HTTPSClient.sendPosts(url, message);
		
		return result;
	}
	
	/**
	 * 库存不足通知
	 * @return
	 */
	public static String sendRepertoryNotice(String access_token,String openid,String content, String time, String auditResult){
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
		String message = "{"
							+"\"touser\": \""+openid+"\", "
							+"\"template_id\": \"TJO_y05LF4-u8dyCMhnu3AsAplRxpFNrG_-plnCEdUA\", "
							+"\"url\": \"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\"您好，您在"+ time +"提交的申请已经审核了！\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ content +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\""+ auditResult +"\","
											+"\"color\":\"#173177\""
										+"}, "
											
									
									+"\"remark\": {"
											+"\"value\":\"\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		
		String result = HTTPSClient.sendPosts(url, message);
		System.out.println(result);
		
		return result;
	}
	
	/**
	 * 发送充值结果通知
	 * @return
	 */
	public static String sendRechargeNotice(Map<String, String> map){
		
		String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + map.get("accessToken");
		String message = "{"
							+"\"touser\": \""+map.get("openId")+"\", "
							+"\"template_id\": \"8PqU7n7yM-yDXOMYFIMoacagEDLofQvxhOMB3NEQs8Y\", "
							+"\"url\": \""+map.get("url")+"\", "
							+"\"data\": {"
									+"\"first\": {"
											+"\"value\":\""+ map.get("first") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									
									+"\"keyword1\": {"
											+"\"value\":\""+ map.get("keyword1") +"\","
											+"\"color\":\"#173177\""
										+"}, "
												
									+"\"keyword2\": {"
											+"\"value\":\""+ map.get("keyword2") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									+"\"keyword3\": {"
											+"\"value\":\""+  map.get("keyword3") +"\","
											+"\"color\":\"#173177\""
										+"}, "
											
								/*	+"\"keyword4\": {"
											+"\"value\":\""+  map.get("keyword4") +"\","
											+"\"color\":\"#173177\""
										+"}, "
									+"\"keyword5\": {"
											+"\"value\":\""+  map.get("keyword5") +"\","
											+"\"color\":\"#173177\""
										+"}, "*/
						
									
									+"\"remark\": {"
											+"\"value\":\""+  map.get("remark") +"\","
											+"\"color\":\"#173177\""
										+"}"
								+"}"
						+"}";
		
		
		String result = HTTPSClient.sendPosts(url, message);
		
		return result;
	}

	 /** @param grant_type 获取access_token填写client_credential
	 * @param appid 第三方用户唯一凭证
	 * @param secret 第三方用户唯一凭证密钥，既appsecret*/
	/**
	 * 获取凭证
	 * @return json  token:获取到的凭证  expires_in:凭证有效时间，单位：秒
	 */
	public static String getFyToken(){
		String result = null;
		String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx9e2de8b5f756a4a4&secret=1d8efde4496e92fa9cbcc95f9ca63d4d";
		
		String res = HTTPSClient.sendGet(url);
		JSONObject jobj = JSONObject.fromObject(res);
		if(jobj.has("access_token")){
			result = jobj.getString("access_token");
		}

		
		return result;
	}
	
}
