| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace common\models\forms;
- use common\components\Model;
- use common\helpers\Date;
- use common\libs\logging\operate\AdminOperate;
- use common\models\BaUser;
- use yii\base\Exception;
- /**
- * Login form
- */
- class BaUserBasicForm extends Model {
- public $userId;
- public $password;
- public $passwordType;
- //个人资料
- public $nation;
- public $realName;
- public $idCard;
- public $mobile;
- public $openBank;
- public $bankAddress;
- public $bankNo;
- public $status;
- public function init() {
- parent::init();
- $this->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;
- }
- }
|