|
@@ -0,0 +1,280 @@
|
|
|
|
+package info.aspirecn.cloud.yysj.cloudyysjlogin.service.impl;
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.entity.database.sjjh_entity.Result;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.entity.request.sjjh.CustomerInfo;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.entity.request.sjjh.User;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.entity.request.sjjh.UserAccountRes;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.enums.MPEnum;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.service.SjjhOrderService;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.utils.SslUtils;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.entity.database.YysjUser;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.entity.request.GetTokenRequest;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.exceptions.SystemBusyException;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.exceptions.UserNotExistException;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.mapper.YysjUserMapper;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.service.CreatUserService;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.service.LoginTokenService;
|
|
|
|
+import info.aspirecn.cloud.yysj.cloudyysjlogin.utils.JwtUtils;
|
|
|
|
+import net.sf.json.JSONObject;
|
|
|
|
+import net.sf.json.xml.XMLSerializer;
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 获取token
|
|
|
|
+ *
|
|
|
|
+ * @author CY
|
|
|
|
+ * return 加密的token
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+
|
|
|
|
+public class LoginTokenServiceImpl implements LoginTokenService {
|
|
|
|
+ public static final String NO_VALUE = "null";
|
|
|
|
+ @Autowired
|
|
|
|
+ private RestTemplate restTemplate;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private YysjUserMapper yysjUserMapper;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private CreatUserService creatUserService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private SjjhOrderService sjjhOrderService;
|
|
|
|
+ @Value("${auth.token.expire}")
|
|
|
|
+ private Integer authTokenExpire;
|
|
|
|
+ @Value("${yysj.logisticsPlatform.tokenUrl}")
|
|
|
|
+ private String logisticsPlatformTokenUrl;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 用户需要访问的服务资源的id
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ @Value("${yysj.logisticsPlatform.resource}")
|
|
|
|
+ private String resource;
|
|
|
|
+
|
|
|
|
+ @Value("${yysj.logisticsPlatform.userInfoUrl}")
|
|
|
|
+ private String userInfoUrl;
|
|
|
|
+
|
|
|
|
+ @Value("${yysj.sjjh.info.contactPerson}")
|
|
|
|
+ private String contactPerson;
|
|
|
|
+
|
|
|
|
+ @Value("${yysj.sjjh.info.contactPhone}")
|
|
|
|
+ private String contactPhone;
|
|
|
|
+
|
|
|
|
+ @Value("${yysj.sjjh.info.customerSource}")
|
|
|
|
+ private Integer customerSource;
|
|
|
|
+
|
|
|
|
+ @Value(("${yysj.redis.prefix}"))
|
|
|
|
+ private String prefix;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取token
|
|
|
|
+ *
|
|
|
|
+ * @param getTokenRequest 用户信息实体类
|
|
|
|
+ * @return 用户id、申请人的令牌
|
|
|
|
+ * @throws Exception 忽略https证书抛出的异常
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public String getLoginToken(GetTokenRequest getTokenRequest) throws Exception {
|
|
|
|
+ //国家物流平台验证用户是否存在
|
|
|
|
+ String token = getTokenFromPlatform(getTokenRequest.getUserId(), getTokenRequest.getPassword());
|
|
|
|
+ //判断token是否有值,没值代表用户名密码远端验证不通过
|
|
|
|
+ if (NO_VALUE.equals(token)) {
|
|
|
|
+ throw new UserNotExistException("用户不存在");
|
|
|
|
+ }
|
|
|
|
+ //查询本地库是否有用户信息
|
|
|
|
+ int count = yysjUserMapper.selectCount((new QueryWrapper<YysjUser>().eq("userId", getTokenRequest.getUserId())));
|
|
|
|
+ //如果本地库不存在
|
|
|
|
+ if (count == 0) {
|
|
|
|
+ //去出数据交互创建用户,并获取appId和secret放在map中
|
|
|
|
+ Map<String, Object> sjjhUserInfoLists = userInfoFromSjjh(getTokenRequest.getUserId());
|
|
|
|
+ //去国家物流平台获取用户信息得到username
|
|
|
|
+ String username = userInfoFromLogisticsPlatform(getTokenRequest.getUserId(), token);
|
|
|
|
+ //将数据加载到本地库实体类中
|
|
|
|
+ YysjUser user = new YysjUser();
|
|
|
|
+ user.setUserId(getTokenRequest.getUserId());
|
|
|
|
+ user.setAppId(String.valueOf(sjjhUserInfoLists.get("appId")));
|
|
|
|
+ user.setSecret(String.valueOf(sjjhUserInfoLists.get("secret")));
|
|
|
|
+ user.setSjjhUserId(Integer.parseInt(String.valueOf(sjjhUserInfoLists.get("sjjhUserId"))));
|
|
|
|
+ user.setUsername(username);
|
|
|
|
+ //将数据存到到本地库
|
|
|
|
+ yysjUserMapper.insert(user);
|
|
|
|
+ }
|
|
|
|
+ //将userid通过jwt加密成token
|
|
|
|
+ String userId = getTokenRequest.getUserId();
|
|
|
|
+ String tokens;
|
|
|
|
+ tokens = encipherUserId(userId);
|
|
|
|
+ //将用户名存在redis中
|
|
|
|
+ String key = prefix + userId;
|
|
|
|
+ redisTemplate.opsForValue().set(key, tokens, 30, TimeUnit.MINUTES);
|
|
|
|
+ return tokens;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 国家物流平台获取token
|
|
|
|
+ *
|
|
|
|
+ * @param userId 用户名
|
|
|
|
+ * @param password 密码
|
|
|
|
+ * @return token
|
|
|
|
+ * @throws Exception 忽略证书抛出的异常
|
|
|
|
+ */
|
|
|
|
+ private String getTokenFromPlatform(String userId, String password) throws Exception {
|
|
|
|
+ //忽略证书
|
|
|
|
+ SslUtils.ignoreSsl();
|
|
|
|
+ /* 从国家物流平台获取token的请求参数列表 */
|
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
|
+ //添加参数
|
|
|
|
+ params.put("userid", userId);
|
|
|
|
+ params.put("password", password);
|
|
|
|
+ params.put("resource", resource);
|
|
|
|
+ //调接口获取数据
|
|
|
|
+ String remoteResult = restTemplate.getForEntity(logisticsPlatformTokenUrl, String.class, params).getBody();
|
|
|
|
+ if (StringUtils.isEmpty(remoteResult)) {
|
|
|
|
+ throw new SystemBusyException("国家物流平台验证token失败");
|
|
|
|
+ }
|
|
|
|
+ //将xml字符串转为json
|
|
|
|
+ JSONObject result = JSONObject.fromObject(remoteResult);
|
|
|
|
+ //获取json串中token字段
|
|
|
|
+ return result.get("token").toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将用户名加密生成token
|
|
|
|
+ *
|
|
|
|
+ * @param userid 用户名
|
|
|
|
+ * @return token
|
|
|
|
+ */
|
|
|
|
+ private String encipherUserId(String userid) {
|
|
|
|
+ // 设置 Token 中的存的内容
|
|
|
|
+ Map<String, String> claim = new HashMap<>();
|
|
|
|
+ claim.put("userid", userid);
|
|
|
|
+ // 创建 Token,设置 60 分钟过期
|
|
|
|
+ String token = JwtUtils.createToken(claim, authTokenExpire);
|
|
|
|
+ return token;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 到数据交互平台创建客户,并获取appId,secret
|
|
|
|
+ *
|
|
|
|
+ * @return appId和secret
|
|
|
|
+ */
|
|
|
|
+ private Map<String, Object> userInfoFromSjjh(String userId) {
|
|
|
|
+ //查找数据交互平台是否有该用户
|
|
|
|
+ Map<String, String> infoFromSjjh = creatUserService.queryAppByCustomName(userId);
|
|
|
|
+ CustomerInfo customerInfo = new CustomerInfo();
|
|
|
|
+ Integer sjjhUserId=null;
|
|
|
|
+ if (infoFromSjjh.size() == 0 || infoFromSjjh.isEmpty()) {
|
|
|
|
+ customerInfo.setEnterpriseName(userId);
|
|
|
|
+ customerInfo.setContactPerson(contactPerson);
|
|
|
|
+ customerInfo.setContactPhone(contactPhone);
|
|
|
|
+ customerInfo.setCustomerSource(customerSource);
|
|
|
|
+ Result result = creatUserService.customerInfoAdd(customerInfo);
|
|
|
|
+ String code = result.getCode();
|
|
|
|
+ if (!MPEnum.SJJH_CREAT_USER_SECUESS.getCode().equals(code)) {
|
|
|
|
+ throw new SystemBusyException("数据交互创建用户失败");
|
|
|
|
+ }
|
|
|
|
+ //数据交互平台获取用户信息
|
|
|
|
+ infoFromSjjh = creatUserService.queryAppByCustomName(userId);
|
|
|
|
+ }
|
|
|
|
+ String appId = infoFromSjjh.get("appId");
|
|
|
|
+ String secret = infoFromSjjh.get("secret");
|
|
|
|
+ //数据交互根据appId和secret查询用户id
|
|
|
|
+ User user = creatUserService.getUserInfo(appId,secret);
|
|
|
|
+ sjjhUserId = user.getId();
|
|
|
|
+ //去数据交互充值库查用户
|
|
|
|
+ UserAccountRes userAccountRes = sjjhOrderService.getUserAccount(sjjhUserId);
|
|
|
|
+ //如果没有这个用户则插入一条数据
|
|
|
|
+ if(userAccountRes.getUserId()==null){
|
|
|
|
+ boolean addUser = sjjhOrderService.addAccountForNewUser(sjjhUserId, (long) 0);
|
|
|
|
+ if(addUser==false){
|
|
|
|
+ throw new SystemBusyException("数据交互充值表插入数据失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //获取appId和secret和userId
|
|
|
|
+ Map<String, Object> sjjhUserInfoLists = new HashMap<>();
|
|
|
|
+ sjjhUserInfoLists.put("appId",appId);
|
|
|
|
+ sjjhUserInfoLists.put("secret",secret);
|
|
|
|
+ sjjhUserInfoLists.put("sjjhUserId",sjjhUserId);
|
|
|
|
+ return sjjhUserInfoLists;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取国家物流平台用户信息
|
|
|
|
+ *
|
|
|
|
+ * @return 返回物流平台用户信息
|
|
|
|
+ * @throws Exception 忽略证书时抛出异常
|
|
|
|
+ */
|
|
|
|
+ private String userInfoFromLogisticsPlatform(String userId, String token) throws Exception {
|
|
|
|
+ //忽略证书
|
|
|
|
+ SslUtils.ignoreSsl();
|
|
|
|
+ //创建消息头
|
|
|
|
+ HttpHeaders requestHeader = new HttpHeaders();
|
|
|
|
+ //添加xml类型消息头
|
|
|
|
+ requestHeader.setContentType(MediaType.APPLICATION_XML);
|
|
|
|
+ StringBuffer xmlString = new StringBuffer();
|
|
|
|
+ //拼接请求body
|
|
|
|
+ xmlString.append("<?xml version=\"1.0\" encoding=\"utf-16\"?>")
|
|
|
|
+ .append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">")
|
|
|
|
+ .append("<soap:Body>")
|
|
|
|
+ .append("<userGeneralQuery xmlns=\"http://client.ws.logink.org/SearchService\">")
|
|
|
|
+ .append("<applicantToken xmlns=\"\">" + token + "</applicantToken>")
|
|
|
|
+ .append(" <username xmlns=\"\"></username>")
|
|
|
|
+ .append(" <userid xmlns=\"\">" + userId + "</userid>\"")
|
|
|
|
+ .append(" </userGeneralQuery>")
|
|
|
|
+ .append(" </soap:Body>")
|
|
|
|
+ .append("</soap:Envelope>");
|
|
|
|
+ // 创建 HttpEntity
|
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<>(xmlString.toString(), requestHeader);
|
|
|
|
+ //发送请求
|
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(userInfoUrl, requestEntity, String.class);
|
|
|
|
+ /* 获取返回的xml格式body的字符串 */
|
|
|
|
+ String xmlbody = responseEntity.getBody();
|
|
|
|
+ return convert(xmlbody);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 提取国家物流平台用户信息的用户名字段
|
|
|
|
+ *
|
|
|
|
+ * @param xmlbody 物流平台返回的xml格式的信息
|
|
|
|
+ * @return String
|
|
|
|
+ */
|
|
|
|
+ private static String convert(String xmlbody) {
|
|
|
|
+ //将xml字符串转换为json格式
|
|
|
|
+ XMLSerializer xmlSerializer = new XMLSerializer();
|
|
|
|
+ String resutStr = xmlSerializer.read(xmlbody).toString();
|
|
|
|
+ JSONObject result = JSONObject.fromObject(resutStr);
|
|
|
|
+ //拆开json串 获取需要的数据
|
|
|
|
+ JSONObject resultInfo;
|
|
|
|
+ resultInfo = result.getJSONObject("soap:Body")
|
|
|
|
+ .getJSONObject("ns2:userGeneralQueryResponse")
|
|
|
|
+ .getJSONObject("SearchResult");
|
|
|
|
+ //获取返回状态码
|
|
|
|
+ String code = resultInfo.get("resultCode").toString();
|
|
|
|
+ //判断状态码如果不是300000则抛出异常
|
|
|
|
+ if (!MPEnum.PLATFORM_GET_USER_INFO_SECUESS.getCode().equals(code)) {
|
|
|
|
+ throw new SystemBusyException("国家物流平台获取用户信息失败");
|
|
|
|
+ }
|
|
|
|
+ //拆分第二层
|
|
|
|
+ JSONObject searchResultList = resultInfo.getJSONArray("searchResult").getJSONObject(0);
|
|
|
|
+ return searchResultList.get("cn").toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|