UseradminController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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($this->getUserId());
  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. Logger::errorMult('auth_change', $users);
  114. foreach ($users['records'] as $user) {
  115. $this->clearAuth($user['id']);
  116. }
  117. Helper::ok();
  118. }
  119. public function actionEditUser()
  120. {
  121. $id = Helper::getPostInt('id');
  122. $username = Helper::getPostString('username');
  123. $password = Helper::getPostString('password');
  124. $phone = Helper::getPostString('phone');
  125. $sex = Helper::getPostInt('sex');
  126. $role_id = Helper::getPostInt('role_id');
  127. $company_ids = Helper::getArrParam($_POST, 'company_ids', 'array_int', []);
  128. $school_ids = Helper::getArrParam($_POST, 'school_ids', 'array_int', []);
  129. // username不能为空和重复
  130. if (!$username) {
  131. Helper::error('用户名不能为空');
  132. }
  133. $cri = DbCriteria::simpleCompare(['username' => $username])->setSelect('id');
  134. if ($id > 0) {
  135. $cri->addCondition('id!=' . $id);
  136. }
  137. if ($fid = DB::getScalerWithCriteria('useradmin', $cri)) {
  138. Helper::error('用户名已存在 ' . $fid);
  139. }
  140. $this->dobuleCheck(1);
  141. $info = [
  142. 'username' => $username,
  143. 'phone' => $phone,
  144. 'sex' => $sex,
  145. 'role_id' => $role_id,
  146. 'company_ids' => $company_ids ? implode(',', $company_ids) : '',
  147. 'school_ids' => $school_ids ? implode(',', $school_ids) : '',
  148. ];
  149. if (!$id) {
  150. // 新增用户
  151. if (!$password) {
  152. Helper::error('密码不能为空');
  153. }
  154. if (!$role_id) {
  155. Helper::error('请选择角色');
  156. }
  157. $info['password'] = md5($password);
  158. DB::addData('useradmin', $info);
  159. } else {
  160. $this->clearAuth($id);
  161. DB::updateById('useradmin', $info, $id);
  162. }
  163. Helper::ok();
  164. }
  165. public function actionDeleteUser()
  166. {
  167. $id = Helper::getPostInt('id');
  168. if ($id < 1) {
  169. Helper::error('参数错误');
  170. }
  171. DB::updateById('useradmin', ['status' => 0], $id);
  172. Helper::ok();
  173. }
  174. public function actionDeleteRole()
  175. {
  176. $id = Helper::getPostInt('id');
  177. if ($id < 1) {
  178. Helper::error('参数错误');
  179. }
  180. if (DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['role_id' => $id, 'status' => 1])->setselect('id'))) {
  181. Helper::error('该角色下有用户,请先删除用户');
  182. }
  183. DB::deleteById('role', $id);
  184. Helper::ok();
  185. }
  186. public function actionEditRole()
  187. {
  188. $id = Helper::getPostInt('id');
  189. $name = Helper::getPostString('name');
  190. $descr = Helper::getPostString('descr');
  191. if (!$name) {
  192. Helper::error('角色名称不能为空');
  193. }
  194. $this->dobuleCheck();
  195. if ($id) {
  196. DB::updateById('role', ['name' => $name, 'descr' => $descr], $id);
  197. } else {
  198. DB::addData('role', ['name' => $name, 'descr' => $descr]);
  199. }
  200. Helper::ok();
  201. }
  202. }