parent
8f08381625
commit
73391b8b84
@ -0,0 +1,19 @@
|
|||||||
|
package com.xjs.properties;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ALAPI平台token参数
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-02-15
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@ConfigurationProperties(prefix = "alapi.open")
|
||||||
|
@Component
|
||||||
|
public class AlApiProperties {
|
||||||
|
|
||||||
|
private String token;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.xjs.common.client.api.alapi;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.xjs.annotation.ApiLog;
|
||||||
|
import com.xjs.common.client.factory.AlapiJokeAllFeignFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
import static com.xjs.consts.ApiConst.ALAPI_JOKE_ALL;
|
||||||
|
import static com.xjs.consts.ApiConst.ALAPI_JOKE_ALL_URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* alapi平台笑话大全feign
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-02-15
|
||||||
|
*/
|
||||||
|
@FeignClient(name = "alapiJokeAll", url = ALAPI_JOKE_ALL_URL, fallbackFactory = AlapiJokeAllFeignFactory.class)
|
||||||
|
public interface AlapiJokeAllFeignClient {
|
||||||
|
@GetMapping( headers ={ "Accept-Encoding=''"})//解决响应乱码
|
||||||
|
@ApiLog(name = ALAPI_JOKE_ALL,
|
||||||
|
url = ALAPI_JOKE_ALL_URL,
|
||||||
|
method = "Get")
|
||||||
|
JSONObject alapiJokeAllApi(@RequestParam("token") String token);
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.xjs.common.client.factory;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import com.xjs.common.client.api.alapi.AlapiJokeAllFeignClient;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import static com.xjs.consts.ApiConst.DEMOTE_ERROR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* alapi平台笑话大全降级处理
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-02-15
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class AlapiJokeAllFeignFactory implements FallbackFactory<AlapiJokeAllFeignClient> {
|
||||||
|
@Override
|
||||||
|
public AlapiJokeAllFeignClient create(Throwable cause) {
|
||||||
|
log.error("api模块alapi平台笑话大全服务调用失败:{},执行降级处理", cause.getMessage());
|
||||||
|
return (token -> {
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
jsonObject.put(DEMOTE_ERROR, R.FAIL);
|
||||||
|
return jsonObject;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.xjs.copywriting.factory.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.ruoyi.common.core.constant.HttpStatus;
|
||||||
|
import com.xjs.common.client.api.alapi.AlapiJokeAllFeignClient;
|
||||||
|
import com.xjs.consts.CopyWritingConst;
|
||||||
|
import com.xjs.copywriting.domain.CopyWriting;
|
||||||
|
import com.xjs.copywriting.domain.RequestBody;
|
||||||
|
import com.xjs.copywriting.factory.CopyWritingFactory;
|
||||||
|
import com.xjs.copywriting.service.CopyWritingService;
|
||||||
|
import com.xjs.exception.ApiException;
|
||||||
|
import com.xjs.properties.AlApiProperties;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* alapi平台笑话大全 工厂实现
|
||||||
|
*
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-02-15
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
@Component
|
||||||
|
public class AlapiJokeAllCopyWritingFactory implements CopyWritingFactory {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CopyWritingService copyWritingService;
|
||||||
|
@Autowired
|
||||||
|
private AlapiJokeAllFeignClient alapiJokeAllFeignClient;
|
||||||
|
@Autowired
|
||||||
|
private AlApiProperties alApiProperties;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CopyWriting productCopyWriting(RequestBody requestBody) {
|
||||||
|
JSONObject jsonObject = alapiJokeAllFeignClient.alapiJokeAllApi(alApiProperties.getToken());
|
||||||
|
if (jsonObject.containsKey(DEMOTE_ERROR)) {
|
||||||
|
throw new ApiException("alapi平台笑话大全接口调用异常");
|
||||||
|
}
|
||||||
|
if (jsonObject.getInteger("code") == HttpStatus.SUCCESS) {
|
||||||
|
JSONObject dataJson = jsonObject.getJSONObject("data");
|
||||||
|
String content = dataJson.getString("content");
|
||||||
|
CopyWriting copyWriting = new CopyWriting();
|
||||||
|
copyWriting.setContent(content);
|
||||||
|
copyWriting.setSource("笑话大全");
|
||||||
|
copyWriting.setType(CopyWritingConst.XHDQ);
|
||||||
|
copyWritingService.save(copyWriting);
|
||||||
|
return copyWriting;
|
||||||
|
} else {
|
||||||
|
throw new ApiException("alapi平台笑话大全接口调用异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue