UserBasicForm.php 10 KB

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