import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.struts2.ServletActionContext;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.opensymphony.xwork2.ActionSupport;
import com.wxsale.pojo.wx_wxmessage;
import com.wxsale.service.wx_wxmessageService;
public class wechatLoginAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private wx_wxmessage wx_wxmessage = new wx_wxmessage();
private wx_wxmessageService wx_wxmessageService;
public void wechatLogin() {
/*CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = null;*/
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
session.setMaxInactiveInterval(-1);
String result = "";
BufferedReader in = null;
try {
String appid = "";
String secret = "";
String js_code = request.getParameter("js_code");
String urlFormat = "https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+js_code+"&grant_type=authorization_code";
URL realUrl = new URL(urlFormat);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println(result);
JSONObject json = JSONObject.fromObject(result);
System.out.println(json.get("openid"));
//登录成功,后台获取该账号是否注册过
if ((json.get("openid"))!=null) {
List<Map<String, Object>> list = wx_wxmessageService.selUserById(json.get("openid")+"");
if (list.size()>0) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date adddate=sdf.parse(list.get(0).get("adddate")+"");
wx_wxmessage.setId(Integer.parseInt(list.get(0).get("id")+""));
wx_wxmessage.setOpenid(list.get(0).get("openid")+"");
wx_wxmessage.setSession_key(list.get(0).get("session_key")+"");
wx_wxmessage.setAdddate(adddate);
wx_wxmessage.setLastdate(new Date());
wx_wxmessage.setWxname(list.get(0).get("wxname")+"");
wx_wxmessageService.upd(wx_wxmessage);
/*session.setAttribute("3rd_session", (list.get(0).get("session_key")+"")+(list.get(0).get("openid")+""));*/
session.setAttribute("rd_session", (list.get(0).get("session_key")+""));
/*Map<String, Object> ttMap = new HashMap<String, Object>();
ttMap.put("rd_session", session.getAttribute("rd_session")+"");
String jsonStr = JSONArray.fromObject(ttMap).toString();
JSONArray jsonrr = JSONArray.fromObject(jsonStr); */
System.out.println("***********");
System.out.println(session.getAttribute("rd_session")+"");
pageOut(response, session.getAttribute("rd_session")+"");
}else {
wx_wxmessage.setOpenid(json.get("openid")+"");
wx_wxmessage.setSession_key(json.get("session_key")+"");
wx_wxmessage.setAdddate(new Date());
wx_wxmessage.setLastdate(new Date());
wx_wxmessageService.add(wx_wxmessage);
System.out.println((json.get("session_key")+"")+(json.get("openid")+""));
/*session.setAttribute("3rd_session", (json.get("session_key")+"")+(json.get("openid")+""));*/
session.setAttribute("rd_session", (json.get("session_key")+""));
/*Map<String, Object> ttMap = new HashMap<String, Object>();
ttMap.put("rd_session", session.getAttribute("rd_session")+"");
String jsonStr = JSONArray.fromObject(ttMap).toString();
JSONArray jsonrr = JSONArray.fromObject(jsonStr); */
pageOut(response, session.getAttribute("rd_session")+"");
}
} else {
pageOut(response, "0");
}
/* HttpGet httpGet = new HttpGet(urlFormat);
response =httpClient.execute(httpGet);
HttpEntity responseEntity = response.getEntity();
System.out.println("************************");
System.out.println(EntityUtils.toString(responseEntity));*/
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
pageOut(response, "0");
}
}
private void pageOut(HttpServletResponse response, String msg) {
response.setContentType("textml");
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException ex) {
String message = "系统异常,错误码#4100035";
}
out.print(msg);
out.close();
}
public wx_wxmessage getWx_wxmessage() {
return wx_wxmessage;
}
public void setWx_wxmessage(wx_wxmessage wx_wxmessage) {
this.wx_wxmessage = wx_wxmessage;
}
public wx_wxmessageService getWx_wxmessageService() {
return wx_wxmessageService;
}
public void setWx_wxmessageService(wx_wxmessageService wx_wxmessageService) {
this.wx_wxmessageService = wx_wxmessageService;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}