UserBasicForm.php 10 KB

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