BaUserBasicForm.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\libs\logging\operate\AdminOperate;
  6. use common\models\BaUser;
  7. use yii\base\Exception;
  8. /**
  9. * Login form
  10. */
  11. class BaUserBasicForm extends Model {
  12. public $userId;
  13. public $password;
  14. public $passwordType;
  15. //个人资料
  16. public $nation;
  17. public $realName;
  18. public $idCard;
  19. public $mobile;
  20. public $openBank;
  21. public $bankAddress;
  22. public $bankNo;
  23. public $status;
  24. public function init() {
  25. parent::init();
  26. $this->adminOperateLogger = new AdminOperate([
  27. 'fetchClass' => BaUser::class,
  28. ]);
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules() {
  34. return [
  35. [['userId', 'password', 'passwordType','realName', 'mobile',/*'openBank','bankAddress','bankNo',*/'status'], 'trim'],
  36. [['userId'], 'required'],
  37. [[/*'idCard', */'allData'], 'required', 'on'=>['addWithUserName']],
  38. [['nation','realName', 'mobile', /*'idCard', 'openBank', 'bankAddress', 'bankNo'*/], 'required', 'on'=>'modifyProfile'],
  39. [['mobile'], 'mobile'],
  40. ];
  41. }
  42. /**
  43. * 指定校验场景
  44. * @return array
  45. */
  46. public function scenarios() {
  47. $parentScenarios = parent::scenarios();
  48. $customScenarios = [
  49. 'modifyPassword' => ['userId', 'password', 'passwordType'],
  50. 'modifyProfile' => ['userId','realName',/*'idCard',*/'mobile',/*'openBank','bankAddress','bankNo'*/],
  51. 'modifyStatus' => ['userId','status'],
  52. 'isModifyPasswordStatus' => ['userId','status'],
  53. ];
  54. return array_merge($parentScenarios, $customScenarios);
  55. }
  56. public function attributeLabels() {
  57. return [
  58. 'ID' => 'ID',
  59. 'password' => '密码',
  60. 'passwordType' => '密码类型',
  61. // 'nation' => '民族',
  62. 'realName' => '真实姓名',
  63. // 'idCard' => '身份证号',
  64. 'mobile' => '手机号',
  65. // 'openBank' => '银行名称',
  66. // 'bankAddress' => '开户支行',
  67. // 'bankNo' => '银行账号',
  68. 'status' => '状态',
  69. ];
  70. }
  71. public function beforeValidate() {
  72. return parent::beforeValidate();
  73. }
  74. /**
  75. * 编辑用户信息
  76. * @return BaUser|null
  77. */
  78. public function edit()
  79. {
  80. if (!$this->validate()) {
  81. return null;
  82. }
  83. $db = \Yii::$app->db;
  84. $transaction = $db->beginTransaction();
  85. try {
  86. $userModel = BaUser::findOne(['ID'=>$this->userId]);
  87. if( $this->passwordType === 'password' ) {
  88. $userModel->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  89. }else {
  90. $userModel->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->password);
  91. }
  92. if( !$userModel->save(false) ) {
  93. throw new Exception($userModel->getErrors());
  94. }
  95. $transaction->commit();
  96. } catch (Exception $e) {
  97. $transaction->rollBack();
  98. return null;
  99. }
  100. return $userModel;
  101. }
  102. /**
  103. * 修改个人资料
  104. * @return BaUser|null
  105. */
  106. public function modifyProfile(){
  107. if(!$this->validate()){
  108. return null;
  109. }
  110. $db = \Yii::$app->db;
  111. $transaction = $db->beginTransaction();
  112. try {
  113. $userModel = BaUser::findOne(['ID' => $this->userId]);
  114. $this->adminOperateLogger->beforeUpdate($userModel);
  115. // $userModel->NATION = $this->nation;
  116. $userModel->REAL_NAME = $this->realName;
  117. $userModel->MOBILE = $this->mobile;
  118. // $userModel->ID_CARD = $this->idCard;
  119. // $userModel->OPEN_BANK = $this->openBank;
  120. // $userModel->BANK_NO = $this->bankNo;
  121. // $userModel->BANK_ADDRESS = $this->bankAddress;
  122. if( !$userModel->save(false) ) {
  123. throw new Exception($userModel->getErrors());
  124. }
  125. $transaction->commit();
  126. $this->adminOperateLogger->afterUpdate($userModel)->clean()->save([
  127. 'optType' => 'Modification of Member information', // 修改会员资料
  128. 'userId' => $this->userId,
  129. 'userName' => $userModel->USER_NAME,
  130. // 'nation' => $this->nation,
  131. 'realName' => $this->realName,
  132. 'mobile' => $this->mobile,
  133. // 'idCard' => $this->idCard,
  134. // 'openBank' => $this->openBank,
  135. // 'bankNo' => $this->bankNo,
  136. // 'bankAddress' => $this->bankAddress,
  137. ]);
  138. }catch (Exception $e) {
  139. $transaction->rollBack();
  140. return null;
  141. }
  142. return $userModel;
  143. }
  144. /**
  145. * 修改会员状态
  146. * @return BaUser|null
  147. */
  148. public function modifyStatus(){
  149. if(!$this->validate()){
  150. return null;
  151. }
  152. $this->adminOperateLogger->beforeUpdate($this->userId, 'ID',['select'=>'ID,STATUS']);
  153. $db = \Yii::$app->db;
  154. $transaction = $db->beginTransaction();
  155. try {
  156. $userModel = BaUser::findOne(['ID' => $this->userId]);
  157. if($userModel->STATUS==$this->status){
  158. $statusName = ($userModel->STATUS == 1) ? 'activation' : 'lock'; // 激活 锁定
  159. throw new Exception('The current member status is【' . $statusName . '】,Do not need to set it again!'); // 当前会员状态已 无需重复设置
  160. }
  161. $userModel->STATUS = $this->status;
  162. $userModel->STATUS_AT = Date::nowTime();
  163. if( !$userModel->save(false) ) {
  164. throw new Exception($userModel->getErrors());
  165. }
  166. $transaction->commit();
  167. }catch (Exception $e) {
  168. $transaction->rollBack();
  169. $this->addError('modifyStatus', $e->getMessage());
  170. return null;
  171. }
  172. $this->adminOperateLogger->afterUpdate($this->userId,'ID',['select'=>'ID,STATUS'])->clean()->save([
  173. 'optType' => ($this->status == 1) ? 'Member activation' : 'Member of the lock', // 会员激活 会员锁定
  174. ]);
  175. return $userModel;
  176. }
  177. /**
  178. * @return BaUser|null
  179. */
  180. public function isModifyPasswordStatus()
  181. {
  182. if(!$this->validate()){
  183. return null;
  184. }
  185. $db = \Yii::$app->db;
  186. $transaction = $db->beginTransaction();
  187. try {
  188. $userModel = BaUser::findOne(['ID' => $this->userId]);
  189. if($userModel->IS_MODIFY_PASSWORD==$this->status){
  190. throw new Exception('The status has not changed, and do not need to set it again');// 状态没有发生改变,无需重复设置!
  191. }
  192. $userModel->IS_MODIFY_PASSWORD = $this->status;
  193. if( !$userModel->save(false) ) {
  194. throw new Exception($userModel->getErrors());
  195. }
  196. $transaction->commit();
  197. }catch (Exception $e) {
  198. $transaction->rollBack();
  199. $this->addError('isModifyPasswordStatus', $e->getMessage());
  200. return null;
  201. }
  202. return $userModel;
  203. }
  204. }