UserBasicForm.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\Tool;
  8. use common\libs\logging\operate\AdminOperate;
  9. use common\models\CurrencyConversions;
  10. use common\models\Instalment;
  11. use common\models\Period;
  12. use common\models\User;
  13. use common\models\UserImmigrant;
  14. use common\models\UserWallet;
  15. use Yii;
  16. use yii\base\Exception;
  17. /**
  18. * Login form
  19. */
  20. class UserBasicForm extends Model {
  21. public $userId;
  22. public $password;
  23. public $passwordType;
  24. //个人资料
  25. public $nation;
  26. public $realName;
  27. public $idCard;
  28. public $mobile;
  29. public $openBank;
  30. public $bankAddress;
  31. public $bankNo;
  32. public $country;
  33. public $language;
  34. public $status;
  35. public function init() {
  36. parent::init();
  37. $this->adminOperateLogger = new AdminOperate([
  38. 'fetchClass' => User::class,
  39. ]);
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. public function rules() {
  45. return [
  46. [['userId', 'password', 'passwordType','realName', 'mobile','openBank','bankAddress','bankNo','status'], 'trim'],
  47. [['userId'], 'required'],
  48. [[/*'idCard', */'allData'], 'required', 'on'=>['addWithUserName']],
  49. [['nation','realName', 'mobile', /*'idCard', */'openBank', 'bankAddress', 'bankNo', 'country', 'language'], 'required', 'on'=>'modifyProfile'],
  50. [['mobile'], 'mobile'],
  51. ];
  52. }
  53. /**
  54. * 指定校验场景
  55. * @return array
  56. */
  57. public function scenarios() {
  58. $parentScenarios = parent::scenarios();
  59. $customScenarios = [
  60. 'modifyPassword' => ['userId', 'password', 'passwordType'],
  61. 'modifyProfile' => ['userId','realName',/*'idCard',*/'mobile','openBank','bankAddress','bankNo', 'country', 'language'],
  62. 'modifyStatus' => ['userId','status'],
  63. 'isModifyPasswordStatus' => ['userId','status'],
  64. ];
  65. return array_merge($parentScenarios, $customScenarios);
  66. }
  67. public function attributeLabels() {
  68. return [
  69. 'ID' => 'ID',
  70. 'password' => '密码',
  71. 'passwordType' => '密码类型',
  72. // 'nation' => '民族',
  73. 'realName' => '真实姓名',
  74. // 'idCard' => '身份证号',
  75. 'mobile' => '手机号',
  76. 'openBank' => '银行名称',
  77. 'bankAddress' => '开户支行',
  78. 'bankNo' => '银行账号',
  79. 'status' => '状态',
  80. 'country' => '国家',
  81. 'language' => '语言',
  82. ];
  83. }
  84. public function beforeValidate() {
  85. return parent::beforeValidate();
  86. }
  87. /**
  88. * 编辑用户信息
  89. * @return null
  90. * @throws \yii\db\Exception
  91. */
  92. public function edit() {
  93. if (!$this->validate()) {
  94. return null;
  95. }
  96. $db = \Yii::$app->db;
  97. $transaction = $db->beginTransaction();
  98. try {
  99. $userModel = User::findOne(['ID'=>$this->userId]);
  100. if( $this->passwordType === 'password' ) {
  101. $userModel->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  102. }else {
  103. $userModel->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->password);
  104. }
  105. if( !$userModel->save(false) ) {
  106. throw new Exception($userModel->getErrors());
  107. }
  108. $transaction->commit();
  109. } catch (Exception $e) {
  110. $transaction->rollBack();
  111. return null;
  112. }
  113. return $userModel;
  114. }
  115. /**
  116. * 修改个人资料
  117. * @return User|null
  118. * @throws \yii\db\Exception
  119. * @throws Exception
  120. */
  121. public function modifyProfile(){
  122. if(!$this->validate()){
  123. return null;
  124. }
  125. // 会员信息
  126. $userModel = User::findOne(['ID' => $this->userId]);
  127. // 原国家
  128. $beforeCountry = $userModel->COUNTRY_ID;
  129. // 移民前汇率
  130. $beforeCurrency = CurrencyConversions::getToUSDRate($beforeCountry);
  131. // 移民后汇率
  132. $afterCurrency = CurrencyConversions::getToUSDRate($this->country);
  133. if (!$afterCurrency) {
  134. throw new Exception(Yii::t('app', 'currencyDoesNotExist'));
  135. }
  136. // 如果移民,则需要进行移民条件检查
  137. if ($this->country != $beforeCountry) {
  138. // 1.是否有进行中的分期订单
  139. $instalmentOrder = Instalment::findOne(['USER_ID' => $this->userId]);
  140. // 订单分期总期数配置
  141. if ($instalmentOrder) {
  142. // 分期的总期数
  143. $instalment = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  144. // 分期商品的期数不能大于总分期数限制
  145. if (intval($instalmentOrder['STAGE']) < $instalment) {
  146. throw new Exception(Yii::t('app', 'instalmentOrderInProcess'));
  147. }
  148. }
  149. }
  150. $db = \Yii::$app->db;
  151. $transaction = $db->beginTransaction();
  152. try {
  153. $this->adminOperateLogger->beforeUpdate($userModel);
  154. // $userModel->NATION = $this->nation;
  155. $userModel->REAL_NAME = $this->realName;
  156. $userModel->MOBILE = $this->mobile;
  157. // $userModel->ID_CARD = $this->idCard;
  158. $userModel->OPEN_BANK = $this->openBank;
  159. $userModel->BANK_NO = $this->bankNo;
  160. $userModel->BANK_ADDRESS = $this->bankAddress;
  161. $userModel->LANGUAGE_ID = $this->language;
  162. $userModel->COUNTRY_ID = $this->country;
  163. if( !$userModel->save(false) ) {
  164. throw new Exception($userModel->getErrors());
  165. }
  166. // 现金钱包余额转换
  167. $userWallet = UserWallet::findOne(['USER_ID' => $this->userId]);
  168. if ($userWallet && $userWallet->CASH > 0) {
  169. $userWallet->CASH = Tool::convertAmount($userWallet->CASH, $beforeCurrency, $afterCurrency);
  170. if (!$userWallet->save()) {
  171. $transaction->rollBack();
  172. throw new Exception($userModel->getErrors());
  173. }
  174. }
  175. // 移民记录
  176. $model = new UserImmigrant();
  177. $model->user_id = $this->userId;
  178. $model->before_country_id = $beforeCountry;
  179. $model->after_country_id = $this->country;
  180. $model->period_num = Period::instance()->getNowPeriodNum();
  181. $model->created_by = \Yii::$app->user->id;
  182. if (!$model->save()) {
  183. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  184. }
  185. $transaction->commit();
  186. $this->adminOperateLogger->afterUpdate($userModel)->clean()->save([
  187. 'optType' => 'Modification of Member information', // 修改会员资料
  188. 'userId' => $this->userId,
  189. 'userName' => $userModel->USER_NAME,
  190. // 'nation' => $this->nation,
  191. 'realName' => $this->realName,
  192. 'mobile' => $this->mobile,
  193. // 'idCard' => $this->idCard,
  194. 'openBank' => $this->openBank,
  195. 'bankNo' => $this->bankNo,
  196. 'bankAddress' => $this->bankAddress,
  197. 'language' => $this->language,
  198. 'country' => $this->country,
  199. ]);
  200. }catch (Exception $e) {
  201. $transaction->rollBack();
  202. return null;
  203. }
  204. return $userModel;
  205. }
  206. /**
  207. * 修改会员状态
  208. * @return User|null
  209. * @throws \yii\db\Exception
  210. */
  211. public function modifyStatus(){
  212. if(!$this->validate()){
  213. return null;
  214. }
  215. $this->adminOperateLogger->beforeUpdate($this->userId, 'ID',['select'=>'ID,STATUS']);
  216. $db = \Yii::$app->db;
  217. $transaction = $db->beginTransaction();
  218. try {
  219. $userModel = User::findOne(['ID' => $this->userId]);
  220. if($userModel->STATUS==$this->status){
  221. $statusName = ($userModel->STATUS == 1) ? 'activation' : 'lock'; // 激活 锁定
  222. throw new Exception('The current member status is【' . $statusName . '】,Do not need to set it again!'); // 当前会员状态已 无需重复设置
  223. }
  224. $userModel->STATUS = $this->status;
  225. $userModel->STATUS_AT = Date::nowTime();
  226. if( !$userModel->save(false) ) {
  227. throw new Exception($userModel->getErrors());
  228. }
  229. $transaction->commit();
  230. }catch (Exception $e) {
  231. $transaction->rollBack();
  232. $this->addError('modifyStatus', $e->getMessage());
  233. return null;
  234. }
  235. $this->adminOperateLogger->afterUpdate($this->userId,'ID',['select'=>'ID,STATUS'])->clean()->save([
  236. 'optType' => ($this->status == 1) ? 'Member activation' : 'Member of the lock', // 会员激活 会员锁定
  237. ]);
  238. return $userModel;
  239. }
  240. /**
  241. * @return User|null
  242. * @throws \yii\db\Exception
  243. */
  244. public function isModifyPasswordStatus(){
  245. if(!$this->validate()){
  246. return null;
  247. }
  248. $db = \Yii::$app->db;
  249. $transaction = $db->beginTransaction();
  250. try {
  251. $userModel = User::findOne(['ID' => $this->userId]);
  252. if($userModel->IS_MODIFY_PASSWORD==$this->status){
  253. throw new Exception('The status has not changed, and do not need to set it again');// 状态没有发生改变,无需重复设置!
  254. }
  255. $userModel->IS_MODIFY_PASSWORD = $this->status;
  256. if( !$userModel->save(false) ) {
  257. throw new Exception($userModel->getErrors());
  258. }
  259. $transaction->commit();
  260. }catch (Exception $e) {
  261. $transaction->rollBack();
  262. $this->addError('isModifyPasswordStatus', $e->getMessage());
  263. return null;
  264. }
  265. return $userModel;
  266. }
  267. }