package wechat.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.ejb.Init;
import javax.enterprise.event.Reception;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import config.ReceptionConfig;
import config.WechatConfig;
import model.ResultMap;
import util.Configuration;
import util.WechatUtil;
import utils.Encryption;

@RestController
public class WechatController {
	private Logger logger = LoggerFactory.getLogger(WechatController.class);

	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/wechat")
	public String check(HttpServletRequest request) {
		String signature = request.getParameter("signature");// 微信加密签名
		String timestamp = request.getParameter("timestamp");// 时间戳
		String nonce = request.getParameter("nonce");// 随机数
		String echostr = request.getParameter("echostr");//
		List<String> params = new ArrayList<>();
		params.add(ReceptionConfig.getValue("wechat_token"));
		params.add(timestamp);
		params.add(nonce);
		Collections.sort(params);
		String temp = params.get(0) + params.get(1) + params.get(2);
		if (Encryption.SHA1(temp).equals(signature)) {
			return echostr;
		}
		return "";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/wechat/init")
	public ResultMap init(String url) {
		url = url.split("#")[0];
		ResultMap result = new ResultMap();
		if (StringUtils.isEmpty(url)) {
			result.setStatus(ResultMap.STATUS_FAILURE);
			result.setInfo("请传入当前页面的完整URL");
			return result;
		}
		logger.info(url + "   urllruhahahah");
		Configuration configuration = WechatConfig.config(url);
		logger.info(JSONObject.toJSONString(configuration));
		result.setStatus(ResultMap.STATUS_SUCCESS);
		result.addContent("configuration", configuration);
		return result;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/token")
	public String token() {
		return ReceptionConfig.getAccessToken();
	}

	@RequestMapping(method = RequestMethod.GET, value = "/user")
	public ResultMap user(String serial, String code, HttpServletRequest request, HttpServletResponse response) {
		ResultMap result = new ResultMap();
		// identify the request origin
		if (WechatUtil.isWechat(request)) {
			if (StringUtils.isEmpty(code)) {
				try {
					String link = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="
							+ ReceptionConfig.getValue("wechat_appid") + "&redirect_uri="
							+ URLEncoder.encode("http://" + ReceptionConfig.getValue("domain_url")
									+ ReceptionConfig.getValue("bind_device") + "/" + serial, "utf-8")
							+ "&response_type=code&scope=snsapi_base&state=view#wechat_redirect";
					response.sendRedirect(link);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				} catch (IOException e) {
					logger.error(e.getMessage());
				}
			} else {
				String openId = WechatUtil.queryOauthOpenId(ReceptionConfig.getValue("wechat_appid"), ReceptionConfig.getValue("wechat_secret"), code);
				result.setStatus(ResultMap.STATUS_SUCCESS);
				result.addContent("openId", openId);
				return result;
			}
		}
		return result;
	}
}

@@ -208,72 +208,8 @@
 t;%0A%0A
-import javax.ejb.Init;%0Aimport javax.enterprise.event.Reception;%0A
 impo
@@ -2304,16 +2304,23 @@
 lue = %22/
+wechat/
 token%22)%0A
@@ -2446,16 +2446,23 @@
 lue = %22/
+wechat/
 user%22)%0A%09

modify url access