SmsClass.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace bizHd\message\classes;
  3. use Yii;
  4. use bizHd\base\classes\BaseClass;
  5. class SmsClass extends BaseClass
  6. {
  7. public static $baseFile = '\bizHd\message\models\Sms';
  8. //每个用户一天可以申请的短信条数
  9. const SMS_LIMIT = 15;
  10. public static $smsType = [
  11. //申请会员
  12. 'applyMember' => ['id' => 0, 'sign' => 'applyMember'],
  13. ];
  14. //用户今天还有没触发短信的机会 ssh 2020.3.3
  15. public static function getRightToSend($userId)
  16. {
  17. $ip = httpUtil::ip();
  18. $statusKey = 'SMS_STATUS_' . $userId . '_' . $ip;
  19. $hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
  20. if (!empty($hasSend)) {
  21. util::fail('请60秒后再操作');
  22. }
  23. $todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $userId . '_' . $ip;
  24. $num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
  25. $num = is_numeric($num) ? $num : 0;
  26. if ($num > self::SMS_LIMIT) {
  27. util::fail('您今天已经申请了'.self::SMS_LIMIT.'条短信,请明天再试');
  28. }
  29. Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
  30. Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
  31. }
  32. }