初始化仓库
This commit is contained in:
147
admin/controllers/system/SubAccountController.php
Normal file
147
admin/controllers/system/SubAccountController.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace admin\controllers\system;
|
||||
|
||||
use admin\models\Admin;
|
||||
use admin\models\Menu;
|
||||
use common\helpers\ArrayHelper;
|
||||
use common\models\SubAccountMenu;
|
||||
use common\modelsgii\RoleMenu;
|
||||
use common\validators\DateRangeValidator;
|
||||
use yii\base\DynamicModel;
|
||||
use yii\base\Exception;
|
||||
use yii\data\ActiveDataProvider;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use common\core\BaseAdminController;
|
||||
|
||||
class SubAccountController extends BaseAdminController
|
||||
{
|
||||
public $modelClass = Admin::class;
|
||||
|
||||
public function actionList()
|
||||
{
|
||||
$m = new DynamicModel(['page','pageSize']);
|
||||
//$m->addRule('aa',DateRangeValidator::class);
|
||||
//$m->addRule('xx','string');
|
||||
$m->addRule('page','default',['value'=>1]);
|
||||
$m->addRule('pageSize','default',['value'=>10]);
|
||||
$m->addRule('page','integer',['max'=>1000]);
|
||||
$m->addRule('pageSize','integer',['min'=>1,'max'=>50]);
|
||||
$m->load($this->post());
|
||||
if(!$m->validate()){
|
||||
throw new Exception(json_encode($m->getFirstErrors()));
|
||||
}
|
||||
$query = Admin::find()->where(['is_sub'=>1])->andWhere([
|
||||
'role'=>\Yii::$app->user->identity->role,
|
||||
'mall_id'=>\Yii::$app->user->identity->mall_id,
|
||||
'is_delete'=>0
|
||||
]);
|
||||
|
||||
$this->field = [
|
||||
Admin::class => [
|
||||
'uid','username','mobile','last_login_time','last_login_ip'=>function($m){
|
||||
return $m->last_login_ip ? long2ip($m->last_login_ip) : '';
|
||||
}
|
||||
]
|
||||
];
|
||||
|
||||
$dataProvider = new ActiveDataProvider([
|
||||
'query' => $query,
|
||||
'sort'=>[
|
||||
'defaultOrder'=>['uid'=>SORT_DESC]
|
||||
],
|
||||
'pagination'=>[
|
||||
'defaultPageSize'=>$m->pageSize,
|
||||
'params'=>['page'=>$m->page]
|
||||
]
|
||||
]);
|
||||
// add conditions that should always apply here
|
||||
|
||||
return $dataProvider;
|
||||
}
|
||||
|
||||
public function actionSave(){
|
||||
$id = (int)$this->get('id');
|
||||
$model = $this->findModel($id);
|
||||
$data = $this->post();
|
||||
$data['password'] = \Yii::$app->security->generatePasswordHash($data['password']);
|
||||
$data['role'] = \Yii::$app->user->identity->role;
|
||||
$data['mall_id'] = \Yii::$app->user->identity->mall_id;
|
||||
$data['is_sub'] = 1;
|
||||
$data['last_login_ip'] = isset($data['last_login_ip']) && $data['last_login_ip'] ? ip2long($data['last_login_ip']) : 0;
|
||||
if($model->load($data,'') && $model->save()){
|
||||
return [];
|
||||
}else{
|
||||
throw new Exception("保存失败:".current($model->getFirstErrors()));
|
||||
}
|
||||
}
|
||||
|
||||
public function actionMenuByUid(){
|
||||
$params = $this->requestValidate($this->post(),[
|
||||
['admin_id','required'],
|
||||
]);
|
||||
$roleMenus = RoleMenu::findAll(['role_id' => \Yii::$app->user->identity->role]);
|
||||
$roleKeys = ArrayHelper::getColumn($roleMenus,'menuUrl');
|
||||
$menus = Menu::find()->where(['in','menuUrl',$roleKeys])->asArray()->all();
|
||||
$userMenus = SubAccountMenu::findAll(['admin_id'=>$params->admin_id]);
|
||||
$keys = ArrayHelper::getColumn($userMenus,'menuUrl');
|
||||
return[
|
||||
'menus'=>ArrayHelper::itemsMerge($menus,0,'menuUrl','parentPath','children'),
|
||||
'checked'=>$keys
|
||||
];
|
||||
}
|
||||
|
||||
public function actionDel(){
|
||||
$params = $this->requestValidate($this->post(),[
|
||||
['admin_id','required']
|
||||
]);
|
||||
$model = $this->findModel($params->admin_id);
|
||||
if(!$model || $model->is_sub!==1){
|
||||
throw new Exception("账号不存在");
|
||||
}
|
||||
$model->delete();
|
||||
// $model->updateAttributes(['is_delete'=>1]);
|
||||
return [];
|
||||
}
|
||||
public function actionSaveMenu(){
|
||||
$params = $this->requestValidate($this->post(),[
|
||||
['admin_id','required'],
|
||||
['admin_id','integer'],
|
||||
['ids','required']
|
||||
]);
|
||||
if(is_array($params->ids)){
|
||||
$insert = [];
|
||||
foreach ($params->ids as $id){
|
||||
$insert[] = [
|
||||
'admin_id'=>$params->admin_id,
|
||||
'menuUrl'=>$id,
|
||||
'created_at'=>time(),
|
||||
'updated_at'=>time()
|
||||
];
|
||||
}
|
||||
SubAccountMenu::deleteAll(['admin_id'=>$params->admin_id]);
|
||||
SubAccountMenu::getDb()->createCommand()->batchInsert(SubAccountMenu::tableName(),array_keys($insert[0]),$insert)->execute();
|
||||
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return ActiveRecord
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
protected function findModel($id)
|
||||
{
|
||||
if ($id == 0) {
|
||||
return new $this->modelClass();
|
||||
}
|
||||
if (($model = $this->modelClass::findOne($id)) !== null) {
|
||||
return $model;
|
||||
} else {
|
||||
throw new NotFoundHttpException('The requested page does not exist.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user