UserBasicForm.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\User;
  7. use yii\base\Exception;
  8. /**
  9. * Login form
  10. */
  11. class UserBasicForm 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' => User::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 null
  77. * @throws \yii\db\Exception
  78. */
  79. public function edit() {
  80. if (!$this->validate()) {
  81. return null;
  82. }
  83. $db = \Yii::$app->db;
  84. $transaction = $db->beginTransaction();
  85. try {
  86. $userModel = User::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 User|null
  105. * @throws \yii\db\Exception
  106. */
  107. public function modifyProfile(){
  108. if(!$this->validate()){
  109. return null;
  110. }
  111. $db = \Yii::$app->db;
  112. $transaction = $db->beginTransaction();
  113. try {
  114. $userModel = User::findOne(['ID' => $this->userId]);
  115. $this->adminOperateLogger->beforeUpdate($userModel);
  116. // $userModel->NATION = $this->nation;
  117. $userModel->REAL_NAME = $this->realName;
  118. $userModel->MOBILE = $this->mobile;
  119. // $userModel->ID_CARD = $this->idCard;
  120. $userModel->OPEN_BANK = $this->openBank;
  121. $userModel->BANK_NO = $this->bankNo;
  122. $userModel->BANK_ADDRESS = $this->bankAddress;
  123. if( !$userModel->save(false) ) {
  124. throw new Exception($userModel->getErrors());
  125. }
  126. $transaction->commit();
  127. $this->adminOperateLogger->afterUpdate($userModel)->clean()->save([
  128. 'optType' => 'Modification of Member information', // 修改会员资料
  129. 'userId' => $this->userId,
  130. 'userName' => $userModel->USER_NAME,
  131. // 'nation' => $this->nation,
  132. 'realName' => $this->realName,
  133. 'mobile' => $this->mobile,
  134. // 'idCard' => $this->idCard,
  135. 'openBank' => $this->openBank,
  136. 'bankNo' => $this->bankNo,
  137. 'bankAddress' => $this->bankAddress,
  138. ]);
  139. }catch (Exception $e) {
  140. $transaction->rollBack();
  141. return null;
  142. }
  143. return $userModel;
  144. }
  145. /**
  146. * 修改会员状态
  147. * @return User|null
  148. * @throws \yii\db\Exception
  149. */
  150. public function modifyStatus(){
  151. if(!$this->validate()){
  152. return null;
  153. }
  154. $this->adminOperateLogger->beforeUpdate($this->userId, 'ID',['select'=>'ID,STATUS']);
  155. $db = \Yii::$app->db;
  156. $transaction = $db->beginTransaction();
  157. try {
  158. $userModel = User::findOne(['ID' => $this->userId]);
  159. if($userModel->STATUS==$this->status){
  160. $statusName = ($userModel->STATUS == 1) ? 'activation' : 'lock'; // 激活 锁定
  161. throw new Exception('The current member status is【' . $statusName . '】,Do not need to set it again!'); // 当前会员状态已 无需重复设置
  162. }
  163. $userModel->STATUS = $this->status;
  164. $userModel->STATUS_AT = Date::nowTime();
  165. if( !$userModel->save(false) ) {
  166. throw new Exception($userModel->getErrors());
  167. }
  168. $transaction->commit();
  169. }catch (Exception $e) {
  170. $transaction->rollBack();
  171. $this->addError('modifyStatus', $e->getMessage());
  172. return null;
  173. }
  174. $this->adminOperateLogger->afterUpdate($this->userId,'ID',['select'=>'ID,STATUS'])->clean()->save([
  175. 'optType' => ($this->status == 1) ? 'Member activation' : 'Member of the lock', // 会员激活 会员锁定
  176. ]);
  177. return $userModel;
  178. }
  179. /**
  180. * @return User|null
  181. * @throws \yii\db\Exception
  182. */
  183. public function isModifyPasswordStatus(){
  184. if(!$this->validate()){
  185. return null;
  186. }
  187. $db = \Yii::$app->db;
  188. $transaction = $db->beginTransaction();
  189. try {
  190. $userModel = User::findOne(['ID' => $this->userId]);
  191. if($userModel->IS_MODIFY_PASSWORD==$this->status){
  192. throw new Exception('The status has not changed, and do not need to set it again');// 状态没有发生改变,无需重复设置!
  193. }
  194. $userModel->IS_MODIFY_PASSWORD = $this->status;
  195. if( !$userModel->save(false) ) {
  196. throw new Exception($userModel->getErrors());
  197. }
  198. $transaction->commit();
  199. }catch (Exception $e) {
  200. $transaction->rollBack();
  201. $this->addError('isModifyPasswordStatus', $e->getMessage());
  202. return null;
  203. }
  204. return $userModel;
  205. }
  206. }