| | |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.core.base.smo.front.AbstractFrontServiceSMO; |
| | | import com.java110.api.properties.WechatAuthProperties; |
| | | import com.java110.api.smo.DefaultAbstractComponentSMO; |
| | | import com.java110.api.smo.login.IOwnerAppLoginSMO; |
| | | import com.java110.core.context.IPageData; |
| | | import com.java110.core.context.PageData; |
| | | import com.java110.core.factory.AuthenticationFactory; |
| | | import com.java110.core.factory.WechatFactory; |
| | | import com.java110.core.log.LoggerFactory; |
| | | import com.java110.dto.owner.OwnerAppUserDto; |
| | | import com.java110.dto.ownerCarOpenUser.OwnerCarOpenUserDto; |
| | | import com.java110.dto.smallWeChat.SmallWeChatDto; |
| | | import com.java110.dto.user.UserDto; |
| | | import com.java110.api.properties.WechatAuthProperties; |
| | | import com.java110.api.smo.login.IOwnerAppLoginSMO; |
| | | import com.java110.intf.user.IOwnerCarOpenUserV1InnerServiceSMO; |
| | | import com.java110.utils.cache.CommonCache; |
| | | import com.java110.utils.constant.*; |
| | | import com.java110.utils.constant.CommonConstant; |
| | | import com.java110.utils.constant.ResponseConstant; |
| | | import com.java110.utils.constant.ServiceCodeConstant; |
| | | import com.java110.utils.constant.WechatConstant; |
| | | import com.java110.utils.exception.SMOException; |
| | | import com.java110.utils.util.Assert; |
| | | import com.java110.utils.util.Base64Convert; |
| | |
| | | import com.java110.utils.util.StringUtil; |
| | | import com.java110.vo.ResultVo; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.http.HttpStatus; |
| | |
| | | * wx登录 |
| | | */ |
| | | @Service("ownerAppLoginSMOImpl") |
| | | public class OwnerAppLoginSMOImpl extends AbstractFrontServiceSMO implements IOwnerAppLoginSMO { |
| | | public class OwnerAppLoginSMOImpl extends DefaultAbstractComponentSMO implements IOwnerAppLoginSMO { |
| | | |
| | | private final static Logger logger = LoggerFactory.getLogger(OwnerAppLoginSMOImpl.class); |
| | | |
| | | private final static int expireTime = 7200; |
| | | |
| | | private final static int LOGIN_PAGE = 1; |
| | | private final static int COMMON_PAGE = 2; |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | |
| | | |
| | | @Autowired |
| | | private WechatAuthProperties wechatAuthProperties; |
| | | |
| | | @Autowired |
| | | private IOwnerCarOpenUserV1InnerServiceSMO ownerCarOpenUserV1InnerServiceSMOImpl; |
| | | |
| | | @Override |
| | | public ResponseEntity<String> doLogin(IPageData pd) throws SMOException { |
| | |
| | | logger.debug("调用微信换去openId " + paramOut); |
| | | if (paramOut.getStatusCode() != HttpStatus.OK) { |
| | | return ResultVo.redirectPage(errorUrl); |
| | | |
| | | } |
| | | |
| | | JSONObject paramObj = JSONObject.parseObject(paramOut.getBody()); |
| | | |
| | | //获取 openId |
| | | String openId = paramObj.getString("openid"); |
| | | |
| | | String userinfo_url = WechatConstant.APP_GET_USER_INFO_URL |
| | | .replace("ACCESS_TOKEN", paramObj.getString("access_token")) |
| | | .replace("OPENID", openId); |
| | |
| | | //处理昵称有特殊符号导致 入库失败问题 |
| | | userinfo_paramObj.put("nickname", Base64Convert.byteToBase64(userinfo_paramObj.getString("nickname").getBytes())); |
| | | |
| | | //公众号未绑定 开放平台 |
| | | if (StringUtil.isEmpty(userinfo_paramObj.getString("unionid"))) { |
| | | userinfo_paramObj.put("unionid", "-1"); |
| | | } |
| | | |
| | | int loginFlag = paramIn.getInteger("loginFlag"); |
| | | |
| | | //说明是登录页面,下发code 就可以,不需要下发key 之类 |
| | |
| | | CommonCache.setValue(code, openId, expireTime); |
| | | CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime); |
| | | CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime); |
| | | CommonCache.setValue(code + "-unionid", userinfo_paramObj.getString("unionid"), expireTime); |
| | | if (errorUrl.indexOf("?") > 0) { |
| | | errorUrl += ("&code=" + code); |
| | | } else { |
| | | errorUrl += ("?code=" + code); |
| | | } |
| | | logger.debug("登录跳转url:{}", errorUrl); |
| | | |
| | | return ResultVo.redirectPage(errorUrl); |
| | | } |
| | | |
| | | if (loginFlag == COMMON_PAGE) { |
| | | //将openId放到redis 缓存,给前段下发临时票据 |
| | | if (errorUrl.indexOf("?") > 0) { |
| | | redirectUrl += ("&openId=" + openId); |
| | | } else { |
| | | redirectUrl += ("?openId=" + openId); |
| | | } |
| | | logger.debug("跳转url:{}", redirectUrl); |
| | | return ResultVo.redirectPage(redirectUrl); |
| | | } |
| | | |
| | | //判断当前openId 是否绑定了业主 |
| | |
| | | CommonCache.setValue(code, openId, expireTime); |
| | | CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime); |
| | | CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime); |
| | | CommonCache.setValue(code + "-unionid", userinfo_paramObj.getString("unionid"), expireTime); |
| | | if (errorUrl.indexOf("?") > 0) { |
| | | errorUrl += ("&code=" + code); |
| | | } else { |
| | |
| | | CommonCache.setValue(code, openId, expireTime); |
| | | CommonCache.setValue(code + "-nickname", userinfo_paramObj.getString("nickname"), expireTime); |
| | | CommonCache.setValue(code + "-headimgurl", userinfo_paramObj.getString("headimgurl"), expireTime); |
| | | CommonCache.setValue(code + "-unionid", userinfo_paramObj.getString("unionid"), expireTime); |
| | | |
| | | if (errorUrl.indexOf("?") > 0) { |
| | | errorUrl += ("&code=" + code); |
| | | } else { |
| | |
| | | return ResultVo.createResponseEntity(ResultVo.CODE_MACHINE_OK, ResultVo.MSG_OK, urlObj); |
| | | } |
| | | |
| | | @Override |
| | | public ResponseEntity<String> refreshOpenId(IPageData pd, String redirectUrl, String wAppId, HttpServletRequest request, HttpServletResponse response) { |
| | | |
| | | SmallWeChatDto smallWeChatDto = null; |
| | | if (!StringUtil.isEmpty(wAppId)) { |
| | | JSONObject paramIn = new JSONObject(); |
| | | paramIn.put("appId", wAppId); |
| | | smallWeChatDto = getSmallWechat(pd, paramIn); |
| | | } |
| | | if (smallWeChatDto == null) { //从配置文件中获取 小程序配置信息 |
| | | smallWeChatDto = new SmallWeChatDto(); |
| | | smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId()); |
| | | smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret()); |
| | | smallWeChatDto.setMchId(wechatAuthProperties.getMchId()); |
| | | smallWeChatDto.setPayPassword(wechatAuthProperties.getKey()); |
| | | wAppId = wechatAuthProperties.getWechatAppId(); |
| | | } |
| | | |
| | | //分配urlCode |
| | | String urlCode = UUID.randomUUID().toString(); |
| | | JSONObject param = new JSONObject(); |
| | | if (redirectUrl.indexOf("appId") < 0) { |
| | | redirectUrl += ("&appId=" + smallWeChatDto.getAppId()); |
| | | } |
| | | param.put("redirectUrl", redirectUrl); |
| | | CommonCache.setValue(urlCode, param.toJSONString(), expireTime); |
| | | |
| | | URL url = null; |
| | | String openUrl = ""; |
| | | try { |
| | | url = new URL(redirectUrl); |
| | | |
| | | String newUrl = url.getProtocol() + "://" + url.getHost(); |
| | | if (url.getPort() > 0) { |
| | | newUrl += (":" + url.getPort()); |
| | | } |
| | | |
| | | openUrl = WechatConstant.OPEN_AUTH |
| | | .replace("APPID", smallWeChatDto.getAppId()) |
| | | .replace("SCOPE", "snsapi_userinfo") |
| | | .replace( |
| | | "REDIRECT_URL", |
| | | URLEncoder |
| | | .encode( |
| | | (newUrl |
| | | + "/app/openServiceNotifyOpenId?appId=992020061452450002&urlCode=" + |
| | | urlCode + "&wId=" + WechatFactory.getWId(wAppId)), |
| | | "UTF-8")).replace("STATE", "1"); |
| | | |
| | | } catch (Exception e) { |
| | | logger.error("微信公众号鉴权 redirectUrl 错误 " + redirectUrl, e); |
| | | throw new SMOException(ResponseConstant.RESULT_CODE_ERROR, e.getLocalizedMessage()); |
| | | } |
| | | JSONObject urlObj = new JSONObject(); |
| | | urlObj.put("openUrl", openUrl); |
| | | |
| | | return ResultVo.createResponseEntity(ResultVo.CODE_MACHINE_OK, ResultVo.MSG_OK, urlObj); |
| | | } |
| | | |
| | | @Override |
| | | public ResponseEntity openServiceNotifyOpenId(IPageData pd, HttpServletRequest request) { |
| | | JSONObject paramIn = JSONObject.parseObject(pd.getReqData()); |
| | | String authCode = paramIn.getString("code"); |
| | | String state = paramIn.getString("state"); |
| | | String paramStr = CommonCache.getAndRemoveValue(paramIn.getString("urlCode")); |
| | | |
| | | if (StringUtil.isEmpty(paramStr)) { |
| | | return ResultVo.redirectPage("/"); |
| | | } |
| | | |
| | | JSONObject param = JSONObject.parseObject(paramStr); |
| | | String redirectUrl = param.getString("redirectUrl"); |
| | | String wId = paramIn.getString("wId"); |
| | | SmallWeChatDto smallWeChatDto = null; |
| | | if (!StringUtil.isEmpty(wId)) { |
| | | paramIn.put("appId", WechatFactory.getAppId(wId)); |
| | | smallWeChatDto = getSmallWechat(pd, paramIn); |
| | | } |
| | | if (smallWeChatDto == null) { //从配置文件中获取 小程序配置信息 |
| | | smallWeChatDto = new SmallWeChatDto(); |
| | | smallWeChatDto.setAppId(wechatAuthProperties.getWechatAppId()); |
| | | smallWeChatDto.setAppSecret(wechatAuthProperties.getWechatAppSecret()); |
| | | smallWeChatDto.setMchId(wechatAuthProperties.getMchId()); |
| | | smallWeChatDto.setPayPassword(wechatAuthProperties.getKey()); |
| | | } |
| | | |
| | | String url = WechatConstant.APP_GET_ACCESS_TOKEN_URL.replace("APPID", smallWeChatDto.getAppId()) |
| | | .replace("SECRET", smallWeChatDto.getAppSecret()) |
| | | .replace("CODE", authCode); |
| | | |
| | | ResponseEntity<String> paramOut = outRestTemplate.getForEntity(url, String.class); |
| | | |
| | | logger.debug("调用微信换去openId " + paramOut); |
| | | if (paramOut.getStatusCode() != HttpStatus.OK) { |
| | | return ResultVo.redirectPage("/"); |
| | | } |
| | | JSONObject paramObj = JSONObject.parseObject(paramOut.getBody()); |
| | | //获取 openId |
| | | String openId = paramObj.getString("openid"); |
| | | redirectUrl = redirectUrl + "&openId=" + openId; |
| | | |
| | | //查询是否有车牌号 |
| | | OwnerCarOpenUserDto ownerCarOpenUserDto = new OwnerCarOpenUserDto(); |
| | | ownerCarOpenUserDto.setOpenId(openId); |
| | | List<OwnerCarOpenUserDto> ownerCarOpenUserDtos = ownerCarOpenUserV1InnerServiceSMOImpl.queryOwnerCarOpenUsers(ownerCarOpenUserDto); |
| | | if (ownerCarOpenUserDtos != null && ownerCarOpenUserDtos.size() > 0) { |
| | | redirectUrl += ("&carNum=" + ownerCarOpenUserDtos.get(0).getCarNum()); |
| | | } |
| | | |
| | | |
| | | //redirectUrl = redirectUrl + (redirectUrl.indexOf("?") > 0 ? "&key=" + tmpUserDto.getKey() : "?key=" + tmpUserDto.getKey()); |
| | | return ResultVo.redirectPage(redirectUrl); |
| | | } |
| | | |
| | | /** |
| | | * 公众号登录 |
| | | * |
| | |
| | | String openId = CommonCache.getValue(code); |
| | | String nickname = CommonCache.getValue(code + "-nickname"); |
| | | String headimgurl = CommonCache.getValue(code + "-headimgurl"); |
| | | String unionid = CommonCache.getValue(code + "-unionid"); |
| | | |
| | | if (StringUtil.isEmpty(openId)) { |
| | | responseEntity = new ResponseEntity<>("页面失效,请刷新后重试", HttpStatus.UNAUTHORIZED); |
| | |
| | | JSONObject userOwnerInfo = new JSONObject(); |
| | | OwnerAppUserDto ownerAppUserDto = new OwnerAppUserDto(); |
| | | ownerAppUserDto.setOpenId(openId); |
| | | ownerAppUserDto.setUnionId(unionid); |
| | | // ownerAppUserDto.setNickName(StringUtil.encodeEmoji(nickname)); |
| | | ownerAppUserDto.setNickName(nickname); |
| | | ownerAppUserDto.setHeadImgUrl(headimgurl); |
| | |
| | | "", "", "", "", |
| | | pd.getAppId()); |
| | | responseEntity = this.callCenterService(restTemplate, pd, "", |
| | | ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId=" |
| | | "smallWeChat.listSmallWeChats?appId=" |
| | | + paramIn.getString("appId") + "&page=1&row=1&communityId=" + ownerAppUserDtos.get(0).getCommunityId(), HttpMethod.GET); |
| | | |
| | | if (responseEntity.getStatusCode() != HttpStatus.OK) { |
| | |
| | | "", "", "", "", |
| | | pd.getAppId()); |
| | | responseEntity = this.callCenterService(restTemplate, pd, "", |
| | | ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId=" |
| | | "smallWeChat.listSmallWeChats?appId=" |
| | | + paramIn.getString("appId") + "&page=1&row=1", HttpMethod.GET); |
| | | |
| | | if (responseEntity.getStatusCode() != HttpStatus.OK) { |
| | |
| | | public void setRestTemplate(RestTemplate restTemplate) { |
| | | this.restTemplate = restTemplate; |
| | | } |
| | | |
| | | |
| | | } |