package org.cs.web.product;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.cs.core.ctl.BaseCtl;
import org.cs.util.StringUtil;
import org.cs.web.product.model.WxaccessToken;
import org.cs.web.product.service.IWxacessTokenService;
import org.cs.web.product.service.impl.WxacessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/web/wx_share")
public class WxShare extends BaseCtl {
	
	public String code="";
	public static String APP_ID="wx5511007567ec7ad1";
	
	/*appId "wx5511007567ec7ad1"
    appSecret "4fbf52b7a61bfba41d806c9a655e03f9"*/
	//jsap
	@RequestMapping(value="getwxconfig")
	@ResponseBody
	public JSONObject updatepasswd(HttpServletRequest request,String signaurl){
		JSONObject json = new JSONObject();
		
		WxacessTokenService wxacessTokenService = new WxacessTokenService();
		
		WxaccessToken	wxtoken = wxacessTokenService.getTokens();
		String ticket=wxtoken.getJsapiTicket();
		signaurl = signaurl.split("#")[0];
		if(StringUtil.isNotBlank(ticket)){
			String timestamp=Long.toString(System.currentTimeMillis() / 1000);
			String noncestr=UUID.randomUUID().toString().replace("-", "");
			if(noncestr.length()>20){
				noncestr=noncestr.substring(0, 32);
			}
			StringBuilder sbclient = new StringBuilder();
			sbclient.append("jsapi_ticket=").append(ticket);
			sbclient.append("&noncestr=").append(noncestr);
			sbclient.append("&timestamp=").append(timestamp);
			sbclient.append("&url=").append(signaurl);
			String signature=DigestUtils.sha1Hex(sbclient.toString()).toLowerCase();
			json.put("noncestr", noncestr);
			json.put("timestamp", timestamp);
			json.put("signature", signature);
			json.put("appid", APP_ID);
		}
		return json;
	}
}

