Browse Source

1.增加梦天工厂数据token 获取

test
pref_mail@163.com 2 years ago
parent
commit
e6ccc3a123
  1. 48
      blade-service/logpm-factory/src/main/java/com/logpm/factory/mt/service/impl/MtFactoryDataServiceImpl.java
  2. 23
      blade-service/logpm-factory/src/main/java/com/logpm/factory/props/MtFactoryProperties.java
  3. 6
      blade-service/logpm-factory/src/main/resources/application-dev.yml
  4. 26
      blade-service/logpm-factory/src/main/resources/application-prod.yml
  5. 28
      blade-service/logpm-factory/src/main/resources/application-test.yml
  6. 49
      blade-service/logpm-factory/src/test/java/com/logpm/factory/TestService.java
  7. 60
      blade-service/logpm-factory/src/test/java/com/logpm/factory/launch/FactoryLauncherServiceImpl.java

48
blade-service/logpm-factory/src/main/java/com/logpm/factory/mt/service/impl/MtFactoryDataServiceImpl.java

@ -2,6 +2,7 @@ package com.logpm.factory.mt.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.Gson; import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.GsonBuilder; import com.alibaba.nacos.shaded.com.google.gson.GsonBuilder;
@ -17,6 +18,7 @@ import com.logpm.factory.mt.service.IMtFactoryDataService;
import com.logpm.factory.mt.service.IMtFactoryOrderMainService; import com.logpm.factory.mt.service.IMtFactoryOrderMainService;
import com.logpm.factory.mt.service.IMtFactoryOrderService; import com.logpm.factory.mt.service.IMtFactoryOrderService;
import com.logpm.factory.mt.service.IMtPackageInfoService; import com.logpm.factory.mt.service.IMtPackageInfoService;
import com.logpm.factory.props.MtFactoryProperties;
import com.logpm.factory.snm.bean.Resp; import com.logpm.factory.snm.bean.Resp;
import com.logpm.factory.snm.entity.MtFactoryOrder; import com.logpm.factory.snm.entity.MtFactoryOrder;
import com.logpm.factory.snm.entity.MtFactoryOrderMain; import com.logpm.factory.snm.entity.MtFactoryOrderMain;
@ -28,9 +30,12 @@ import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springblade.common.exception.CustomerException; import org.springblade.common.exception.CustomerException;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.*; import java.util.*;
@ -47,6 +52,10 @@ public class MtFactoryDataServiceImpl implements IMtFactoryDataService {
private final IAdvanceDetailClient advanceDetailClient; private final IAdvanceDetailClient advanceDetailClient;
private final IWarehouseClient warehouseClient; private final IWarehouseClient warehouseClient;
private final BladeRedis bladeRedis;
private final MtFactoryProperties mtFactoryProperties;
@Transactional @Transactional
@Override @Override
public R dealWithData(MtFactoryDataDTO mtFactoryDataDTO) { public R dealWithData(MtFactoryDataDTO mtFactoryDataDTO) {
@ -151,7 +160,7 @@ public class MtFactoryDataServiceImpl implements IMtFactoryDataService {
String s = gson.toJson(map); String s = gson.toJson(map);
//先获取token //先获取token
String panToken = "getPanToken()"; String panToken = getMtToken();
//请求参数 //请求参数
logger.info("##############handleData: 推送包件状态请求参数 {}",s); logger.info("##############handleData: 推送包件状态请求参数 {}",s);
@ -180,6 +189,43 @@ public class MtFactoryDataServiceImpl implements IMtFactoryDataService {
} }
return Resp.success("SUCCESS"); return Resp.success("SUCCESS");
}
/**
* 获取梦天的工厂的数据token
* @return
*/
public String getMtToken() {
String mtToken =bladeRedis.get("mt_token");
if(StringUtil.isBlank(mtToken)){
// 重新获取
JSONObject jsonObject1 =new JSONObject();
jsonObject1.put("APPKEY",mtFactoryProperties.getAppkey());
jsonObject1.put("APPSECRET",mtFactoryProperties.getAppsecret());
jsonObject1.put("USERID",mtFactoryProperties.getUserid());
jsonObject1.put("USERPWD",mtFactoryProperties.getUserpwd());
String result = HttpRequest.post(mtFactoryProperties.getUrl())
.header("Content-Type","application/json").body(jsonObject1.toJSONString()).timeout(5 * 1000)
.execute()
.body();
logger.info("##############getMengtianToken: 获取梦天token返回参数 {}",result);
//获取的结果转成json对象
JSONObject jsonObject = JSONObject.parseObject(result);
logger.info("##############getMengtianToken: 获取梦天token返回参数 {}",result);
String access_token = jsonObject.getString("Data");
if(StringUtil.hasLength(access_token)){
mtToken= access_token;
// 加入redis
bladeRedis.setEx("mt_token",mtToken,25*60L);
}else{
throw new CustomerException(402,"获取梦天token失败");
}
}
return mtToken;
} }

23
blade-service/logpm-factory/src/main/java/com/logpm/factory/props/MtFactoryProperties.java

@ -0,0 +1,23 @@
package com.logpm.factory.props;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@ConfigurationProperties(prefix = "mengtian")
@Component
public class MtFactoryProperties {
private String url;
private String appkey ;
private String appsecret ;
private String userid ;
private String userpwd ;
}

6
blade-service/logpm-factory/src/main/resources/application-dev.yml

@ -43,8 +43,4 @@ spring:
username: ${blade.datasource.factory.slave.username} username: ${blade.datasource.factory.slave.username}
password: ${blade.datasource.factory.slave.password} password: ${blade.datasource.factory.slave.password}
pan:
url: http://157.122.179.34:15019
clientId: hzero-front-dev
clientSecret: c2VjcmV0
grantType: client_credentials

26
blade-service/logpm-factory/src/main/resources/application-prod.yml

@ -29,3 +29,29 @@ spring:
url: ${blade.datasource.factory.slave.url} url: ${blade.datasource.factory.slave.url}
username: ${blade.datasource.factory.slave.username} username: ${blade.datasource.factory.slave.username}
password: ${blade.datasource.factory.slave.password} password: ${blade.datasource.factory.slave.password}
#rabbitmq配置
rabbitmq:
host: 172.16.128.146
port: 5672
username: admin
password: Slwk@123654
#虚拟host 可以不设置,使用server默认host
virtual-host: /
#确认消息已发送到队列(Queue)
publisher-returns: true
publisher-confirm-type: correlated
# 手动提交消息
listener:
simple:
acknowledge-mode: auto
default-requeue-rejected: false
retry:
enabled: true # 开启消费者失败重试
initial-interval: 1000 # 初识的失败等待时长为1秒
multiplier: 1 # 失败的等待时长倍数,下次等待时长 = multiplier * last-interval
max-attempts: 3 # 最大重试次数
stateless: true # true无状态;false有状态。如果业务中包含事务,这里改为false
direct:
acknowledge-mode: manual
template:
mandatory: true

28
blade-service/logpm-factory/src/main/resources/application-test.yml

@ -29,3 +29,31 @@ spring:
url: ${blade.datasource.factory.slave.url} url: ${blade.datasource.factory.slave.url}
username: ${blade.datasource.factory.slave.username} username: ${blade.datasource.factory.slave.username}
password: ${blade.datasource.factory.slave.password} password: ${blade.datasource.factory.slave.password}
#rabbitmq配置
rabbitmq:
host: 172.16.128.146
port: 5672
username: admin
password: Slwk@123654
#虚拟host 可以不设置,使用server默认host
virtual-host: /
#确认消息已发送到队列(Queue)
publisher-returns: true
publisher-confirm-type: correlated
# 手动提交消息
listener:
simple:
acknowledge-mode: auto
default-requeue-rejected: false
retry:
enabled: true # 开启消费者失败重试
initial-interval: 1000 # 初识的失败等待时长为1秒
multiplier: 1 # 失败的等待时长倍数,下次等待时长 = multiplier * last-interval
max-attempts: 3 # 最大重试次数
stateless: true # true无状态;false有状态。如果业务中包含事务,这里改为false
direct:
acknowledge-mode: manual
template:
mandatory: true

49
blade-service/logpm-factory/src/test/java/com/logpm/factory/TestService.java

@ -1,41 +1,28 @@
package com.logpm.factory; package com.logpm.factory;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.logpm.factory.mt.service.impl.MtFactoryDataServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springblade.core.test.BladeBootTest;
import org.springblade.core.test.BladeSpringExtension;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Random; import java.util.Random;
//@ExtendWith(BladeSpringExtension.class)
//@BladeBootTest(appName = "logpm-factory", enableLoader = true)
public class TestService { public class TestService {
public static void main(String[] args) { @Autowired
private MtFactoryDataServiceImpl mtFactoryDataService;
@Test
public void contextLoads() {
String code = getCharAndNumr(32); String tolk =mtFactoryDataService.getMtToken();
System.out.println(code); System.out.println(tolk);
}
/**
* 方法1生成随机数字和字母组合
* @param length
* @return
*/
public static String getCharAndNumr(int length) {
Random random = new Random();
StringBuffer valSb = new StringBuffer();
String charStr = "0123456789abcdefghijklmnopqrstuvwxyz";
int charLength = charStr.length();
for (int i = 0; i < length; i++) {
int index = random.nextInt(charLength);
valSb.append(charStr.charAt(index));
}
return valSb.toString();
} }

60
blade-service/logpm-factory/src/test/java/com/logpm/factory/launch/FactoryLauncherServiceImpl.java

@ -0,0 +1,60 @@
/*
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the dreamlu.net developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: Chill 庄骞 (smallchill@163.com)
*/
package com.logpm.factory.launch;
import org.springblade.core.auto.service.AutoService;
import org.springblade.core.launch.constant.NacosConstant;
import org.springblade.core.launch.service.LauncherService;
import org.springblade.core.launch.utils.PropsUtil;
import org.springframework.boot.builder.SpringApplicationBuilder;
import java.util.Properties;
/**
* 启动参数拓展
*
* @author Chill
*/
@AutoService(LauncherService.class)
public class FactoryLauncherServiceImpl implements LauncherService {
@Override
public void launcher(SpringApplicationBuilder builder, String appName, String profile, boolean isLocalDev) {
Properties props = System.getProperties();
// 开启多数据源
PropsUtil.setProperty(props, "spring.datasource.dynamic.enabled", "true");
// 指定注册配置信息
PropsUtil.setProperty(props, "spring.cloud.nacos.config.extension-configs[0].data-id", NacosConstant.dataId("factory", profile));
PropsUtil.setProperty(props, "spring.cloud.nacos.config.extension-configs[0].group", NacosConstant.NACOS_CONFIG_GROUP);
PropsUtil.setProperty(props, "spring.cloud.nacos.config.extension-configs[0].refresh", NacosConstant.NACOS_CONFIG_REFRESH);
// 指定注册IP
// PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.ip", "127.0.0.1");
// 指定注册端口
// PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.port", "8200");
// 自定义命名空间
// PropsUtil.setProperty(props, "spring.cloud.nacos.config.namespace", LauncherConstant.NACOS_NAMESPACE);
// PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.namespace", LauncherConstant.NACOS_NAMESPACE);
// 自定义分组
// PropsUtil.setProperty(props, "spring.cloud.nacos.config.group", NacosConstant.NACOS_CONFIG_GROUP);
// PropsUtil.setProperty(props, "spring.cloud.nacos.discovery.group", NacosConstant.NACOS_CONFIG_GROUP);
}
@Override
public int getOrder() {
return 20;
}
}
Loading…
Cancel
Save