UseradminController.php 7.1 KB

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