UseradminController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. 'userId' => $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. public function actionUserList()
  58. {
  59. $name = Helper::getPostString('name');
  60. $name = $name ? '%' . $name : null;
  61. $cri = DbCriteria::simpleCompareWithPage(['username' => $name])
  62. ->setAlias('u')
  63. ->setDebugUntil('234', '-1')
  64. ->setSelect('u.id, u.username, r.name as role_name, u.is_using, u.phone, u.create_date, u.avatar, u.update_date')
  65. ->setJoin('left join wx_role r on u.role_id = r.id');
  66. $data = DB::getListWithCriteria('useradmin', $cri);
  67. if (!empty($data['records'])) {
  68. $data['records'] = array_map(function ($item) {
  69. $item['avatar'] = Helper::getImageUrl($item['avatar']);
  70. return $item;
  71. }, $data['records']);
  72. }
  73. Helper::ok($data);
  74. }
  75. public function actionSaveRoleAuth()
  76. {
  77. $id = Helper::getPostInt('id');
  78. $leaf_ids = Helper::getArrParam($_POST, 'leaf_ids', 'array_int', []);
  79. $half_Leaf_ids = Helper::getArrParam($_POST, 'half_Leaf_ids', 'array_int', []);
  80. if ($id < 0 || !$leaf_ids) {
  81. return Helper::error('参数错误');
  82. }
  83. $info = [
  84. 'auth_ids' => implode(',', Helper::concatArray($half_Leaf_ids, $leaf_ids)),
  85. 'show_ids' => $leaf_ids ? implode(',', $leaf_ids) : '',
  86. ];
  87. DB::updateById('role', $info, $id);
  88. Helper::ok();
  89. }
  90. public function actionEditUser()
  91. {
  92. $id = Helper::getPostInt('id');
  93. $username = Helper::getPostString('username');
  94. $password = Helper::getPostString('password');
  95. $phone = Helper::getPostString('phone');
  96. $sex = Helper::getPostInt('sex');
  97. $role_id = Helper::getPostInt('role_id');
  98. // username不能为空和重复
  99. if (!$username) {
  100. Helper::error('用户名不能为空');
  101. }
  102. $cri = DbCriteria::simpleCompare(['username' => $username])->setSelect('id');
  103. if ($id > 0) {
  104. $cri->addCondition('id!=' . $id);
  105. }
  106. if ($id = DB::getScalerWithCriteria('useradmin', $cri)) {
  107. Helper::error('用户名已存在 ' . $id);
  108. }
  109. $info = [
  110. 'username' => $username,
  111. 'phone' => $phone,
  112. 'sex' => $sex,
  113. ];
  114. if (!$id) {
  115. // 新增用户
  116. if (!$password) {
  117. Helper::error('密码不能为空');
  118. }
  119. if (!$role_id) {
  120. Helper::error('请选择角色');
  121. }
  122. $info['password'] = md5($password);
  123. $info['roe_id'] = $role_id;
  124. DB::addData('useradmin', $info);
  125. } else {
  126. DB::updateById('useradmin', $info, $id);
  127. }
  128. Helper::ok();
  129. }
  130. public function actionDeleteUser()
  131. {
  132. }
  133. public function actionCheckpwd(){
  134. $pass = $_POST['pass'];
  135. $new_passwd = trim($_POST['new_passwd']);
  136. $confir_passwd = trim($_POST['confir_passwd']);
  137. if( !$pass ) {
  138. $arr = array('status'=>'failed','code'=>0);
  139. }
  140. if( !$new_passwd ) {
  141. $arr = array('status'=>'failed','code'=>1);
  142. }
  143. if( !$confir_passwd ) {
  144. $arr = array('status'=>'failed','code'=>2);
  145. }
  146. if( $new_passwd != $confir_passwd ) {
  147. $arr = array('status'=>'failed','code'=>4);
  148. $this->response($arr);
  149. }
  150. $id = Yii::app()->user->_id;
  151. $userAdminModel = Useradmin::model()->findByPk($id);
  152. if( md5($pass) != $userAdminModel->password ) {
  153. $arr = array('status'=>'failed','code'=>5);
  154. $this->response($arr);
  155. }
  156. $userAdminModel->password = md5($new_passwd);
  157. $userAdminModel->verifypassword = md5($new_passwd);
  158. if($userAdminModel->save()) {
  159. Yii::app()->user->logout();
  160. $this->response(array('status'=>'success'));
  161. }
  162. }
  163. // protected function response($arr, $dataType = 'json'){
  164. // switch($dataType) {
  165. // case 'json':
  166. // echo json_encode($arr);
  167. // break;
  168. // }
  169. // exit;
  170. // }
  171. public function actionSetting()
  172. {
  173. $this->render('setting');
  174. }
  175. }