UseradminController.php 6.4 KB

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