package cn.com.dzqc.controller;
import cn.com.dzqc.entity.QcEmployeeInformation;
import cn.com.dzqc.service.QcEmployeeInformationService;
import cn.com.dzqc.util.HttpGetUtil;
import cn.com.dzqc.util.SystemConstants;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/weixin")
public class Weixin {
@Autowired
private QcEmployeeInformationService emplService;
@RequestMapping("/frist")
public void bianma(HttpServletRequest request, HttpServletResponse response)
throws IOException {
System.out.println("进入-----------");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String redirect_uri = URLEncoder.encode("http://m.dzqcedu.com/salary/weixin/openid", "UTF-8");
StringBuffer url = new StringBuffer("https://open.weixin.qq.com/connect/oauth2/authorize?redirect_uri=" + redirect_uri +
"&appid=" + "开发者IDxxxxxxxxxxxxxxxx" + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
System.out.println("重定向-----------");
response.sendRedirect(url.toString());
}
@RequestMapping("/openid")
public ModelAndView huidai(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
QcEmployeeInformation qcEmployeeInformation=null;
response.setContentType("text/html");
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
/*获取openid*/
String attribute = (String) session.getAttribute("openid");
if (attribute != null) {
/*查询用户表是否有这个openID*/
qcEmployeeInformation = this.emplService.selByOpenid(attribute);
} else {
response.setCharacterEncoding("UTF-8");
String code = request.getParameter("code");
Map params = new HashMap();
params.put("secret", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
params.put("appid", "xxxxxxxxxxxxxxxxx");
params.put("grant_type", "authorization_code");
params.put("code", code);
String result = HttpGetUtil.httpRequestToString(
"https://api.weixin.qq.com/sns/oauth2/access_token", params);
JSONObject jsonObject = JSONObject.fromObject(result);
String openid = jsonObject.get("openid").toString();
System.out.println("得到的openid为:" + openid);
if(openid==null|| openid.equals("")){
ModelAndView m = new ModelAndView();
m.setViewName("index");
return m;
}
qcEmployeeInformation = this.emplService.selByOpenid(openid);
session.setAttribute("openid", openid);
}
ModelAndView m = new ModelAndView();
if (qcEmployeeInformation != null) {
String username=qcEmployeeInformation.getName();
String idCord=qcEmployeeInformation.getIdCard();
m.addObject("username", username);
m.addObject("idCard", idCord);
m.setViewName("zhbg_home");
return m;
}
System.out.println("//"+session.getAttribute("openid"));
m.setViewName("index");
return m;
}
}