UserBasicForm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. $userWallet = UserWallet::findOne(['USER_ID' => $this->userId]);
  146. if ($userWallet && $userWallet->CASH > 0) {
  147. throw new Exception(Yii::t('app', 'cashThanNotAllowModificationCountry'));
  148. }
  149. // 2.有进行中的分期订单不可以移民
  150. $instalmentOrder = Instalment::findOne(['USER_ID' => $this->userId]);
  151. // 订单分期总期数配置
  152. if ($instalmentOrder) {
  153. // 分期的总期数
  154. $instalment = intval(Cache::getSystemConfig()['instalment']['VALUE'] ?? 3);
  155. // 分期商品的期数不能大于总分期数限制
  156. if (intval($instalmentOrder['STAGE']) < $instalment) {
  157. throw new Exception(Yii::t('app', 'instalmentOrderInProcess'));
  158. }
  159. }
  160. }
  161. $db = \Yii::$app->db;
  162. $transaction = $db->beginTransaction();
  163. try {
  164. $this->adminOperateLogger->beforeUpdate($userModel);
  165. $userModel->REAL_NAME = $this->realName;
  166. $userModel->MOBILE = $this->mobile;
  167. $userModel->OPEN_BANK = $this->openBank;
  168. $userModel->BANK_NO = $this->bankNo;
  169. $userModel->BANK_ADDRESS = $this->bankAddress;
  170. $userModel->LANGUAGE_ID = $this->language;
  171. $userModel->COUNTRY_ID = $this->country;
  172. if( !$userModel->save(false) ) {
  173. $transaction->rollBack();
  174. throw new Exception($userModel->getErrors());
  175. }
  176. // 移民操作
  177. if ($this->country != $beforeCountry) {
  178. // 移民记录
  179. $model = new UserImmigrant();
  180. $model->ID = PageSnowFake::instance()->generateId();
  181. $model->user_id = $this->userId;
  182. $model->before_country_id = $beforeCountry;
  183. $model->after_country_id = $this->country;
  184. $model->period_num = Period::instance()->getNowPeriodNum();
  185. $model->created_by = \Yii::$app->user->id;
  186. if (!$model->save()) {
  187. $transaction->rollBack();
  188. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  189. }
  190. // 现金钱包余额转换
  191. $userWallet = UserWallet::findOne(['USER_ID' => $this->userId]);
  192. if ($userWallet && $userWallet->CASH > 0) {
  193. $userWallet->CASH = Tool::convertAmount($userWallet->CASH, $beforeCurrencyRate, $afterCurrencyRate);
  194. if (!$userWallet->save()) {
  195. $transaction->rollBack();
  196. throw new Exception($userModel->getErrors());
  197. }
  198. }
  199. }
  200. $transaction->commit();
  201. $this->adminOperateLogger->afterUpdate($userModel)->clean()->save([
  202. 'optType' => 'Modification of Member information', // 修改会员资料
  203. 'userId' => $this->userId,
  204. 'userName' => $userModel->USER_NAME,
  205. 'realName' => $this->realName,
  206. 'mobile' => $this->mobile,
  207. 'openBank' => $this->openBank,
  208. 'bankNo' => $this->bankNo,
  209. 'bankAddress' => $this->bankAddress,
  210. 'language' => $this->language,
  211. 'country' => $this->country,
  212. ]);
  213. }catch (Exception $e) {
  214. $transaction->rollBack();
  215. throw new Exception($e->getFile() . ' ' . $e->getLine() . ' ' . $e->getMessage());
  216. }
  217. return $userModel;
  218. }
  219. /**
  220. * 修改会员状态
  221. * @return User|null
  222. * @throws \yii\db\Exception
  223. */
  224. public function modifyStatus(){
  225. if(!$this->validate()){
  226. return null;
  227. }
  228. $this->adminOperateLogger->beforeUpdate($this->userId, 'ID',['select'=>'ID,STATUS']);
  229. $db = \Yii::$app->db;
  230. $transaction = $db->beginTransaction();
  231. try {
  232. $userModel = User::findOne(['ID' => $this->userId]);
  233. if($userModel->STATUS==$this->status){
  234. $statusName = ($userModel->STATUS == 1) ? 'activation' : 'lock'; // 激活 锁定
  235. throw new Exception('The current member status is【' . $statusName . '】,Do not need to set it again!'); // 当前会员状态已 无需重复设置
  236. }
  237. $userModel->STATUS = $this->status;
  238. $userModel->STATUS_AT = Date::nowTime();
  239. if( !$userModel->save(false) ) {
  240. throw new Exception($userModel->getErrors());
  241. }
  242. $transaction->commit();
  243. }catch (Exception $e) {
  244. $transaction->rollBack();
  245. $this->addError('modifyStatus', $e->getMessage());
  246. return null;
  247. }
  248. $this->adminOperateLogger->afterUpdate($this->userId,'ID',['select'=>'ID,STATUS'])->clean()->save([
  249. 'optType' => ($this->status == 1) ? 'Member activation' : 'Member of the lock', // 会员激活 会员锁定
  250. ]);
  251. return $userModel;
  252. }
  253. /**
  254. * @return User|null
  255. * @throws \yii\db\Exception
  256. */
  257. public function isModifyPasswordStatus(){
  258. if(!$this->validate()){
  259. return null;
  260. }
  261. $db = \Yii::$app->db;
  262. $transaction = $db->beginTransaction();
  263. try {
  264. $userModel = User::findOne(['ID' => $this->userId]);
  265. if($userModel->IS_MODIFY_PASSWORD==$this->status){
  266. throw new Exception('The status has not changed, and do not need to set it again');// 状态没有发生改变,无需重复设置!
  267. }
  268. $userModel->IS_MODIFY_PASSWORD = $this->status;
  269. if( !$userModel->save(false) ) {
  270. throw new Exception($userModel->getErrors());
  271. }
  272. $transaction->commit();
  273. }catch (Exception $e) {
  274. $transaction->rollBack();
  275. $this->addError('isModifyPasswordStatus', $e->getMessage());
  276. return null;
  277. }
  278. return $userModel;
  279. }
  280. }