UseradminController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. 'role_id' => Helper::getPostInt('role_id')?:null,
  71. 'phone' => Helper::getPostString('phone')?:null,
  72. ];
  73. $cri = DbCriteria::simpleCompareWithPage($filters)
  74. ->setAlias('u')
  75. ->setDebugUntil('234', '-1')
  76. ->setSelect('u.id, u.username, r.name as role_name, u.is_using, u.sex, u.phone, u.create_date, u.avatar, u.update_date')
  77. ->setJoin('left join wx_role r on u.role_id = r.id');
  78. $data = DB::getListWithCriteria('useradmin', $cri);
  79. if (!empty($data['records'])) {
  80. $data['records'] = array_map(function ($item) {
  81. $item['avatar'] = Helper::getImageUrl($item['avatar']);
  82. return $item;
  83. }, $data['records']);
  84. }
  85. Helper::ok($data);
  86. }
  87. public function actionSaveRoleAuth()
  88. {
  89. $id = Helper::getPostInt('id');
  90. $leaf_ids = Helper::getArrParam($_POST, 'leaf_ids', 'array_int', []);
  91. $half_Leaf_ids = Helper::getArrParam($_POST, 'half_Leaf_ids', 'array_int', []);
  92. if ($id < 0 || !$leaf_ids) {
  93. return Helper::error('参数错误');
  94. }
  95. $info = [
  96. 'auth_ids' => implode(',', Helper::concatArray($half_Leaf_ids, $leaf_ids)),
  97. 'show_ids' => $leaf_ids ? implode(',', $leaf_ids) : '',
  98. ];
  99. DB::updateById('role', $info, $id);
  100. Helper::ok();
  101. }
  102. public function actionEditUser()
  103. {
  104. $id = Helper::getPostInt('id');
  105. $username = Helper::getPostString('username');
  106. $password = Helper::getPostString('password');
  107. $phone = Helper::getPostString('phone');
  108. $sex = Helper::getPostInt('sex');
  109. $role_id = Helper::getPostInt('role_id');
  110. // username不能为空和重复
  111. if (!$username) {
  112. Helper::error('用户名不能为空');
  113. }
  114. $cri = DbCriteria::simpleCompare(['username' => $username])->setSelect('id');
  115. if ($id > 0) {
  116. $cri->addCondition('id!=' . $id);
  117. }
  118. if ($fid = DB::getScalerWithCriteria('useradmin', $cri)) {
  119. Helper::error('用户名已存在 ' . $fid);
  120. }
  121. $info = [
  122. 'username' => $username,
  123. 'phone' => $phone,
  124. 'sex' => $sex,
  125. ];
  126. if (!$id) {
  127. // 新增用户
  128. if (!$password) {
  129. Helper::error('密码不能为空');
  130. }
  131. if (!$role_id) {
  132. Helper::error('请选择角色');
  133. }
  134. $info['password'] = md5($password);
  135. $info['role_id'] = $role_id;
  136. DB::addData('useradmin', $info);
  137. } else {
  138. DB::updateById('useradmin', $info, $id);
  139. }
  140. Helper::ok();
  141. }
  142. public function actionDeleteUser()
  143. {
  144. $id = Helper::getPostInt('id');
  145. if ($id < 1) {
  146. Helper::error('参数错误');
  147. }
  148. DB::deleteById('useradmin', $id);
  149. Helper::ok();
  150. }
  151. public function actionDeleteRole()
  152. {
  153. $id = Helper::getPostInt('id');
  154. if ($id < 1) {
  155. Helper::error('参数错误');
  156. }
  157. DB::deleteById('role', $id);
  158. Helper::ok();
  159. }
  160. public function actionEditRole()
  161. {
  162. $id = Helper::getPostInt('id');
  163. $name = Helper::getPostString('name');
  164. $descr = Helper::getPostString('descr');
  165. if (!$name) {
  166. Helper::error('角色名称不能为空');
  167. }
  168. if ($id) {
  169. DB::updateById('role', ['name' => $name, 'descr' => $descr], $id);
  170. } else {
  171. DB::addData('role', ['name' => $name, 'descr' => $descr]);
  172. }
  173. Helper::ok();
  174. }
  175. public function actionCheckpwd(){
  176. $pass = $_POST['pass'];
  177. $new_passwd = trim($_POST['new_passwd']);
  178. $confir_passwd = trim($_POST['confir_passwd']);
  179. if( !$pass ) {
  180. $arr = array('status'=>'failed','code'=>0);
  181. }
  182. if( !$new_passwd ) {
  183. $arr = array('status'=>'failed','code'=>1);
  184. }
  185. if( !$confir_passwd ) {
  186. $arr = array('status'=>'failed','code'=>2);
  187. }
  188. if( $new_passwd != $confir_passwd ) {
  189. $arr = array('status'=>'failed','code'=>4);
  190. $this->response($arr);
  191. }
  192. $id = Yii::app()->user->_id;
  193. $userAdminModel = Useradmin::model()->findByPk($id);
  194. if( md5($pass) != $userAdminModel->password ) {
  195. $arr = array('status'=>'failed','code'=>5);
  196. $this->response($arr);
  197. }
  198. $userAdminModel->password = md5($new_passwd);
  199. $userAdminModel->verifypassword = md5($new_passwd);
  200. if($userAdminModel->save()) {
  201. Yii::app()->user->logout();
  202. $this->response(array('status'=>'success'));
  203. }
  204. }
  205. }