parent
c782855cec
commit
02ca4bd22a
|
|
@ -80,6 +80,8 @@ public class ApiConst {
|
|||
|
||||
public static final String ROLL_JOKE = "ROLL-搞笑段子";
|
||||
|
||||
public static final String BAIDU_ASSOCIATION = "百度联想";
|
||||
|
||||
|
||||
//-------------------url------------------------------
|
||||
|
||||
|
|
@ -202,7 +204,7 @@ public class ApiConst {
|
|||
/**
|
||||
* 百度智能联想语义API地址
|
||||
*/
|
||||
public static final String BAI_DU_AI_LX_URL= "http://suggestion.baidu.com/su?";
|
||||
public static final String BAIDU_ASSOCIATION_URL= "http://suggestion.baidu.com/su?";
|
||||
|
||||
|
||||
//-----------------------api请求参数常量-----------------------------
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
package com.xjs.ai.controller;
|
||||
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.common.security.annotation.RequiresLogin;
|
||||
import com.xjs.ai.factory.AssociationFactory;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 联想controller
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("association")
|
||||
@Api(tags = "业务模块-联想管理")
|
||||
@Log4j2
|
||||
public class AssociationController {
|
||||
|
||||
@Autowired
|
||||
private AssociationFactory<List<String>> baiduAssociationFactory;
|
||||
|
||||
|
||||
@GetMapping("getAssociation")
|
||||
@ApiOperation("获取联想词汇")
|
||||
@RequiresLogin
|
||||
public AjaxResult getAssociation(@RequestParam("content") String content) {
|
||||
List<String> data = baiduAssociationFactory.getData(content);
|
||||
return AjaxResult.success(data);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.xjs.ai.factory;
|
||||
|
||||
/**
|
||||
* 联想 工厂
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
public interface AssociationFactory<T> {
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @param content 输入内容
|
||||
* @return T
|
||||
*/
|
||||
T getData(String content);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
package com.xjs.ai.factory.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.xjs.ai.factory.AssociationFactory;
|
||||
import com.xjs.common.client.api.baidu.BaiduAssociationFeignClient;
|
||||
import com.xjs.exception.ApiException;
|
||||
import com.xjs.exception.BusinessException;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 百度联想工厂实现
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class BaiduAssociationFactory implements AssociationFactory<List<String>> {
|
||||
|
||||
public static final String filter= "window.baidu.sug";
|
||||
|
||||
@Autowired
|
||||
private BaiduAssociationFeignClient baiduAssociationFeignClient;
|
||||
|
||||
@Override
|
||||
public List<String> getData(String content) {
|
||||
|
||||
String data = baiduAssociationFeignClient.associationApi(content);
|
||||
if (StringUtils.isEmpty(data)) {
|
||||
throw new ApiException("百度联想api接口调用异常");
|
||||
}
|
||||
|
||||
if (data.contains(filter)) {
|
||||
String substring = data.substring(filter.length() + 1, data.length() - 2);
|
||||
try {
|
||||
JSONObject jsonObject = JSONObject.parseObject(substring);
|
||||
JSONArray jsonArray = jsonObject.getJSONArray("s");
|
||||
return jsonArray.toJavaList(String.class);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("json格式转换异常!!!");
|
||||
throw new BusinessException("json格式转换异常");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.xjs.common.client.api.baidu;
|
||||
|
||||
import com.xjs.annotation.ApiLog;
|
||||
import com.xjs.common.client.factory.BaiduAssociationFeignFactory;
|
||||
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.BAIDU_ASSOCIATION;
|
||||
import static com.xjs.consts.ApiConst.BAIDU_ASSOCIATION_URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* 百度语义联想api
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@FeignClient(name = "baiduAssociation", url = BAIDU_ASSOCIATION_URL, fallbackFactory = BaiduAssociationFeignFactory.class)
|
||||
public interface BaiduAssociationFeignClient {
|
||||
|
||||
@GetMapping
|
||||
@ApiLog(name = BAIDU_ASSOCIATION,
|
||||
url = BAIDU_ASSOCIATION_URL,
|
||||
method = "Get")
|
||||
String associationApi(@RequestParam("wd") String wd);
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -10,9 +10,9 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||
import static com.xjs.consts.ApiConst.*;
|
||||
|
||||
/**
|
||||
* 百度翻译接口api调用
|
||||
* @author xiejs
|
||||
* @desc 百度翻译接口api调用
|
||||
* @create 2021-12-25
|
||||
* @since 2021-12-25
|
||||
*/
|
||||
@FeignClient(name = "baidu", url = BAIDU_FY_URL, fallbackFactory = BaiduTranslationFeignFactory.class)
|
||||
public interface BaiduTranslationFeignClient {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
package com.xjs.common.client.factory;
|
||||
|
||||
import com.xjs.common.client.api.baidu.BaiduAssociationFeignClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class BaiduAssociationFeignFactory implements FallbackFactory<BaiduAssociationFeignClient> {
|
||||
|
||||
@Override
|
||||
public BaiduAssociationFeignClient create(Throwable cause) {
|
||||
log.error("百度联想服务调用失败:{},执行降级处理", cause.getMessage());
|
||||
return (wd ->"");
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ import com.xjs.annotation.ApiLog;
|
|||
import com.xjs.business.warning.RemoteWarningCRUDFeign;
|
||||
import com.xjs.business.warning.domain.ApiRecord;
|
||||
import com.xjs.common.client.api.alapi.AlapiJokeAllFeignClient;
|
||||
import com.xjs.common.client.api.baidu.BaiduAssociationFeignClient;
|
||||
import com.xjs.common.client.api.baidu.BaiduTranslationFeignClient;
|
||||
import com.xjs.common.client.api.gaode.GaodeWeatherFeignClient;
|
||||
import com.xjs.common.client.api.lq.LqAWordFeignClient;
|
||||
|
|
@ -150,6 +151,8 @@ public class CheckApiStatusTask {
|
|||
private TianXingWYYFeignClient tianXingWYYFeignClient;
|
||||
@Autowired
|
||||
private YouDaoFeignClient youDaoFeignClient;
|
||||
@Autowired
|
||||
private BaiduAssociationFeignClient baiduAssociationFeignClient;
|
||||
|
||||
/**
|
||||
* 检查api状态 <br>
|
||||
|
|
@ -350,6 +353,12 @@ public class CheckApiStatusTask {
|
|||
};
|
||||
new Thread(runCheckYouDaoTranslation).start();
|
||||
|
||||
Runnable runCheckBaiduAssociation = () -> {
|
||||
log.info("线程启动:" + Thread.currentThread().getName());
|
||||
this.checkBaiduAssociation();
|
||||
};
|
||||
new Thread(runCheckBaiduAssociation).start();
|
||||
|
||||
//this.checkAlapiJoke();
|
||||
//this.checkBaiduTranslation();
|
||||
//this.checkGaodeWeather();
|
||||
|
|
@ -382,6 +391,7 @@ public class CheckApiStatusTask {
|
|||
//this.checkTianXingWXRS();
|
||||
//this.checkTianXingWYY();
|
||||
//this.checkYouDaoTranslation();
|
||||
//this.checkBaiduAssociation();
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
|
|
@ -393,6 +403,21 @@ public class CheckApiStatusTask {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查百度平台 联想API
|
||||
*/
|
||||
private void checkBaiduAssociation() {
|
||||
String data = baiduAssociationFeignClient.associationApi(content);
|
||||
if (!StringUtils.isEmpty(data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String[] info = this.getAnnotationInfo(BaiduAssociationFeignClient.class).get(0);
|
||||
this.selectAndUpdate(info);
|
||||
log.error("检查发现百度平台 联想API异常");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查有道平台 翻译API
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 文案controller
|
||||
* @author xiejs
|
||||
* @desc
|
||||
* @create 2021-12-27
|
||||
* @since 2021-12-27
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("copyWriting")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
package com.xjs.ai.factory.impl;
|
||||
|
||||
import com.xjs.XjsOpenApiApp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @since 2022-02-24
|
||||
*/
|
||||
@SpringBootTest(classes = XjsOpenApiApp.class)
|
||||
class BaiduAssociationFactoryTest {
|
||||
|
||||
@Autowired
|
||||
BaiduAssociationFactory baiduAssociationFactory;
|
||||
|
||||
@Test
|
||||
void getData() {
|
||||
List<String> xx = baiduAssociationFactory.getData("哈哈");
|
||||
|
||||
for (String s : xx) {
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue