UseradminController.php 6.1 KB

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