284cfbcb4b85d31ad1fedec781a10845af8462d3.svn-base 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.yihu.jkedu.action;
  2. import java.util.Calendar;
  3. import java.util.Date;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import net.sf.json.JSONObject;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import com.coreframework.util.AppConfig;
  10. import com.yihu.base.ConfigUtil;
  11. import com.yihu.utils.ApiUtil;
  12. import com.yihu.utils.StringUtil;
  13. import com.yihu.utils.WxSDK;
  14. import com.yihu.wsgw.api.ServiceBus;
  15. @Controller
  16. @RequestMapping("/weixin")
  17. public class WeiXinControl
  18. {
  19. @RequestMapping(value = "/getWeiXinConfig")
  20. public void getWeiXinConfig(HttpServletRequest request,
  21. HttpServletResponse response)throws Exception {
  22. response.setContentType("application/json;charset=UTF-8");
  23. String url = StringUtil.isEmpty(request.getParameter("url")) ? null : request.getParameter("url");
  24. if(StringUtil.isEmpty(url)){
  25. response.getWriter().write(ApiUtil.jsonResult(-10000, "url不能为空,分享不了该文章").toString());
  26. return;
  27. }
  28. String urls=AppConfig.getValue("siteUrl")+url;
  29. Date time=(Date)request.getSession().getServletContext().getAttribute("time");
  30. System.out.println("time:"+time);
  31. Date date=Calendar.getInstance().getTime();
  32. String jsapi_ticket ="";
  33. WxSDK wsdk = new WxSDK();
  34. JSONObject resultJson=null;
  35. try{
  36. if(null ==time){//不存在数据
  37. request.getSession().getServletContext().setAttribute("time", Calendar.getInstance().getTime());
  38. jsapi_ticket =getJsApiTicket(response);
  39. if(StringUtil.isEmpty(jsapi_ticket)){
  40. response.getWriter().write(ApiUtil.jsonResult(-10000, "获取微信jsapi_ticket异常").toString());
  41. return;
  42. }
  43. request.getSession().getServletContext().setAttribute("jsapi_ticket",jsapi_ticket);
  44. resultJson = wsdk.getConfig(jsapi_ticket,urls);
  45. resultJson.put("Code", 10000);
  46. resultJson.put("Message", "成功");
  47. response.getWriter().write(resultJson.toString());
  48. return;
  49. }
  50. if((null !=time) && (date.getTime()>(time.getTime()+6200*1000))){//存在数据,但已经过期了
  51. request.getSession().getServletContext().setAttribute("time", Calendar.getInstance().getTime());
  52. jsapi_ticket =getJsApiTicket(response);
  53. if(StringUtil.isEmpty(jsapi_ticket)){
  54. response.getWriter().write(ApiUtil.jsonResult(-10000, "获取微信jsapi_ticket异常").toString());
  55. return;
  56. }
  57. request.getSession().getServletContext().setAttribute("jsapi_ticket",jsapi_ticket);
  58. resultJson = wsdk.getConfig(jsapi_ticket,urls);
  59. resultJson.put("Code", 10000);
  60. resultJson.put("Message", "成功");
  61. response.getWriter().write(resultJson.toString());
  62. return;
  63. }
  64. if((null !=time) && (date.getTime()<(time.getTime()+6200*1000))){//存在数据,在有效期内
  65. jsapi_ticket=(String)request.getSession().getServletContext().getAttribute("jsapi_ticket");
  66. resultJson = wsdk.getConfig(jsapi_ticket,urls);
  67. resultJson.put("Code", 10000);
  68. resultJson.put("Message", "成功");
  69. response.getWriter().write(resultJson.toString());
  70. return;
  71. }
  72. }catch(Exception e){
  73. e.printStackTrace();
  74. response.getWriter().write(ApiUtil.jsonResult(-10000, "获取微信认证异常").toString());
  75. return;
  76. }
  77. }
  78. public String getJsApiTicket(HttpServletResponse response)throws Exception{
  79. try{
  80. JSONObject params=new JSONObject();
  81. String result = ServiceBus.getInstance(
  82. null,
  83. ConfigUtil.getInstance().getAppId()).call(
  84. "baseinfo.CommonApi.getJsapiTicket", params.toString(), false);
  85. JSONObject formResultObj = JSONObject.fromObject(result);
  86. int code = formResultObj.getInt("Code");
  87. String msg = formResultObj.getString("Message");
  88. if(code == 10000){
  89. return formResultObj.getString("ticket");
  90. }else{
  91. return null;
  92. }
  93. }catch(Exception e){
  94. e.printStackTrace();
  95. response.getWriter().write(ApiUtil.jsonResult(-10000, "获取微信 jsapi_ticket异常").toString());
  96. return null;
  97. }
  98. }
  99. }