IndexController.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.uas.service.donate.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.uas.service.donate.api.WxPayApi;
  4. import com.uas.service.donate.config.WxConfig;
  5. import com.uas.service.donate.model.Project;
  6. import com.uas.service.donate.model.WechatUserInfo;
  7. import com.uas.service.donate.service.CarouselService;
  8. import com.uas.service.donate.service.ProjectRecodeService;
  9. import com.uas.service.donate.service.ProjectService;
  10. import com.uas.service.donate.util.WechatConnector;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.data.domain.Page;
  15. import org.springframework.data.domain.PageRequest;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.ui.Model;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestMethod;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.io.IOException;
  24. import java.net.URLEncoder;
  25. import java.util.List;
  26. @Controller
  27. public class IndexController {
  28. @Autowired
  29. private ProjectRecodeService projectRecodeService;
  30. @Autowired
  31. private ProjectService projectService;
  32. @Autowired
  33. private CarouselService carouselService;
  34. @Autowired
  35. protected WxPayApi wxPayApi;
  36. private Logger logger = LoggerFactory.getLogger(IndexController.class);
  37. @RequestMapping(value = "/", method = RequestMethod.GET)
  38. public String returnaDefault(HttpServletRequest request, HttpServletResponse response) throws IOException {
  39. String code = request.getParameter("code");
  40. String state = request.getParameter("state");
  41. String ua = request.getHeader("user-agent").toLowerCase();
  42. if (ua.indexOf("micromessenger") >= 0) {// 是微信浏览器
  43. Object openId = request.getSession().getAttribute("openId");
  44. if (openId != null) {
  45. //有openid 已授权
  46. logger.info("有openid 已授权");
  47. return "forward:/index";
  48. } else {
  49. //需要授权
  50. logger.info("公众号获取:--需要授权");
  51. //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";
  52. String url = WechatConnector.createAuthorizeUrl(WxConfig.oauthUserScope, wxPayApi.getConfig().getAppId(), URLEncoder.encode("http://lj.ubtob.com", "UTF-8"));
  53. response.sendRedirect(url);
  54. return null;
  55. }
  56. }
  57. if (code != null) {
  58. //授权回调
  59. logger.info("公众号获取,授权回调:authCode=" + code + ",state=" + state);
  60. String openId = null;
  61. try {
  62. if ("snsapi_userinfo".equals(WxConfig.oauthUserScope)) {
  63. WechatUserInfo wechatUserInfo= WechatConnector.getOauthUser(code, wxPayApi.getConfig().getAppId(), WxConfig.APPSECRET); //TODO APPSECRET
  64. logger.info("微信用户授权信息:" + wechatUserInfo.toString());
  65. openId = wechatUserInfo.getOpenId();
  66. } else if ("snsapi_base".equals(WxConfig.oauthUserScope)) {
  67. JSONObject tokenJson = WechatConnector.getAccessToken(code, wxPayApi.getConfig().getAppId(), WxConfig.APPSECRET);//TODO APPSECRET
  68. openId = tokenJson.getString("openid");
  69. }
  70. //openId = wxPayApi.authcode2Openid(code);
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. request.getSession().setAttribute("openId", openId);
  75. logger.info("当前微信用户openId:" + openId);
  76. }
  77. /*User user = SystemSession.getUser();
  78. if (ua.indexOf("micromessenger") >= 0) {// 是微信浏览器
  79. if (user != null && user.getWxOpenId() != null) {
  80. //有用户 有openid 已授权
  81. logger.info("公众号获取:--有用户 有openid 已授权");
  82. return "forward:/index";
  83. } else if (user != null && user.getWxOpenId() == null && code == null) {
  84. //有用户 无openid 需要授权
  85. logger.info("公众号获取:--有用户 无openid 需要授权");
  86. //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";
  87. String url = WechatConnector.createAuthorizeUrl(WxConfig.oauthUserScope, wxPayApi.getConfig().getAppId(), URLEncoder.encode("http://lj.ubtob.com", "UTF-8"));
  88. response.sendRedirect(url);
  89. } else if (user == null && code == null) {
  90. //需要授权
  91. logger.info("公众号获取:--需要授权");
  92. //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";
  93. String url = WechatConnector.createAuthorizeUrl(WxConfig.oauthUserScope, wxPayApi.getConfig().getAppId(), URLEncoder.encode("http://lj.ubtob.com", "UTF-8"));
  94. response.sendRedirect(url);
  95. }
  96. }
  97. if (code != null) {
  98. //授权回调
  99. logger.info("公众号获取,授权回调:authCode=" + code + ",state=" + state);
  100. if (user == null) {
  101. user = new User();
  102. }
  103. String openId = null;
  104. try {
  105. if ("snsapi_userinfo".equals(WxConfig.oauthUserScope)) {
  106. WechatUserInfo wechatUserInfo= WechatConnector.getOauthUser(code, wxPayApi.getConfig().getAppId(), WxConfig.APPSECRET); //TODO APPSECRET
  107. logger.info("微信用户授权信息:" + wechatUserInfo.toString());
  108. openId = wechatUserInfo.getOpenId();
  109. } else if ("snsapi_base".equals(WxConfig.oauthUserScope)) {
  110. JSONObject tokenJson = WechatConnector.getAccessToken(code, wxPayApi.getConfig().getAppId(), WxConfig.APPSECRET);//TODO APPSECRET
  111. openId = tokenJson.getString("openid");
  112. }
  113. //openId = wxPayApi.authcode2Openid(code);
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. }
  117. user.setWxOpenId(openId);
  118. logger.info("当前用户信息:" + user.toString());
  119. SystemSession.setUser(user);
  120. }*/
  121. return "forward:/index";
  122. }
  123. /**
  124. * 返回首页index.ftl的页面
  125. */
  126. @RequestMapping(value = "/index",method = RequestMethod.GET)
  127. public String returnIndex(Model model, @RequestParam(value = "page", defaultValue = "0") int page,
  128. @RequestParam(value = "size", defaultValue = "7") int size,
  129. @RequestParam(value = "area", defaultValue = "全部") String area,
  130. @RequestParam(value = "status", defaultValue = "全部") String status,
  131. @RequestParam(value = "search",required = false)String search,
  132. @RequestParam(value = "useFor",defaultValue ="donate")String useFor) {
  133. PageRequest pageable=new PageRequest(page,size);
  134. //得到历史参与总人数
  135. Long historyPerson = projectRecodeService.historyPerson();
  136. //得到历史参与总捐款额
  137. Double totality = projectRecodeService.totality();
  138. //得到轮播图
  139. List<JSONObject> carouselList=carouselService.getCarousels(useFor);
  140. //动态获取所有领域分类
  141. List<String> areaList=projectService.allArea();
  142. //展示不同的状态列表
  143. if (!"全部".equals(status) && "全部".equals(area)) {
  144. if("进行中".equals(status)){
  145. Page<Project> projects=projectService.findInProcess(pageable);
  146. model.addAttribute("page", projects);
  147. }
  148. if("已结束".equals(status)){
  149. Page<Project> projects= projectService.findEndProject(pageable);
  150. model.addAttribute("page", projects);
  151. }
  152. } else if ("全部".equals(status) && !"全部".equals(area)) {
  153. Page<Project> projects = projectService.findPageArea(area,pageable);
  154. model.addAttribute("page", projects);
  155. } else if ("全部".equals(status) && "全部".equals(area)) {
  156. Page<Project> projects = projectService.findAll(pageable);
  157. model.addAttribute("page", projects);
  158. } else {
  159. if("进行中".equals(status)){
  160. Page<Project> projects= projectService.findInProcessArea(pageable,area);
  161. model.addAttribute("page",projects);
  162. }else if("已结束".equals(status)){
  163. Page<Project> projects= projectService.findEndProjectArea(pageable,area);
  164. model.addAttribute("page",projects);
  165. }
  166. }
  167. if(search!=null){
  168. Page<Project> projects=projectService.searchPage(pageable,search);
  169. model.addAttribute("page",projects);
  170. }
  171. model.addAttribute("areaList",areaList);
  172. model.addAttribute("historyPerson", historyPerson);
  173. model.addAttribute("totality", totality);
  174. model.addAttribute("carouselList",carouselList);
  175. return "index";
  176. }
  177. }