perf: 优化表情搜索

This commit is contained in:
kuaifan 2024-09-18 18:48:00 +08:00
parent 947e106f19
commit 0598a36b19
3 changed files with 97 additions and 24 deletions

View File

@ -2601,4 +2601,28 @@ class DialogController extends AbstractController
//
return Base::retSuccess('success', $topMsg);
}
/**
* @api {get} api/dialog/sticker/search 54. 搜索在线表情
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup dialog
* @apiName sticker__search
*
* @apiParam {String} key 关键词
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function sticker__search()
{
User::auth();
//
$key = trim(Request::input('key'));
return Base::retSuccess('success', [
'list' => Extranet::sticker($key)
]);
}
}

View File

@ -330,18 +330,66 @@ class Extranet
return $text;
}
/**
* 获取搜狗表情包
* @param $keyword
* @return array
*/
public static function sticker($keyword)
{
$data = self::curl("https://pic.sogou.com/napi/wap/searchlist", 1800, 15, [], [
'CURLOPT_CUSTOMREQUEST' => 'POST',
'CURLOPT_POSTFIELDS' => json_encode([
"initQuery" => $keyword . " 表情",
"queryFrom" => "wap",
"ie" => "utf8",
"keyword" => $keyword . " 表情",
// "mode" => 20,
"showMode" => 0,
"start" => 1,
"reqType" => "client",
"reqFrom" => "wap_result",
"prevIsRedis" => "n",
"pagetype" => 0,
"amsParams" => []
]),
'CURLOPT_HTTPHEADER' => [
'Content-Type: application/json',
'Referer: https://pic.sogou.com/'
]
]);
$data = Base::json2array($data);
if ($data['status'] === 0 && $data['data']['picResult']['items']) {
$data = $data['data']['picResult']['items'];
$data = array_filter($data, function ($item) {
return intval($item['thumbHeight']) > 10 && intval($item['thumbWidth']) > 10;
});
return array_map(function ($item) {
return [
'name' => $item['title'],
'src' => $item['thumbUrl'],
'height' => $item['thumbHeight'],
'width' => $item['thumbWidth'],
];
}, $data);
}
return [];
}
/**
* @param $url
* @param int $cacheSecond 缓存时间如果结果为空则缓存有效30秒
* @param int $timeout
* @param array $post
* @param array $extra
* @return string
*/
private static function curl($url, int $cacheSecond = 0, int $timeout = 15): string
private static function curl($url, int $cacheSecond = 0, int $timeout = 15, array $post = [], array $extra = []): string
{
if ($cacheSecond > 0) {
$key = "curlCache::" . md5($url);
$content = Cache::remember($key, Carbon::now()->addSeconds($cacheSecond), function () use ($cacheSecond, $key, $timeout, $url) {
$result = Ihttp::ihttp_request($url, [], [], $timeout);
$key = "curlCache::" . md5($url) . "::" . md5(json_encode($post)) . "::" . md5(json_encode($extra));
$content = Cache::remember($key, Carbon::now()->addSeconds($cacheSecond), function () use ($extra, $post, $cacheSecond, $key, $timeout, $url) {
$result = Ihttp::ihttp_request($url, $post, $extra, $timeout);
$content = Base::isSuccess($result) ? trim($result['data']) : '';
if (empty($content) && $cacheSecond > 30) {
Cache::put($key, "", Carbon::now()->addSeconds(30));
@ -349,7 +397,7 @@ class Extranet
return $content;
});
} else {
$result = Ihttp::ihttp_request($url, [], [], $timeout);
$result = Ihttp::ihttp_request($url, $post, $extra, $timeout);
$content = Base::isSuccess($result) ? trim($result['data']) : '';
}
//

View File

@ -177,31 +177,32 @@ export default {
this.emosearchLoad = true;
this.emosearchTimer && clearTimeout(this.emosearchTimer)
this.emosearchTimer = setTimeout(_ => {
jsonp('https://pic.sogou.com/napi/wap/pic', {
query: this.emosearchKey + ' 表情'
}).then(data => {
this.$store.dispatch("call", {
url: 'dialog/sticker/search',
data: {
key: this.emosearchKey,
},
}).then(({data}) => {
this.emosearchList = []
if (data.status === 0) {
const items = data.data.items
if (items.length > 0) {
this.emosearchList = items.map(item => {
return {
type: 'emoticon',
asset: 'emosearch',
name: item.title,
src: item.thumbUrl,
height: item.thumbHeight,
width: item.thumbWidth,
}
})
}
const items = data.list
if (items.length > 0) {
this.emosearchList = items.map(item => {
return {
type: 'emoticon',
asset: 'emosearch',
name: item.name,
src: item.src,
height: item.height,
width: item.width,
}
})
}
if (this.emosearchList.length === 0) {
$A.noticeWarning("没有搜索到任何表情")
$A.messageWarning("没有搜索到任何表情")
}
}).catch(_ => {
this.emosearchList = []
$A.noticeWarning("搜索结果为空")
$A.messageWarning("搜索结果为空")
}).finally(_ => {
this.emosearchLoad = false;
})