111 lines
3.5 KiB
PHP
111 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use support\Request;
|
|
use support\Response;
|
|
use hg\apidoc\annotation as Apidoc;
|
|
use app\model\User;
|
|
use app\model\Album as AlbumModel;
|
|
|
|
/**
|
|
* 群相册
|
|
*/
|
|
class AlbumController extends BaseController
|
|
{
|
|
public $noNeedAuth = ['*'];
|
|
public $noNeedLogin = [];
|
|
/**
|
|
* @Apidoc\Title("群相册列表")
|
|
* @Apidoc\Method("POST")
|
|
* @Apidoc\Param("groupID", type="string", require=true, desc="群ID")
|
|
* @Apidoc\Param("offset", type="int", require=false, desc="偏移量,和页码二选一",default=0)
|
|
* @Apidoc\Param("page", type="int", require=false, desc="页码",default=1)
|
|
* @Apidoc\Param("limit", type="int", require=true, desc="分页大小",default=10)
|
|
*/
|
|
function list(Request $request): Response
|
|
{
|
|
$user = \support\Jwt::getUser();
|
|
$limit = $request->post('limit',10);
|
|
$offset = $request->post('offset',0);
|
|
$groupID = $request->post('groupID') ?:0;
|
|
if(!$groupID){
|
|
return $this->error('groupID is invalid');
|
|
}
|
|
//$ls = $this->get_user_in_group(groupID);
|
|
$query = AlbumModel::where('groupID',$groupID)
|
|
->order('id','desc');
|
|
if($offset){
|
|
$list = $query->where('id','<',$offset)->limit($offset,$limit);
|
|
}else{
|
|
$list = $query->paginate($limit);
|
|
}
|
|
$list->each(function($item){
|
|
if($item->image){
|
|
$item['image_detail'] = \support\think\Db::name('gallery')->where('id',$item->image)->find();
|
|
}
|
|
return $item;
|
|
});
|
|
return $this->success('ok',$list);
|
|
}
|
|
/**
|
|
* @Apidoc\Title("创建相册")
|
|
* @Apidoc\Method("POST")
|
|
* @Apidoc\Param("groupID", type="string", require=true, desc="群ID")
|
|
* @Apidoc\Param("title", type="string", require=true, desc="标题")
|
|
* @Apidoc\Param("image", type="int", require=false, desc="封面ID")
|
|
*/
|
|
function create(Request $request): Response
|
|
{
|
|
$user_id = \support\Jwt\JwtToken::getCurrentId();
|
|
$data = [
|
|
'userID' => $user_id,
|
|
'groupID' => input('groupID'),
|
|
'title' => input('title'),
|
|
'image' => input('image'),
|
|
];
|
|
log_alert($data);
|
|
|
|
if(!$data['groupID']){
|
|
return $this->error(__('Invalid parameters'));
|
|
}
|
|
$result = AlbumModel::create($data);
|
|
return $this->success('ok',$result);
|
|
}
|
|
/**
|
|
* @Apidoc\Title("更新")
|
|
* @Apidoc\Method("POST")
|
|
* @Apidoc\Param("id", type="string", require=true, desc="ID")
|
|
* @Apidoc\Param("title", type="string", require=true, desc="标题")
|
|
* @Apidoc\Param("image", type="int", require=false, desc="封面ID")
|
|
*/
|
|
function update(Request $request): Response
|
|
{
|
|
$id = $request->input('id');
|
|
$image = $request->input('image');
|
|
$title = $request->input('title');
|
|
$album = AlbumModel::find($id);
|
|
if($title){
|
|
$album->title = $title;
|
|
}
|
|
if($image){
|
|
$album->image = $image;
|
|
}
|
|
$album->save();
|
|
return $this->success('ok',$album);
|
|
}
|
|
/**
|
|
* @Apidoc\Title("删除")
|
|
* @Apidoc\Method("POST")
|
|
* @Apidoc\Param("id", type="string", require=true, desc="ID")
|
|
*/
|
|
function delete(Request $request): Response
|
|
{
|
|
$id = Input('id');
|
|
$album = AlbumModel::whereIn('id',condition: $id)->find();
|
|
$album->delete();
|
|
|
|
return $this->success('ok');
|
|
}
|
|
}
|