|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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.dto.WxLoginOutDTO; | 
|---|
|  |  |  | import doumeemes.dao.ext.vo.CompanyUserExtListVO; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Value("${project.version}") | 
|---|
|  |  |  | private String systemVersion; | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 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) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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)); | 
|---|
|  |  |  | return  loginByUnionIdAndReturn(userInfoJson.getString("unionid"),openId,request); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //注:公众号-设置与开发-基本设置,必须【已绑定的微信开放平台账号】 | 
|---|
|  |  |  | String unionId = tokenJson.getString("unionid"); | 
|---|
|  |  |  | return  loginByUnionIdAndReturn(unionId,Constants.OPENID_WX+openId,request); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public WxLoginVO wxProgramLogin(String code, HttpServletRequest request) { | 
|---|
|  |  |  | //TODO---------------任康 | 
|---|
|  |  |  | 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")); | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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)); | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"微信授权失败,请联系管理员"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return  loginByUnionIdAndReturn(userInfoJson.getString("unionid"),openId,request); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | private WxLoginVO loginByUnionIdAndReturn(String unionid,String openId,HttpServletRequest request) { | 
|---|