parent
02ca4bd22a
commit
fc065088fa
|
|
@ -0,0 +1,10 @@
|
||||||
|
import request from '@/utils/request'
|
||||||
|
|
||||||
|
//获取联想词汇
|
||||||
|
export function getAssociation(content) {
|
||||||
|
return request({
|
||||||
|
url: '/openapi/association/getAssociation',
|
||||||
|
method: 'get',
|
||||||
|
params: content
|
||||||
|
})
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 3.8 KiB |
|
|
@ -11,6 +11,29 @@
|
||||||
<el-col :span="12">
|
<el-col :span="12">
|
||||||
<div>
|
<div>
|
||||||
<el-card shadow="hover" class="card">
|
<el-card shadow="hover" class="card">
|
||||||
|
<!--logo -->
|
||||||
|
<div style="width: 500px;margin: 0 auto;margin-top: 20px">
|
||||||
|
<el-image
|
||||||
|
style="width: 272px; height: 72px;margin-left: 120px"
|
||||||
|
:src="baiduLogo"
|
||||||
|
></el-image>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!--输入框 -->
|
||||||
|
<div style="margin-top: 30px">
|
||||||
|
<el-autocomplete
|
||||||
|
style="width: 92%"
|
||||||
|
v-model="searchContent"
|
||||||
|
@input="getAssociation()"
|
||||||
|
@select="handleSelect"
|
||||||
|
:fetch-suggestions="querySearchAsync"
|
||||||
|
placeholder="请输入你想要搜索的内容"></el-autocomplete>
|
||||||
|
<el-button
|
||||||
|
style="width: 8%"
|
||||||
|
|
||||||
|
type="primary" icon="el-icon-search" @click="toRescue"></el-button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
</el-card>
|
</el-card>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -44,10 +67,23 @@ require('echarts/lib/component/tooltip');
|
||||||
require('echarts/lib/chart/gauge');
|
require('echarts/lib/chart/gauge');
|
||||||
require('echarts/lib/component/title');
|
require('echarts/lib/component/title');
|
||||||
|
|
||||||
|
import baiduLogo from "@/assets/images/baidu_logo.png"
|
||||||
|
import {getAssociation} from "@/api/business/openapi/ai";
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "Index",
|
name: "Index",
|
||||||
data() {
|
data() {
|
||||||
return {};
|
return {
|
||||||
|
baiduLogo,
|
||||||
|
|
||||||
|
//百度输入框内容
|
||||||
|
searchContent: null,
|
||||||
|
|
||||||
|
//联想词汇
|
||||||
|
associationList: [],
|
||||||
|
|
||||||
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
created() {
|
created() {
|
||||||
|
|
@ -60,6 +96,7 @@ export default {
|
||||||
|
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
|
//获取echarts图
|
||||||
initRenderers() {
|
initRenderers() {
|
||||||
var myDate = new Date();
|
var myDate = new Date();
|
||||||
var s = myDate.getSeconds();
|
var s = myDate.getSeconds();
|
||||||
|
|
@ -126,9 +163,56 @@ export default {
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
//获取联想词汇
|
||||||
|
getAssociation() {
|
||||||
|
if (this.searchContent === '' || this.searchContent === null || this.searchContent === undefined) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let data = {
|
||||||
|
content: this.searchContent,
|
||||||
|
};
|
||||||
|
getAssociation(data).then(res => {
|
||||||
|
this.associationList = res.data
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
querySearchAsync(queryString, cb) {
|
||||||
|
|
||||||
|
let list = this.handleAssociationList(this.associationList);
|
||||||
|
|
||||||
|
cb(list);
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理返回的list
|
||||||
|
* @param restaurants
|
||||||
|
*/
|
||||||
|
handleAssociationList(restaurants) {
|
||||||
|
|
||||||
|
let list = []
|
||||||
|
|
||||||
|
restaurants.forEach(s => {
|
||||||
|
let obj = {};
|
||||||
|
let key = "value"
|
||||||
|
var value = s
|
||||||
|
obj[key] = value
|
||||||
|
list.push(obj)
|
||||||
|
})
|
||||||
|
|
||||||
|
return list;
|
||||||
|
},
|
||||||
|
|
||||||
|
//跳转到外部链接
|
||||||
|
toRescue() {
|
||||||
|
// window.location.href = 'https://www.baidu.com/s?wd=' + this.searchContent
|
||||||
|
window.open('https://www.baidu.com/s?wd='+ this.searchContent)
|
||||||
|
},
|
||||||
|
|
||||||
|
handleSelect(item) {
|
||||||
|
console.log(item);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
@ -136,6 +220,7 @@ export default {
|
||||||
|
|
||||||
<style scoped lang="scss">
|
<style scoped lang="scss">
|
||||||
.card {
|
.card {
|
||||||
|
width: 100%;
|
||||||
height: 410px;
|
height: 410px;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,8 @@ public class ApiConst {
|
||||||
|
|
||||||
public static final String BAIDU_ASSOCIATION = "百度联想";
|
public static final String BAIDU_ASSOCIATION = "百度联想";
|
||||||
|
|
||||||
|
public static final String SOUGOU_ASSOCIATION = "搜狗联想";
|
||||||
|
|
||||||
|
|
||||||
//-------------------url------------------------------
|
//-------------------url------------------------------
|
||||||
|
|
||||||
|
|
@ -204,7 +206,12 @@ public class ApiConst {
|
||||||
/**
|
/**
|
||||||
* 百度智能联想语义API地址
|
* 百度智能联想语义API地址
|
||||||
*/
|
*/
|
||||||
public static final String BAIDU_ASSOCIATION_URL= "http://suggestion.baidu.com/su?";
|
public static final String BAIDU_ASSOCIATION_URL= "http://suggestion.baidu.com/su";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜狗智能联想语义API地址
|
||||||
|
*/
|
||||||
|
public static final String SOUGOU__ASSOCIATION_URL= "http://w.sugg.sogou.com/sugg/ajaj_json.jsp";
|
||||||
|
|
||||||
|
|
||||||
//-----------------------api请求参数常量-----------------------------
|
//-----------------------api请求参数常量-----------------------------
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.xjs.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json工具类
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-02-24
|
||||||
|
*/
|
||||||
|
public class JsonUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析jsonp
|
||||||
|
* @param jsonp 一种json文本格式
|
||||||
|
* @return JSONObject
|
||||||
|
*/
|
||||||
|
public static JSONObject parseJsonp(String jsonp) {
|
||||||
|
int startIndex = jsonp.indexOf("(");
|
||||||
|
int endIndex = jsonp.lastIndexOf(")");
|
||||||
|
String json = jsonp.substring(startIndex+1, endIndex);
|
||||||
|
return JSON.parseObject(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.xjs.ai.controller;
|
package com.xjs.ai.controller;
|
||||||
|
|
||||||
|
import com.ruoyi.common.core.utils.StringUtils;
|
||||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||||
import com.ruoyi.common.security.annotation.RequiresLogin;
|
import com.ruoyi.common.security.annotation.RequiresLogin;
|
||||||
import com.xjs.ai.factory.AssociationFactory;
|
import com.xjs.ai.factory.AssociationFactory;
|
||||||
|
|
@ -34,6 +35,9 @@ public class AssociationController {
|
||||||
@ApiOperation("获取联想词汇")
|
@ApiOperation("获取联想词汇")
|
||||||
@RequiresLogin
|
@RequiresLogin
|
||||||
public AjaxResult getAssociation(@RequestParam("content") String content) {
|
public AjaxResult getAssociation(@RequestParam("content") String content) {
|
||||||
|
if (StringUtils.isBlank(content)) {
|
||||||
|
return AjaxResult.success("参数为空");
|
||||||
|
}
|
||||||
List<String> data = baiduAssociationFactory.getData(content);
|
List<String> data = baiduAssociationFactory.getData(content);
|
||||||
return AjaxResult.success(data);
|
return AjaxResult.success(data);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,17 @@ import com.xjs.ai.factory.AssociationFactory;
|
||||||
import com.xjs.common.client.api.baidu.BaiduAssociationFeignClient;
|
import com.xjs.common.client.api.baidu.BaiduAssociationFeignClient;
|
||||||
import com.xjs.exception.ApiException;
|
import com.xjs.exception.ApiException;
|
||||||
import com.xjs.exception.BusinessException;
|
import com.xjs.exception.BusinessException;
|
||||||
|
import com.xjs.utils.JsonUtils;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 百度联想工厂实现
|
* 百度联想工厂实现
|
||||||
|
*
|
||||||
* @author xiejs
|
* @author xiejs
|
||||||
* @since 2022-02-24
|
* @since 2022-02-24
|
||||||
*/
|
*/
|
||||||
|
|
@ -36,22 +39,25 @@ public class BaiduAssociationFactory implements AssociationFactory<List<String>>
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.contains(filter)) {
|
if (data.contains(filter)) {
|
||||||
String substring = data.substring(filter.length() + 1, data.length() - 2);
|
//String substring = data.substring(filter.length() + 1, data.length() - 2);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JSONObject jsonObject = JSONObject.parseObject(substring);
|
JSONObject jsonObject = JsonUtils.parseJsonp(data);
|
||||||
JSONArray jsonArray = jsonObject.getJSONArray("s");
|
JSONArray jsonArray = jsonObject.getJSONArray("s");
|
||||||
|
|
||||||
|
if (jsonArray.size() == 0) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
return jsonArray.toJavaList(String.class);
|
return jsonArray.toJavaList(String.class);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("json格式转换异常!!!");
|
log.error("json格式转换异常!!!"+e.getMessage());
|
||||||
throw new BusinessException("json格式转换异常");
|
throw new BusinessException("json格式转换异常");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.xjs.common.client.api.sougou;
|
||||||
|
|
||||||
|
import com.xjs.annotation.ApiLog;
|
||||||
|
import com.xjs.common.client.factory.SouGouAssociationFeignFactory;
|
||||||
|
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.SOUGOU_ASSOCIATION;
|
||||||
|
import static com.xjs.consts.ApiConst.SOUGOU__ASSOCIATION_URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜狗语义联想api feign
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-02-25
|
||||||
|
*/
|
||||||
|
@FeignClient(name = "souGouAssociation", url = SOUGOU__ASSOCIATION_URL, fallbackFactory = SouGouAssociationFeignFactory.class)
|
||||||
|
public interface SouGouAssociationFeignClient {
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
@ApiLog(name = SOUGOU_ASSOCIATION,
|
||||||
|
url = SOUGOU__ASSOCIATION_URL,
|
||||||
|
method = "Get")
|
||||||
|
String associationApi(@RequestParam("key") String key);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.xjs.common.client.factory;
|
||||||
|
|
||||||
|
import com.xjs.common.client.api.sougou.SouGouAssociationFeignClient;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 搜狗语义联想api 降级
|
||||||
|
* @author xiejs
|
||||||
|
* @since 2022-02-25
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class SouGouAssociationFeignFactory implements FallbackFactory<SouGouAssociationFeignClient> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SouGouAssociationFeignClient create(Throwable cause) {
|
||||||
|
log.error("搜狗联想服务调用失败:{},执行降级处理", cause.getMessage());
|
||||||
|
return (wd ->"");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue