2020-11-04 11:57:16 +08:00
|
|
|
|
<?php
|
2020-12-26 21:33:40 +08:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* This is my open source code, please do not use it for commercial applications.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information,
|
|
|
|
|
* please view the LICENSE file that was distributed with this source code
|
|
|
|
|
*
|
|
|
|
|
* @author Yuandong<837215079@qq.com>
|
|
|
|
|
* @link https://github.com/gzydong/hyperf-chat
|
|
|
|
|
*/
|
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\Di\Annotation\Inject;
|
|
|
|
|
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;
|
2020-11-29 14:44:11 +08:00
|
|
|
|
use App\Constants\ResponseCode;
|
|
|
|
|
use App\Model\Emoticon;
|
|
|
|
|
use App\Model\EmoticonDetail;
|
2020-11-07 22:57:10 +08:00
|
|
|
|
use App\Service\EmoticonService;
|
2020-11-29 17:39:24 +08:00
|
|
|
|
use App\Service\UploadService;
|
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
|
|
|
|
|
*
|
|
|
|
|
* @Controller(path="/api/v1/emoticon")
|
|
|
|
|
* @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
|
|
|
|
/**
|
|
|
|
|
* @Inject
|
|
|
|
|
* @var EmoticonService
|
|
|
|
|
*/
|
2020-11-29 17:39:24 +08:00
|
|
|
|
private $emoticonService;
|
2020-11-04 11:57:16 +08:00
|
|
|
|
|
2020-11-07 22:57:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取用户表情包列表
|
|
|
|
|
*
|
|
|
|
|
* @RequestMapping(path="user-emoticon", methods="get")
|
|
|
|
|
*/
|
|
|
|
|
public function getUserEmoticon()
|
|
|
|
|
{
|
|
|
|
|
$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)) {
|
|
|
|
|
$items = Emoticon::whereIn('id', $ids)->get(['id', 'name', 'url']);
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
|
$emoticonList[] = [
|
|
|
|
|
'emoticon_id' => $item->id,
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'url' => get_media_url($item->url),
|
|
|
|
|
'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]
|
|
|
|
|
])
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取系统表情包
|
|
|
|
|
*
|
|
|
|
|
* @RequestMapping(path="system-emoticon", methods="get")
|
|
|
|
|
*/
|
|
|
|
|
public function getSystemEmoticon()
|
|
|
|
|
{
|
|
|
|
|
$items = Emoticon::get(['id', 'name', 'url'])->toArray();
|
|
|
|
|
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-04-20 16:30:57 +08:00
|
|
|
|
$item['url'] = get_media_url($item['url']);
|
2020-11-07 22:57:10 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->response->success($items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 安装或移除系统表情包
|
|
|
|
|
*
|
|
|
|
|
* @RequestMapping(path="set-user-emoticon", methods="post")
|
|
|
|
|
*/
|
|
|
|
|
public function setUserEmoticon()
|
|
|
|
|
{
|
|
|
|
|
$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
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$user_id = $this->uid();
|
|
|
|
|
if ($params['type'] == 2) {
|
|
|
|
|
// 移除表情包
|
|
|
|
|
$isTrue = $this->emoticonService->removeSysEmoticon($user_id, $params['emoticon_id']);
|
|
|
|
|
if (!$isTrue) {
|
|
|
|
|
return $this->response->fail('移除表情包失败...');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->response->success([], '移除表情包成功...');
|
|
|
|
|
} else {
|
|
|
|
|
// 添加表情包
|
|
|
|
|
$emoticonInfo = Emoticon::where('id', $params['emoticon_id'])->first(['id', 'name', 'url']);
|
|
|
|
|
if (!$emoticonInfo) {
|
|
|
|
|
return $this->response->fail('添加表情包失败...');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$this->emoticonService->installSysEmoticon($user_id, $params['emoticon_id'])) {
|
|
|
|
|
return $this->response->fail('添加表情包失败...');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'emoticon_id' => $emoticonInfo->id,
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'url' => get_media_url($emoticonInfo->url),
|
|
|
|
|
'name' => $emoticonInfo->name,
|
|
|
|
|
'list' => $this->emoticonService->getDetailsAll([
|
2020-11-07 22:57:10 +08:00
|
|
|
|
['emoticon_id', '=', $emoticonInfo->id]
|
|
|
|
|
])
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->response->success($data, '添加表情包成功...');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 自定义上传表情包
|
|
|
|
|
*
|
|
|
|
|
* @RequestMapping(path="upload-emoticon", methods="post")
|
2020-11-29 17:39:24 +08:00
|
|
|
|
*
|
|
|
|
|
* @param UploadService $uploadService
|
2021-04-20 16:30:57 +08:00
|
|
|
|
*
|
2020-12-26 21:33:40 +08:00
|
|
|
|
* @return ResponseInterface
|
2020-11-07 22:57:10 +08:00
|
|
|
|
*/
|
2020-11-29 17:39:24 +08:00
|
|
|
|
public function uploadEmoticon(UploadService $uploadService)
|
2020-11-07 22:57:10 +08:00
|
|
|
|
{
|
|
|
|
|
$file = $this->request->file('emoticon');
|
|
|
|
|
if (!$file->isValid()) {
|
|
|
|
|
return $this->response->fail(
|
|
|
|
|
'图片上传失败,请稍后再试...',
|
|
|
|
|
ResponseCode::VALIDATION_ERROR
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$ext = $file->getExtension();
|
|
|
|
|
if (!in_array($ext, ['jpg', 'png', 'jpeg', 'gif', 'webp'])) {
|
|
|
|
|
return $this->response->fail(
|
|
|
|
|
'图片格式错误,目前仅支持jpg、png、jpeg、gif和webp',
|
|
|
|
|
ResponseCode::VALIDATION_ERROR
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 17:39:24 +08:00
|
|
|
|
// 读取图片信息
|
2020-12-12 23:36:20 +08:00
|
|
|
|
$imgInfo = @getimagesize($file->getRealPath());
|
2020-12-26 21:33:40 +08:00
|
|
|
|
if (!$imgInfo) {
|
2020-12-12 23:36:20 +08:00
|
|
|
|
return $this->response->fail('表情包上传失败...');
|
|
|
|
|
}
|
2020-11-29 17:39:24 +08:00
|
|
|
|
|
|
|
|
|
$save_path = $uploadService->media($file, 'media/images/emoticon', create_image_name($ext, $imgInfo[0], $imgInfo[1]));
|
|
|
|
|
if (!$save_path) {
|
|
|
|
|
return $this->response->fail('图片上传失败');
|
|
|
|
|
}
|
2020-11-07 22:57:10 +08:00
|
|
|
|
|
|
|
|
|
$result = EmoticonDetail::create([
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'user_id' => $this->uid(),
|
|
|
|
|
'url' => $save_path,
|
2020-11-07 22:57:10 +08:00
|
|
|
|
'file_suffix' => $ext,
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'file_size' => $file->getSize(),
|
|
|
|
|
'created_at' => time()
|
2020-11-07 22:57:10 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
return $this->response->fail('表情包上传失败...');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->response->success([
|
|
|
|
|
'media_id' => $result->id,
|
2021-04-20 16:30:57 +08:00
|
|
|
|
'src' => get_media_url($result->url)
|
2020-11-07 22:57:10 +08:00
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 收藏聊天图片的我的表情包
|
|
|
|
|
*
|
|
|
|
|
* @RequestMapping(path="collect-emoticon", methods="post")
|
|
|
|
|
*/
|
|
|
|
|
public function collectEmoticon()
|
|
|
|
|
{
|
2020-11-29 17:39:24 +08:00
|
|
|
|
$params = $this->request->inputs(['record_id']);
|
2020-11-07 22:57:10 +08:00
|
|
|
|
$this->validate($params, [
|
|
|
|
|
'record_id' => 'required|integer'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
[$isTrue, $data] = $this->emoticonService->collect($this->uid(), $params['record_id']);
|
|
|
|
|
|
|
|
|
|
if (!$isTrue) {
|
|
|
|
|
return $this->response->fail('添加表情失败');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->response->success([
|
|
|
|
|
'emoticon' => $data
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 移除收藏的表情包
|
|
|
|
|
*
|
|
|
|
|
* @RequestMapping(path="del-collect-emoticon", methods="post")
|
|
|
|
|
*/
|
|
|
|
|
public function delCollectEmoticon()
|
|
|
|
|
{
|
2020-11-29 17:39:24 +08:00
|
|
|
|
$params = $this->request->inputs(['ids']);
|
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
|
|
|
|
]);
|
|
|
|
|
|
2020-11-29 17:39:24 +08:00
|
|
|
|
return $this->emoticonService->deleteCollect($this->uid(), parse_ids($params['ids'])) ?
|
|
|
|
|
$this->response->success([]) :
|
|
|
|
|
$this->response->fail();
|
2020-11-07 22:57:10 +08:00
|
|
|
|
}
|
2020-11-04 11:57:16 +08:00
|
|
|
|
}
|