123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace laytp\traits;
- use laytp\library\CommonFun;
- use think\facade\Config;
- use think\facade\Db;
- trait Backend
- {
- /**
- * 列表
- * @return mixed
- */
- public function index()
- {
- global $_W;
- $where = $this->buildSearchParams();
- $where[] = ['uniacid','=',$_W['uniacid']];
- $order = $this->buildOrder();
- $data = $this->model->where($where)->order($order);
- $paging = $this->request->param('paging', false);
- if ($paging) {
- $limit = $this->request->param('limit', Config::get('paginate.limit'));
- $data = $data->paginate($limit)->toArray();
- $data['data'] = $this->getSelectedData($data['data']);
- } else {
- $data = $data->select()->toArray();
- }
- return $this->success('数据获取成功', $data);
- }
- //查看详情
- public function info()
- {
- $id = $this->request->param('id');
- $info = $this->model->find($id);
- return $this->success('获取成功', $info);
- }
- //添加
- public function add()
- {
- global $_W;
- $post = CommonFun::filterPostData($this->request->post());
- $post['uniacid'] = $_W['uniacid'];
- try {
- if ($this->model->create($post)) {
- return $this->success('添加成功', $post);
- } else {
- return $this->error('操作失败');
- }
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- //编辑
- public function edit()
- {
- $id = $this->request->param('id');
- $info = $this->model->find($id);
- if (!$info) {
- return $this->error('ID参数错误');
- }
- $post = CommonFun::filterPostData($this->request->post());
- foreach ($post as $k => $v) {
- $info->$k = $v;
- }
- try {
- $updateRes = $info->save();
- if ($updateRes) {
- return $this->success('编辑成功');
- } else {
- return $this->error('编辑失败');
- }
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- //删除
- public function del()
- {
- global $_W;
- $ids = array_filter($this->request->post('ids'));
- if (!$ids) {
- return $this->error('参数ids不能为空');
- }
- try {
- if ($this->model->destroy($ids)) {
- return $this->success('数据删除成功');
- } else {
- return $this->error('数据删除失败');
- }
- } catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- //回收站
- public function recycle()
- {
- global $_W;
- $where = $this->buildSearchParams();
- $where[] = ['uniacid','=',$_W['uniacid']];
- $order = $this->buildOrder();
- $data = $this->model->onlyTrashed()->where($where)->order($order);
- $limit = $this->request->param('limit', Config::get('paginate.limit'));
- $data = $data->paginate($limit)->toArray();
- $data['data'] = $this->getSelectedData($data['data']);
- return $this->success('回收站数据获取成功', $data);
- }
- //还原
- public function restore()
- {
- $where[] = ['id', 'in', $this->request->post('ids')];
- if ($this->model->restore($where)) {
- return $this->success('数据成功还原');
- } else {
- return $this->error('操作失败');
- }
- }
- //真实删除
- public function trueDel()
- {
- Db::startTrans();
- try {
- $ids = $this->request->post('ids');
- $res = $this->model->onlyTrashed()->where('id', 'in', $ids)->select();
- foreach ($res as $key => $item) {
- $delRes = $item->force()->delete();
- if (!$delRes) throw new \Exception('删除失败');
- }
- Db::commit();
- return $this->success('数据已经彻底删除');
- } catch (\Exception $e) {
- Db::rollback();
- return $this->error('数据库异常,操作失败');
- }
- }
- }
|