Institution.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\admin\controller\cpuser;
  3. use app\admin\BaseController;
  4. use app\admin\controller\Admin;
  5. use app\admin\model\Admin as AdminList;
  6. use app\admin\model\Department as DepartmentModel;
  7. use app\admin\validate\AdminCheck;
  8. use app\admin\validate\FinancialValidate;
  9. use think\exception\ValidateException;
  10. use think\facade\Db;
  11. use think\facade\Session;
  12. use think\facade\View;
  13. class Institution extends BaseController
  14. {
  15. /**
  16. * 构造函数
  17. */
  18. public function __construct()
  19. {
  20. $this->adminModel = new AdminList();
  21. $this->model = new DepartmentModel();
  22. $this->uid = get_login_admin('id');
  23. }
  24. public function datalist()
  25. {
  26. if (request()->isAjax()) {
  27. // 获取单位名称
  28. $session_admin = get_config('app.session_admin');
  29. $unit_name = Session::get($session_admin)['unit_name'];
  30. // halt($unit_name);
  31. $param = get_params();
  32. if (!empty($param['keywords'])) {
  33. $where[] = ['id|username|nickname|desc|mobile', 'like', '%' . $param['keywords'] . '%'];
  34. }
  35. $where = [
  36. ["delete_time","=",0],
  37. ["pid","=",0],
  38. ["type","=",1]
  39. ];
  40. $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
  41. $order = empty($param['order']) ? 'id desc' : $param['order'];
  42. $list = $this->model->where($where)->field('id,title,leader_id,phone,landline,address,company_type,authorization_module')
  43. ->order($order)->paginate($rows, false, ['query' => $param])
  44. ->each(function ($item){
  45. $item->leader = Db::name("admin")->where("id",$item->leader_id)->value("nickname");
  46. $item->username = Db::name("admin")->where("id",$item->leader_id)->value("username");
  47. });
  48. // halt($admin);
  49. return table_assign(0, '', $list);
  50. } else {
  51. return view();
  52. }
  53. }
  54. public function add()
  55. {
  56. if (request()->isAjax()) {
  57. $param = get_params();
  58. // 检验完整性
  59. // try {
  60. // validate(FinancialValidate::class)->check($param);
  61. // } catch (ValidateException $e) {
  62. // // 验证失败 输出错误信息
  63. // return to_assign(1, $e->getError());
  64. // }
  65. $param = array_merge($param,['type'=>1]);
  66. $insertId = 0;
  67. try {
  68. $param['create_time'] = time();
  69. $insertId = $this->model->strict(false)->field(true)->insertGetId($param);
  70. add_log('add', $insertId, $param);
  71. } catch(\Exception $e) {
  72. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  73. }
  74. return to_assign(0,'操作成功',['aid'=>$insertId]);
  75. // halt($param);
  76. }else{
  77. return view();
  78. }
  79. }
  80. /**
  81. * 编辑
  82. */
  83. public function edit()
  84. {
  85. $param = get_params();
  86. // halt($param);
  87. if (request()->isAjax()) {
  88. // 检验完整性
  89. try {
  90. validate(FinancialValidate::class)->check($param);
  91. } catch (ValidateException $e) {
  92. // 验证失败 输出错误信息
  93. return to_assign(1, $e->getError());
  94. }
  95. try {
  96. $param['update_time'] = time();
  97. $this->model->where('id', $param['id'])->strict(false)->field(true)->update($param);
  98. add_log('edit', $param['id'], $param);
  99. } catch(\Exception $e) {
  100. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  101. }
  102. return to_assign();
  103. }else{
  104. $id = isset($param['id']) ? $param['id'] : 0;
  105. $detail = $this->model->where('id', $id)->find();
  106. // halt($id);
  107. if (!empty($detail)) {
  108. View::assign('detail', $detail);
  109. return view();
  110. }
  111. else{
  112. throw new \think\exception\HttpException(404, '找不到页面');
  113. }
  114. }
  115. }
  116. /**
  117. * 查看信息
  118. */
  119. public function read()
  120. {
  121. $param = get_params();
  122. $id = isset($param['id']) ? $param['id'] : 0;
  123. $detail = $this->model->where('id', $id)->find();
  124. if (!empty($detail)) {
  125. View::assign('detail', $detail);
  126. return view();
  127. }
  128. else{
  129. throw new \think\exception\HttpException(404, '找不到页面');
  130. }
  131. }
  132. /**
  133. * 删除
  134. * type=0,逻辑删除,默认
  135. * type=1,物理删除
  136. */
  137. public function del()
  138. {
  139. $param = get_params();
  140. $id = isset($param['id']) ? $param['id'] : 0;
  141. $type = isset($param['type']) ? $param['type'] : 0;
  142. if($type==0){
  143. //逻辑删除
  144. try {
  145. $param['delete_time'] = time();
  146. $this->model->where('id', $id)->update(['delete_time'=>time()]);
  147. add_log('delete', $id);
  148. } catch(\Exception $e) {
  149. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  150. }
  151. }
  152. else{
  153. //物理删除
  154. try {
  155. $this->model->where('id', $id)->delete();
  156. add_log('delete', $id);
  157. } catch(\Exception $e) {
  158. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  159. }
  160. }
  161. return to_assign();
  162. }
  163. }