Blame view

src/main/java/com/zteits/oa/report/web/OAuthController.java 8.18 KB
900781d3   王富生   提交
1
2
  package com.zteits.oa.report.web;
  
5e8d9026   王富生   提交
3
  import com.alibaba.fastjson.JSONObject;
b1704d7c   王富生   提交
4
  import com.xiaoleilu.hutool.util.CollectionUtil;
900781d3   王富生   提交
5
  import com.zteits.oa.api.base.bean.BizResult;
9bd9248e   xiejianpeng   登录权限处理
6
  import com.zteits.oa.api.base.bean.PageBean;
900781d3   王富生   提交
7
8
9
10
11
12
  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   王富生   提交
13
  import com.zteits.oa.api.service.report.query.AsraOpQueryService;
0b156ed7   王富生   提交
14
  import com.zteits.oa.report.vo.OAuthResult;
900781d3   王富生   提交
15
  import com.zteits.oa.util.MD5Utils;
900781d3   王富生   提交
16
17
  import io.swagger.annotations.Api;
  import io.swagger.annotations.ApiOperation;
9bd9248e   xiejianpeng   登录权限处理
18
19
20
21
22
23
24
25
26
27
28
29
30
  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;
  import org.springframework.web.bind.annotation.RestController;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpSession;
  import java.util.ArrayList;
  import java.util.List;
900781d3   王富生   提交
31
32
33
34
35
36
37
38
  
  @Api("用户登录授权")
  @RestController
  @RequestMapping("/oauth")
  public class OAuthController {
  	 private static final Logger logger = LoggerFactory.getLogger(OAuthController.class);
  	 
  	 @Autowired
9ce77292   xiejianpeng   员工管理
39
  	 private AsraOpQueryService asraOpQueryService;
900781d3   王富生   提交
40
41
42
43
44
45
  	 
  	 @Autowired
  	 private HttpServletRequest request;
  	 
  	 @ApiOperation("用户登录")
  	 @PostMapping("/login")
0b156ed7   王富生   提交
46
47
  	 public OAuthResult<LoginOathRes> login(@RequestBody LoginOauthReq req ) throws Exception {
  		 OAuthResult<LoginOathRes> result =  this._login(req);
900781d3   王富生   提交
48
49
50
51
52
53
54
55
  		 return result;
  	 }
  	 /**
  	  * 登陆验证.<br/>
  	  * @param req
  	  * @return
  	  * 2018731  wangfs.<br/>
  	  */
0b156ed7   王富生   提交
56
57
  	 private OAuthResult<LoginOathRes> _login(LoginOauthReq req ){
  		 OAuthResult<LoginOathRes> result = new OAuthResult<LoginOathRes>(false);
900781d3   王富生   提交
58
59
60
61
62
  		 LoginOathRes loginOathRes = new  LoginOathRes();
  		 String loginCode = req.getLoginCode();
  		 String passWord = req.getPassWord();
  		 if(StringUtils.isEmpty(loginCode) || StringUtils.isEmpty(passWord)) {
          	  logger.info("校验登录信息,用户名 或者 登录密码为空!");
0b156ed7   王富生   提交
63
          	  result.setErrorType(ErrorType.PARAMM_NULL, "用户名 或者 登录密码为空");
900781d3   王富生   提交
64
65
66
67
68
69
  	      }
  		 AsraOpQueryReq asraOpQueryRe = new AsraOpQueryReq();
  		 AsraOpDTO asraOpDTO = new AsraOpDTO();
  		 boolean isCheckSuccess = false;
  		 //1.判断登录账号/密码
  		 asraOpQueryRe.setLoginCode(loginCode);
9ce77292   xiejianpeng   员工管理
70
  		 BizResult<AsraOpDTO> asraOpReult = asraOpQueryService.queryAsraOp(asraOpQueryRe);
900781d3   王富生   提交
71
72
73
74
  		 if(asraOpReult != null && asraOpReult.getData() != null){
  			 asraOpDTO = asraOpReult.getData();
  			 if(StringUtils.isEmpty(asraOpDTO.getLoginCode())){
  				 logger.info("{}登录账号不存在",loginCode);
0b156ed7   王富生   提交
75
  	        	  result.setErrorType(ErrorType.AUTH_LOGIN_ERROR, "登录账号不存在!");
900781d3   王富生   提交
76
77
78
  			 }else{
  				 if(!asraOpDTO.getLoginPassword().equalsIgnoreCase(MD5Utils.enMD5(passWord))){
  					 logger.info("{}登录账号输入的密码不正确",loginCode);
0b156ed7   王富生   提交
79
  		        	  result.setErrorType(ErrorType.AUTH_PASS_ERROR, "登录密码不匹配!");
900781d3   王富生   提交
80
81
82
83
84
85
  				 }else{
  					 isCheckSuccess = true;
  				 }
  			 }
  			 
  		 }else{
0b156ed7   王富生   提交
86
  			 result.setErrorType(ErrorType.BIZ_ERROR, "用户登录失败");
900781d3   王富生   提交
87
88
89
90
  		 }
  		 
  		 
  		 if(isCheckSuccess){
5e8d9026   王富生   提交
91
92
93
  			 
  			 List<Long> opIds = new ArrayList<>();
  			 opIds.add(asraOpDTO.getId());
4b2bb18a   xiejianpeng   登录权限处理
94
95
  			 List<Long> opParentIds = new ArrayList<>();
  			 opParentIds.add(asraOpDTO.getId());
5e8d9026   王富生   提交
96
  			 /**递归查询员工ids.*/
2f027043   xiejianpeng   Merge remote-trac...
97
  			 opIds = queryOpTreeByOpIds(opParentIds,opIds);
5e8d9026   王富生   提交
98
99
  			 logger.info("---获取到的opIds={}",JSONObject.toJSON(opIds));
  			 asraOpDTO.setOpIds(opIds);
900781d3   王富生   提交
100
101
102
103
104
105
106
107
108
  			 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   王富生   提交
109
  			 loginOathRes.setRoleId(asraOpDTO.getRoleId());
900781d3   王富生   提交
110
  			 result.setData(loginOathRes); 
0b156ed7   王富生   提交
111
  			 result.setErrorType(ErrorType.BIZ_SUCCESS, "登录成功");
900781d3   王富生   提交
112
113
114
115
116
  		 }
  		 
  		
  		 return result;
  	 }
d365b3fa   王富生   提交
117
118
  	 /**
  	  * 退出登录
4b2bb18a   xiejianpeng   登录权限处理
119
  	  * @param
d365b3fa   王富生   提交
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  	  * @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   王富生   提交
136
137
138
      
      /**
       * 递归查询员工id.<br/>
4b2bb18a   xiejianpeng   登录权限处理
139
       * @param
5e8d9026   王富生   提交
140
141
142
143
       * @param opList
       * @return
       * 201882  wangfs.<br/>
       */
2f027043   xiejianpeng   Merge remote-trac...
144
      private List<Long> queryOpTreeByOpIds(List<Long> queryOpList,List<Long> opList){
4b2bb18a   xiejianpeng   登录权限处理
145
      	  if(queryOpList != null && CollectionUtil.isNotEmpty(queryOpList)){
5e8d9026   王富生   提交
146
      		  AsraOpQueryReq asraOpQueryRe = new AsraOpQueryReq();
4b2bb18a   xiejianpeng   登录权限处理
147
148
149
150
      		  asraOpQueryRe.setOpParentIdLists(queryOpList);
  			  asraOpQueryRe.getBaseRequest().setPageNum(1);
  			  asraOpQueryRe.getBaseRequest().setPageSize(0);
  			  BizResult<PageBean<AsraOpDTO>>  asraOpReult = asraOpQueryService.queryAsraOpForPage(asraOpQueryRe);
5e8d9026   王富生   提交
151
      	      if(asraOpReult != null && asraOpReult.getData() != null){
4b2bb18a   xiejianpeng   登录权限处理
152
153
154
155
156
157
158
  				  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   王富生   提交
159
      	    	  }
9bd9248e   xiejianpeng   登录权限处理
160
      	    	  queryOpTreeByOpIds(queryOpList,opList);
5e8d9026   王富生   提交
161
162
      	      }
      	  }
5e8d9026   王富生   提交
163
164
      	return opList;
      }
4b2bb18a   xiejianpeng   登录权限处理
165
166
  
  
2f027043   xiejianpeng   Merge remote-trac...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  	/**
  	 * 递归查询员工id.<br/>
  	 * @param parentOpId
  	 * @param opList
  	 * @return
  	 * 201882  wangfs.<br/>
  	 */
  	private List<Long> queryOpTreeByOpId(Long opId){
  		List<Long> rootAllList = new ArrayList<>();
  		rootAllList.add(opId);
  		//1.查找第一级下的所有用户
  		List<Long> rootOpIds = this.getRootOpIds(opId);
  
  		//2.查找第二级以下所有用户
  		if(CollectionUtil.isNotEmpty(rootOpIds)){
  			rootAllList.addAll(rootOpIds);
  			for(Long getOpId:rootOpIds ){
  				List<Long> childOpIds = new ArrayList<>();
  				this.getChildOpIds(getOpId,childOpIds);
  				rootAllList.addAll(childOpIds);
  			}
  
  		}
  		return rootAllList;
  	}
  
  	/**
  	 * 递归查找一级以下的用户
  	 * @param parentId
  	 * @return
  	 * 201882  wangfs.<br/>
  	 */
  	private List<Long>  getChildOpIds(Long parentId,List<Long> childOpIds){
  		AsraOpQueryReq asraOpQueryRe = new AsraOpQueryReq();
  		asraOpQueryRe.setParentId(parentId);
  		BizResult<List<AsraOpDTO>> asraOpReult = asraOpQueryService.queryAsraOpByParentId(asraOpQueryRe);
  		if(asraOpReult != null && CollectionUtil.isNotEmpty(asraOpReult.getData())){
  			List<AsraOpDTO> list = asraOpReult.getData();
  			for(AsraOpDTO asraOpDTO :list){
  				if(asraOpDTO == null){
  					continue;
  				}
  				childOpIds.add(asraOpDTO.getId());
  			}
  
  			//遍历下一级
  			if(CollectionUtil.isNotEmpty(childOpIds)){
  				for(Long opId:childOpIds){
  					if(opId != null && !opId.equals(parentId)){
  						getChildOpIds(opId,childOpIds);
  					}
  					break;
  
  				}
  
  			}
  		}
  
  		return childOpIds;
  	}
  
  	/**
  	 * 查找登陆用户下一级用户
  	 * @param opId
  	 * @param opList
  	 * @return
  	 * 201882  wangfs.<br/>
  	 */
  	private List<Long> getRootOpIds(Long parentId){
  		List<Long> opList = new ArrayList<>();
  		AsraOpQueryReq asraOpQueryRe = new AsraOpQueryReq();
  		asraOpQueryRe.setParentId(parentId);
  		BizResult<List<AsraOpDTO>> asraOpReult = asraOpQueryService.queryAsraOpByParentId(asraOpQueryRe);
  		if(asraOpReult != null && CollectionUtil.isNotEmpty(asraOpReult.getData())){
  			List<AsraOpDTO> list = asraOpReult.getData();
  			for(AsraOpDTO asraOpDTO :list){
  				if(asraOpDTO == null){
  					continue;
  				}
  				opList.add(asraOpDTO.getId());
  			}
  		}
  		return opList;
  	}
4b2bb18a   xiejianpeng   登录权限处理
251
252
  
  
900781d3   王富生   提交
253
254
  
  }