|
- package com.lecoo.kjg.web.controller;
-
- import com.jeesite.common.codec.DesUtils;
- import com.jeesite.common.config.Global;
- import com.jeesite.common.lang.ObjectUtils;
- import com.jeesite.common.lang.StringUtils;
- import com.jeesite.common.shiro.authc.FormToken;
- import com.jeesite.common.web.http.ServletUtils;
- import com.jeesite.modules.sys.entity.User;
- import com.jeesite.modules.sys.service.OfficeService;
- import com.jeesite.modules.sys.utils.UserUtils;
- import com.lecoo.kjg.web.config.RecordStatus;
- import com.lecoo.kjg.web.config.WxMpProperties;
- import com.lecoo.kjg.web.sys.dao.UserExtraDao;
- import com.lecoo.kjg.web.sys.dao.VisitRecordsDao;
- import com.lecoo.kjg.web.sys.entity.VisitRecords;
- import com.lecoo.kjg.web.sys.utils.RedisUtils;
- import com.lecoo.kjg.web.sys.utils.DateUtils;
- import lombok.AllArgsConstructor;
- import me.chanjar.weixin.common.api.WxConsts;
- import me.chanjar.weixin.common.bean.WxJsapiSignature;
- import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
- import me.chanjar.weixin.common.error.WxErrorException;
- import me.chanjar.weixin.mp.api.WxMpService;
- import org.apache.http.HttpResponse;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.*;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.io.IOException;
- import java.security.NoSuchAlgorithmException;
- import java.util.ArrayList;
- import java.util.List;
-
- @AllArgsConstructor
- @Controller
- @RequestMapping("${frontPath}/wx")
- public class WxFrontController extends WxBaseController {
- private final WxMpService wxService;
-
- @Autowired
- private UserExtraDao userDao;
-
- @Autowired
- private WxMpProperties wxMpProperties;
-
- @Autowired
- private VisitRecordsDao visitRecordsDao;
-
- @Autowired
- private RedisUtils redisUtils;
-
- @Autowired
- private OfficeService officeService;
-
- @RequestMapping(value = "/access-token", method = RequestMethod.GET)
- public void accessToken(@RequestParam("code") String code, @RequestParam(value = "redirect", required = false) String redirect, HttpServletRequest request, HttpServletResponse response) {
- try {
- String baseUrl = Global.getConfig("sys.baseUrl");
- WxOAuth2AccessToken wot = this.wxService.getOAuth2Service().getAccessToken(code);
- String openid = wot.getOpenId();
-
- String secretKey = Global.getConfig("shiro.loginSubmit.secretKey");
-
- String userType = "0";
- /*if (StringUtils.isNotEmpty(redirect) && !("null".equals(redirect))) {
- response.sendRedirect(redirect+"/"+encodeStr);
- } else {
- response.sendRedirect(Global.getFrontPath()+"/ticket/topIndex/"+encodeStr);
- }*/
- // userCode = DesUtils.decode(userCode, secretKey);
-
-
- // userCode = DesUtils.decode(userCode, secretKey);
-
- User user = this.userDao.findByWxOpenid(openid, userType);
- // UserUtils.getSubject().logout();
- if (ObjectUtils.anyNotNull(user)) {//如果有对应员工
-
- request.getSession().setAttribute("openId", userType+openid);
-
- if (StringUtils.isNotEmpty(redirect)) {
-
- response.sendRedirect(redirect);
- // ServletUtils.redirectUrl(request, response, redirect);
- } else {
- String openidStr = DesUtils.encode(userType+openid,secretKey);
- response.sendRedirect(baseUrl+"/account2/info/"+openidStr);
- // ServletUtils.redirectUrl(request, response, baseUrl+"/account2/info");
- }
-
- } else {
-
- openid = DesUtils.encode(openid, secretKey);;
-
- ServletUtils.redirectUrl(request, response, baseUrl+"/account2/regUser?type="+userType+"&openid=" + openid);
-
-
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
-
- @RequestMapping(value = "/oauth2", method = RequestMethod.GET)
- public void oauth2(@RequestParam(value = "redirect", required = false) String redirect,@RequestParam(value = "type", required = false) String type, HttpServletResponse response) {
-
- String baseUrl = Global.getConfig("sys.baseUrl");
- String url = this.wxService.getOAuth2Service().buildAuthorizationUrl(baseUrl+"/f/wx/access-token?redirect="+redirect, WxConsts.OAuth2Scope.SNSAPI_BASE, null);
- try {
- response.sendRedirect(url);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- }
-
- @RequestMapping(value = "/oauth3", method = RequestMethod.GET)
- public void oauth3(HttpServletResponse response) {
- String baseUrl = Global.getConfig("sys.baseUrl");
- String url = this.wxService.getOAuth2Service().buildAuthorizationUrl(baseUrl+"/f/wx/access-token2", WxConsts.OAuth2Scope.SNSAPI_BASE, null);
- try {
- response.sendRedirect(url);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- }
-
- @RequestMapping(value = "/today")
- public String today(Model model){
- List<VisitRecords> list = this.visitRecordsDao.findListToday(RecordStatus.getRecordStatusEstatePass());
- List<VisitRecords> haslist = new ArrayList<>();
- List<VisitRecords> nolist = new ArrayList<>();
- list.forEach((VisitRecords v)->{
- if (1 == v.getHasCar()) {
- haslist.add(v);
- } else {
- nolist.add(v);
- }
- });
-
-
- model.addAttribute("hasList", haslist);
- model.addAttribute("noList", nolist);
- model.addAttribute("list", list);
-
- return "wx/todayVisitor";
- }
-
-
-
- @Autowired
- private WxMpService wxMpService;
-
- @GetMapping("/config")
- @ResponseBody
- public WxJsapiSignature config(@Param("url") String url, HttpResponse httpResponse) throws IOException, NoSuchAlgorithmException {
- // 此时便可打开微信的摄像头以及相册
- WxJsapiSignature wxJsapiSignature = null;
- try {
- String secretKey = Global.getConfig("shiro.loginSubmit.secretKey");
- url = DesUtils.decode(url, secretKey);
-
- System.err.println(StringUtils.substring(url, 0, url.indexOf("?")));
- wxJsapiSignature = wxMpService.createJsapiSignature(url);
- return wxJsapiSignature;
- } catch (WxErrorException e) {
- e.printStackTrace();
- httpResponse.setStatusCode(500);
- return null;
- }
-
- }
-
-
- }
|