package com.yihu.jkedu.action; import java.util.Calendar; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.coreframework.util.AppConfig; import com.yihu.base.ConfigUtil; import com.yihu.utils.ApiUtil; import com.yihu.utils.StringUtil; import com.yihu.utils.WxSDK; import com.yihu.wsgw.api.ServiceBus; @Controller @RequestMapping("/weixin") public class WeiXinControl { @RequestMapping(value = "/getWeiXinConfig") public void getWeiXinConfig(HttpServletRequest request, HttpServletResponse response)throws Exception { response.setContentType("application/json;charset=UTF-8"); String url = StringUtil.isEmpty(request.getParameter("url")) ? null : request.getParameter("url"); if(StringUtil.isEmpty(url)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "url不能为空,分享不了该文章").toString()); return; } String urls=AppConfig.getValue("siteUrl")+url; Date time=(Date)request.getSession().getServletContext().getAttribute("time"); System.out.println("time:"+time); Date date=Calendar.getInstance().getTime(); String jsapi_ticket =""; WxSDK wsdk = new WxSDK(); JSONObject resultJson=null; try{ if(null ==time){//不存在数据 request.getSession().getServletContext().setAttribute("time", Calendar.getInstance().getTime()); jsapi_ticket =getJsApiTicket(response); if(StringUtil.isEmpty(jsapi_ticket)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "获取微信jsapi_ticket异常").toString()); return; } request.getSession().getServletContext().setAttribute("jsapi_ticket",jsapi_ticket); resultJson = wsdk.getConfig(jsapi_ticket,urls); resultJson.put("Code", 10000); resultJson.put("Message", "成功"); response.getWriter().write(resultJson.toString()); return; } if((null !=time) && (date.getTime()>(time.getTime()+6200*1000))){//存在数据,但已经过期了 request.getSession().getServletContext().setAttribute("time", Calendar.getInstance().getTime()); jsapi_ticket =getJsApiTicket(response); if(StringUtil.isEmpty(jsapi_ticket)){ response.getWriter().write(ApiUtil.jsonResult(-10000, "获取微信jsapi_ticket异常").toString()); return; } request.getSession().getServletContext().setAttribute("jsapi_ticket",jsapi_ticket); resultJson = wsdk.getConfig(jsapi_ticket,urls); resultJson.put("Code", 10000); resultJson.put("Message", "成功"); response.getWriter().write(resultJson.toString()); return; } if((null !=time) && (date.getTime()<(time.getTime()+6200*1000))){//存在数据,在有效期内 jsapi_ticket=(String)request.getSession().getServletContext().getAttribute("jsapi_ticket"); resultJson = wsdk.getConfig(jsapi_ticket,urls); resultJson.put("Code", 10000); resultJson.put("Message", "成功"); response.getWriter().write(resultJson.toString()); return; } }catch(Exception e){ e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-10000, "获取微信认证异常").toString()); return; } } public String getJsApiTicket(HttpServletResponse response)throws Exception{ try{ JSONObject params=new JSONObject(); String result = ServiceBus.getInstance( null, ConfigUtil.getInstance().getAppId()).call( "baseinfo.CommonApi.getJsapiTicket", params.toString(), false); JSONObject formResultObj = JSONObject.fromObject(result); int code = formResultObj.getInt("Code"); String msg = formResultObj.getString("Message"); if(code == 10000){ return formResultObj.getString("ticket"); }else{ return null; } }catch(Exception e){ e.printStackTrace(); response.getWriter().write(ApiUtil.jsonResult(-10000, "获取微信 jsapi_ticket异常").toString()); return null; } } }