package com.uas.service.donate.controller; import com.alibaba.fastjson.JSONObject; import com.uas.service.donate.api.WxPayApi; import com.uas.service.donate.config.WxConfig; import com.uas.service.donate.core.model.Status; import com.uas.service.donate.model.Project; import com.uas.service.donate.model.WechatUserInfo; import com.uas.service.donate.service.CarouselService; import com.uas.service.donate.service.ProjectRecodeService; import com.uas.service.donate.service.ProjectService; import com.uas.service.donate.util.WechatConnector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.List; @Controller public class IndexController { @Autowired private ProjectRecodeService projectRecodeService; @Autowired private ProjectService projectService; @Autowired private CarouselService carouselService; @Autowired protected WxPayApi wxPayApi; private Logger logger = LoggerFactory.getLogger(IndexController.class); @RequestMapping(value = "/", method = RequestMethod.GET) public String returnaDefault(HttpServletRequest request, HttpServletResponse response) throws IOException { String code = request.getParameter("code"); String state = request.getParameter("state"); String ua = request.getHeader("user-agent").toLowerCase(); if (ua.indexOf("micromessenger") >= 0) {// 是微信浏览器 Object openId = request.getSession().getAttribute("openId"); if (openId != null) { //有openid 已授权 logger.info("有openid 已授权"); return "forward:/index"; } else { //需要授权 logger.info("公众号获取:--需要授权"); //String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxPayApi.getConfig().getAppId() + "&redirect_uri=" + URLEncoder.encode("http://lj.ubtob.com/", "UTF-8") + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; String url = WechatConnector.createAuthorizeUrl(WxConfig.oauthUserScope, wxPayApi.getConfig().getAppId(), URLEncoder.encode("http://lj.ubtob.com", "UTF-8")); response.sendRedirect(url); return null; } } if (code != null) { //授权回调 logger.info("公众号获取,授权回调:authCode=" + code + ",state=" + state); String openId = null; try { if ("snsapi_userinfo".equals(WxConfig.oauthUserScope)) { WechatUserInfo wechatUserInfo= WechatConnector.getOauthUser(code, wxPayApi.getConfig().getAppId(), WxConfig.APPSECRET); //TODO APPSECRET logger.info("微信用户授权信息:" + wechatUserInfo.toString()); openId = wechatUserInfo.getOpenId(); } else if ("snsapi_base".equals(WxConfig.oauthUserScope)) { JSONObject tokenJson = WechatConnector.getAccessToken(code, wxPayApi.getConfig().getAppId(), WxConfig.APPSECRET);//TODO APPSECRET openId = tokenJson.getString("openid"); } //openId = wxPayApi.authcode2Openid(code); } catch (Exception e) { e.printStackTrace(); } request.getSession().setAttribute("openId", openId); logger.info("当前微信用户openId:" + openId); } /*User user = SystemSession.getUser(); if (ua.indexOf("micromessenger") >= 0) {// 是微信浏览器 if (user != null && user.getWxOpenId() != null) { //有用户 有openid 已授权 logger.info("公众号获取:--有用户 有openid 已授权"); return "forward:/index"; } else if (user != null && user.getWxOpenId() == null && code == null) { //有用户 无openid 需要授权 logger.info("公众号获取:--有用户 无openid 需要授权"); //String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxPayApi.getConfig().getAppId() + "&redirect_uri=" + URLEncoder.encode("http://lj.ubtob.com/", "UTF-8") + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; String url = WechatConnector.createAuthorizeUrl(WxConfig.oauthUserScope, wxPayApi.getConfig().getAppId(), URLEncoder.encode("http://lj.ubtob.com", "UTF-8")); response.sendRedirect(url); } else if (user == null && code == null) { //需要授权 logger.info("公众号获取:--需要授权"); //String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wxPayApi.getConfig().getAppId() + "&redirect_uri=" + URLEncoder.encode("http://lj.ubtob.com/", "UTF-8") + "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; String url = WechatConnector.createAuthorizeUrl(WxConfig.oauthUserScope, wxPayApi.getConfig().getAppId(), URLEncoder.encode("http://lj.ubtob.com", "UTF-8")); response.sendRedirect(url); } } if (code != null) { //授权回调 logger.info("公众号获取,授权回调:authCode=" + code + ",state=" + state); if (user == null) { user = new User(); } String openId = null; try { if ("snsapi_userinfo".equals(WxConfig.oauthUserScope)) { WechatUserInfo wechatUserInfo= WechatConnector.getOauthUser(code, wxPayApi.getConfig().getAppId(), WxConfig.APPSECRET); //TODO APPSECRET logger.info("微信用户授权信息:" + wechatUserInfo.toString()); openId = wechatUserInfo.getOpenId(); } else if ("snsapi_base".equals(WxConfig.oauthUserScope)) { JSONObject tokenJson = WechatConnector.getAccessToken(code, wxPayApi.getConfig().getAppId(), WxConfig.APPSECRET);//TODO APPSECRET openId = tokenJson.getString("openid"); } //openId = wxPayApi.authcode2Openid(code); } catch (Exception e) { e.printStackTrace(); } user.setWxOpenId(openId); logger.info("当前用户信息:" + user.toString()); SystemSession.setUser(user); }*/ return "forward:/index"; } /** * 返回首页index.ftl的页面 */ @RequestMapping(value = "/index",method = RequestMethod.GET) public String returnIndex(Model model, @RequestParam(value = "page", defaultValue = "0") int page, @RequestParam(value = "size", defaultValue = "7") int size, @RequestParam(value = "area", defaultValue = "全部") String area, @RequestParam(value = "status", defaultValue = "全部") String status, @RequestParam(value = "search",defaultValue = "")String search, @RequestParam(value = "useFor",defaultValue ="donate")String useFor) { PageRequest pageable=new PageRequest(page,size); //查询项目中所有领域分类 List allArea=projectService.allArea(); //得到历史参与总人数 Long historyPerson = projectRecodeService.historyPerson(); //得到历史参与总捐款额 Double totality = projectRecodeService.totality(); //得到轮播图 List carouselList=carouselService.getCarousels(useFor); //动态获取所有领域分类 List areaList = projectService.allArea(); //展示不同的状态列表 if (!Status.ALL.getPhrase().equals(status) && Status.ALL.getPhrase().equals(area)) { if (Status.LOADING.getPhrase().equals(status)) { Page projects = projectService.findInProcess(pageable); model.addAttribute("page", projects); } if (Status.END.getPhrase().equals(status)) { Page projects = projectService.findEndProject(pageable); model.addAttribute("page", projects); } } else if (Status.ALL.getPhrase().equals(status) && !Status.ALL.getPhrase().equals(area)) { Page projects = projectService.findPageArea(area, pageable); model.addAttribute("page", projects); } else if (Status.ALL.getPhrase().equals(status) && Status.ALL.getPhrase().equals(area)) { Page projects = projectService.findAll(pageable); model.addAttribute("page", projects); } else { if (Status.ALL.getPhrase().equals(status)) { Page projects = projectService.findInProcessArea(pageable, area); model.addAttribute("page", projects); } else if (Status.END.getPhrase().equals(status)) { Page projects = projectService.findEndProjectArea(pageable, area); model.addAttribute("page", projects); } } if(!"".equals(search)){ Page projects=projectService.searchPage(pageable,search); System.out.println("总条数"+projects.getTotalElements()); model.addAttribute("page",projects); } model.addAttribute("allArea",allArea); model.addAttribute("areaList",areaList); model.addAttribute("historyPerson", historyPerson); model.addAttribute("totality", totality); model.addAttribute("carouselList",carouselList); return "index"; } }