cxfoot/modules/admin/controllers/auth/RoleUserController.php
2023-10-27 14:25:12 +08:00

131 lines
4.2 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年9月15日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\admin\controllers\auth;
use yii\helpers\VarDumper;
use app\modules\admin\controllers\Controller;
use app\modules\admin\behaviors\LoginBehavior;
use app\models\common\auth\CommonRoleUserActionForm;
use app\models\common\auth\CommonRoleUserEditForm;
use app\models\common\auth\CommonRoleUserListForm;
use app\models\auth\Role;
use app\models\User;
use app\models\auth\RoleUser;
class RoleUserController extends Controller
{
public function behaviors()
{
return array_merge(parent::behaviors(), [
'login' => [
'class' => LoginBehavior::className(),
],
]);
}
public function actionIndex()
{
if(\Yii::$app->request->isAjax){
$form = new CommonRoleUserListForm();
$form->attributes = \Yii::$app->request->get();
$form->cx_mch_id = $this->cx_mch_id;
//本人创建的员工
$form->creator_user_id = \Yii::$app->admin->identity->user_id;
$form->type = User::TYPE_STAFF;//此type查询时作废
$data = $form->search();
return $this->responseHandler($data);
}
return $this->render('index');
}
public function actionEdit($id = 0)
{
$model = User::findOne([
'cx_mch_id' => $this->cx_mch_id,
'is_delete' => 0,
'id' => $id,
'type' => [User::TYPE_STAFF,User::TYPE_BOSS_STAFF,User::TYPE_ADMIN_STAFF]
]);
if($model == null)
$model = new User();
if(\Yii::$app->request->isPost){
$form = new CommonRoleUserEditForm();
if($model->isNewRecord){
$form->scenario = 'add';
}
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->model = $model;
$form->creator_user_id = \Yii::$app->admin->identity->user_id;
$form->type = User::TYPE_STAFF;
$data = $form->save();
return $this->responseHandler($data);
}
$role_list = Role::find()
->where([
'is_delete' => 0,
'cx_mch_id' => $this->cx_mch_id,
'creator_user_id' => \Yii::$app->admin->identity->user_id
])
->select('id,name')->asArray()->all();
$user_id = $model->isNewRecord ? 0 : $model->id;
$user_role_list = RoleUser::find()
->where([
'is_delete' => 0,
'cx_mch_id' => $this->cx_mch_id,
'user_id' => $user_id
])->select('role_id')->column();
foreach($role_list as $index => $item){
$item['checked'] = in_array($item['id'], $user_role_list) ? true : false;
$role_list[$index] = $item;
}
$return_url = \Yii::$app->request->referrer;
return $this->render('edit', [
'model' => $model,
'return_url' => $return_url,
'role_list' => $role_list
]);
}
public function actionResetPwd()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonRoleUserActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = User::TYPE_STAFF;
$data = $form->resetPwd();
return $this->responseHandler($data);
}
public function actionDelete()
{
if(!\Yii::$app->request->isPost){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new CommonRoleUserActionForm();
$form->attributes = \Yii::$app->request->post();
$form->cx_mch_id = $this->cx_mch_id;
$form->type = User::TYPE_STAFF;
$data = $form->delete();
return $this->responseHandler($data);
}
}