You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

188 line
6.8 KiB

  1. package com.lecoo.kjg.web.controller;
  2. import com.jeesite.common.codec.DesUtils;
  3. import com.jeesite.common.config.Global;
  4. import com.jeesite.common.lang.ObjectUtils;
  5. import com.jeesite.common.lang.StringUtils;
  6. import com.jeesite.common.shiro.authc.FormToken;
  7. import com.jeesite.common.web.http.ServletUtils;
  8. import com.jeesite.modules.sys.entity.User;
  9. import com.jeesite.modules.sys.service.OfficeService;
  10. import com.jeesite.modules.sys.utils.UserUtils;
  11. import com.lecoo.kjg.web.config.RecordStatus;
  12. import com.lecoo.kjg.web.config.WxMpProperties;
  13. import com.lecoo.kjg.web.sys.dao.UserExtraDao;
  14. import com.lecoo.kjg.web.sys.dao.VisitRecordsDao;
  15. import com.lecoo.kjg.web.sys.entity.VisitRecords;
  16. import com.lecoo.kjg.web.sys.utils.RedisUtils;
  17. import com.lecoo.kjg.web.sys.utils.DateUtils;
  18. import lombok.AllArgsConstructor;
  19. import me.chanjar.weixin.common.api.WxConsts;
  20. import me.chanjar.weixin.common.bean.WxJsapiSignature;
  21. import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
  22. import me.chanjar.weixin.common.error.WxErrorException;
  23. import me.chanjar.weixin.mp.api.WxMpService;
  24. import org.apache.http.HttpResponse;
  25. import org.apache.ibatis.annotations.Param;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Controller;
  28. import org.springframework.ui.Model;
  29. import org.springframework.web.bind.annotation.*;
  30. import javax.servlet.http.HttpServletRequest;
  31. import javax.servlet.http.HttpServletResponse;
  32. import javax.servlet.http.HttpSession;
  33. import java.io.IOException;
  34. import java.security.NoSuchAlgorithmException;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. @AllArgsConstructor
  38. @Controller
  39. @RequestMapping("${frontPath}/wx")
  40. public class WxFrontController extends WxBaseController {
  41. private final WxMpService wxService;
  42. @Autowired
  43. private UserExtraDao userDao;
  44. @Autowired
  45. private WxMpProperties wxMpProperties;
  46. @Autowired
  47. private VisitRecordsDao visitRecordsDao;
  48. @Autowired
  49. private RedisUtils redisUtils;
  50. @Autowired
  51. private OfficeService officeService;
  52. @RequestMapping(value = "/access-token", method = RequestMethod.GET)
  53. public void accessToken(@RequestParam("code") String code, @RequestParam(value = "redirect", required = false) String redirect, HttpServletRequest request, HttpServletResponse response) {
  54. try {
  55. String baseUrl = Global.getConfig("sys.baseUrl");
  56. WxOAuth2AccessToken wot = this.wxService.getOAuth2Service().getAccessToken(code);
  57. String openid = wot.getOpenId();
  58. String secretKey = Global.getConfig("shiro.loginSubmit.secretKey");
  59. String userType = "0";
  60. /*if (StringUtils.isNotEmpty(redirect) && !("null".equals(redirect))) {
  61. response.sendRedirect(redirect+"/"+encodeStr);
  62. } else {
  63. response.sendRedirect(Global.getFrontPath()+"/ticket/topIndex/"+encodeStr);
  64. }*/
  65. // userCode = DesUtils.decode(userCode, secretKey);
  66. // userCode = DesUtils.decode(userCode, secretKey);
  67. User user = this.userDao.findByWxOpenid(openid, userType);
  68. // UserUtils.getSubject().logout();
  69. if (ObjectUtils.anyNotNull(user)) {//如果有对应员工
  70. request.getSession().setAttribute("openId", userType+openid);
  71. if (StringUtils.isNotEmpty(redirect)) {
  72. response.sendRedirect(redirect);
  73. // ServletUtils.redirectUrl(request, response, redirect);
  74. } else {
  75. String openidStr = DesUtils.encode(userType+openid,secretKey);
  76. response.sendRedirect(baseUrl+"/account2/info/"+openidStr);
  77. // ServletUtils.redirectUrl(request, response, baseUrl+"/account2/info");
  78. }
  79. } else {
  80. openid = DesUtils.encode(openid, secretKey);;
  81. ServletUtils.redirectUrl(request, response, baseUrl+"/account2/regUser?type="+userType+"&openid=" + openid);
  82. }
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. @RequestMapping(value = "/oauth2", method = RequestMethod.GET)
  88. public void oauth2(@RequestParam(value = "redirect", required = false) String redirect,@RequestParam(value = "type", required = false) String type, HttpServletResponse response) {
  89. String baseUrl = Global.getConfig("sys.baseUrl");
  90. String url = this.wxService.getOAuth2Service().buildAuthorizationUrl(baseUrl+"/f/wx/access-token?redirect="+redirect, WxConsts.OAuth2Scope.SNSAPI_BASE, null);
  91. try {
  92. response.sendRedirect(url);
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. @RequestMapping(value = "/oauth3", method = RequestMethod.GET)
  98. public void oauth3(HttpServletResponse response) {
  99. String baseUrl = Global.getConfig("sys.baseUrl");
  100. String url = this.wxService.getOAuth2Service().buildAuthorizationUrl(baseUrl+"/f/wx/access-token2", WxConsts.OAuth2Scope.SNSAPI_BASE, null);
  101. try {
  102. response.sendRedirect(url);
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. }
  107. @RequestMapping(value = "/today")
  108. public String today(Model model){
  109. List<VisitRecords> list = this.visitRecordsDao.findListToday(RecordStatus.getRecordStatusEstatePass());
  110. List<VisitRecords> haslist = new ArrayList<>();
  111. List<VisitRecords> nolist = new ArrayList<>();
  112. list.forEach((VisitRecords v)->{
  113. if (1 == v.getHasCar()) {
  114. haslist.add(v);
  115. } else {
  116. nolist.add(v);
  117. }
  118. });
  119. model.addAttribute("hasList", haslist);
  120. model.addAttribute("noList", nolist);
  121. model.addAttribute("list", list);
  122. return "wx/todayVisitor";
  123. }
  124. @Autowired
  125. private WxMpService wxMpService;
  126. @GetMapping("/config")
  127. @ResponseBody
  128. public WxJsapiSignature config(@Param("url") String url, HttpResponse httpResponse) throws IOException, NoSuchAlgorithmException {
  129. // 此时便可打开微信的摄像头以及相册
  130. WxJsapiSignature wxJsapiSignature = null;
  131. try {
  132. String secretKey = Global.getConfig("shiro.loginSubmit.secretKey");
  133. url = DesUtils.decode(url, secretKey);
  134. System.err.println(StringUtils.substring(url, 0, url.indexOf("?")));
  135. wxJsapiSignature = wxMpService.createJsapiSignature(url);
  136. return wxJsapiSignature;
  137. } catch (WxErrorException e) {
  138. e.printStackTrace();
  139. httpResponse.setStatusCode(500);
  140. return null;
  141. }
  142. }
  143. }