PhUser.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\WorkerBalance;
  4. use think\App;
  5. use think\Db;
  6. use think\Exception;
  7. class PhUser extends Auth {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->model = new \app\common\model\PhUser();
  11. $this->table = 'ph_user';
  12. }
  13. public function index() {
  14. if (request()->isAjax()) {
  15. //分页参数
  16. $length = input('rows',10,'intval'); //每页条数
  17. $page = input('page',1,'intval'); //第几页
  18. $start = ($page - 1) * $length; //分页开始位置
  19. $name = input('name','','trim');
  20. if ($name != '') {
  21. $ids =Db::name('user')
  22. ->alias('a')
  23. ->join('user_org b','a.id = b.user_id')
  24. ->where('a.real_name','=',$name)
  25. ->where('b.org_id',$this->orgId)
  26. ->column('a.id');
  27. $map[] = ['user','in',$ids];
  28. }
  29. $map[] = ['org_id','=',$this->orgId];
  30. $map[] = ['del','=',0];
  31. $map= empty($map) ? true: $map;
  32. //数据查询
  33. $user= Db::name('user')
  34. ->alias('a')
  35. ->join('user_org b','a.id = b.user_id')
  36. ->where('b.org_id','=',$this->orgId)
  37. ->where('a.del','=',0)
  38. ->where('a.enable','=',1)
  39. ->column('a.real_name','a.id');
  40. $dep= Db::name('dep')
  41. ->where('org_id','=',$this->orgId)
  42. ->where('del','=',0)
  43. ->where('enable','=',1)
  44. ->column('title','id');
  45. $list = Db::name($this->table)->where($map)->limit($start,$length)->order('id desc')->select();
  46. foreach ($list as $k=>$v){
  47. $list[$k]['userName'] = $user[$v['user']];
  48. $depList = explode(',',$v['dep']);
  49. foreach ($depList as $k1=>$v1){
  50. $depList[$k1] = $dep[$v1];
  51. }
  52. $list[$k]['depName'] = implode(',',$depList);
  53. }
  54. $totalCount = Db::name($this->table)->where($map)->count();
  55. $totalPage = ceil($totalCount/$length);
  56. $result['page'] = $page;
  57. $result['total'] = $totalPage;
  58. $result['records'] = $totalCount;
  59. $result['rows'] = $list;
  60. return json($result);
  61. }
  62. else {
  63. return $this->fetch();
  64. }
  65. }
  66. /**
  67. * 新增/编辑
  68. */
  69. public function add($id = 0) {
  70. $model = $this->model;
  71. if (request()->isPost()) {
  72. $res = $model->updates($this->orgId);
  73. if ($res) {
  74. $this->success('操作成功', url('index'));
  75. }
  76. else {
  77. $this->error($model->getError());
  78. }
  79. }
  80. else {
  81. if ($id) {
  82. $info = Db::name($this->table)->where('id',$id)->find();
  83. $info['dep'] = explode(',',$info['dep']);
  84. $this->assign('info', $info);
  85. }
  86. $depLisr = model('Dep')->getList();
  87. $this->assign('depList', $depLisr);
  88. $userList= Db::name('user')
  89. ->alias('a')
  90. ->join('user_org b','a.id = b.user_id')
  91. ->where('b.org_id','=',$this->orgId)
  92. ->where('a.del','=',0)
  93. ->where('a.enable','=',1)
  94. ->field('a.id,a.real_name as title')
  95. ->select();
  96. $this->assign('userList', $userList);
  97. return $this->fetch();
  98. }
  99. }
  100. /**
  101. * 删除记录
  102. * @param int $id
  103. */
  104. public function del($id = 0) {
  105. if (!$id) {
  106. $this->error('参数错误');
  107. }
  108. $res = db('user')->where('id', $id)->setField('del', 1);
  109. if ($res) {
  110. $this->success('删除成功');
  111. }
  112. else {
  113. $this->error('删除失败');
  114. }
  115. }
  116. /**
  117. * 改变字段值
  118. * @param int $fv
  119. * @param string $fn
  120. * @param int $fv
  121. */
  122. public function changeField($id = 0, $fn = '', $fv = 0) {
  123. if (!$fn || !$id) {
  124. $this->error('参数错误');
  125. }
  126. if ($fn == 'enable') {
  127. $res = Db::name($this->table)->where('id', $id)->update([$fn => $fv]);
  128. }
  129. if ($res) {
  130. $this->success('操作成功');
  131. }
  132. else {
  133. $this->error('操作失败');
  134. }
  135. }
  136. }