package com.ssj.framework.weixin.util;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.Map;

/**
 * 小程序调用接口
 * @author DELL
 *
 */
public class WxUtil {
	/**
	 * 同伴作业督导的
	 */
	public static Map<String ,String > map = new HashMap<String, String>();
	static{
		map.put("zuoybAppId", "wxac42ec4f0d2fe8bd");
		map.put("wxac42ec4f0d2fe8bd", "ca6dc7f7907b9875cfe4336ec1f93385");


		map.put("tloveAppId", "wxaa0f1e58f4a0e729");
		map.put("wxaa0f1e58f4a0e729", "e0e078a129cb46c527174444968bcd4f");
		map.put("tPhotoAppId", "wx19a38542cb0c734f");//时光合影
		map.put("wx19a38542cb0c734f", "fed21a1e3ba0cf2ff42ed1a0af9571f8");
		//阅读吧值用的zuoyb后续需要修改
		map.put("readAppId", "wxac42ec4f0d2fe8bd");
		map.put("wxac42ec4f0d2fe8bd", "ca6dc7f7907b9875cfe4336ec1f93385");

		//古诗词
		map.put("gsAppId", "wxff6c314fb66f1999");
		map.put("wxff6c314fb66f1999", "5c07f8e05e15af92bdd07209daf1ecae");


		//课后管理的小程序
		//	map.put("courseAppId", "wx6949a6f1d1b8c219");
		//	map.put("wx6949a6f1d1b8c219", "0987a321428b4c73d4b45bab0ab66e00");


		map.put("jzdAppId", "wx3805cdb5bcb8646a");//家长端小程序微信小程序根据code
		map.put("wx3805cdb5bcb8646a", "d5e0ad2b2a8c4d5b97581dc33c96429f");

		map.put("lsdAppId", "wx68b843a7f0ea6d31");//老师端小程序
		map.put("wx68b843a7f0ea6d31", "6b035aa0e54d6f008e835c6156a1a4f7");

		map.put("readingAppId", "wxe9cdca7c3365e448");//共享阅读吧小程序(找好书)
		map.put("wxe9cdca7c3365e448", "2b0d13802782ca82cc9a7111ae1a0beb");

		map.put("courseAppId", "wx699a1534b9be642e");//课程内容产品小程序(找好课)
		map.put("wx699a1534b9be642e", "b4713855f50c495773d945de096b0f40");




		map.put("lsdMobileAppId", "wx147b3c1b7472b566");//移动应用老师端
		map.put("jzdMobileAppId", "wx659f3c384093e279");//移动应用家长端

		map.put("photoAppId", "wx21518c1a56ccf0cd");//拍照小程序
		map.put("wx21518c1a56ccf0cd", "7faeb7b0788e3f71e1b04c7ecd2d7419");

		map.put("gameAppId", "wxb85618936c73086b");//游戏小程序
		map.put("wxb85618936c73086b", "d230ceacf0279776d9d80e4ae78f01fa");//游戏小程序

		map.put("intelligenceTestAppId", "wx9ad2e36a388c96e3");//智力测试小程序AppId
		map.put("wx9ad2e36a388c96e3", "079dfc5935d788e002b9ca0f4acb1a4b");//智力测试小程序AppSecret

		//机构老师端
		map.put("jglsdAppId", "wx8d4fa7dd726242e3");
		map.put("wx8d4fa7dd726242e3", "5b86a1bbe55f1ebd707151499b1b86dc");

		//馆长端小程序
		map.put("curatorAppId", "wx5a97ed416d40ac39");
		map.put("wx5a97ed416d40ac39", "2cb18db08a5ee8f364b99b433fbdddc3");

		//幼升小
		map.put("goUpAppId", "wx01596d5d7c5a1f24");
		map.put("wx01596d5d7c5a1f24", "2d723a60d5d8add07c3d98143d817a37");

		//小塾学伴测评
		map.put("openTestAppId", "wx52a20fae76b68bc2");
		map.put("wx52a20fae76b68bc2", "de2ccebb7ccf5b3fce1600ae3d96d32f");

	}

	private static Logger log = LoggerFactory.getLogger(WxUtil.class);

	private static String SESSION_KEY ="https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
//	private static String WX_SEND_TEMPLATE ="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN";


	private static String wxSessionkey = "F3UENUg3JcI31O2RpoBQ9n8J77Tf1LgZUyGyz";
	/**
	 * 根据code 获取 openid ,session_key
	 * @param code
	 */
	public static SessionKey getSeesionAndOpendId(String code,String wxappId){
		SessionKey  key  =new  SessionKey();
		if("123456789".equals(code)){
			key.setOpenId("openid123456");
			key.setSessionKey("sessionKey");
			key.setUnionid("unionid");
			return  key;
		}
		String wxappSecret = map.get(wxappId);
		log.info("wxappId-->"+wxappId);
		log.info("wxappSecret-->"+wxappSecret);
		String url =SESSION_KEY.replace("APPID", wxappId).replace("SECRET", wxappSecret).replace("JSCODE", code);
		JSONObject jsonObject = WeixinUtil.httpRequest(url, "GET", null);
		log.info("jsonObject:"+jsonObject.toJSONString());
		if (null != jsonObject) {
			try {
				key.setOpenId(jsonObject.getString("openid"));
				key.setSessionKey(jsonObject.getString("session_key"));
				key.setUnionid(jsonObject.getString("unionid"));
				log.info("微信小程序根据code 获取 openid ---("+jsonObject.getString("openid"));
				log.info("微信小程序根据code 获取 session_key---("+jsonObject.getString("session_key"));
				log.info("微信小程序根据code 获取 unionid---("+jsonObject.getString("unionid"));
			} catch (Exception e) {
				int errorCode = jsonObject.getIntValue("errcode");
				String errorMsg = jsonObject.getString("errmsg");
				log.error("微信小程序获取用户信息失败 errcode:{} errmsg:{}", errorCode, errorMsg);
			}
		}
		return  key;
	}

	public static String get3rdSession() {
		StringBuffer sb = new StringBuffer();
		try {
			String[] cmdA = { "/bin/sh", "-c", "head -n 80 /dev/urandom | tr -dc A-Za-z0-9 | head -c 64" };
			Process process = Runtime.getRuntime().exec(cmdA);
			LineNumberReader br = new LineNumberReader(new InputStreamReader(
					process.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			//如果本地测试，会报空指针异常，所以为了不让报错，索性返回有值即可
			sb.append(wxSessionkey).append(System.currentTimeMillis());
		}
		log.info("wxSessionkey---("+sb.toString());
		return sb.toString();
	}

	public static String getWxAccessToken(String wxappId) {
		String  token =null;
		try {
			Map<String,Object>  map = new  HashMap<String,Object>();
			map.put("grant_type", "client_credential");
			map.put("appid", wxappId);
			map.put("secret", map.get(wxappId));
			JSONObject json =  WeixinUtil.httpRequest(WeixinUtil.TOKEN_URL, "POST", JSONObject.toJSONString(map));
			log.info("获取getWxAccessToken--》"+json.toJSONString());
			token  = json.getString("access_token");
		} catch (Exception e) {
			log.error("获取getWxAccessToken异常", e);
		}
		return token;
	}
	/**
	 * 微信小程序发送消息模板
	 * @return
	 *//*
	public static JSONObject  sendWxTemplate(String accessToken,WxSendTemplate template){
		String url = WX_SEND_TEMPLATE.replace("ACCESS_TOKEN", accessToken);
		String json = JSONObject.toJSONString(template);
		JSONObject jsonObject =  WeixinUtil.httpRequest(url, "POST", json);
		return jsonObject;
	}*/

}
