MateCate.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class MateCate extends Auth {
  5. public function index() {
  6. if (request()->isAjax()) {
  7. //分页参数
  8. $length = input('rows', 10, 'intval'); //每页条数
  9. $page = input('page', 1, 'intval'); //第几页
  10. $start = ($page - 1) * $length; //分页开始位置
  11. //排序
  12. $sortRow = input('sidx', 'id', 'trim'); //排序列
  13. $sort = input('sord', 'asc', 'trim'); //排序方式
  14. $order = $sortRow . ' ' . $sort;
  15. $title = input('title', '', 'trim');
  16. if ($title) {
  17. $map[] = ['title', 'like', '%' . $title . '%'];
  18. }
  19. $map[] = ['org_id', '=', $this->orgId];
  20. $map[] = ['del', '=',0];
  21. $map = empty($map) ? true : $map;
  22. //数据查询
  23. $lists = Db::name('mate_cate')
  24. ->where($map)->limit($start, $length)
  25. ->order($order)->select();
  26. //数据返回
  27. $totalCount = Db::name('mate_cate')->where($map)->count();
  28. $totalPage = ceil($totalCount / $length);
  29. $result['page'] = $page;
  30. $result['total'] = $totalPage;
  31. $result['records'] = $totalCount;
  32. $result['rows'] = $lists;
  33. return json($result);
  34. }
  35. else {
  36. $this->assign('meta_title', '物品分类列表');
  37. return $this->fetch();
  38. }
  39. }
  40. /**
  41. * 新增/编辑
  42. */
  43. public function add($id = 0) {
  44. if (request()->isPost()) {
  45. $model = new \app\common\model\MateCate();
  46. $res = $model->updates();
  47. if ($res) {
  48. $this->success('操作成功', url('index'));
  49. }
  50. else {
  51. $this->error($model->getError());
  52. }
  53. }
  54. else {
  55. $meta_title = '新增物品分类';
  56. if ($id) {
  57. $info = Db::name('mate_cate')->where('id', $id)->find();
  58. $this->assign('info', $info);
  59. $meta_title = '编辑物品分类';
  60. }
  61. $this->assign('meta_title', $meta_title);
  62. return $this->fetch();
  63. }
  64. }
  65. /**
  66. * 删除记录
  67. * @param int $id
  68. */
  69. public function del($id = 0) {
  70. if (!$id) {
  71. $this->error('参数错误');
  72. }
  73. $res = Db::name('mate_cate')->where('id',$id)->setField('del',1);
  74. if ($res) {
  75. $this->success('删除成功');
  76. }
  77. else {
  78. $this->error('删除失败');
  79. }
  80. }
  81. }