package com.nova.sankuai.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.nova.sankuai.domain.dto.LiandengPageJumpAutoDownloadDTO; import com.nova.sankuai.domain.dto.LiandengPageJumpAutoLoginDTO; import com.nova.sankuai.domain.entity.UnionloginRelay; import com.nova.sankuai.infra.config.CommonException; import com.nova.sankuai.infra.mapper.UnionloginRelayMapper; import com.nova.sankuai.infra.utils.RsaSecretUtils; import com.nova.sankuai.service.ILiandengPageJumpService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.regex.Matcher; import java.util.regex.Pattern; @Slf4j @Service public class ILiandengPageJumpServiceImpl implements ILiandengPageJumpService { @Resource RsaSecretUtils rsa; @Resource UnionloginRelayMapper unionloginRelayMapper; @Override public String checkAutoLogin(LiandengPageJumpAutoLoginDTO param) throws Exception { byte[] autoLoginBytes = rsa.base642Byte(param.getAutologin()); JSONObject autoLogin = JSON.parseObject(new String(rsa.privateDecrypt(autoLoginBytes), StandardCharsets.UTF_8)); if (param.getAppId() == null || !StringUtils.equals(param.getAppId(), autoLogin.getString("appId"))) { throw new CommonException("校验失败,appId 填写错误"); } UnionloginRelay record = unionloginRelayMapper.selectByAppId(param.getAppId()); if (checkParam(param.getAppId(), autoLogin, record)) { String appId = autoLogin.getString("appId"); byte[] bytes = RsaSecretUtils.publicEncrypt(autoLogin.toJSONString().getBytes(StandardCharsets.UTF_8), record.getPublicKey()); String enc = Base64.getEncoder().encodeToString(bytes); return String.format(record.getDistUrl(), appId, URLEncoder.encode(enc, "UTF-8")); } throw new CommonException("校验失败,不存在对应渠道"); } private boolean checkParam(String appId, JSONObject json, UnionloginRelay record) { if (record == null) throw new CommonException("校验失败,appId 不存在"); if (record.getEnable() != 1) throw new CommonException("跳转规则未启用"); Pattern pattern = Pattern.compile(record.getChannelCodePattern()); Matcher matcher = pattern.matcher(json.getString("channelCode")); if (matcher.matches()) { json.put("appId", record.getAppIdOut()); return true; } return false; } @Override public boolean checkAutoDownload(LiandengPageJumpAutoDownloadDTO param) throws Exception { byte[] autodownloadBytes = rsa.base642Byte(param.getAutodownload()); JSONObject autoDownload = JSON.parseObject(new String(rsa.privateDecrypt(autodownloadBytes), StandardCharsets.UTF_8)); if (param.getAppId() == null || !StringUtils.equals(param.getAppId(), autoDownload.getString("appId"))) { throw new CommonException("校验失败,appId 填写错误"); } UnionloginRelay record = unionloginRelayMapper.selectByAppId(param.getAppId()); return checkParam(param.getAppId(), autoDownload, record); } }