UserConfigForm.php 7.0 KB

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