sms.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace common\components;
  3. use biz\merchant\services\MerchantAssetService;
  4. use biz\weixin\services\WxOpenService;
  5. use Yii;
  6. /**
  7. * 短信接口
  8. */
  9. class sms
  10. {
  11. //商家发短信 shish 2019.12.24
  12. public static function merchantSend($mobile, $msg, $merchant)
  13. {
  14. $merchantId = $merchant['id'];
  15. $asset = MerchantAssetService::getByMerchantId($merchantId, true);
  16. $remainSmsNum = $asset->remainSmsNum;
  17. if ($remainSmsNum <= 0) {
  18. Yii::info($merchant['merchantName'] . '短信余额不足');
  19. return false;
  20. }
  21. $asset->remainSmsNum = --$remainSmsNum;
  22. $asset->save();
  23. self::freeSend($mobile, $msg, $merchant);
  24. }
  25. //自由发送无限制 shish 2019.12.24
  26. public static function freeSend($mobile, $msg, $merchant = null)
  27. {
  28. $open = WxOpenService::getById(1);
  29. $openName = $open['openName'];
  30. $sms = new chuanglanSMS('N2890342', 'abc178c7');
  31. $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$openName}】";
  32. $msg .= $sign;
  33. $sms->send($mobile, $msg);
  34. }
  35. //发短信
  36. public static function send($mobile, $msg, $merchant = null)
  37. {
  38. $open = WxOpenService::getById(1);
  39. $openName = $open['openName'];
  40. $sms = new chuanglanSMS('N2890342', 'abc178c7');
  41. $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$openName}】";
  42. $msg .= $sign;
  43. //验证是否有权限发短信
  44. $uniqueId = isset($merchant['id']) ? $merchant['id'] : 1;
  45. $ip = httpUtil::ip();
  46. $statusKey = 'SMS_STATUS_' . $uniqueId . '_' . $ip;
  47. $hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
  48. if (!empty($hasSend)) {
  49. util::fail('请60秒后再操作');
  50. }
  51. $todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $uniqueId . '_' . $ip;
  52. $num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
  53. $num = is_numeric($num) ? $num : 0;
  54. if ($num > 10) {
  55. util::fail('您今天申请的短信条数达到上限');
  56. }
  57. Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
  58. Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
  59. $sms->send($mobile, $msg);
  60. }
  61. }