UserConfigForm.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\Tool;
  7. use common\helpers\user\Balance;
  8. use common\helpers\user\Info;
  9. use common\helpers\user\Status;
  10. use common\libs\logging\operate\UserOperate;
  11. use common\models\DealType;
  12. use common\models\Period;
  13. use common\models\User;
  14. use common\models\UserInfo;
  15. use Yii;
  16. use yii\db\Exception;
  17. /**
  18. * Login form
  19. */
  20. class UserConfigForm extends Model {
  21. public $isAutoWithdraw;
  22. public $allowReconsumeSms;
  23. private $_config;
  24. public function init() {
  25. parent::init();
  26. $this->userOperateLogger = new UserOperate([
  27. 'fetchClass' => UserInfo::class,
  28. ]);
  29. }
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules() {
  34. return [
  35. [['isAutoWithdraw', 'allowReconsumeSms'], 'required'],
  36. [['isAutoWithdraw', 'allowReconsumeSms'], 'boolean'],
  37. [['isAutoWithdraw'], 'isCanAutoWithdraw'],
  38. [['allowReconsumeSms'], 'isAllowReconsumeSms'],
  39. ];
  40. }
  41. public function attributeLabels() {
  42. return [
  43. 'isAutoWithdraw' => Yii::t('app', 'isAutoWithdraw'),
  44. 'allowReconsumeSms' => Yii::t('app', 'allowReconsumeSms'),
  45. ];
  46. }
  47. /**
  48. * 指定场景
  49. * @return array
  50. */
  51. public function scenarios() {
  52. $parentScenarios = parent::scenarios();
  53. $customScenarios = [
  54. 'autoWithdraw' => ['isAutoWithdraw'],
  55. 'allowReconsumeSms' => ['allowReconsumeSms'],
  56. ];
  57. return array_merge($parentScenarios, $customScenarios);
  58. }
  59. /**
  60. * 是否可以修改自动提现
  61. * @param $attribute
  62. */
  63. public function isCanAutoWithdraw($attribute) {
  64. if (!Info::isVerified(\Yii::$app->user->id)) {
  65. $this->addError($attribute, Yii::t('app', 'pleaseAuthentication'));
  66. }
  67. $this->_config = Cache::getSystemConfig();
  68. if (!$this->_config['isCanAutoWithdraw']['VALUE'] && $this->isAutoWithdraw) {
  69. $this->addError('scenario', Yii::t('app', 'systemCloseAutoWithdraw'));
  70. }
  71. }
  72. /**
  73. * 是否可以开启复销短信通知
  74. * @param $attribute
  75. */
  76. public function isAllowReconsumeSms($attribute) {
  77. $this->_config = Cache::getSystemConfig();
  78. if (!$this->_config['smsOpen']['VALUE'] && $this->isAutoWithdraw) {
  79. $this->addError('scenario', Yii::t('app', 'systemCloseReconsumeSMSNotification'));
  80. }
  81. }
  82. /**
  83. * 更新自动提现
  84. * @return bool|null
  85. * @throws Exception
  86. */
  87. public function autoWithdraw() {
  88. if (!$this->validate()) {
  89. return null;
  90. }
  91. $this->userOperateLogger->beforeUpdate(\Yii::$app->user->id,'USER_ID',['select'=>'IS_AUTO_WITHDRAW']);
  92. $transaction = \Yii::$app->db->beginTransaction();
  93. try {
  94. if (!UserInfo::updateAll(['IS_AUTO_WITHDRAW' => $this->isAutoWithdraw], 'USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id])) {
  95. throw new Exception(Yii::t('app', 'updateUserConfigFailed'));
  96. }
  97. $transaction->commit();
  98. } catch (Exception $e) {
  99. $transaction->rollBack();
  100. $this->addError('autoWithdraw', $e->getMessage());
  101. return null;
  102. }
  103. $this->userOperateLogger->afterUpdate(\Yii::$app->user->id,'USER_ID',['select'=>'IS_AUTO_WITHDRAW']);
  104. $this->userOperateLogger->clean()->save([
  105. 'optType' => '开启关闭自动提现',
  106. 'userId' => \Yii::$app->user->id,
  107. 'userName' => Info::getUserNameByUserId(\Yii::$app->user->id),
  108. ]);
  109. return true;
  110. }
  111. /**
  112. * 更新复销短信提醒
  113. * @return bool|null
  114. * @throws Exception
  115. * @throws \yii\base\Exception
  116. */
  117. public function allowReconsumeSms() {
  118. if (!$this->validate()) {
  119. return null;
  120. }
  121. $this->userOperateLogger->beforeUpdate(\Yii::$app->user->id,'USER_ID',['select'=>'ALLOW_RECONSUME_SMS,ALLOW_RECONSUME_SMS_TO']);
  122. $balanceData = Balance::getLogData(\Yii::$app->user->id);
  123. $beforeData = $this->userOperateLogger->saveBeforeContent;
  124. $this->userOperateLogger->saveBeforeContent = array_merge($beforeData,$balanceData);
  125. $transaction = \Yii::$app->db->beginTransaction();
  126. try {
  127. $userId = \Yii::$app->user->id;
  128. if (!UserInfo::updateAll(['ALLOW_RECONSUME_SMS' => $this->allowReconsumeSms?$this->allowReconsumeSms:0], 'USER_ID=:USER_ID', [':USER_ID' => $userId])) {
  129. throw new Exception(Yii::t('app', 'updateUserConfigFailed'));
  130. }
  131. //开启扣费
  132. $smsTo = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId], 'ALLOW_RECONSUME_SMS_TO');
  133. //如果开启并且会员短信不在有效期
  134. if ($this->allowReconsumeSms && $smsTo['ALLOW_RECONSUME_SMS_TO'] < Date::nowTime()) {
  135. $smsWallet = explode(",", $this->_config['smsWallet']['VALUE']);
  136. $smsFee = $this->_config['smsFee']['VALUE'];
  137. //获取剩余月份
  138. $period = Period::instance();
  139. $year = $period->getNowYear();
  140. $monthLeft = Period::getMonthLeft();
  141. $smsFee = Tool::formatPrice($smsFee * $monthLeft);
  142. $to = Date::yearEnd();
  143. foreach ($smsWallet as $item) {
  144. //1奖金钱包2现金钱包
  145. if ($item == 1) {
  146. //看余额是否充足
  147. if (Balance::getAvailableBalance($userId) < $smsFee) continue;
  148. Balance::changeUserBonus($userId, 'bonus', -abs($smsFee), ['DEAL_TYPE_ID' => DealType::SMS, 'REMARK' => $year . '年复销提醒短信服务费']);
  149. if (!UserInfo::updateAll(['ALLOW_RECONSUME_SMS_TO' => $to], 'USER_ID=:USER_ID', [':USER_ID' => $userId])) {
  150. throw new Exception(Yii::t('app', 'updateUserSMSExpiredFailed'));
  151. }
  152. break;
  153. } elseif ($item == 2) {
  154. throw new Exception(Yii::t('app', 'modeDoesNotExist'));
  155. break;
  156. }
  157. }
  158. }
  159. $transaction->commit();
  160. } catch (Exception $e) {
  161. $transaction->rollBack();
  162. $this->addError('allowReconsumeSms', $e->getMessage());
  163. return null;
  164. }
  165. $this->userOperateLogger->afterUpdate(\Yii::$app->user->id,'USER_ID',['select'=>'ALLOW_RECONSUME_SMS,ALLOW_RECONSUME_SMS_TO']);
  166. $balanceData = Balance::getLogData(\Yii::$app->user->id);
  167. $afterData = $this->userOperateLogger->saveAfterContent;
  168. $this->userOperateLogger->saveAfterContent = array_merge($afterData,$balanceData);
  169. unset($beforeData,$afterData);
  170. $this->userOperateLogger->clean()->save([
  171. 'optType' => '开启关闭复销短信提醒',
  172. 'userId' => \Yii::$app->user->id,
  173. 'userName' => Info::getUserNameByUserId(\Yii::$app->user->id),
  174. ]);
  175. return true;
  176. }
  177. }