UserBasicForm.php 7.4 KB

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