hyperf-chat/app/Controller/Api/V1/EmoticonController.php

203 lines
6.2 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2022-01-22 12:48:28 +08:00
declare(strict_types=1);
2021-04-20 16:30:57 +08:00
2020-11-04 11:57:16 +08:00
namespace App\Controller\Api\V1;
2020-11-07 22:57:10 +08:00
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
2020-11-24 23:23:12 +08:00
use App\Middleware\JWTAuthMiddleware;
2022-01-20 21:20:58 +08:00
use App\Model\Emoticon\Emoticon;
use App\Model\Emoticon\EmoticonItem;
2020-11-07 22:57:10 +08:00
use App\Service\EmoticonService;
2021-06-30 19:27:49 +08:00
use League\Flysystem\Filesystem;
2020-12-26 21:33:40 +08:00
use Psr\Http\Message\ResponseInterface;
2020-11-04 11:57:16 +08:00
2020-11-07 22:57:10 +08:00
/**
* Class EmoticonController
2022-01-23 15:13:58 +08:00
*
2021-07-05 21:52:44 +08:00
* @Controller(prefix="/api/v1/emoticon")
2020-11-07 22:57:10 +08:00
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
2020-11-04 11:57:16 +08:00
class EmoticonController extends CController
{
2020-11-07 22:57:10 +08:00
/**
* @var EmoticonService
*/
2020-11-29 17:39:24 +08:00
private $emoticonService;
2020-11-04 11:57:16 +08:00
2022-01-22 21:02:53 +08:00
public function __construct(EmoticonService $service)
{
parent::__construct();
$this->emoticonService = $service;
}
2020-11-07 22:57:10 +08:00
/**
* 获取用户表情包列表
2021-04-22 16:14:34 +08:00
*
2021-09-11 12:41:28 +08:00
* @RequestMapping(path="list", methods="get")
2020-11-07 22:57:10 +08:00
*/
2021-09-11 12:41:28 +08:00
public function list(): ResponseInterface
2020-11-07 22:57:10 +08:00
{
$emoticonList = [];
2021-04-20 16:30:57 +08:00
$user_id = $this->uid();
2020-11-07 22:57:10 +08:00
if ($ids = $this->emoticonService->getInstallIds($user_id)) {
2021-07-05 21:52:44 +08:00
$items = Emoticon::whereIn('id', $ids)->get(['id', 'name', 'icon']);
2020-11-07 22:57:10 +08:00
foreach ($items as $item) {
$emoticonList[] = [
'emoticon_id' => $item->id,
2021-07-05 21:52:44 +08:00
'url' => get_media_url($item->icon),
2021-04-20 16:30:57 +08:00
'name' => $item->name,
'list' => $this->emoticonService->getDetailsAll([
2020-11-07 22:57:10 +08:00
['emoticon_id', '=', $item->id],
['user_id', '=', 0]
])
];
}
}
return $this->response->success([
2021-04-20 16:30:57 +08:00
'sys_emoticon' => $emoticonList,
2020-11-07 22:57:10 +08:00
'collect_emoticon' => $this->emoticonService->getDetailsAll([
['emoticon_id', '=', 0],
['user_id', '=', $user_id]
])
]);
}
/**
* 获取系统表情包
2021-04-22 16:14:34 +08:00
*
2022-01-22 12:48:28 +08:00
* @RequestMapping(path="system/list", methods="get")
2020-11-07 22:57:10 +08:00
*/
2021-09-11 12:41:28 +08:00
public function system(): ResponseInterface
2020-11-07 22:57:10 +08:00
{
2021-07-05 21:52:44 +08:00
$items = Emoticon::get(['id', 'name', 'icon'])->toArray();
2020-11-07 22:57:10 +08:00
if ($items) {
$ids = $this->emoticonService->getInstallIds($this->uid());
array_walk($items, function (&$item) use ($ids) {
$item['status'] = in_array($item['id'], $ids) ? 1 : 0;
2021-07-05 21:52:44 +08:00
$item['icon'] = get_media_url($item['icon']);
2020-11-07 22:57:10 +08:00
});
}
return $this->response->success($items);
}
/**
* 安装或移除系统表情包
2021-04-22 16:14:34 +08:00
*
2022-01-22 12:48:28 +08:00
* @RequestMapping(path="system/install", methods="post")
2020-11-07 22:57:10 +08:00
*/
2021-09-11 12:41:28 +08:00
public function setUserEmoticon(): ResponseInterface
2020-11-07 22:57:10 +08:00
{
$params = $this->request->all();
$this->validate($params, [
'emoticon_id' => 'required|integer',
2021-04-20 16:30:57 +08:00
'type' => 'required|in:1,2'
2020-11-07 22:57:10 +08:00
]);
2022-01-22 12:48:28 +08:00
$params['emoticon_id'] = intval($params['emoticon_id']);
2020-11-07 22:57:10 +08:00
$user_id = $this->uid();
2021-07-05 21:52:44 +08:00
// 移除表情包
2020-11-07 22:57:10 +08:00
if ($params['type'] == 2) {
$isTrue = $this->emoticonService->removeSysEmoticon($user_id, $params['emoticon_id']);
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('移除表情包失败!');
2020-11-07 22:57:10 +08:00
}
return $this->response->success([], '移除表情包成功...');
2021-07-05 21:52:44 +08:00
}
2020-11-07 22:57:10 +08:00
2021-07-05 21:52:44 +08:00
// 添加表情包
$emoticonInfo = Emoticon::where('id', $params['emoticon_id'])->first(['id', 'name', 'icon']);
if (!$emoticonInfo) {
return $this->response->fail('添加表情包失败!');
}
2020-11-07 22:57:10 +08:00
2021-07-05 21:52:44 +08:00
if (!$this->emoticonService->installSysEmoticon($user_id, $params['emoticon_id'])) {
return $this->response->fail('添加表情包失败!');
2020-11-07 22:57:10 +08:00
}
2021-07-05 21:52:44 +08:00
$data = [
'emoticon_id' => $emoticonInfo->id,
'url' => get_media_url($emoticonInfo->icon),
'name' => $emoticonInfo->name,
'list' => $this->emoticonService->getDetailsAll([
['emoticon_id', '=', $emoticonInfo->id]
])
];
return $this->response->success($data, '添加表情包成功...');
2020-11-07 22:57:10 +08:00
}
/**
* 自定义上传表情包
2020-11-29 17:39:24 +08:00
*
2022-01-22 12:48:28 +08:00
* @RequestMapping(path="customize/create", methods="post")
*
2021-06-30 19:27:49 +08:00
* @param Filesystem $filesystem
2020-12-26 21:33:40 +08:00
* @return ResponseInterface
2020-11-07 22:57:10 +08:00
*/
2021-09-11 12:41:28 +08:00
public function upload(Filesystem $filesystem): ResponseInterface
2020-11-07 22:57:10 +08:00
{
$file = $this->request->file('emoticon');
if (!$file->isValid()) {
2022-01-22 12:48:28 +08:00
return $this->response->invalidParams('上传文件验证失败!');
2020-11-07 22:57:10 +08:00
}
$ext = $file->getExtension();
if (!in_array($ext, ['jpg', 'png', 'jpeg', 'gif', 'webp'])) {
2022-01-22 12:48:28 +08:00
return $this->response->invalidParams('图片格式错误目前仅支持jpg、png、jpeg、gif和webp');
2020-11-07 22:57:10 +08:00
}
2021-06-30 19:27:49 +08:00
try {
2022-01-22 12:48:28 +08:00
$path = 'public/media/images/emoticon/' . date('Ymd') . '/' . create_image_name($ext, getimagesize($file->getRealPath()));
2021-06-30 19:27:49 +08:00
$filesystem->write($path, file_get_contents($file->getRealPath()));
} catch (\Exception $e) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('图片上传失败!');
2020-11-29 17:39:24 +08:00
}
2020-11-07 22:57:10 +08:00
2021-07-05 21:52:44 +08:00
$result = EmoticonItem::create([
2021-04-20 16:30:57 +08:00
'user_id' => $this->uid(),
2022-01-22 12:48:28 +08:00
'url' => get_media_url($path),
2020-11-07 22:57:10 +08:00
'file_suffix' => $ext,
2021-04-20 16:30:57 +08:00
'file_size' => $file->getSize(),
2020-11-07 22:57:10 +08:00
]);
if (!$result) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('表情包上传失败!');
2020-11-07 22:57:10 +08:00
}
return $this->response->success([
'media_id' => $result->id,
2022-01-22 12:48:28 +08:00
'src' => $result->url,
2020-11-07 22:57:10 +08:00
]);
}
/**
* 移除收藏的表情包
2021-04-22 16:14:34 +08:00
*
2022-01-22 12:48:28 +08:00
* @RequestMapping(path="customize/delete", methods="post")
* @throws \Exception
2020-11-07 22:57:10 +08:00
*/
2021-09-11 12:41:28 +08:00
public function delCollectEmoticon(): ResponseInterface
2020-11-07 22:57:10 +08:00
{
2020-11-29 17:39:24 +08:00
$params = $this->request->inputs(['ids']);
2022-01-20 21:20:58 +08:00
2020-11-07 22:57:10 +08:00
$this->validate($params, [
2020-11-29 17:39:24 +08:00
'ids' => 'required|ids'
2020-11-07 22:57:10 +08:00
]);
2022-01-20 21:20:58 +08:00
$isTrue = $this->emoticonService->deleteCollect($this->uid(), parse_ids($params['ids']));
return $isTrue ? $this->response->success() : $this->response->fail();
2020-11-07 22:57:10 +08:00
}
2020-11-04 11:57:16 +08:00
}