From 1f4e7d0f73a73e7350cf5a1df279d5f30904c5d5 Mon Sep 17 00:00:00 2001 From: jiangping <jp@doumee.com> Date: 星期三, 06 十二月 2023 08:41:32 +0800 Subject: [PATCH] bug --- server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java | 79 +++++++++++++++++++++++++++++++-------- 1 files changed, 63 insertions(+), 16 deletions(-) diff --git a/server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java b/server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java index 83c9733..77d7d74 100644 --- a/server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java +++ b/server/src/main/java/doumeemes/service/system/impl/WxLoginServiceImpl.java @@ -1,5 +1,6 @@ package doumeemes.service.system.impl; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; @@ -11,32 +12,30 @@ import doumeemes.core.utils.Constants; import doumeemes.core.utils.HttpsUtil; import doumeemes.core.utils.Utils; +import doumeemes.core.utils.WxMiniConfig; +import doumeemes.dao.business.model.Company; import doumeemes.dao.business.model.CompanyUser; import doumeemes.dao.business.model.Department; import doumeemes.dao.ext.CompanyExtMapper; import doumeemes.dao.ext.CompanyUserExtMapper; import doumeemes.dao.ext.DepartmentExtMapper; -import doumeemes.dao.ext.dto.QueryCompanyUserExtDTO; import doumeemes.dao.ext.dto.WxLoginDTO; -import doumeemes.dao.ext.vo.CompanyUserExtListVO; +import doumeemes.dao.ext.dto.WxLoginOutDTO; import doumeemes.dao.ext.vo.WxLoginVO; import doumeemes.dao.system.SystemUserMapper; import doumeemes.dao.system.dto.LoginDTO; import doumeemes.dao.system.model.SystemLoginLog; import doumeemes.dao.system.model.SystemUser; -import doumeemes.service.common.CaptchaService; -import doumeemes.service.ext.CompanyExtService; -import doumeemes.service.ext.CompanyUserExtService; import doumeemes.service.system.SystemLoginLogService; import doumeemes.service.system.WxLoginService; import lombok.extern.slf4j.Slf4j; +import me.chanjar.weixin.common.error.WxErrorException; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; @@ -56,7 +55,6 @@ @Autowired private SystemDictDataBiz systemDictDataBiz; - @Value("${project.version}") private String systemVersion; @@ -69,6 +67,8 @@ @Autowired private SystemLoginLogService systemLoginLogService; + @Autowired + private CompanyExtMapper companyExtMapper; /** @@ -81,24 +81,51 @@ */ public static final String GET_USER_INFO_URL = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN"; + public static final String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN"; + /** + * 娉細鍏紬鍙�-璁剧疆涓庡紑鍙�-鍩烘湰璁剧疆锛屽繀椤汇�愬凡缁戝畾鐨勫井淇″紑鏀惧钩鍙拌处鍙枫�� + * @param code + * @param request + * @return + */ @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); JSONObject tokenJson = JSONObject.parseObject(HttpsUtil.get(getTokenUrl,true)); - if(Objects.isNull(tokenJson.get("access_token"))){ - throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),tokenJson.getString("errmsg")); - } - String accessToken = tokenJson.getString("access_token"); 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)); + //娉細鍏紬鍙�-璁剧疆涓庡紑鍙�-鍩烘湰璁剧疆锛屽繀椤汇�愬凡缁戝畾鐨勫井淇″紑鏀惧钩鍙拌处鍙枫�� + String unionId = tokenJson.getString("unionid"); + return loginByUnionIdAndReturn(unionId,Constants.OPENID_WX+openId,request); + } + + + @Override + public WxLoginVO wxProgramLogin(String code, HttpServletRequest request) { + try { + WxMaJscode2SessionResult session = WxMiniConfig.wxMaService.getUserService().getSessionInfo(code); + String unionid = session.getUnionid(); + String openId = session.getOpenid(); + return loginByUnionIdAndReturn(unionid,Constants.OPENID_MINI+openId,request); + } catch (WxErrorException e) { + e.printStackTrace(); + } + throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"寰俊鎺堟潈澶辫触锛岃鑱旂郴绠$悊鍛�"); + + + + } + 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 +133,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()); //鏌ヨ鐢ㄦ埛鏁版嵁 @@ -132,10 +164,14 @@ loginLog.setCompanyId(loginUser.getCompany()!=null?loginUser.getCompany().getId():null); loginLog.setCompanyUserId(loginUser.getCompanyUser()!=null?loginUser.getCompanyUser().getId():null); loginLog.setSuccess(Boolean.TRUE); + loginLog.setOrgin(Constants.USER_LOGIN_ORIGIN.wx); systemLoginLogService.create(loginLog); String session = (String)subject.getSession().getId(); wxLoginVO.setLoginStatus(Constants.ZERO); wxLoginVO.setSession(session); + return wxLoginVO; + }catch (BusinessException e) { + wxLoginVO.setLoginStatus(Constants.ONE); return wxLoginVO; }catch (AuthenticationException e) { BusinessException ee = null; @@ -153,6 +189,7 @@ throw ee; } } + @@ -212,4 +249,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()) + ); + } + } + } -- Gitblit v1.9.3