adminOperateLogger = new AdminOperate([ 'fetchClass' => BaUser::class, ]); } /** * @inheritdoc */ public function rules() { return [ [['userId', 'password', 'passwordType','realName', 'mobile',/*'openBank','bankAddress','bankNo',*/'status'], 'trim'], [['userId'], 'required'], [[/*'idCard', */'allData'], 'required', 'on'=>['addWithUserName']], [['nation','realName', 'mobile', /*'idCard', 'openBank', 'bankAddress', 'bankNo'*/], 'required', 'on'=>'modifyProfile'], [['mobile'], 'mobile'], ]; } /** * 指定校验场景 * @return array */ public function scenarios() { $parentScenarios = parent::scenarios(); $customScenarios = [ 'modifyPassword' => ['userId', 'password', 'passwordType'], 'modifyProfile' => ['userId','realName',/*'idCard',*/'mobile',/*'openBank','bankAddress','bankNo'*/], 'modifyStatus' => ['userId','status'], 'isModifyPasswordStatus' => ['userId','status'], ]; return array_merge($parentScenarios, $customScenarios); } public function attributeLabels() { return [ 'ID' => 'ID', 'password' => '密码', 'passwordType' => '密码类型', // 'nation' => '民族', 'realName' => '真实姓名', // 'idCard' => '身份证号', 'mobile' => '手机号', // 'openBank' => '银行名称', // 'bankAddress' => '开户支行', // 'bankNo' => '银行账号', 'status' => '状态', ]; } public function beforeValidate() { return parent::beforeValidate(); } /** * 编辑用户信息 * @return BaUser|null */ public function edit() { if (!$this->validate()) { return null; } $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { $userModel = BaUser::findOne(['ID'=>$this->userId]); if( $this->passwordType === 'password' ) { $userModel->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password); }else { $userModel->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->password); } if( !$userModel->save(false) ) { throw new Exception($userModel->getErrors()); } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); return null; } return $userModel; } /** * 修改个人资料 * @return BaUser|null */ public function modifyProfile(){ if(!$this->validate()){ return null; } $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { $userModel = BaUser::findOne(['ID' => $this->userId]); $this->adminOperateLogger->beforeUpdate($userModel); // $userModel->NATION = $this->nation; $userModel->REAL_NAME = $this->realName; $userModel->MOBILE = $this->mobile; // $userModel->ID_CARD = $this->idCard; // $userModel->OPEN_BANK = $this->openBank; // $userModel->BANK_NO = $this->bankNo; // $userModel->BANK_ADDRESS = $this->bankAddress; if( !$userModel->save(false) ) { throw new Exception($userModel->getErrors()); } $transaction->commit(); $this->adminOperateLogger->afterUpdate($userModel)->clean()->save([ 'optType' => 'Modification of Member information', // 修改会员资料 'userId' => $this->userId, 'userName' => $userModel->USER_NAME, // 'nation' => $this->nation, 'realName' => $this->realName, 'mobile' => $this->mobile, // 'idCard' => $this->idCard, // 'openBank' => $this->openBank, // 'bankNo' => $this->bankNo, // 'bankAddress' => $this->bankAddress, ]); }catch (Exception $e) { $transaction->rollBack(); return null; } return $userModel; } /** * 修改会员状态 * @return BaUser|null */ public function modifyStatus(){ if(!$this->validate()){ return null; } $this->adminOperateLogger->beforeUpdate($this->userId, 'ID',['select'=>'ID,STATUS']); $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { $userModel = BaUser::findOne(['ID' => $this->userId]); if($userModel->STATUS==$this->status){ $statusName = ($userModel->STATUS == 1) ? 'activation' : 'lock'; // 激活 锁定 throw new Exception('The current member status is【' . $statusName . '】,Do not need to set it again!'); // 当前会员状态已 无需重复设置 } $userModel->STATUS = $this->status; $userModel->STATUS_AT = Date::nowTime(); if( !$userModel->save(false) ) { throw new Exception($userModel->getErrors()); } $transaction->commit(); }catch (Exception $e) { $transaction->rollBack(); $this->addError('modifyStatus', $e->getMessage()); return null; } $this->adminOperateLogger->afterUpdate($this->userId,'ID',['select'=>'ID,STATUS'])->clean()->save([ 'optType' => ($this->status == 1) ? 'Member activation' : 'Member of the lock', // 会员激活 会员锁定 ]); return $userModel; } /** * @return BaUser|null */ public function isModifyPasswordStatus() { if(!$this->validate()){ return null; } $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { $userModel = BaUser::findOne(['ID' => $this->userId]); if($userModel->IS_MODIFY_PASSWORD==$this->status){ throw new Exception('The status has not changed, and do not need to set it again');// 状态没有发生改变,无需重复设置! } $userModel->IS_MODIFY_PASSWORD = $this->status; if( !$userModel->save(false) ) { throw new Exception($userModel->getErrors()); } $transaction->commit(); }catch (Exception $e) { $transaction->rollBack(); $this->addError('isModifyPasswordStatus', $e->getMessage()); return null; } return $userModel; } }