1、高德天气预报api调用 feign 降级 factory编写

2、factory类中相关魔法值优化
pull/254/head
xjs 4 years ago
parent ea5dd49c7a
commit dffb2aa293

@ -85,7 +85,7 @@
</el-button>
</el-col>
<el-col :span="1.5">
<router-link :to="'/openapi/english/collect/'" class="link-type">
<router-link :to="'/business/english/collect/'" class="link-type">
<el-button
type="warning"
plain

@ -0,0 +1,18 @@
package com.xjs.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
*
* @author xiejs
* @since 2022-01-16
*/
@Data
@ConfigurationProperties(prefix = "gaode.open")
@Component
public class GaodeProperties {
private String key;
}

@ -43,6 +43,8 @@ public class ApiConst {
public static final String ROLL_IP= "ROLL-IP信息";
public static final String GAODE_WEATHER= "高德-天气预报";
@ -84,6 +86,51 @@ public class ApiConst {
public static final String ROLL_IP_URL= "https://www.mxnzp.com/api/ip/aim_ip";
/**
*
* https://lbs.amap.com/api/webservice/guide/api/weatherinfo
*/
public static final String GAODE_WEATHER_URL= "https://restapi.amap.com/v3/weather/weatherInfo";
//-----------------------api请求参数常量-----------------------------
/**
* Extensions
*/
public static final String GAODE_EXTENSIONS_ALL="all" ;
public static final String GAODE_EXTENSIONS_BASE="base" ;
//-----------------------api响应参数-----------------------------------
/**
* rollcode
*/
public static final Integer ROLL_CODE_SUCCESS=1 ;
//---------------------自定义相关请求响应常量----------------------------
/**
* key
*/
public static final String DEMOTE_ERROR= "error";

@ -19,26 +19,26 @@ import java.util.regex.Pattern;
public class IPUtils {
/**
* ip
* ip
* @return
*/
public static String getV4IP() throws Exception {
String ip = null;
// 第种方式
// 第种方式
try {
ip = IPUtils.getNowIP1();
ip = IPUtils.getNowIP2();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP1 failed ~ ");
System.out.println("getPublicIP - getNowIP2 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;
// 第种方式
// 第种方式
try {
ip = IPUtils.getNowIP2();
ip = IPUtils.getNowIP1();
ip.trim();
} catch (Exception e) {
System.out.println("getPublicIP - getNowIP2 failed ~ ");
System.out.println("getPublicIP - getNowIP1 failed ~ ");
}
if (!StringUtils.isEmpty(ip))
return ip;

@ -19,6 +19,8 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
*
*
@ -39,7 +41,7 @@ public class TianXingAWordFactory implements AWordFactory {
public ApiAWord productApiAWord(RequestBody requestBody) {
requestBody.setKey(tianXingProperties.getKey());
JSONObject jsonObject = tianXingMMYJFeignClient.aWordApi(requestBody);
if (jsonObject.containsKey("error")) {
if (jsonObject.containsKey(DEMOTE_ERROR)) {
throw new ApiException("天行每日一句接口调用异常");
}
if (HttpStatus.HTTP_OK == jsonObject.getInteger("code")) {

@ -0,0 +1,27 @@
package com.xjs.common.client.api.gaode;
import com.alibaba.fastjson.JSONObject;
import com.xjs.annotation.ApiLog;
import com.xjs.common.client.factory.GaodeWeatherFeignFactory;
import com.xjs.weather.domain.RequestBody;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import static com.xjs.consts.ApiConst.GAODE_WEATHER;
import static com.xjs.consts.ApiConst.GAODE_WEATHER_URL;
/**
* api feign
* @author xiejs
* @since 2022-01-16
*/
@FeignClient(name = "gaodeWeather", url = GAODE_WEATHER, fallbackFactory = GaodeWeatherFeignFactory.class)
public interface GaodeWeatherFeignClient {
@GetMapping()
@ApiLog(name = GAODE_WEATHER,
url = GAODE_WEATHER_URL,
method = "Get")
JSONObject WeatherApi(@SpringQueryMap RequestBody requestBody);
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -20,7 +22,7 @@ public class BaiduFeignFactory implements FallbackFactory<BaiduFeignClient> {
log.error("api模块百度翻译服务调用失败:{},执行降级处理", cause.getMessage());
return qo -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -0,0 +1,30 @@
package com.xjs.common.client.factory;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.xjs.common.client.api.gaode.GaodeWeatherFeignClient;
import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* api
* @author xiejs
* @since 2022-01-16
*/
@Component
@Log4j2
public class GaodeWeatherFeignFactory implements FallbackFactory<GaodeWeatherFeignClient> {
@Override
public GaodeWeatherFeignClient create(Throwable cause) {
log.error("api模块高德天气预报服务调用失败:{},执行降级处理", cause.getMessage());
return requestBody -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @since 2022-01-15
@ -20,7 +22,7 @@ public class RollIPFeignFactory implements FallbackFactory<RollIPFeignClient> {
log.error("api模块roll IP服务调用失败:{},执行降级处理", cause.getMessage());
return requestBody -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* roll
* @author xiejs
@ -20,7 +22,7 @@ public class RollMMYJFeignFactory implements FallbackFactory<RollMMYJFeignClient
log.error("api模块roll每日一句服务调用失败:{},执行降级处理", cause.getMessage());
return requestBody -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* roll
* @author xiejs
@ -20,7 +22,7 @@ public class RollTranslationFeignFactory implements FallbackFactory<RollTranslat
log.error("api模块roll翻译服务调用失败:{},执行降级处理", cause.getMessage());
return qo -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @since 2022-01-11
@ -20,7 +22,7 @@ public class TianXingBDRSFeignFactory implements FallbackFactory<TianXingBDRSFei
return key -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @since 2022-01-12
@ -19,7 +21,7 @@ public class TianXingDYRSFeignFactory implements FallbackFactory<TianXingDYRSFei
log.error("api模块天行抖音热搜榜服务调用失败:{},执行降级处理", cause.getMessage());
return key -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -22,7 +24,7 @@ public class TianXingJDTCFeignFactory implements FallbackFactory<TianXingJDTCFei
return requestBody -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -22,7 +24,7 @@ public class TianXingMMMYFeignFactory implements FallbackFactory<TianXingMMMYFei
return requestBody -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @since 2022-01-08
@ -20,7 +22,7 @@ public class TianXingMMYJFeignFactory implements FallbackFactory<TianXingMMYJFei
log.error("api模块天行每日一句服务调用失败:{},执行降级处理", cause.getMessage());
return requestBody -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -21,7 +23,7 @@ public class TianXingOneEnglishFeignFactory implements FallbackFactory<TianXingO
log.error("api模块英语一言服务调用失败:{},执行降级处理", cause.getMessage());
return requestBody -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -21,7 +23,7 @@ public class TianXingPYQFeignFactory implements FallbackFactory<TianXingPYQFeign
return requestBody -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @since 2022-01-10
@ -20,8 +22,7 @@ public class TianXingQWRSFeignFactory implements FallbackFactory<TianXingQWRSFei
log.error("api模块天行全网热搜榜服务调用失败:{},执行降级处理", cause.getMessage());
return key -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc api
@ -20,7 +22,7 @@ public class TianXingTranDictFeignFactory implements FallbackFactory<TianXingTra
log.error("api模块翻译字典服务调用失败:{},执行降级处理", cause.getMessage());
return requestBody -> {
JSONObject jsonObject = new JSONObject();
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* feign
* @author xiejs
@ -21,7 +23,7 @@ public class TianXingWBRSFeignFactory implements FallbackFactory<TianXingWBRSFei
return key -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @since 2022-01-11
@ -20,7 +22,7 @@ public class TianXingWXRSFeignFactory implements FallbackFactory<TianXingWXRSFei
return key -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -21,7 +23,7 @@ public class TianXingWYYFeignFactory implements FallbackFactory<TianXingWYYFeign
return requestBody -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -7,6 +7,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -22,7 +24,7 @@ public class YouDaoFeignFactory implements FallbackFactory<YouDaoFeignClient> {
return qo -> {
JSONObject jsonObject = new JSONObject();
//构建一个异常json给下层接口处理
jsonObject.put("error", R.FAIL);
jsonObject.put(DEMOTE_ERROR, R.FAIL);
return jsonObject;
};
}

@ -19,6 +19,8 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* roll
* @author xiejs
@ -41,7 +43,7 @@ public class RollMMYJCopyWritingFactory implements CopyWritingFactory {
requestBody.setApp_id(rollProperties.getApp_id());
requestBody.setApp_secret(rollProperties.getApp_secret());
JSONObject jsonObject = rollMMYJFeignClient.copyWritingApi(requestBody);
if (jsonObject.containsKey("error")) {
if (jsonObject.containsKey(DEMOTE_ERROR)) {
throw new ApiException("roll每日一句接口调用异常");
}
JSONArray jsonArray = jsonObject.getJSONArray("data");

@ -17,6 +17,8 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -51,7 +53,7 @@ public class TianXingJDTCCopyWritingFactory implements CopyWritingFactory {
return copyWriting;
}else {
//调用服务失败的降级之后的处理
if (jsonObject.containsKey("error")) {
if (jsonObject.containsKey(DEMOTE_ERROR)) {
return copyWritingMapper.getOneToRandom();
}
return new CopyWriting();

@ -17,6 +17,8 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -51,7 +53,7 @@ public class TianXingMMMYCopyWritingFactory implements CopyWritingFactory {
return copyWriting;
}else {
//调用服务失败的降级之后的处理
if (jsonObject.containsKey("error")) {
if (jsonObject.containsKey(DEMOTE_ERROR)) {
return copyWritingMapper.getOneToRandom();
}
return new CopyWriting();

@ -17,6 +17,8 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -53,7 +55,7 @@ public class TianXingPYQCopyWritingFactory implements CopyWritingFactory {
return copyWriting;
}else {
//调用服务失败的降级之后的处理
if (jsonObject.containsKey("error")) {
if (jsonObject.containsKey(DEMOTE_ERROR)) {
return copyWritingMapper.getOneToRandom();
}
return new CopyWriting();

@ -17,6 +17,8 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -53,7 +55,7 @@ public class TianXingWYYCopyWritingFactory implements CopyWritingFactory {
return copyWriting;
}else {
//调用服务失败的降级之后的处理
if (jsonObject.containsKey("error")) {
if (jsonObject.containsKey(DEMOTE_ERROR)) {
return copyWritingMapper.getOneToRandom();
}
return new CopyWriting();

@ -1,4 +1,4 @@
package com.xjs.oneenglish.factory;
package com.xjs.oneenglish.factory.impl;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONArray;
@ -9,6 +9,7 @@ import com.xjs.config.TianXingProperties;
import com.xjs.exception.ApiException;
import com.xjs.oneenglish.domain.ApiEnglish;
import com.xjs.oneenglish.domain.RequestBody;
import com.xjs.oneenglish.factory.OneEnglishFactory;
import com.xjs.oneenglish.mapper.ApiEnglishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -19,6 +20,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
import static com.xjs.consts.RedisConst.ONE_ENGLISH;
import static com.xjs.consts.RedisConst.ONE_ENGLISH_EXPIRE;
@ -52,7 +54,7 @@ public class TianXingOneEnglishFactory implements OneEnglishFactory {
}
requestBody.setKey(tianXingProperties.getKey());
JSONObject jsonObject = tianXingOneEnglishFeignClient.oneEnglishApi(requestBody);
if (!jsonObject.containsKey("error")) {
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
if (jsonObject.getInteger("code") == HttpStatus.HTTP_OK) {
JSONArray newslist = jsonObject.getJSONArray("newslist");
JSONObject content = newslist.getJSONObject(0);

@ -1,12 +1,10 @@
package com.xjs.topsearch.factory.impl;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.HttpStatus;
import com.xjs.common.client.api.tianxing.TianXingQWRSFeignClient;
import com.xjs.config.TianXingProperties;
import com.xjs.exception.ApiException;
import com.xjs.topsearch.domain.ApiTopsearchAllnetwork;
import com.xjs.topsearch.factory.TopserachFactory;
import com.xjs.topsearch.service.ApiTopsearchAllnetworkService;
@ -17,9 +15,10 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
*
*
@ -42,7 +41,7 @@ public class TianXingTopsearchAllnetworkFactory implements TopserachFactory<ApiT
@Transactional
public List<ApiTopsearchAllnetwork> topSearchApi() {
JSONObject jsonObject = tianXingQWRSFeignClient.topSearchApi(tianXingProperties.getKey());
if (!jsonObject.containsKey("error")) {
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
if (jsonObject.getInteger("code") == HttpStatus.SUCCESS) {
JSONArray newslist = jsonObject.getJSONArray("newslist");
List<ApiTopsearchAllnetwork> collect = newslist.stream().map(arrayJson -> {

@ -17,6 +17,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
*
*
@ -37,7 +39,7 @@ public class TianXingTopsearchBaiduFactory implements TopserachFactory<ApiTopsea
@Override
public List<ApiTopsearchBaidu> topSearchApi() {
JSONObject jsonObject = tianXingBDRSFeignClient.topSearchApi(tianXingProperties.getKey());
if (!jsonObject.containsKey("error")) {
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
if (jsonObject.getInteger("code") == HttpStatus.SUCCESS) {
JSONArray newslist = jsonObject.getJSONArray("newslist");
List<ApiTopsearchBaidu> collect = newslist.stream().map(arrayJson -> {

@ -19,6 +19,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
*
*
@ -42,7 +44,7 @@ public class TianXingTopsearchDouyinFactory implements TopserachFactory<ApiTopse
@Override
public List<ApiTopsearchDouyin> topSearchApi() {
JSONObject jsonObject = tianXingDYRSFeignClient.topSearchApi(tianXingProperties.getKey());
if (!jsonObject.containsKey("error")) {
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
if (jsonObject.getInteger("code") == HttpStatus.SUCCESS) {
JSONArray newslist = jsonObject.getJSONArray("newslist");
List<ApiTopsearchDouyin> collect = newslist.stream().map(arrayJson -> {

@ -18,6 +18,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* api
*
@ -40,7 +42,7 @@ public class TianXingTopsearchWechatFactory implements TopserachFactory<ApiTopse
@Transactional
public List<ApiTopsearchWechat> topSearchApi() {
JSONObject jsonObject = tianXingWXRSFeignClient.topSearchApi(tianXingProperties.getKey());
if (!jsonObject.containsKey("error")) {
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
if (jsonObject.getInteger("code") == HttpStatus.SUCCESS) {
JSONArray newslist = jsonObject.getJSONArray("newslist");
List<ApiTopsearchWechat> collect = newslist.stream().map(arrayJson -> {

@ -18,6 +18,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* api
*
@ -42,7 +44,7 @@ public class TianXingTopsearchWeiboFactory implements TopserachFactory<ApiTopsea
@Transactional
public List<ApiTopsearchWeibo> topSearchApi() {
JSONObject jsonObject = tianXingWBRSFeignClient.topSearchApi(tianXingProperties.getKey());
if (!jsonObject.containsKey("error")) {
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
if (jsonObject.getInteger("code") == HttpStatus.SUCCESS) {
JSONArray newslist = jsonObject.getJSONArray("newslist");
List<ApiTopsearchWeibo> collect = newslist.stream().map(arrayJson -> {

@ -15,6 +15,8 @@ import org.springframework.stereotype.Service;
import java.util.*;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -43,7 +45,7 @@ public class BaiDuTranslationFactory implements TranslationFactory {
JSONObject jsonObject = baiduFeignClient.translationApi(baiDuTranslationQo);
System.out.println(jsonObject);
//接口内部错误以及网络错误都抛异常
if(jsonObject.containsKey("error_code") || jsonObject.containsKey("error")){
if(jsonObject.containsKey("error_code") || jsonObject.containsKey(DEMOTE_ERROR)){
throw new ApiException("百度翻译接口调用异常");
}
TranslationVo translationVo = new TranslationVo();

@ -15,6 +15,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* roll
*
@ -35,7 +37,7 @@ public class RollTranslationFactory implements TranslationFactory {
rollTranslationQo.setApp_secret(rollProperties.getApp_secret());
rollTranslationQo.setContent(translationQo.getQ());
JSONObject translationApi = rollTranslationFeignClient.translationApi(rollTranslationQo);
if (translationApi.containsKey("error")) {
if (translationApi.containsKey(DEMOTE_ERROR)) {
throw new ApiException("ROLL翻译接口调用异常");
}
if (translationApi.getInteger("code") == 1) {

@ -13,6 +13,8 @@ import com.xjs.translation.factory.TranslationFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -32,7 +34,7 @@ public class TianXingTranDictFactory implements TranslationFactory {
requestBody.setKey(tianXingProperties.getKey());
JSONObject jsonObject = tianXingTranDictClient.tranDictApi(requestBody);
TranslationVo translationVo = new TranslationVo();
if (!jsonObject.containsKey("error")) {
if (!jsonObject.containsKey(DEMOTE_ERROR)) {
//代表没进入降级类
if (jsonObject.getInteger("code") == 250) {
throw new ApiException("内容输入错误");

@ -15,6 +15,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
/**
* @author xiejs
* @desc
@ -32,7 +34,7 @@ public class YouDaoTranslationFactory implements TranslationFactory {
youDaoTranslationQo.setI(translationQo.getQ());
JSONObject translationApi = youDaoFeignClient.translationApi(youDaoTranslationQo);
//接口内部错误以及网络错误都抛异常
if(!"0".equals(translationApi.getString("errorCode"))|| translationApi.containsKey("error") ){
if(!"0".equals(translationApi.getString("errorCode"))|| translationApi.containsKey(DEMOTE_ERROR) ){
if(!"40".equals(translationApi.getString("errorCode"))){
throw new ApiException("有道翻译接口调用异常");
}

@ -0,0 +1,65 @@
package com.xjs.weather.domain;
import lombok.Data;
/**
*
* @author xiejs
* @since 2022-01-16
*/
@Data
public class Casts {
/**
*
*/
private String date;
/**
*
*/
private String week;
/**
*
*/
private String dayweather;
/**
*
*/
private String nightweather;
/**
*
*/
private String daytemp;
/**
*
*/
private String nighttemp;
/**
*
*/
private String daywind;
/**
*
*/
private String nightwind;
/**
*
*/
private String daypower;
/**
*
*/
private String nightpower;
}

@ -0,0 +1,44 @@
package com.xjs.weather.domain;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
*
*
* @author xiejs
* @since 2022-01-16
*/
@Data
public class ForecastWeather implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
private String city;
/**
*
*/
private String adcode;
/**
*
*/
private String province;
/**
*
*/
private String reporttime;
/**
* listcast,
*/
private List<Casts> CastsList;
}

@ -0,0 +1,89 @@
package com.xjs.weather.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ruoyi.common.core.annotation.Excel;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
*
*
* @author xiejs
* @since 2022-01-16
*/
@Data
@TableName("api_now_weather")
public class NowWeather implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
private Long id;
/**
*
*/
@Excel(name = "省份名")
private String province;
/**
*
*/
@Excel(name = "城市名")
private String city;
/**
*
*/
@Excel(name = "城市编码")
private String adcode;
/**
*
*/
@Excel(name = "天气现象")
private String weather;
/**
*
*/
@Excel(name = "实时气温,单位:摄氏度")
private String temperature;
/**
*
*/
@Excel(name = "风向描述")
private String winddirection;
/**
*
*/
@Excel(name = "风力级别,单位:级")
private String windpower;
/**
* 湿
*/
@Excel(name = "空气湿度")
private String humidity;
/**
*
*/
@Excel(name = "数据发布的时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private Date reporttime;
@Excel(name = "创建时间" ,dateFormat = "yyyy-MM-dd HH:mm:ss")
@TableField(fill = FieldFill.INSERT)
private Date create_time;
}

@ -0,0 +1,43 @@
package com.xjs.weather.domain;
import lombok.Data;
import lombok.experimental.Accessors;
/**
*
* @author xiejs
* @since 2022-01-16
*/
@Data
//链式调用
@Accessors(chain = true)
public class RequestBody {
/**
* :
* webAPIKEY
*/
private String key;
/**
* :
* adcodeadcode
*/
private String city;
/**
* :
* base/all
* base:
* all:
*/
private String extensions;
/**
*:
* JSON,XML
*/
private String output = "JSON";
}

@ -0,0 +1,18 @@
package com.xjs.weather.factory;
/**
* api
* @author xiejs
* @since 2022-01-16
*/
public interface WeatherFactory<T> {
/**
* api
* @return T
*/
T weatherApi();
}

@ -0,0 +1,48 @@
package com.xjs.weather.factory.impl;
import com.alibaba.fastjson.JSONObject;
import com.xjs.common.client.api.gaode.GaodeWeatherFeignClient;
import com.xjs.config.GaodeProperties;
import com.xjs.weather.domain.IPInfoVo;
import com.xjs.weather.domain.NowWeather;
import com.xjs.weather.domain.RequestBody;
import com.xjs.weather.factory.WeatherFactory;
import com.xjs.weather.service.IPService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.GAODE_EXTENSIONS_BASE;
/**
*
* @author xiejs
* @since 2022-01-16
*/
@Component
@Log4j2
public class GaodeNowWeatherFactory implements WeatherFactory<NowWeather> {
@Autowired
private GaodeProperties gaodeProperties;
@Autowired
private GaodeWeatherFeignClient gaodeWeatherFeignClient;
@Autowired
private IPService ipService;
@Override
public NowWeather weatherApi() {
RequestBody requestBody = new RequestBody();
//获取城市编码
IPInfoVo ipApiData = ipService.getIPApiData();
requestBody.setKey(gaodeProperties.getKey())
.setCity(ipApiData.getCityId())
.setExtensions(GAODE_EXTENSIONS_BASE);
JSONObject jsonObject = gaodeWeatherFeignClient.WeatherApi(requestBody);
return null;
}
}

@ -12,6 +12,8 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
import static com.xjs.consts.ApiConst.ROLL_CODE_SUCCESS;
import static com.xjs.consts.RedisConst.IP_INFO;
/**
@ -42,7 +44,7 @@ public class RollIPFactory implements IPFactory<IPInfoVo> {
requestBody.setApp_id(rollProperties.getApp_id());
requestBody.setApp_secret(rollProperties.getApp_secret());
JSONObject jsonObject = rollIPFeignClient.IpApi(requestBody);
if (!jsonObject.containsKey("error") && jsonObject.getInteger("code") == 1) {
if (!jsonObject.containsKey(DEMOTE_ERROR) && jsonObject.getInteger("code") == ROLL_CODE_SUCCESS.intValue()) {
JSONObject data = jsonObject.getJSONObject("data");
return data.toJavaObject(IPInfoVo.class);
} else {

@ -7,6 +7,7 @@ import com.xjs.copywriting.mapper.CopyWritingMapper;
import com.xjs.copywriting.service.CopyWritingService;
import com.xjs.oneenglish.domain.ApiEnglish;
import com.xjs.oneenglish.domain.RequestBody;
import com.xjs.oneenglish.factory.impl.TianXingOneEnglishFactory;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

@ -26,6 +26,7 @@
<where>
<if test="apiName != null and apiName != ''"> and api_name like concat('%', #{apiName}, '%')</if>
</where>
order by create_time desc
</select>
<select id="selectApiRecordById" parameterType="Long" resultMap="ApiRecordResult">

Loading…
Cancel
Save