Address.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\controller\base\Permissions;
  4. use think\Db;
  5. class Address extends Permissions
  6. {
  7. private function getModel()
  8. {
  9. return new \app\common\model\Address();
  10. }
  11. public function index()
  12. {
  13. if ($this->request->isAjax()) {
  14. $post = $this->request->param();
  15. $where = [];
  16. if (isset($post['ids']) and !empty($post['ids'])) {
  17. $where['id'] = ['in', $post['ids']];
  18. }
  19. if (!empty($post["title"])) {
  20. $where["title"] = ['like', '%' . $post["title"] . '%'];
  21. }
  22. if (!empty($post["address"])) {
  23. $where["address"] = ['like', '%' . $post["address"] . '%'];
  24. }
  25. $model = $this->getModel();
  26. $count = $model->where($where)->count();
  27. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();
  28. return array('code' => 0, 'count' => $count, 'data' => $data);
  29. } else {
  30. return $this->fetch();
  31. }
  32. }
  33. public function publish()
  34. {
  35. $id = $this->request->param('id', 0, 'intval');
  36. $model = $this->getModel();
  37. $post = $this->request->post();
  38. if ($this->request->isPost()) {
  39. //验证
  40. $validate = new \think\Validate([
  41. ['title|标题', 'max:50'],
  42. ['address|地址', 'max:500'],
  43. ]);
  44. if (!$validate->check($post)) {
  45. $this->error('提交失败:' . $validate->getError());
  46. }
  47. }
  48. if ($id > 0) {
  49. //修改
  50. $data = $model->where('id', $id)->find();
  51. if (empty($data)) {
  52. $this->error('id不正确');
  53. }
  54. if ($this->request->isPost()) {
  55. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  56. $this->error('修改失败');
  57. } else {
  58. $this->success('修改成功');
  59. }
  60. } else {
  61. $this->assign('data', $data);
  62. return $this->fetch();
  63. }
  64. } else {
  65. //新增
  66. if ($this->request->isPost()) {
  67. if (false == $model->allowField(true)->save($post)) {
  68. $this->error('添加失败');
  69. } else {
  70. $this->success('添加成功', 'index');
  71. }
  72. } else {
  73. return $this->fetch();
  74. }
  75. }
  76. }
  77. public function delete()
  78. {
  79. if ($this->request->isAjax()) {
  80. $id = $this->request->param('id', 0, 'intval');
  81. if (false == $this->getModel()->where('id', $id)->delete()) {
  82. $this->error('删除失败');
  83. } else {
  84. $this->success('删除成功', 'index');
  85. }
  86. }
  87. }
  88. public function deletes()
  89. {
  90. if ($this->request->isAjax()) {
  91. $post = $this->request->param();
  92. $ids = $post['ids'];
  93. if ($this->getModel()->where('id', 'in', $ids)->delete()) {
  94. $this->success('删除成功');
  95. }
  96. }
  97. }
  98. }