OAuthController.java 8.15 KB
package com.zteits.oa.report.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

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 com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.zteits.oa.api.base.bean.BizResult;
import com.zteits.oa.api.base.bean.PageBean;
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;
import com.zteits.oa.api.service.report.query.AsraOpQueryService;
import com.zteits.oa.report.vo.OAuthResult;
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
	 private AsraOpQueryService asraOpQueryService;
	 
	 @Autowired
	 private HttpServletRequest request;
	 
	 @ApiOperation("用户登录")
	 @PostMapping("/login")
	 public OAuthResult<LoginOathRes> login(@RequestBody LoginOauthReq req ) throws Exception {
		 OAuthResult<LoginOathRes> result =  this._login(req);
		 return result;
	 }
	 /**
	  * 登陆验证.<br/>
	  * @param req
	  * @return
	  * 2018年7月31日  wangfs.<br/>
	  */
	 private OAuthResult<LoginOathRes> _login(LoginOauthReq req ){
		 OAuthResult<LoginOathRes> result = new OAuthResult<LoginOathRes>(false);
		 LoginOathRes loginOathRes = new  LoginOathRes();
		 String loginCode = req.getLoginCode();
		 String passWord = req.getPassWord();
		 if(StringUtils.isEmpty(loginCode) || StringUtils.isEmpty(passWord)) {
        	  logger.info("校验登录信息,用户名 或者 登录密码为空!");
        	  result.setErrorType(ErrorType.PARAMM_NULL, "用户名 或者 登录密码为空");
	      }
		 AsraOpQueryReq asraOpQueryRe = new AsraOpQueryReq();
		 AsraOpDTO asraOpDTO = new AsraOpDTO();
		 boolean isCheckSuccess = false;
		 //1.判断登录账号/密码
		 asraOpQueryRe.setLoginCode(loginCode);
		 BizResult<AsraOpDTO> asraOpReult = asraOpQueryService.queryAsraOp(asraOpQueryRe);
		 if(asraOpReult != null && asraOpReult.getData() != null){
			 asraOpDTO = asraOpReult.getData();
			 if(StringUtils.isEmpty(asraOpDTO.getLoginCode())){
				 logger.info("{}登录账号不存在",loginCode);
	        	  result.setErrorType(ErrorType.AUTH_LOGIN_ERROR, "登录账号不存在!");
			 }else{
				 if(!asraOpDTO.getLoginPassword().equalsIgnoreCase(MD5Utils.enMD5(passWord))){
					 logger.info("{}登录账号输入的密码不正确",loginCode);
		        	  result.setErrorType(ErrorType.AUTH_PASS_ERROR, "登录密码不匹配!");
				 }else{
					 isCheckSuccess = true;
				 }
			 }
			 
		 }else{
			 result.setErrorType(ErrorType.BIZ_ERROR, "用户登录失败");
		 }
		 
		 
		 if(isCheckSuccess){
			 
			 List<Long> opIds = new ArrayList<>();
			 opIds.add(asraOpDTO.getId());
			 List<Long> opParentIds = new ArrayList<>();
			 opParentIds.add(asraOpDTO.getId());
			 /**递归查询员工ids.*/
			 opIds = queryOpTreeByOpIds(opParentIds,opIds);
			 logger.info("---获取到的opIds={}",JSONObject.toJSON(opIds));
			 asraOpDTO.setOpIds(opIds);
			 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());
			 loginOathRes.setRoleId(asraOpDTO.getRoleId());
			 result.setData(loginOathRes); 
			 result.setErrorType(ErrorType.BIZ_SUCCESS, "登录成功");
		 }
		 
		
		 return result;
	 }
	 /**
	  * 退出登录
	  * @param
	  * @return
	  * 2018年7月31日  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);
    }
    
    
    private List<Long> queryOpTreeByOpIds(List<Long> queryOpList,List<Long> opList){
    	  if(queryOpList != null && CollectionUtil.isNotEmpty(queryOpList)){
    		  AsraOpQueryReq asraOpQueryRe = new AsraOpQueryReq();
    		  asraOpQueryRe.setOpParentIdLists(queryOpList);
			  asraOpQueryRe.getBaseRequest().setPageNum(1);
			  asraOpQueryRe.getBaseRequest().setPageSize(0);
			  BizResult<PageBean<AsraOpDTO>>  asraOpReult = asraOpQueryService.queryAsraOpForPage(asraOpQueryRe);
    	      if(asraOpReult != null && asraOpReult.getData() != null){
				  queryOpList.clear();
    	    	  List<AsraOpDTO> data = asraOpReult.getData().getDataList();
    	    	  if(CollectionUtil.isNotEmpty(data)){
					  for(AsraOpDTO dto:data){
						  queryOpList.add(dto.getId());
						  opList.add(dto.getId());
					  }
    	    	  }
    	    	  queryOpTreeByOpIds(queryOpList,opList);
    	      }
    	  }
    	return opList;
    }


    /**
     * 递归查询员工id.<br/>
     * @param
     * @param opList
     * @return
     * 2018年8月2日  wangfs.<br/>
     */
    private List<Long> queryOpTreeByOpId(Long opId){
    	List<Long> rootAllList = new ArrayList<>();
    	rootAllList.add(opId);
    	//1.查找第一级下的所有用户
    	List<Long> rootOpIds = this.getRootOpIds(opId);
    	rootAllList.addAll(rootOpIds);
    	//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
	 * 2018年8月2日  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
	 * 2018年8月2日  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;
	}



}