135 lines
4.2 KiB
PHP
135 lines
4.2 KiB
PHP
<?php
|
||
namespace app\controller\manager;
|
||
|
||
use app\model\{Link as MLink, System, Log};
|
||
use app\validate\Link as VLink;
|
||
use think\exception\ValidateException;
|
||
use think\facade\Cache;
|
||
|
||
class Link extends Base
|
||
{
|
||
//删除
|
||
public function del()
|
||
{
|
||
Cache::delete('links');//删除缓存
|
||
if ($this->request->isPost()) {
|
||
$ids = input('post.ids/a');
|
||
if(empty($ids) || !is_array($ids)) {
|
||
return $this->json(2, '参数错误,请核对之后再操作!');
|
||
}
|
||
$items = MLink::getListByIds($ids);
|
||
if(!empty($items)){
|
||
$delIds = [];
|
||
foreach($items as $item){
|
||
$delIds[] = $item['id'];
|
||
}
|
||
MLink::destroy($delIds);
|
||
Log::write('link', 'del', '删除了友情链接,涉及到的ID为:' . implode(',', $delIds));
|
||
return $this->json();
|
||
}else{
|
||
return $this->json(3, '待删除友情链接为空');
|
||
}
|
||
}
|
||
return $this->json(1, '非法请求!');
|
||
}
|
||
|
||
//排序
|
||
public function sort()
|
||
{
|
||
Cache::delete('links');//删除缓存
|
||
if($this->request->isPost()){
|
||
$id = input('post.id/d');
|
||
$sort = input('post.sort/d');
|
||
|
||
$item = MLink::findById($id);
|
||
if(empty($item)){
|
||
return $this->json(3, '该友情链接信息不存在!');
|
||
}
|
||
$item->sort = $sort;
|
||
$item->save();
|
||
return $this->json();
|
||
|
||
}
|
||
return $this->json(1, '非法请求!');
|
||
}
|
||
|
||
//编辑
|
||
public function edit()
|
||
{
|
||
Cache::delete('links');//删除缓存
|
||
if($this->request->isPost()){
|
||
$item = input('post.item/a');
|
||
$id = input('post.id/d');
|
||
$img = input('post.img');
|
||
if(is_numeric($id) && $id > 0) {
|
||
$link = MLink::findById($id);
|
||
if(empty($link)) {
|
||
return $this->json(2, '该友情链接信息不存在!');
|
||
}
|
||
if(!empty($img)){
|
||
$item['src'] = $img;
|
||
}
|
||
try {
|
||
validate(VLink::class)->check($item);
|
||
$link->save( $item);
|
||
Log::write('link', 'edit', "友情链接编辑,ID:{$id} ,标题:{$item['title']}");
|
||
return $this->json();
|
||
} catch (ValidateException $e) {
|
||
return $this->json(3, $e->getError());
|
||
}
|
||
}
|
||
return $this->json(1, '参数错误,请核对之后再操作!');
|
||
} else {
|
||
$id = input('param.id/d');
|
||
$item = MLink::getById($id);
|
||
$imgSize = System::getLinkImageSize();
|
||
|
||
$this->data['item'] = $item;
|
||
$this->data['img_size'] = $imgSize;
|
||
return $this->view();
|
||
}
|
||
}
|
||
|
||
//添加
|
||
public function add()
|
||
{
|
||
Cache::delete('links');//删除缓存
|
||
if($this->request->isPost()){
|
||
$item = input('post.item/a');
|
||
$img = input('post.img');
|
||
|
||
if(!empty($img)){
|
||
$item['src'] = $img;
|
||
}
|
||
try {
|
||
validate(VLink::class)->check($item);
|
||
$link = MLink::create($item);
|
||
Log::write('link', 'add', "友情链接新增,ID:{$link->id} ,标题:{$item['title']}");
|
||
return $this->json();
|
||
} catch (ValidateException $e) {
|
||
return $this->json(2, $e->getError());
|
||
}
|
||
} else {
|
||
$imgSize = System::getLinkImageSize();
|
||
$this->data['img_size'] = $imgSize;
|
||
return $this->view();
|
||
}
|
||
}
|
||
|
||
public function index()
|
||
{
|
||
if ($this->request->isPost()) {
|
||
$page = $this->request->param('page/d', 1);
|
||
$size = $this->request->param('size/d', 30);
|
||
|
||
$whereMap = [];
|
||
$orders = ['sort'=>'asc'];
|
||
|
||
$list = MLink::findList($whereMap, [], $page, $size, null, $orders);
|
||
|
||
return $this->json(0, 'success', $list);
|
||
}
|
||
return $this->view();
|
||
}
|
||
}
|