UserBasicForm.php 10 KB

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