feat : suggest
This commit is contained in:
27
app/Controller/Admin/SuggestController.php
Normal file
27
app/Controller/Admin/SuggestController.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Controller\AbstractController;
|
||||
use App\Middleware\Admin\JwtAuthMiddleware;
|
||||
use App\Service\Admin\System\SuggestService;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use Hyperf\Validation\Annotation\Scene;
|
||||
|
||||
#[Controller(prefix: "admin/suggest")]
|
||||
#[Middlewares([
|
||||
JwtAuthMiddleware::class,
|
||||
])]
|
||||
class SuggestController extends AbstractController
|
||||
{
|
||||
#[RequestMapping(path: "list", methods: "GET")]
|
||||
#[Scene(scene: "list")]
|
||||
public function list(): array
|
||||
{
|
||||
return (new SuggestService)->handle();
|
||||
}
|
||||
}
|
||||
41
app/Controller/Api/SuggestController.php
Normal file
41
app/Controller/Api/SuggestController.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\Controller\AbstractController;
|
||||
use App\Middleware\Api\JwtAuthMiddleware;
|
||||
use App\Service\Api\Suggest\MySuggestListService;
|
||||
use App\Service\Api\Suggest\SubmissionService;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use Hyperf\Validation\Annotation\Scene;
|
||||
|
||||
#[Controller(prefix: 'api/suggest')]
|
||||
#[Middlewares([
|
||||
JwtAuthMiddleware::class,
|
||||
])]
|
||||
class SuggestController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "submission", methods: "POST")]
|
||||
#[Scene(scene: "submission")]
|
||||
public function submission(): array
|
||||
{
|
||||
return (new SubmissionService)->handle();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "list", methods: "GET")]
|
||||
#[Scene(scene: "list")]
|
||||
public function list(): array
|
||||
{
|
||||
return (new MySuggestListService)->handle();
|
||||
}
|
||||
}
|
||||
34
app/Model/Suggest.php
Normal file
34
app/Model/Suggest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $category
|
||||
* @property int $user_id
|
||||
* @property string $content
|
||||
* @property string $image_ids
|
||||
* @property string $create_time
|
||||
* @property string $update_time
|
||||
*/
|
||||
class Suggest extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'suggest';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'category' => 'integer', 'user_id' => 'integer'];
|
||||
}
|
||||
78
app/Service/Admin/System/SuggestService.php
Normal file
78
app/Service/Admin/System/SuggestService.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\System;
|
||||
|
||||
use App\Model\Suggest;
|
||||
use App\Model\User;
|
||||
use App\Service\Admin\BaseService;
|
||||
use App\Service\ServiceTrait\Common\OssTrait;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class SuggestService extends BaseService
|
||||
{
|
||||
use OssTrait;
|
||||
|
||||
/**
|
||||
* @var Suggest
|
||||
*/
|
||||
#[Inject]
|
||||
protected Suggest $suggestModel;
|
||||
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
#[Inject]
|
||||
protected User $userModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$limit = $this->request->input('limit', 10);
|
||||
$searchUserId = $this->request->input('search_user_id');
|
||||
|
||||
$data = $this->suggestModel
|
||||
->when($searchUserId, function ($query, $searchUserId) {
|
||||
return $query->where('user_id', $searchUserId);
|
||||
})
|
||||
->orderByDesc('id')
|
||||
->paginate($limit)
|
||||
->toArray();
|
||||
|
||||
if (empty($data['data'])) return $this->return->success('success',$data);
|
||||
|
||||
$userIds = array_column($data['data'],'user_id');
|
||||
$userList = $this->userModel->getInfoByIds($userIds);
|
||||
|
||||
$imageIds = array_column($userList,'avatar_id');
|
||||
|
||||
$imageIdArr = array_column($data['data'],'image_ids');
|
||||
$listImageIds = array_unique(explode(',',implode(',',$imageIdArr)));
|
||||
$totalImageIds = array_merge($listImageIds,$imageIds);
|
||||
unset($imageIds,$imageIdArr,$listImageIds);
|
||||
|
||||
$imageList = $this->getOssObjects($totalImageIds);
|
||||
|
||||
foreach ($data['data'] as &$item) {
|
||||
$item['nickname'] = $userList[$item['user_id']]['nickname'] ?? '';
|
||||
$item['avatar'] = $imageList[$userList[$item['user_id']]['avatar_id']]['url'] ?? '';
|
||||
|
||||
$oneImage = [];
|
||||
foreach (explode(',',$item['image_ids']) as $imageId) {
|
||||
$oneImage[] = $imageList[$imageId]['url'] ?? '';
|
||||
}
|
||||
$item['content_image_list'] = $oneImage;
|
||||
}
|
||||
|
||||
return $this->return->success('success',$data);
|
||||
}
|
||||
}
|
||||
57
app/Service/Api/Suggest/MySuggestListService.php
Normal file
57
app/Service/Api/Suggest/MySuggestListService.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Suggest;
|
||||
|
||||
use App\Model\Suggest;
|
||||
use App\Service\Api\BaseService;
|
||||
use App\Service\ServiceTrait\Common\OssTrait;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class MySuggestListService extends BaseService
|
||||
{
|
||||
use OssTrait;
|
||||
|
||||
/**
|
||||
* @var Suggest
|
||||
*/
|
||||
#[Inject]
|
||||
protected Suggest $suggestModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$limit = $this->request->input('limit', 10);
|
||||
|
||||
$data = $this->suggestModel
|
||||
->where('user_id', $this->userId)
|
||||
->orderByDesc('id')
|
||||
->paginate($limit)
|
||||
->toArray();
|
||||
|
||||
if (empty($data['data'])) return $this->return->success('success',$data);
|
||||
|
||||
$imageIdArr = array_column($data['data'],'image_ids');
|
||||
$listImageIds = array_unique(explode(',',implode(',',$imageIdArr)));
|
||||
$imageList = $this->getOssObjects($listImageIds);
|
||||
|
||||
foreach ($data['data'] as &$item) {
|
||||
$oneImage = [];
|
||||
foreach (explode(',',$item['image_ids']) as $imageId) {
|
||||
$oneImage[] = $imageList[$imageId]['url'] ?? '';
|
||||
}
|
||||
$item['content_image_list'] = $oneImage;
|
||||
}
|
||||
|
||||
return $this->return->success('success',$data);
|
||||
}
|
||||
}
|
||||
45
app/Service/Api/Suggest/SubmissionService.php
Normal file
45
app/Service/Api/Suggest/SubmissionService.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Suggest;
|
||||
|
||||
use App\Exception\ErrException;
|
||||
use App\Model\Suggest;
|
||||
use App\Service\Api\BaseService;
|
||||
use App\Service\ServiceTrait\Common\OssTrait;
|
||||
use Hyperf\DbConnection\Db;
|
||||
|
||||
class SubmissionService extends BaseService
|
||||
{
|
||||
use OssTrait;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
Db::transaction(function () {
|
||||
$insertModel = new Suggest();
|
||||
|
||||
$imageIds = $this->request->input('image_ids');
|
||||
|
||||
$insertModel->user_id = $this->userId;
|
||||
$insertModel->content = $this->request->input('content');
|
||||
if (!empty($imageIds)) {
|
||||
$insertModel->image_ids = $imageIds;
|
||||
$this->updateOssObjects(explode(',', $imageIds));
|
||||
}
|
||||
|
||||
if (!$insertModel->save()) throw new ErrException('提交失败');
|
||||
});
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user