Blame view

src/main/java/com/zteits/oa/report/web/OAuthController.java 6.01 KB
900781d3   王富生   提交
1
2
  package com.zteits.oa.report.web;
  
5e8d9026   王富生   提交
3
4
5
  import java.util.ArrayList;
  import java.util.List;
  
900781d3   王富生   提交
6
7
8
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  
4b2bb18a   xiejianpeng   登录权限处理
9
10
  import com.xiaoleilu.hutool.util.CollectionUtil;
  import com.zteits.oa.api.base.bean.PageBean;
900781d3   王富生   提交
11
12
13
14
15
16
17
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.util.StringUtils;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
900781d3   王富生   提交
18
19
  import org.springframework.web.bind.annotation.RestController;
  
5e8d9026   王富生   提交
20
  import com.alibaba.fastjson.JSONObject;
900781d3   王富生   提交
21
22
23
24
25
26
27
  import com.zteits.oa.api.base.bean.BizResult;
  import com.zteits.oa.api.base.constants.ErrorType;
  import com.zteits.oa.api.base.constants.SessionEnum;
  import com.zteits.oa.api.dto.asraop.AsraOpDTO;
  import com.zteits.oa.api.dto.asraop.LoginOathRes;
  import com.zteits.oa.api.dto.asraop.param.AsraOpQueryReq;
  import com.zteits.oa.api.dto.asraop.param.LoginOauthReq;
5e8d9026   王富生   提交
28
  import com.zteits.oa.api.service.report.query.AsraOpQueryService;
0b156ed7   王富生   提交
29
  import com.zteits.oa.report.vo.OAuthResult;
900781d3   王富生   提交
30
31
32
33
34
35
36
37
38
39
40
41
  import com.zteits.oa.util.MD5Utils;
  
  import io.swagger.annotations.Api;
  import io.swagger.annotations.ApiOperation;
  
  @Api("用户登录授权")
  @RestController
  @RequestMapping("/oauth")
  public class OAuthController {
  	 private static final Logger logger = LoggerFactory.getLogger(OAuthController.class);
  	 
  	 @Autowired
9ce77292   xiejianpeng   员工管理
42
  	 private AsraOpQueryService asraOpQueryService;
900781d3   王富生   提交
43
44
45
46
47
48
  	 
  	 @Autowired
  	 private HttpServletRequest request;
  	 
  	 @ApiOperation("用户登录")
  	 @PostMapping("/login")
0b156ed7   王富生   提交
49
50
  	 public OAuthResult<LoginOathRes> login(@RequestBody LoginOauthReq req ) throws Exception {
  		 OAuthResult<LoginOathRes> result =  this._login(req);
900781d3   王富生   提交
51
52
53
54
55
56
57
58
  		 return result;
  	 }
  	 /**
  	  * 登陆验证.<br/>
  	  * @param req
  	  * @return
  	  * 2018731  wangfs.<br/>
  	  */
0b156ed7   王富生   提交
59
60
  	 private OAuthResult<LoginOathRes> _login(LoginOauthReq req ){
  		 OAuthResult<LoginOathRes> result = new OAuthResult<LoginOathRes>(false);
900781d3   王富生   提交
61
62
63
64
65
  		 LoginOathRes loginOathRes = new  LoginOathRes();
  		 String loginCode = req.getLoginCode();
  		 String passWord = req.getPassWord();
  		 if(StringUtils.isEmpty(loginCode) || StringUtils.isEmpty(passWord)) {
          	  logger.info("校验登录信息,用户名 或者 登录密码为空!");
0b156ed7   王富生   提交
66
          	  result.setErrorType(ErrorType.PARAMM_NULL, "用户名 或者 登录密码为空");
900781d3   王富生   提交
67
68
69
70
71
72
  	      }
  		 AsraOpQueryReq asraOpQueryRe = new AsraOpQueryReq();
  		 AsraOpDTO asraOpDTO = new AsraOpDTO();
  		 boolean isCheckSuccess = false;
  		 //1.判断登录账号/密码
  		 asraOpQueryRe.setLoginCode(loginCode);
9ce77292   xiejianpeng   员工管理
73
  		 BizResult<AsraOpDTO> asraOpReult = asraOpQueryService.queryAsraOp(asraOpQueryRe);
900781d3   王富生   提交
74
75
76
77
  		 if(asraOpReult != null && asraOpReult.getData() != null){
  			 asraOpDTO = asraOpReult.getData();
  			 if(StringUtils.isEmpty(asraOpDTO.getLoginCode())){
  				 logger.info("{}登录账号不存在",loginCode);
0b156ed7   王富生   提交
78
  	        	  result.setErrorType(ErrorType.AUTH_LOGIN_ERROR, "登录账号不存在!");
900781d3   王富生   提交
79
80
81
  			 }else{
  				 if(!asraOpDTO.getLoginPassword().equalsIgnoreCase(MD5Utils.enMD5(passWord))){
  					 logger.info("{}登录账号输入的密码不正确",loginCode);
0b156ed7   王富生   提交
82
  		        	  result.setErrorType(ErrorType.AUTH_PASS_ERROR, "登录密码不匹配!");
900781d3   王富生   提交
83
84
85
86
87
88
  				 }else{
  					 isCheckSuccess = true;
  				 }
  			 }
  			 
  		 }else{
0b156ed7   王富生   提交
89
  			 result.setErrorType(ErrorType.BIZ_ERROR, "用户登录失败");
900781d3   王富生   提交
90
91
92
93
  		 }
  		 
  		 
  		 if(isCheckSuccess){
5e8d9026   王富生   提交
94
95
96
  			 
  			 List<Long> opIds = new ArrayList<>();
  			 opIds.add(asraOpDTO.getId());
4b2bb18a   xiejianpeng   登录权限处理
97
98
  			 List<Long> opParentIds = new ArrayList<>();
  			 opParentIds.add(asraOpDTO.getId());
5e8d9026   王富生   提交
99
  			 /**递归查询员工ids.*/
4b2bb18a   xiejianpeng   登录权限处理
100
  			 opIds = queryOpTreeByOpId(opParentIds,opIds);
5e8d9026   王富生   提交
101
102
  			 logger.info("---获取到的opIds={}",JSONObject.toJSON(opIds));
  			 asraOpDTO.setOpIds(opIds);
900781d3   王富生   提交
103
104
105
106
107
108
109
110
111
  			 HttpSession session = request.getSession();
  			 session.setAttribute(SessionEnum.USER_INFO.key(), asraOpDTO);
  			 logger.info("---获取到的session_id={}",session.getId());
  			 loginOathRes.setOpId(asraOpDTO.getId());
  			 loginOathRes.setLoginCode(loginCode);
  			 loginOathRes.setUserName(asraOpDTO.getOpName());
  			 loginOathRes.setCityId(asraOpDTO.getCityId());
  			 loginOathRes.setCityName(asraOpDTO.getCityName());
  			 loginOathRes.setAccessToken(session.getId());
0b156ed7   王富生   提交
112
  			 loginOathRes.setRoleId(asraOpDTO.getRoleId());
900781d3   王富生   提交
113
  			 result.setData(loginOathRes); 
0b156ed7   王富生   提交
114
  			 result.setErrorType(ErrorType.BIZ_SUCCESS, "登录成功");
900781d3   王富生   提交
115
116
117
118
119
  		 }
  		 
  		
  		 return result;
  	 }
d365b3fa   王富生   提交
120
121
  	 /**
  	  * 退出登录
4b2bb18a   xiejianpeng   登录权限处理
122
  	  * @param
d365b3fa   王富生   提交
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  	  * @return
  	  * 2018731  wangfs.<br/>
  	  */
      @ApiOperation("用户登出")
      @RequestMapping("/loginout")
      public OAuthResult<LoginOathRes> loginOut() {
          HttpSession session = request.getSession();
          AsraOpDTO userInfo = (AsraOpDTO)request.getSession().getAttribute(SessionEnum.USER_INFO.key());
          //登出
          session.invalidate();
          if(userInfo==null){
          	return new OAuthResult<>(true);
          }
          logger.info("end用户登出..");
      	return new OAuthResult<>(true);
      }
5e8d9026   王富生   提交
139
140
141
      
      /**
       * 递归查询员工id.<br/>
4b2bb18a   xiejianpeng   登录权限处理
142
       * @param
5e8d9026   王富生   提交
143
144
145
146
       * @param opList
       * @return
       * 201882  wangfs.<br/>
       */
4b2bb18a   xiejianpeng   登录权限处理
147
148
      private List<Long> queryOpTreeByOpId(List<Long> queryOpList,List<Long> opList){
      	  if(queryOpList != null && CollectionUtil.isNotEmpty(queryOpList)){
5e8d9026   王富生   提交
149
      		  AsraOpQueryReq asraOpQueryRe = new AsraOpQueryReq();
4b2bb18a   xiejianpeng   登录权限处理
150
151
152
153
      		  asraOpQueryRe.setOpParentIdLists(queryOpList);
  			  asraOpQueryRe.getBaseRequest().setPageNum(1);
  			  asraOpQueryRe.getBaseRequest().setPageSize(0);
  			  BizResult<PageBean<AsraOpDTO>>  asraOpReult = asraOpQueryService.queryAsraOpForPage(asraOpQueryRe);
5e8d9026   王富生   提交
154
      	      if(asraOpReult != null && asraOpReult.getData() != null){
4b2bb18a   xiejianpeng   登录权限处理
155
156
157
158
159
160
161
  				  queryOpList.clear();
      	    	  List<AsraOpDTO> data = asraOpReult.getData().getDataList();
      	    	  if(CollectionUtil.isNotEmpty(data)){
  					  for(AsraOpDTO dto:data){
  						  queryOpList.add(dto.getId());
  						  opList.add(dto.getId());
  					  }
5e8d9026   王富生   提交
162
      	    	  }
4b2bb18a   xiejianpeng   登录权限处理
163
      	    	  queryOpTreeByOpId(queryOpList,opList);
5e8d9026   王富生   提交
164
165
      	      }
      	  }
5e8d9026   王富生   提交
166
167
      	return opList;
      }
4b2bb18a   xiejianpeng   登录权限处理
168
169
170
171
  
  
  
  
900781d3   王富生   提交
172
173
  
  }