HospitaldepartmentsController.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\HospitalDepartments;
  5. class HospitaldepartmentsController extends Base
  6. {
  7. function index()
  8. {
  9. $keyword = input('post.keyword', '', 'serach_in');
  10. $query = HospitalDepartments::where('weid', weid());
  11. if (!empty($keyword)) {
  12. $query->where('title', 'like', '%' . $keyword . '%');
  13. }
  14. $list = $query->order('sort asc')->select()->toArray();
  15. foreach ($list as &$vo) {
  16. $vo['image'] = toimg($vo['image']);
  17. }
  18. $data['data'] = _generateListTree($list, 0, ['id', 'pid']);
  19. return $this->json($data);
  20. }
  21. function listUpdate()
  22. {
  23. $data = only('id,is_binding,status,sort');
  24. if (!$data['id']) throw new ValidateException('参数错误');
  25. HospitalDepartments::update($data);
  26. return $this->json(['msg' => '操作成功']);
  27. }
  28. public function update()
  29. {
  30. $id = $this->request->post('id');
  31. $data = input('post.');
  32. unset($data['create_time']);
  33. $data['pid'] = (int)$data['pid'];
  34. if (empty($id)) {
  35. $data['weid'] = weid();
  36. try {
  37. $res = HospitalDepartments::create($data);
  38. if ($res->id && empty($data['sort'])) {
  39. HospitalDepartments::update(['sort' => $res->id, 'id' => $res->id]);
  40. }
  41. } catch (\Exception $e) {
  42. throw new ValidateException($e->getMessage());
  43. }
  44. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  45. } else {
  46. try {
  47. HospitalDepartments::update($data);
  48. } catch (\Exception $e) {
  49. throw new ValidateException($e->getMessage());
  50. }
  51. return $this->json(['msg' => '修改成功']);
  52. }
  53. }
  54. function getInfo()
  55. {
  56. $id = $this->request->post('id', '', 'serach_in');
  57. if (!$id) throw new ValidateException('参数错误');
  58. $res = HospitalDepartments::find($id);
  59. if ($res) {
  60. $res = $res->toArray();
  61. $res['image'] = toimg($res['image']);
  62. }
  63. return $this->json(['data' => $res]);
  64. }
  65. function delete()
  66. {
  67. return $this->del(new HospitalDepartments());
  68. }
  69. function getField()
  70. {
  71. $data['pids'] = _generateSelectTree(HospitalDepartments::getpcarray());
  72. return $this->json(['data' => $data]);
  73. }
  74. function getTree()
  75. {
  76. $alldata[0]['val'] = '0';
  77. $alldata[0]['key'] = '全部分类';
  78. $alldata[0]['pid'] = 0;
  79. $cdata = _generateSelectTree(HospitalDepartments::getpcarray());
  80. $data = array_merge($alldata,$cdata);
  81. return $this->json(['data' => $data]);
  82. }
  83. }