Address.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. foreach ($data as $key => $value) {
  29. $value['thumb_url'] = geturl($value['thumb']);
  30. $data[$key] = $value;
  31. }
  32. return array('code' => 0, 'count' => $count, 'data' => $data);
  33. } else {
  34. return $this->fetch();
  35. }
  36. }
  37. public function publish()
  38. {
  39. $id = $this->request->param('id', 0, 'intval');
  40. $model = $this->getModel();
  41. $post = $this->request->post();
  42. if ($this->request->isPost()) {
  43. //验证
  44. $validate = new \think\Validate([
  45. ['title|地名', 'max:50'],
  46. ['address|详细地址', 'max:500'],
  47. ]);
  48. if (!$validate->check($post)) {
  49. $this->error('提交失败:' . $validate->getError());
  50. }
  51. }
  52. if ($id > 0) {
  53. //修改
  54. $data = $model->where('id', $id)->find();
  55. if (empty($data)) {
  56. $this->error('id不正确');
  57. }
  58. if ($this->request->isPost()) {
  59. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  60. $this->error('修改失败');
  61. } else {
  62. $this->success('修改成功');
  63. }
  64. } else {
  65. $this->assign('data', $data);
  66. return $this->fetch();
  67. }
  68. } else {
  69. //新增
  70. if ($this->request->isPost()) {
  71. if (false == $model->allowField(true)->save($post)) {
  72. $this->error('添加失败');
  73. } else {
  74. $this->success('添加成功', 'index');
  75. }
  76. } else {
  77. return $this->fetch();
  78. }
  79. }
  80. }
  81. public function delete()
  82. {
  83. if ($this->request->isAjax()) {
  84. $id = $this->request->param('id', 0, 'intval');
  85. if (false == $this->getModel()->where('id', $id)->delete()) {
  86. $this->error('删除失败');
  87. } else {
  88. $this->success('删除成功', 'index');
  89. }
  90. }
  91. }
  92. public function deletes()
  93. {
  94. if ($this->request->isAjax()) {
  95. $post = $this->request->param();
  96. $ids = $post['ids'];
  97. if ($this->getModel()->where('id', 'in', $ids)->delete()) {
  98. $this->success('删除成功');
  99. }
  100. }
  101. }
  102. }