mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-18 07:12:49 +00:00
perf: 优化表情搜索
This commit is contained in:
parent
947e106f19
commit
0598a36b19
@ -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)
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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']) : '';
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
|
|||||||
@ -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.name,
|
||||||
name: item.title,
|
src: item.src,
|
||||||
src: item.thumbUrl,
|
height: item.height,
|
||||||
height: item.thumbHeight,
|
width: item.width,
|
||||||
width: item.thumbWidth,
|
}
|
||||||
}
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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;
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user