Backend.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace laytp\traits;
  3. use laytp\library\CommonFun;
  4. use think\facade\Config;
  5. use think\facade\Db;
  6. trait Backend
  7. {
  8. /**
  9. * 列表
  10. * @return mixed
  11. */
  12. public function index()
  13. {
  14. global $_W;
  15. $where = $this->buildSearchParams();
  16. $where[] = ['uniacid','=',$_W['uniacid']];
  17. $order = $this->buildOrder();
  18. $data = $this->model->where($where)->order($order);
  19. $paging = $this->request->param('paging', false);
  20. if ($paging) {
  21. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  22. $data = $data->paginate($limit)->toArray();
  23. $data['data'] = $this->getSelectedData($data['data']);
  24. } else {
  25. $data = $data->select()->toArray();
  26. }
  27. return $this->success('数据获取成功', $data);
  28. }
  29. //查看详情
  30. public function info()
  31. {
  32. $id = $this->request->param('id');
  33. $info = $this->model->find($id);
  34. return $this->success('获取成功', $info);
  35. }
  36. //添加
  37. public function add()
  38. {
  39. global $_W;
  40. $post = CommonFun::filterPostData($this->request->post());
  41. $post['uniacid'] = $_W['uniacid'];
  42. try {
  43. if ($this->model->create($post)) {
  44. return $this->success('添加成功', $post);
  45. } else {
  46. return $this->error('操作失败');
  47. }
  48. } catch (\Exception $e) {
  49. return $this->exceptionError($e);
  50. }
  51. }
  52. //编辑
  53. public function edit()
  54. {
  55. $id = $this->request->param('id');
  56. $info = $this->model->find($id);
  57. if (!$info) {
  58. return $this->error('ID参数错误');
  59. }
  60. $post = CommonFun::filterPostData($this->request->post());
  61. foreach ($post as $k => $v) {
  62. $info->$k = $v;
  63. }
  64. try {
  65. $updateRes = $info->save();
  66. if ($updateRes) {
  67. return $this->success('编辑成功');
  68. } else {
  69. return $this->error('编辑失败');
  70. }
  71. } catch (\Exception $e) {
  72. return $this->exceptionError($e);
  73. }
  74. }
  75. //删除
  76. public function del()
  77. {
  78. global $_W;
  79. $ids = array_filter($this->request->post('ids'));
  80. if (!$ids) {
  81. return $this->error('参数ids不能为空');
  82. }
  83. try {
  84. if ($this->model->destroy($ids)) {
  85. return $this->success('数据删除成功');
  86. } else {
  87. return $this->error('数据删除失败');
  88. }
  89. } catch (\Exception $e) {
  90. return $this->exceptionError($e);
  91. }
  92. }
  93. //回收站
  94. public function recycle()
  95. {
  96. global $_W;
  97. $where = $this->buildSearchParams();
  98. $where[] = ['uniacid','=',$_W['uniacid']];
  99. $order = $this->buildOrder();
  100. $data = $this->model->onlyTrashed()->where($where)->order($order);
  101. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  102. $data = $data->paginate($limit)->toArray();
  103. $data['data'] = $this->getSelectedData($data['data']);
  104. return $this->success('回收站数据获取成功', $data);
  105. }
  106. //还原
  107. public function restore()
  108. {
  109. $where[] = ['id', 'in', $this->request->post('ids')];
  110. if ($this->model->restore($where)) {
  111. return $this->success('数据成功还原');
  112. } else {
  113. return $this->error('操作失败');
  114. }
  115. }
  116. //真实删除
  117. public function trueDel()
  118. {
  119. Db::startTrans();
  120. try {
  121. $ids = $this->request->post('ids');
  122. $res = $this->model->onlyTrashed()->where('id', 'in', $ids)->select();
  123. foreach ($res as $key => $item) {
  124. $delRes = $item->force()->delete();
  125. if (!$delRes) throw new \Exception('删除失败');
  126. }
  127. Db::commit();
  128. return $this->success('数据已经彻底删除');
  129. } catch (\Exception $e) {
  130. Db::rollback();
  131. return $this->error('数据库异常,操作失败');
  132. }
  133. }
  134. }