Specialist.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\controller\base\Permissions;
  4. use think\Db;
  5. class Specialist extends Permissions
  6. {
  7. private function getModel()
  8. {
  9. return new \app\common\model\Specialist();
  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["name"])) {
  20. $where["name"] = ['like', '%' . $post["name"] . '%'];
  21. }
  22. if (!empty($post["head_pic"])) {
  23. $where["head_pic"] = ['like', '%' . $post["head_pic"] . '%'];
  24. }
  25. if (isset($post["sex"]) and ""!=$post["sex"]) {
  26. $where["sex"] = $post["sex"];
  27. }
  28. if (!empty($post["desc"])) {
  29. $where["desc"] = ['like', '%' . $post["desc"] . '%'];
  30. }
  31. if (!empty($post["consultation_direction"])) {
  32. $where["consultation_direction"] = ['like', '%' . $post["consultation_direction"] . '%'];
  33. }
  34. if (isset($post["address_id"]) and ""!=$post["address_id"]) {
  35. $where["address_id"] = $post["address_id"];
  36. }
  37. $model = $this->getModel();
  38. $count = $model->where($where)->count();
  39. $data = $model->where($where)->page($post['page']??0, $post['limit']??15)->order('id desc')->select();
  40. return array('code' => 0, 'count' => $count, 'data' => $data);
  41. } else {
  42. return $this->fetch();
  43. }
  44. }
  45. public function publish()
  46. {
  47. $id = $this->request->param('id', 0, 'intval');
  48. $model = $this->getModel();
  49. $post = $this->request->post();
  50. if ($this->request->isPost()) {
  51. //验证
  52. $validate = new \think\Validate([
  53. ['name|名字', 'max:50'],
  54. ['head_pic|头像', 'max:255'],
  55. ['sex|性别', 'number'],
  56. ['desc|简介', 'max:500'],
  57. ['consultation_direction|咨询方向', 'max:500'],
  58. ['address_id|地址', 'require|number'],
  59. ]);
  60. if (!$validate->check($post)) {
  61. $this->error('提交失败:' . $validate->getError());
  62. }
  63. }
  64. if ($id > 0) {
  65. //修改
  66. $data = $model->where('id', $id)->find();
  67. if (empty($data)) {
  68. $this->error('id不正确');
  69. }
  70. if ($this->request->isPost()) {
  71. if (false == $model->allowField(true)->save($post, ['id' => $id])) {
  72. $this->error('修改失败');
  73. } else {
  74. $this->success('修改成功');
  75. }
  76. } else {
  77. $this->assign('data', $data);
  78. return $this->fetch();
  79. }
  80. } else {
  81. //新增
  82. if ($this->request->isPost()) {
  83. if (false == $model->allowField(true)->save($post)) {
  84. $this->error('添加失败');
  85. } else {
  86. $this->success('添加成功', 'index');
  87. }
  88. } else {
  89. return $this->fetch();
  90. }
  91. }
  92. }
  93. public function delete()
  94. {
  95. if ($this->request->isAjax()) {
  96. $id = $this->request->param('id', 0, 'intval');
  97. if (false == $this->getModel()->where('id', $id)->delete()) {
  98. $this->error('删除失败');
  99. } else {
  100. $this->success('删除成功', 'index');
  101. }
  102. }
  103. }
  104. public function deletes()
  105. {
  106. if ($this->request->isAjax()) {
  107. $post = $this->request->param();
  108. $ids = $post['ids'];
  109. if ($this->getModel()->where('id', 'in', $ids)->delete()) {
  110. $this->success('删除成功');
  111. }
  112. }
  113. }
  114. }