sms.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. 平台系统发短信
  10. 考虑短信总数是否足够
  11. 平台客户发短信需要限制IP和数量(user_id)
  12. 商家发短信
  13. 考虑总条数
  14. 考虑商家短信是否充值
  15. 商家系统发短信息不需要限制IP和数量
  16. 商家的客户发短信需要限制IP和数量
  17. * 如果平台余额充足,分不限制和根据ip数量限制二种
  18. * 如果商家余额充足,分不限制和根据ip数量限制二种
  19. * 如果不足就记录一下,不要发送
  20. */
  21. class sms
  22. {
  23. //短信服务帐号
  24. const ACCOUNT = 'N2890342';
  25. //短信服务密码
  26. const PASSWORD = 'abc178c7';
  27. //商家发短信 shish 2019.12.24
  28. public static function merchantSend($mobile, $msg, $merchant)
  29. {
  30. $merchantId = $merchant['id'];
  31. $asset = MerchantAssetService::getByMerchantId($merchantId, true);
  32. $remainSmsNum = $asset->remainSmsNum;
  33. if ($remainSmsNum <= 0) {
  34. Yii::info($merchant['merchantName'] . '短信余额不足');
  35. return false;
  36. }
  37. $asset->remainSmsNum = --$remainSmsNum;
  38. $asset->save();
  39. self::freeSend($mobile, $msg, $merchant);
  40. }
  41. //自由发短,无ip和数量限制 shish 2019.12.24
  42. public static function freeSend($mobile, $msg, $merchant = null)
  43. {
  44. $open = WxOpenService::getById(1);
  45. $openName = $open['openName'];
  46. $sms = new chuanglanSMS(self::ACCOUNT, self::PASSWORD);
  47. $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$openName}】";
  48. $msg .= $sign;
  49. $sms->send($mobile, $msg);
  50. }
  51. //发短信,有ip和用户数量限制 shish 2020.2.27
  52. public static function send($mobile, $msg, $merchant = null)
  53. {
  54. $open = WxOpenService::getById(1);
  55. $openName = $open['openName'];
  56. $sms = new chuanglanSMS(self::ACCOUNT, self::PASSWORD);
  57. $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$openName}】";
  58. $msg .= $sign;
  59. //验证是否有权限发短信
  60. $uniqueId = isset($merchant['id']) ? $merchant['id'] : 1;
  61. $ip = httpUtil::ip();
  62. $statusKey = 'SMS_STATUS_' . $uniqueId . '_' . $ip;
  63. $hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
  64. if (!empty($hasSend)) {
  65. util::fail('请60秒后再操作');
  66. }
  67. $todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $uniqueId . '_' . $ip;
  68. $num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
  69. $num = is_numeric($num) ? $num : 0;
  70. if ($num > self::LIMIT) {
  71. util::fail('您今天申请的短信条数达到上限');
  72. }
  73. Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
  74. Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
  75. $sms->send($mobile, $msg);
  76. }
  77. }