UserBasicForm.php 7.3 KB

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