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); 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; 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 $url
* @param int $cacheSecond 缓存时间如果结果为空则缓存有效30秒 * @param int $cacheSecond 缓存时间如果结果为空则缓存有效30秒
* @param int $timeout * @param int $timeout
* @param array $post
* @param array $extra
* @return string * @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) { if ($cacheSecond > 0) {
$key = "curlCache::" . md5($url); $key = "curlCache::" . md5($url) . "::" . md5(json_encode($post)) . "::" . md5(json_encode($extra));
$content = Cache::remember($key, Carbon::now()->addSeconds($cacheSecond), function () use ($cacheSecond, $key, $timeout, $url) { $content = Cache::remember($key, Carbon::now()->addSeconds($cacheSecond), function () use ($extra, $post, $cacheSecond, $key, $timeout, $url) {
$result = Ihttp::ihttp_request($url, [], [], $timeout); $result = Ihttp::ihttp_request($url, $post, $extra, $timeout);
$content = Base::isSuccess($result) ? trim($result['data']) : ''; $content = Base::isSuccess($result) ? trim($result['data']) : '';
if (empty($content) && $cacheSecond > 30) { if (empty($content) && $cacheSecond > 30) {
Cache::put($key, "", Carbon::now()->addSeconds(30)); Cache::put($key, "", Carbon::now()->addSeconds(30));
@ -349,7 +397,7 @@ class Extranet
return $content; return $content;
}); });
} else { } else {
$result = Ihttp::ihttp_request($url, [], [], $timeout); $result = Ihttp::ihttp_request($url, $post, $extra, $timeout);
$content = Base::isSuccess($result) ? trim($result['data']) : ''; $content = Base::isSuccess($result) ? trim($result['data']) : '';
} }
// //

View File

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