UseradminController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. class UseradminController extends Controller
  3. {
  4. public function filters()
  5. {
  6. return array(
  7. 'accessControl', // perform access control for CRUD operations
  8. );
  9. }
  10. public function actionInfo()
  11. {
  12. $model = Useradmin::model()->findByPk(Yii::app()->user->_id);
  13. if (!$model) {
  14. Helper::error('信息未找到');
  15. }
  16. $authIds = DB::getScalerWithCriteria('role', DbCriteria::simpleCompare(['id' => $model->role_id])->setSelect('auth_ids'));
  17. $authIds = $authIds ? explode(',', $authIds) : [];
  18. $authIds = array_map(function ($item) {
  19. return (int)$item;
  20. }, $authIds);
  21. Helper::ok([
  22. 'id' => $model->id,
  23. 'username' => $model->username,
  24. 'auth_ids' => $authIds,
  25. 'buttons' => [],
  26. 'avatar' => Helper::getImageUrl($model->avatar),
  27. 'email' => $model->email,
  28. 'phone' => $model->phone,
  29. 'descr' => $model->descr,
  30. ]);
  31. }
  32. public function actionRoleList()
  33. {
  34. $name = Helper::getPostString('name');
  35. $name = $name ? '%' . $name : null;
  36. $cri = DbCriteria::simpleCompareWithPage(['name' => $name])
  37. ->setSelect('id, name, auth_ids, descr, create_date, show_ids')
  38. ->setOrder('id desc');
  39. $data = DB::getListWithCriteria('role', $cri);
  40. if (!empty($data['records'])) {
  41. $data['records'] = array_map(function ($item) {
  42. $item['auth_ids'] = $item['auth_ids'] ? explode(',', $item['auth_ids']) : [];
  43. $item['auth_ids'] = array_map(function ($aid) {
  44. return (int)$aid;
  45. }, $item['auth_ids']);
  46. $item['show_ids'] = $item['show_ids'] ? explode(',', $item['show_ids']) : [];
  47. $item['show_ids'] = array_map(function ($aid) {
  48. return (int)$aid;
  49. }, $item['show_ids']);
  50. return $item;
  51. }, $data['records']);
  52. }
  53. Helper::ok($data);
  54. }
  55. public function actionUserList()
  56. {
  57. $name = Helper::getPostString('name');
  58. $name = $name ? '%' . $name : null;
  59. $filters = [
  60. 'username' => $name,
  61. 'u.id' => '!=1',
  62. 'u.status' => '1',
  63. 'role_id' => Helper::getPostInt('role_id')?:null,
  64. 'phone' => Helper::getPostString('phone')?:null,
  65. ];
  66. $cri = DbCriteria::simpleCompareWithPage($filters)
  67. ->setAlias('u')
  68. ->setDebugUntil('234', '-1')
  69. ->setSelect('u.id, u.username, r.name as role_name, u.status, u.role_id, u.sex, u.phone, u.create_date, u.avatar, u.update_date, u.company_ids, u.school_ids')
  70. ->setJoin('left join wx_role r on u.role_id = r.id')
  71. ->setOrder('id desc');
  72. $data = DB::getListWithCriteria('useradmin', $cri);
  73. if (!empty($data['records'])) {
  74. $schools = Helper::arrayColumn(
  75. DB::getListWithCriteria('school', DbCriteria::simpleCompare([])->setSelect('id, name')),
  76. 'name',
  77. 'id'
  78. );
  79. $companys = Helper::arrayColumn(
  80. DB::getListWithCriteria('company', DbCriteria::simpleCompare([])->setSelect('id, name')),
  81. 'name',
  82. 'id'
  83. );
  84. $data['records'] = array_map(function ($item) use ($schools, $companys) {
  85. $item['avatar'] = Helper::getImageUrl($item['avatar']);
  86. $item['company_ids'] = $item['company_ids'] ? array_map(function ($item) {return (int)$item;}, explode(',', $item['company_ids'])) : [];
  87. $item['school_ids'] = $item['school_ids'] ? array_map(function ($item) {return (int)$item;}, explode(',', $item['school_ids'])) : [];
  88. $item['company_names'] = array_map(function ($item) use ($companys) {
  89. if (isset($companys[$item])) return $companys[$item];
  90. }, $item['company_ids']);
  91. $item['school_names'] = array_map(function ($item) use ($schools) {
  92. if (isset($schools[$item])) return $schools[$item];
  93. }, $item['school_ids']);
  94. return $item;
  95. }, $data['records']);
  96. }
  97. Helper::ok($data);
  98. }
  99. public function actionSaveRoleAuth()
  100. {
  101. $id = Helper::getPostInt('id');
  102. $leaf_ids = Helper::getArrParam($_POST, 'leaf_ids', 'array_int', []);
  103. $half_Leaf_ids = Helper::getArrParam($_POST, 'half_Leaf_ids', 'array_int', []);
  104. if ($id < 0 || !$leaf_ids) {
  105. Helper::error('参数错误');
  106. }
  107. $info = [
  108. 'auth_ids' => implode(',', Helper::concatArray($half_Leaf_ids, $leaf_ids)),
  109. 'show_ids' => $leaf_ids ? implode(',', $leaf_ids) : '',
  110. ];
  111. DB::updateById('role', $info, $id);
  112. $users = DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare(['role_id' => $id])->setSelect('id'))?:[];
  113. foreach ($users as $user) {
  114. $this->clearAuth($user['id']);
  115. }
  116. Helper::ok();
  117. }
  118. public function actionEditUser()
  119. {
  120. $id = Helper::getPostInt('id');
  121. $username = Helper::getPostString('username');
  122. $password = Helper::getPostString('password');
  123. $phone = Helper::getPostString('phone');
  124. $sex = Helper::getPostInt('sex');
  125. $role_id = Helper::getPostInt('role_id');
  126. $company_ids = Helper::getArrParam($_POST, 'company_ids', 'array_int', []);
  127. $school_ids = Helper::getArrParam($_POST, 'school_ids', 'array_int', []);
  128. // username不能为空和重复
  129. if (!$username) {
  130. Helper::error('用户名不能为空');
  131. }
  132. $cri = DbCriteria::simpleCompare(['username' => $username])->setSelect('id');
  133. if ($id > 0) {
  134. $cri->addCondition('id!=' . $id);
  135. }
  136. if ($fid = DB::getScalerWithCriteria('useradmin', $cri)) {
  137. Helper::error('用户名已存在 ' . $fid);
  138. }
  139. $info = [
  140. 'username' => $username,
  141. 'phone' => $phone,
  142. 'sex' => $sex,
  143. 'role_id' => $role_id,
  144. 'company_ids' => $company_ids ? implode(',', $company_ids) : '',
  145. 'school_ids' => $school_ids ? implode(',', $school_ids) : '',
  146. ];
  147. if (!$id) {
  148. // 新增用户
  149. if (!$password) {
  150. Helper::error('密码不能为空');
  151. }
  152. if (!$role_id) {
  153. Helper::error('请选择角色');
  154. }
  155. $info['password'] = md5($password);
  156. DB::addData('useradmin', $info);
  157. } else {
  158. $this->clearAuth($id);
  159. DB::updateById('useradmin', $info, $id);
  160. }
  161. Helper::ok();
  162. }
  163. public function actionDeleteUser()
  164. {
  165. $id = Helper::getPostInt('id');
  166. if ($id < 1) {
  167. Helper::error('参数错误');
  168. }
  169. DB::updateById('useradmin', ['status' => 0], $id);
  170. Helper::ok();
  171. }
  172. public function actionDeleteRole()
  173. {
  174. $id = Helper::getPostInt('id');
  175. if ($id < 1) {
  176. Helper::error('参数错误');
  177. }
  178. if (DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['role_id' => $id]))->setselect('id')) {
  179. Helper::error('该角色下有用户,请先删除用户');
  180. }
  181. DB::deleteById('role', $id);
  182. Helper::ok();
  183. }
  184. public function actionEditRole()
  185. {
  186. $id = Helper::getPostInt('id');
  187. $name = Helper::getPostString('name');
  188. $descr = Helper::getPostString('descr');
  189. if (!$name) {
  190. Helper::error('角色名称不能为空');
  191. }
  192. if ($id) {
  193. DB::updateById('role', ['name' => $name, 'descr' => $descr], $id);
  194. } else {
  195. DB::addData('role', ['name' => $name, 'descr' => $descr]);
  196. }
  197. Helper::ok();
  198. }
  199. }