package cn.pluss.platform.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;

import cn.pluss.platform.entitiy.WechatQRCode;
import cn.pluss.platform.util.HttpResult;
import cn.pluss.platform.util.HttpUtil;
import cn.pluss.platform.util.ParametersUtil;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.util.TemporaryQRcode;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("test")
public class TestController {

	@PostMapping("/upload")
	@ResponseBody
	public String upload(@RequestParam("file") MultipartFile file) {

		return "success";
	}

	private static final Logger logger = LoggerFactory.getLogger(TestController.class);

	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String sayHello() {
		Map<String, Object> map = new HashMap<String, Object>(16);
		map.put("grant_type", "client_credential");
		map.put("appid", "wxed824282c345b7a0");
		map.put("secret", "155350e41b5bc5bff95cc41d306a8161");
		try {
			HttpResult doGet = HttpUtil.doGet("https://api.weixin.qq.com/cgi-bin/token", map);
			logger.error("=====获取token接口返回值=====", JSONObject.toJSONString(doGet));
			Integer code = doGet.getCode();
			String msg = null;
			boolean flag = false;
			if (code == 200) {
				String body = doGet.getBody();
				JSONObject parseObject = JSONObject.parseObject(body);
				Integer errorCode = parseObject.getInteger("errcode");
				if (!StringUtil.isEmpty(errorCode)) {
					switch (errorCode) {
					case -1:
						msg = "系统繁忙，此时请开发者稍候再试";
						break;
					case 0:
						msg = "请求成功";
						flag = true;
						break;
					case 40001:
						msg = "AppSecret错误或者AppSecret不属于这个公众号，请开发者确认AppSecret的正确性";
						break;
					case 40002:
						msg = "请确保grant_type字段值为client_credential";
						break;
					case 40164:
						msg = "调用接口的IP地址不在白名单中，请在接口IP白名单中进行设置。（小程序及小游戏调用不要求IP地址在白名单内。）";
						break;
					default:
						msg = "请求成功";
						break;
					}
				} else {
					flag = true;
				}
				if (flag) {
					String accessToken = parseObject.getString("access_token");
					Map<String, Object> map1 = new HashMap<String, Object>(16);
					map1.put("expire_seconds", 2592000);
					map1.put("action_name", "QR_STR_SCENE");
					String sceneStr = null;
					WechatQRCode wechatQRCode = TemporaryQRcode.createForeverTicket(accessToken, 2592000, sceneStr);
					Properties prop = System.getProperties();
					String os = prop.getProperty("os.name");
					String path = null;
					if (os.startsWith("win") || os.startsWith("Win")) {
						path = "D:/Pictures";// windows目录
					} else {
						path = ParametersUtil.upload_save_path;
					}
					String rQcode = TemporaryQRcode.getRQcode(wechatQRCode.getTicket(), path);
					System.out.println(rQcode);
					return rQcode;
				} else {
					System.out.println(msg);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private static String getAccessToken() {
		Map<String, Object> map = new HashMap<String, Object>(16);
		map.put("grant_type", "client_credential");
		map.put("appid", "wx20403c16079b1b59");
		map.put("secret", "bca9c306d375840f4c5d7bfea22d5f74");
		try {
			HttpResult doGet = HttpUtil.doGet("https://api.weixin.qq.com/cgi-bin/token", map);
			logger.error("=====获取token接口返回值=====", JSONObject.toJSONString(doGet));
			Integer code = doGet.getCode();
			String msg = null;
			boolean flag = false;
			if (code == 200) {
				String body = doGet.getBody();
				JSONObject parseObject = JSONObject.parseObject(body);
				Integer errorCode = parseObject.getInteger("errcode");
				if (!StringUtil.isEmpty(errorCode)) {
					switch (errorCode) {
					case -1:
						msg = "系统繁忙，此时请开发者稍候再试";
						break;
					case 0:
						msg = "请求成功";
						flag = true;
						break;
					case 40001:
						msg = "AppSecret错误或者AppSecret不属于这个公众号，请开发者确认AppSecret的正确性";
						break;
					case 40002:
						msg = "请确保grant_type字段值为client_credential";
						break;
					case 40164:
						msg = "调用接口的IP地址不在白名单中，请在接口IP白名单中进行设置。（小程序及小游戏调用不要求IP地址在白名单内。）";
						break;
					default:
						msg = "请求成功";
						break;
					}
				} else {
					flag = true;
				}
				if (flag) {
					String accessToken = parseObject.getString("access_token");
					return accessToken;
				} else {
					System.out.println(msg);
					return msg;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}
	
	
	
	

	/**
	 * 
	 * creatMenu:(创建菜单). <br/>
	 * 
	 *
	 * @author Administrator
	 * @return
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/creatMenu", method = RequestMethod.GET)
	public String creatMenu() {
		String accessToken = getAccessToken();
		if (StringUtil.isNotEmpty(accessToken)) {
			String string = "{\"button\":[{\"name\":\"福利社\",\"sub_button\":{\"list\":[{\"type\":\"miniprogram\",\"name\":\"积分商城\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/pages\\/creditshop\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/creditshop\\/index\"},{\"type\":\"miniprogram\",\"name\":\"生活圈\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/pages\\/home\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/home\\/index\"},{\"type\":\"miniprogram\",\"name\":\"优惠券\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/pages\\/channel\\/coupon?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/channel\\/coupon\"},{\"type\":\"miniprogram\",\"name\":\"天天特价\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/plugin\\/pages\\/bargain\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/bargain\\/index\"},{\"type\":\"miniprogram\",\"name\":\"拼团抢购\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/gohome\\/pages\\/home\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"gohome\\/pages\\/home\\/index\"}]}},{\"type\":\"text\",\"name\":\"广告位\",\"value\":\"暂未开放\"},{\"name\":\"更多服务\",\"sub_button\":{\"list\":[{\"type\":\"miniprogram\",\"name\":\"会员卡\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/pages\\/home\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/card\\/mine\"},{\"type\":\"text\",\"name\":\"商户入驻\",\"value\":\"1\"},{\"type\":\"text\",\"name\":\"广告合作\",\"value\":\"1\"},{\"type\":\"text\",\"name\":\"在线客服\",\"value\":\"1\"}]}}]}";
			String json = HttpUtil.HttpPostWithJson("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken + "", string);
			return json;
		}
		return null;
	}

	public static void main(String[] args) throws Exception {
		createMenu();
	}

	private static void createMenu() {
		String accessToken = getAccessToken();
		if (StringUtil.isNotEmpty(accessToken)) {
			String string = "{\"button\":[{\"name\":\"福利社\",\"sub_button\":[{\"type\":\"miniprogram\",\"name\":\"同城外卖\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/pages\\/home\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/home\\/index\"},{\"type\":\"miniprogram\",\"name\":\"积分商城\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/pages\\/creditshop\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/creditshop\\/index\"},{\"type\":\"miniprogram\",\"name\":\"优惠券\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/pages\\/channel\\/coupon?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/channel\\/coupon\"},{\"type\":\"miniprogram\",\"name\":\"天天特价\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/plugin\\/pages\\/bargain\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/bargain\\/index\"},{\"type\":\"miniprogram\",\"name\":\"拼团抢购\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/gohome\\/pages\\/home\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"gohome\\/pages\\/home\\/index\"}]},{\"type\":\"miniprogram\",\"name\":\"同城信息\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/gohome\\/pages\\/tongcheng\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"gohome/pages/tongcheng/index\"},{\"name\":\"更多服务\",\"sub_button\":[{\"type\":\"miniprogram\",\"name\":\"会员卡\",\"url\":\"https:\\/\\/shop.shouyinbei.net\\/addons\\/we7_wmall\\/template\\/vue\\/index.html?menu=#\\/pages\\/home\\/index?i=1\",\"appid\":\"wx00f4ba80787fbdbb\",\"pagepath\":\"pages\\/card\\/mine\"},{\"type\":\"click\",\"name\":\"商户入驻\",\"key\":\"syb_settle\"},{\"type\":\"view\",\"name\":\"广告合作\",\"url\":\"https://jinshuju.net/f/Kwn8TI\"},{\"type\":\"click\",\"name\":\"在线客服\",\"key\":\"syb_service\"}]}]}";
			String json = HttpUtil.HttpPostWithJson("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken + "", string);
			System.out.println("=================创建菜单结果==========>"+json);
		}
	}

	private static void getMenu() throws Exception {
		String accessToken = getAccessToken();
		String url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" + accessToken;
		System.out.println("==========token=============>"+accessToken);
		HttpResult doPost = HttpUtil.doGet(url, null);
		String body = doPost.getBody();
		System.out.println("json=================>"+body);
	}
	//

	/**
	 * 
	 * deleteMenu:(删除菜单). <br/>
	 * 
	 *
	 * @author Administrator
	 * @return
	 * @throws Exception
	 * @since JDK 1.8
	 */
	@RequestMapping(value = "/deleteMenu", method = RequestMethod.GET)
	public String deleteMenu() throws Exception {
		String accessToken = getAccessToken();
		if (StringUtil.isNotEmpty(accessToken)) {
			HttpResult doPost = HttpUtil.doGet("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken + "", null);
			return JSONObject.toJSONString(doPost);
		}
		return null;
	}
	
	


}
