jiangping
2023-08-25 6baf2c39511f7364bb7c7f8071228317aab21203
server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java
@@ -11,6 +11,7 @@
import doumeemes.core.utils.Constants;
import doumeemes.core.utils.HttpsUtil;
import doumeemes.core.utils.Utils;
import doumeemes.dao.business.model.Company;
import doumeemes.dao.business.model.CompanyUser;
import doumeemes.dao.business.model.Department;
import doumeemes.dao.ext.CompanyExtMapper;
@@ -18,6 +19,7 @@
import doumeemes.dao.ext.DepartmentExtMapper;
import doumeemes.dao.ext.dto.QueryCompanyUserExtDTO;
import doumeemes.dao.ext.dto.WxLoginDTO;
import doumeemes.dao.ext.dto.WxLoginOutDTO;
import doumeemes.dao.ext.vo.CompanyUserExtListVO;
import doumeemes.dao.ext.vo.WxLoginVO;
import doumeemes.dao.system.SystemUserMapper;
@@ -69,6 +71,8 @@
    @Autowired
    private SystemLoginLogService systemLoginLogService;
    @Autowired
    private CompanyExtMapper companyExtMapper;
    /**
@@ -84,7 +88,7 @@
    @Override
    public WxLoginVO wxLogin(String code, HttpServletRequest request) {
        WxLoginVO wxLoginVO = new WxLoginVO();
        String appId = systemDictDataBiz.queryByCode(Constants.WX_CONFIG,Constants.APPID).getCode();
        String appSecret = systemDictDataBiz.queryByCode(Constants.WX_CONFIG,Constants.APPSECRET).getCode();
        String getTokenUrl = GET_ACCESS_TOKEN_URL.replace("CODE", code).replace("APPID", appId).replace("SECRET", appSecret);
@@ -96,9 +100,19 @@
        String openId = tokenJson.getString("openid");
        String getUserInfoUrl = GET_USER_INFO_URL.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
        JSONObject userInfoJson = JSONObject.parseObject(HttpsUtil.get(getUserInfoUrl,true));
        return  loginByUnionIdAndReturn(userInfoJson.getString("unionid"),openId,request);
    }
    private WxLoginVO loginByUnionIdAndReturn(String unionid,String openId,HttpServletRequest request) {
        WxLoginVO wxLoginVO = new WxLoginVO();
        wxLoginVO.setOpenid(openId);
        wxLoginVO.setUnionid(userInfoJson.getString("unionid"));
        CompanyUser companyUser = companyUserExtMapper.selectOne(new QueryWrapper<CompanyUser>().eq("openid",openId).last(" limit 1 "));
        wxLoginVO.setUnionid(unionid);
        CompanyUser companyUser = companyUserExtMapper.selectOne(new QueryWrapper<CompanyUser>().lambda()
                .eq(CompanyUser::getUnionid,wxLoginVO.getUnionid())
                .eq(CompanyUser::getDeleted,Constants.ZERO)
                .last(" limit 1 "));
//        CompanyUser companyUser = companyUserExtMapper.selectOne(new QueryWrapper<CompanyUser>().eq("openid",openId).last(" limit 1 "));
        if(Objects.isNull(companyUser)){
            wxLoginVO.setLoginStatus(Constants.ONE);
            return wxLoginVO;
@@ -106,6 +120,11 @@
        Department department = departmentExtMapper.selectById(companyUser.getRootDepartId());
        if(Objects.isNull(department)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到绑定部门信息");
        }
        Company company = companyExtMapper.selectById(department.getCompanyId());
        if(Objects.isNull(company)||company.getStatus().equals(Constants.ZERO)){
            wxLoginVO.setLoginStatus(Constants.ONE);
            return wxLoginVO;
        }
        SystemUser systemUser = systemUserMapper.selectById(companyUser.getUserId());
        //查询用户数据
@@ -137,6 +156,9 @@
            wxLoginVO.setLoginStatus(Constants.ZERO);
            wxLoginVO.setSession(session);
            return wxLoginVO;
        }catch (BusinessException e) {
            wxLoginVO.setLoginStatus(Constants.ONE);
            return wxLoginVO;
        }catch (AuthenticationException e) {
            BusinessException ee = null;
            loginLog.setSuccess(Boolean.FALSE);
@@ -152,6 +174,13 @@
            systemLoginLogService.create(loginLog);
            throw  ee;
        }
        return  wxLoginVO;
    }
    @Override
    public WxLoginVO wxProgramLogin(String unionId,String openId, HttpServletRequest request) {
        return  loginByUnionIdAndReturn(unionId,openId,request);
    }
@@ -212,4 +241,14 @@
        }
    }
    @Override
    public void wxLoginOut(WxLoginOutDTO wxLoginOutDTO) {
        if(!Objects.isNull(wxLoginOutDTO)&&!Objects.isNull(wxLoginOutDTO.getCompanyUserId())){
            companyUserExtMapper.update(null,new UpdateWrapper<CompanyUser>()
                    .set("UNIONID","").set("OPENID","")
                    .eq("id",wxLoginOutDTO.getCompanyUserId())
            );
        }
    }
}