Address.php 3.4 KB

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