| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- class UseradminController extends Controller
- {
- public function filters()
- {
- return array(
- 'accessControl', // perform access control for CRUD operations
- );
- }
- public function actionInfo()
- {
- $model = Useradmin::model()->findByPk(Yii::app()->user->_id);
- if (!$model) {
- Helper::error('信息未找到');
- }
- $authIds = DB::getScalerWithCriteria('role', DbCriteria::simpleCompare(['id' => $model->role_id])->setSelect('auth_ids'));
- $authIds = $authIds ? explode(',', $authIds) : [];
- $authIds = array_map(function ($item) {
- return (int)$item;
- }, $authIds);
- Helper::ok([
- 'id' => $model->id,
- 'username' => $model->username,
- 'auth_ids' => $authIds,
- 'buttons' => [],
- 'avatar' => Helper::getImageUrl($model->avatar),
- 'email' => $model->email,
- 'phone' => $model->phone,
- 'descr' => $model->descr,
- ]);
- }
- public function actionRoleList()
- {
- $name = Helper::getPostString('name');
- $name = $name ? '%' . $name : null;
- $cri = DbCriteria::simpleCompareWithPage(['name' => $name])->setSelect('id, name, auth_ids, descr, create_date, show_ids');
- $data = DB::getListWithCriteria('role', $cri);
- if (!empty($data['records'])) {
- $data['records'] = array_map(function ($item) {
- $item['auth_ids'] = $item['auth_ids'] ? explode(',', $item['auth_ids']) : [];
- $item['auth_ids'] = array_map(function ($aid) {
- return (int)$aid;
- }, $item['auth_ids']);
- $item['show_ids'] = $item['show_ids'] ? explode(',', $item['show_ids']) : [];
- $item['show_ids'] = array_map(function ($aid) {
- return (int)$aid;
- }, $item['show_ids']);
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- /**
- * 角色下拉列表获取
- * @return void
- */
- public function actionGetRoleSelect()
- {
- $cri = DbCriteria::simpleCompare([])->setSelect('id, name');
- $data = DB::getListWithCriteria('role', $cri);
- Helper::ok($data['records']??[]);
- }
- public function actionUserList()
- {
- $name = Helper::getPostString('name');
- $name = $name ? '%' . $name : null;
- $filters = [
- 'username' => $name,
- 'u.id' => '!=1',
- 'u.status' => '1',
- 'role_id' => Helper::getPostInt('role_id')?:null,
- 'phone' => Helper::getPostString('phone')?:null,
- ];
- $cri = DbCriteria::simpleCompareWithPage($filters)
- ->setAlias('u')
- ->setDebugUntil('234', '-1')
- ->setSelect('u.id, u.username, r.name as role_name, u.status, u.sex, u.phone, u.create_date, u.avatar, u.update_date')
- ->setJoin('left join wx_role r on u.role_id = r.id');
- $data = DB::getListWithCriteria('useradmin', $cri);
- if (!empty($data['records'])) {
- $data['records'] = array_map(function ($item) {
- $item['avatar'] = Helper::getImageUrl($item['avatar']);
- return $item;
- }, $data['records']);
- }
- Helper::ok($data);
- }
- public function actionSaveRoleAuth()
- {
- $id = Helper::getPostInt('id');
- $leaf_ids = Helper::getArrParam($_POST, 'leaf_ids', 'array_int', []);
- $half_Leaf_ids = Helper::getArrParam($_POST, 'half_Leaf_ids', 'array_int', []);
- if ($id < 0 || !$leaf_ids) {
- return Helper::error('参数错误');
- }
- $info = [
- 'auth_ids' => implode(',', Helper::concatArray($half_Leaf_ids, $leaf_ids)),
- 'show_ids' => $leaf_ids ? implode(',', $leaf_ids) : '',
- ];
- DB::updateById('role', $info, $id);
- Helper::ok();
- }
- public function actionEditUser()
- {
- $id = Helper::getPostInt('id');
- $username = Helper::getPostString('username');
- $password = Helper::getPostString('password');
- $phone = Helper::getPostString('phone');
- $sex = Helper::getPostInt('sex');
- $role_id = Helper::getPostInt('role_id');
- // username不能为空和重复
- if (!$username) {
- Helper::error('用户名不能为空');
- }
- $cri = DbCriteria::simpleCompare(['username' => $username])->setSelect('id');
- if ($id > 0) {
- $cri->addCondition('id!=' . $id);
- }
- if ($fid = DB::getScalerWithCriteria('useradmin', $cri)) {
- Helper::error('用户名已存在 ' . $fid);
- }
- $info = [
- 'username' => $username,
- 'phone' => $phone,
- 'sex' => $sex,
- ];
- if (!$id) {
- // 新增用户
- if (!$password) {
- Helper::error('密码不能为空');
- }
- if (!$role_id) {
- Helper::error('请选择角色');
- }
- $info['password'] = md5($password);
- $info['role_id'] = $role_id;
- DB::addData('useradmin', $info);
- } else {
- DB::updateById('useradmin', $info, $id);
- }
- Helper::ok();
- }
- public function actionDeleteUser()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- DB::updateById('useradmin', ['status' => 0], $id);
- Helper::ok();
- }
- public function actionDeleteRole()
- {
- $id = Helper::getPostInt('id');
- if ($id < 1) {
- Helper::error('参数错误');
- }
- DB::deleteById('role', $id);
- Helper::ok();
- }
- public function actionEditRole()
- {
- $id = Helper::getPostInt('id');
- $name = Helper::getPostString('name');
- $descr = Helper::getPostString('descr');
- if (!$name) {
- Helper::error('角色名称不能为空');
- }
- if ($id) {
- DB::updateById('role', ['name' => $name, 'descr' => $descr], $id);
- } else {
- DB::addData('role', ['name' => $name, 'descr' => $descr]);
- }
- Helper::ok();
- }
- }
|