UseradminController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /**
  56. * 角色下拉列表获取
  57. * @return void
  58. */
  59. public function actionGetRoleSelect()
  60. {
  61. $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
  62. $data = DB::getListWithCriteria('role', $cri);
  63. Helper::ok($data['records']??[]);
  64. }
  65. public function actionUserList()
  66. {
  67. $name = Helper::getPostString('name');
  68. $name = $name ? '%' . $name : null;
  69. $filters = [
  70. 'username' => $name,
  71. 'u.id' => '!=1',
  72. 'u.status' => '1',
  73. 'role_id' => Helper::getPostInt('role_id')?:null,
  74. 'phone' => Helper::getPostString('phone')?:null,
  75. ];
  76. $cri = DbCriteria::simpleCompareWithPage($filters)
  77. ->setAlias('u')
  78. ->setDebugUntil('234', '-1')
  79. ->setSelect('u.id, u.username, r.name as role_name, u.status, u.sex, u.phone, u.create_date, u.avatar, u.update_date')
  80. ->setJoin('left join wx_role r on u.role_id = r.id')
  81. ->setOrder('id desc');
  82. $data = DB::getListWithCriteria('useradmin', $cri);
  83. if (!empty($data['records'])) {
  84. $data['records'] = array_map(function ($item) {
  85. $item['avatar'] = Helper::getImageUrl($item['avatar']);
  86. return $item;
  87. }, $data['records']);
  88. }
  89. Helper::ok($data);
  90. }
  91. public function actionGetSelectList()
  92. {
  93. $data = DB::getListWithCriteria('useradmin', DbCriteria::simpleCompare(['id' => '!=1', 'status' => 1])->setSelect('id, username as name'));
  94. Helper::ok($data['records']);
  95. }
  96. public function actionSaveRoleAuth()
  97. {
  98. $id = Helper::getPostInt('id');
  99. $leaf_ids = Helper::getArrParam($_POST, 'leaf_ids', 'array_int', []);
  100. $half_Leaf_ids = Helper::getArrParam($_POST, 'half_Leaf_ids', 'array_int', []);
  101. if ($id < 0 || !$leaf_ids) {
  102. return Helper::error('参数错误');
  103. }
  104. $info = [
  105. 'auth_ids' => implode(',', Helper::concatArray($half_Leaf_ids, $leaf_ids)),
  106. 'show_ids' => $leaf_ids ? implode(',', $leaf_ids) : '',
  107. ];
  108. DB::updateById('role', $info, $id);
  109. Helper::ok();
  110. }
  111. public function actionEditUser()
  112. {
  113. $id = Helper::getPostInt('id');
  114. $username = Helper::getPostString('username');
  115. $password = Helper::getPostString('password');
  116. $phone = Helper::getPostString('phone');
  117. $sex = Helper::getPostInt('sex');
  118. $role_id = Helper::getPostInt('role_id');
  119. // username不能为空和重复
  120. if (!$username) {
  121. Helper::error('用户名不能为空');
  122. }
  123. $cri = DbCriteria::simpleCompare(['username' => $username])->setSelect('id');
  124. if ($id > 0) {
  125. $cri->addCondition('id!=' . $id);
  126. }
  127. if ($fid = DB::getScalerWithCriteria('useradmin', $cri)) {
  128. Helper::error('用户名已存在 ' . $fid);
  129. }
  130. $info = [
  131. 'username' => $username,
  132. 'phone' => $phone,
  133. 'sex' => $sex,
  134. 'role_id' => $role_id,
  135. ];
  136. if (!$id) {
  137. // 新增用户
  138. if (!$password) {
  139. Helper::error('密码不能为空');
  140. }
  141. if (!$role_id) {
  142. Helper::error('请选择角色');
  143. }
  144. $info['password'] = md5($password);
  145. DB::addData('useradmin', $info);
  146. } else {
  147. DB::updateById('useradmin', $info, $id);
  148. }
  149. Helper::ok();
  150. }
  151. public function actionDeleteUser()
  152. {
  153. $id = Helper::getPostInt('id');
  154. if ($id < 1) {
  155. Helper::error('参数错误');
  156. }
  157. DB::updateById('useradmin', ['status' => 0], $id);
  158. Helper::ok();
  159. }
  160. public function actionDeleteRole()
  161. {
  162. $id = Helper::getPostInt('id');
  163. if ($id < 1) {
  164. Helper::error('参数错误');
  165. }
  166. if (DB::getScalerWithCriteria('useradmin', DbCriteria::simpleCompare(['role_id' => $id]))->setselect('id')) {
  167. Helper::error('该角色下有用户,请先删除用户');
  168. }
  169. DB::deleteById('role', $id);
  170. Helper::ok();
  171. }
  172. public function actionEditRole()
  173. {
  174. $id = Helper::getPostInt('id');
  175. $name = Helper::getPostString('name');
  176. $descr = Helper::getPostString('descr');
  177. if (!$name) {
  178. Helper::error('角色名称不能为空');
  179. }
  180. if ($id) {
  181. DB::updateById('role', ['name' => $name, 'descr' => $descr], $id);
  182. } else {
  183. DB::addData('role', ['name' => $name, 'descr' => $descr]);
  184. }
  185. Helper::ok();
  186. }
  187. }