MateCate.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 = empty($map) ? true : $map;
  21. //数据查询
  22. $lists = Db::name('mate_cate')
  23. ->where($map)->limit($start, $length)
  24. ->order($order)->select();
  25. //数据返回
  26. $totalCount = Db::name('mate_cate')->where($map)->count();
  27. $totalPage = ceil($totalCount / $length);
  28. $result['page'] = $page;
  29. $result['total'] = $totalPage;
  30. $result['records'] = $totalCount;
  31. $result['rows'] = $lists;
  32. return json($result);
  33. }
  34. else {
  35. $this->assign('meta_title', '物品分类列表');
  36. return $this->fetch();
  37. }
  38. }
  39. /**
  40. * 新增/编辑
  41. */
  42. public function add($id = 0) {
  43. if (request()->isPost()) {
  44. $model = new \app\common\model\MateCate();
  45. $res = $model->updates();
  46. if ($res) {
  47. $this->success('操作成功', url('index'));
  48. }
  49. else {
  50. $this->error($model->getError());
  51. }
  52. }
  53. else {
  54. $meta_title = '新增物品分类';
  55. if ($id) {
  56. $info = Db::name('mate_cate')->where('id', $id)->find();
  57. $this->assign('info', $info);
  58. $meta_title = '编辑物品分类';
  59. }
  60. $this->assign('meta_title', $meta_title);
  61. return $this->fetch();
  62. }
  63. }
  64. /**
  65. * 删除记录
  66. * @param int $id
  67. */
  68. public function del($id = 0) {
  69. if (!$id) {
  70. $this->error('参数错误');
  71. }
  72. $res = Db::name('mate_cate')->where('id',$id)->setField('del',1);
  73. if ($res) {
  74. $this->success('删除成功');
  75. }
  76. else {
  77. $this->error('删除失败');
  78. }
  79. }
  80. }