sms.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace common\components;
  3. use biz\weixin\services\WxOpenService;
  4. use Yii;
  5. /**
  6. * 短信接口
  7. */
  8. class sms
  9. {
  10. //发短信
  11. public static function send($mobile, $msg, $merchant = null)
  12. {
  13. $open = WxOpenService::getById(1);
  14. $openName = $open['openName'];
  15. $sms = new chuanglanSMS('N2890342', 'abc178c7');
  16. $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$openName}】";
  17. $msg .= $sign;
  18. //验证是否有权限发短信
  19. $uniqueId = isset($merchant['id']) ? $merchant['id'] : 1;
  20. $ip = httpUtil::ip();
  21. $statusKey = 'SMS_STATUS_' . $uniqueId . '_' . $ip;
  22. $hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
  23. if (!empty($hasSend)) {
  24. util::fail('请60秒后再操作');
  25. }
  26. $todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $uniqueId . '_' . $ip;
  27. $num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
  28. $num = is_numeric($num) ? $num : 0;
  29. if ($num > 10) {
  30. util::fail('您今天申请的短信条数达到上限');
  31. }
  32. Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
  33. Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
  34. $sms->send($mobile, $msg);
  35. }
  36. }