// +---------------------------------------------------------------------- namespace app\controller\admin; use app\controller\Admin; use app\model\Link as LinkModel; class Link extends Admin{ /** * @title 友链列表 */ public function index(LinkModel $link){ if ($this->request->isAjax()) { $res = $link->paginate($this->request->pageConfig); $data = $res->toArray(); $this->data['data'] = $data; return $this->data; } } /** * @title 添加友链 */ public function add(LinkModel $link){ if ($this->request->isPost()) { $data = $this->request->post(); $result = $link->save($data); if (false !== $result) { return $this->success('成功添加', url('/admin/link/index')); }else{ return $this->error($this->model->getError()); } }else{ $info['appid'] = rand_string(10, 1); //八位数字appid $info['appsecret'] = rand_string(32); //32位数字加字母秘钥 $this->data['data'] = array( 'info' => $info ); return $this->data; } } /** * @title 编辑友链 */ public function edit(LinkModel $link){ if ($this->request->isPost()) { $data = $this->request->post(); if (!isset($data['id']) || !$data['id']) { return $this->error('非法操作!'); } $result = $link->exists(true)->save($data); if (false !== $result) { return $this->success('修改成功', url('/admin/link/index')); }else{ return $this->error($this->model->getError()); } }else{ $info = $link->where('id', $this->request->param('id'))->find(); $this->data['template'] = "add"; $this->data['data'] = array( 'info' => $info ); return $this->data; } } /** * @title 删除友链 */ public function del(LinkModel $link){ $ids = $this->request->param('ids', 0); if(!$ids){ return $this->error('非法操作!'); }else{ $ids = \explode(",", $ids); } $result = $link->where('id', 'IN', $ids)->delete(); if(false !== $result){ return $this->success('删除成功!'); }else{ return $this->error('删除失败!'); } } }