UseradminController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. class UseradminController extends Controller
  3. {
  4. public $layout='//layouts/main';
  5. public $FirstMenu = '系统设置';
  6. public $SecondMenu = '修改密码';
  7. public function filters()
  8. {
  9. return array(
  10. 'accessControl', // perform access control for CRUD operations
  11. 'postOnly + delete', // we only allow deletion via POST request
  12. );
  13. }
  14. public function actionInfo()
  15. {
  16. $model = Useradmin::model()->findByPk(Yii::app()->user->_id);
  17. if (!$model) {
  18. Helper::error('信息未找到');
  19. }
  20. $authIds = DB::getScalerWithCriteria('role', DbCriteria::simpleCompare(['id' => $model->role_id])->setSelect('auth_ids'));
  21. $authIds = $authIds ? explode(',', $authIds) : [];
  22. $authIds = array_map(function ($item) {
  23. return (int)$item;
  24. }, $authIds);
  25. Helper::ok([
  26. 'id' => $model->id,
  27. 'username' => $model->username,
  28. 'auth_ids' => $authIds,
  29. 'buttons' => [],
  30. 'avatar' => Helper::getImageUrl($model->avatar),
  31. 'email' => $model->email,
  32. 'phone' => $model->phone,
  33. 'descr' => $model->descr,
  34. ]);
  35. }
  36. public function actionRoleList()
  37. {
  38. $name = Helper::getPostString('name');
  39. $name = $name ? '%' . $name : null;
  40. $cri = DbCriteria::simpleCompareWithPage(['name' => $name])->setSelect('id, name, auth_ids, descr, create_date, show_ids');
  41. $data = DB::getListWithCriteria('role', $cri);
  42. if (!empty($data['records'])) {
  43. $data['records'] = array_map(function ($item) {
  44. $item['auth_ids'] = $item['auth_ids'] ? explode(',', $item['auth_ids']) : [];
  45. $item['auth_ids'] = array_map(function ($aid) {
  46. return (int)$aid;
  47. }, $item['auth_ids']);
  48. $item['show_ids'] = $item['show_ids'] ? explode(',', $item['show_ids']) : [];
  49. $item['show_ids'] = array_map(function ($aid) {
  50. return (int)$aid;
  51. }, $item['show_ids']);
  52. return $item;
  53. }, $data['records']);
  54. }
  55. Helper::ok($data);
  56. }
  57. /**
  58. * 角色下拉列表获取
  59. * @return void
  60. */
  61. public function actionGetRoleSelect()
  62. {
  63. $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
  64. $data = DB::getListWithCriteria('role', $cri);
  65. Helper::ok($data['records']??[]);
  66. }
  67. public function actionUserList()
  68. {
  69. $name = Helper::getPostString('name');
  70. $name = $name ? '%' . $name : null;
  71. $filters = [
  72. 'username' => $name,
  73. 'u.id' => '!=1',
  74. 'role_id' => Helper::getPostInt('role_id')?:null,
  75. 'phone' => Helper::getPostString('phone')?:null,
  76. ];
  77. $cri = DbCriteria::simpleCompareWithPage($filters)
  78. ->setAlias('u')
  79. ->setDebugUntil('234', '-1')
  80. ->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')
  81. ->setJoin('left join wx_role r on u.role_id = r.id');
  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 actionSaveRoleAuth()
  92. {
  93. $id = Helper::getPostInt('id');
  94. $leaf_ids = Helper::getArrParam($_POST, 'leaf_ids', 'array_int', []);
  95. $half_Leaf_ids = Helper::getArrParam($_POST, 'half_Leaf_ids', 'array_int', []);
  96. if ($id < 0 || !$leaf_ids) {
  97. return Helper::error('参数错误');
  98. }
  99. $info = [
  100. 'auth_ids' => implode(',', Helper::concatArray($half_Leaf_ids, $leaf_ids)),
  101. 'show_ids' => $leaf_ids ? implode(',', $leaf_ids) : '',
  102. ];
  103. DB::updateById('role', $info, $id);
  104. Helper::ok();
  105. }
  106. public function actionEditUser()
  107. {
  108. $id = Helper::getPostInt('id');
  109. $username = Helper::getPostString('username');
  110. $password = Helper::getPostString('password');
  111. $phone = Helper::getPostString('phone');
  112. $sex = Helper::getPostInt('sex');
  113. $role_id = Helper::getPostInt('role_id');
  114. // username不能为空和重复
  115. if (!$username) {
  116. Helper::error('用户名不能为空');
  117. }
  118. $cri = DbCriteria::simpleCompare(['username' => $username])->setSelect('id');
  119. if ($id > 0) {
  120. $cri->addCondition('id!=' . $id);
  121. }
  122. if ($fid = DB::getScalerWithCriteria('useradmin', $cri)) {
  123. Helper::error('用户名已存在 ' . $fid);
  124. }
  125. $info = [
  126. 'username' => $username,
  127. 'phone' => $phone,
  128. 'sex' => $sex,
  129. ];
  130. if (!$id) {
  131. // 新增用户
  132. if (!$password) {
  133. Helper::error('密码不能为空');
  134. }
  135. if (!$role_id) {
  136. Helper::error('请选择角色');
  137. }
  138. $info['password'] = md5($password);
  139. $info['role_id'] = $role_id;
  140. DB::addData('useradmin', $info);
  141. } else {
  142. DB::updateById('useradmin', $info, $id);
  143. }
  144. Helper::ok();
  145. }
  146. public function actionDeleteUser()
  147. {
  148. $id = Helper::getPostInt('id');
  149. if ($id < 1) {
  150. Helper::error('参数错误');
  151. }
  152. DB::deleteById('useradmin', $id);
  153. }
  154. public function actionDeleteRole()
  155. {
  156. $id = Helper::getPostInt('id');
  157. if ($id < 1) {
  158. Helper::error('参数错误');
  159. }
  160. DB::deleteById('role', $id);
  161. }
  162. public function actionCheckpwd(){
  163. $pass = $_POST['pass'];
  164. $new_passwd = trim($_POST['new_passwd']);
  165. $confir_passwd = trim($_POST['confir_passwd']);
  166. if( !$pass ) {
  167. $arr = array('status'=>'failed','code'=>0);
  168. }
  169. if( !$new_passwd ) {
  170. $arr = array('status'=>'failed','code'=>1);
  171. }
  172. if( !$confir_passwd ) {
  173. $arr = array('status'=>'failed','code'=>2);
  174. }
  175. if( $new_passwd != $confir_passwd ) {
  176. $arr = array('status'=>'failed','code'=>4);
  177. $this->response($arr);
  178. }
  179. $id = Yii::app()->user->_id;
  180. $userAdminModel = Useradmin::model()->findByPk($id);
  181. if( md5($pass) != $userAdminModel->password ) {
  182. $arr = array('status'=>'failed','code'=>5);
  183. $this->response($arr);
  184. }
  185. $userAdminModel->password = md5($new_passwd);
  186. $userAdminModel->verifypassword = md5($new_passwd);
  187. if($userAdminModel->save()) {
  188. Yii::app()->user->logout();
  189. $this->response(array('status'=>'success'));
  190. }
  191. }
  192. // protected function response($arr, $dataType = 'json'){
  193. // switch($dataType) {
  194. // case 'json':
  195. // echo json_encode($arr);
  196. // break;
  197. // }
  198. // exit;
  199. // }
  200. public function actionSetting()
  201. {
  202. $this->render('setting');
  203. }
  204. }