sms.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace common\components;
  3. use biz\sj\services\MerchantAssetService;
  4. use bizHd\wx\services\WxOpenService;
  5. use Yii;
  6. /**
  7. * 短信接口
  8. *
  9. *
  10. * 平台系统发短信
  11. * 考虑短信总数是否足够
  12. * 平台客户发短信需要限制IP和数量(user_id)
  13. *
  14. * 商家发短信
  15. * 考虑总条数
  16. * 考虑商家短信是否充值
  17. *
  18. * 商家系统发短信息不需要限制IP和数量
  19. *
  20. * 商家的客户发短信需要限制IP和数量
  21. * 如果平台余额充足,分不限制和根据ip数量限制二种
  22. * 如果商家余额充足,分不限制和根据ip数量限制二种
  23. * 如果不足就记录一下,不要发送
  24. */
  25. class sms
  26. {
  27. //短信服务帐号
  28. const ACCOUNT = 'N2890342';
  29. //短信服务密码
  30. const PASSWORD = 'abc178c7';
  31. //每个用户每天可以申请的短信数量
  32. const LIMIT = 15;
  33. //商家发短信 shish 2019.12.24
  34. public static function merchantSend($mobile, $msg, $merchant)
  35. {
  36. $merchantId = $merchant['id'];
  37. $asset = MerchantAssetService::getByMerchantId($merchantId, true);
  38. $remainSmsNum = $asset->remainSmsNum;
  39. if ($remainSmsNum <= 0) {
  40. Yii::info($merchant['merchantName'] . '短信余额不足');
  41. return false;
  42. }
  43. $asset->remainSmsNum = --$remainSmsNum;
  44. $asset->save();
  45. self::freeSend($mobile, $msg, $merchant);
  46. }
  47. //自由发短,无ip和数量限制 shish 2019.12.24
  48. public static function freeSend($mobile, $msg, $merchant = null)
  49. {
  50. $open = WxOpenService::getById(1);
  51. $name = isset($open['name']) ? $open['name'] : '花卉宝';
  52. $sms = new chuanglanSMS(self::ACCOUNT, self::PASSWORD);
  53. $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$name}】";
  54. $msg .= $sign;
  55. $sms->send($mobile, $msg);
  56. }
  57. //发短信,有ip和用户数量限制 shish 2020.2.27
  58. public static function send($mobile, $msg, $merchant, $isOpen = 1)
  59. {
  60. $open = [];
  61. if ($isOpen == 1) {
  62. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => \biz\wx\classes\WxOpenClass::OPEN_STYLE_HD]);
  63. }
  64. if ($isOpen == 2) {
  65. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => \biz\wx\classes\WxOpenClass::OPEN_STYLE_GHS]);
  66. }
  67. if ($isOpen == 3) {
  68. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => \biz\wx\classes\WxOpenClass::OPEN_STYLE_MALL]);
  69. }
  70. if (empty($open)) {
  71. util::fail('没有找到平台');
  72. }
  73. $name = isset($open['name']) ? $open['name'] : '花卉宝';
  74. $sms = new chuanglanSMS(self::ACCOUNT, self::PASSWORD);
  75. $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$name}】";
  76. $msg .= $sign;
  77. //验证是否有权限发短信
  78. $uniqueId = isset($merchant['id']) ? $merchant['id'] : 1;
  79. $ip = httpUtil::ip();
  80. $statusKey = 'SMS_STATUS_' . $uniqueId . '_' . $ip;
  81. $hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
  82. if (!empty($hasSend)) {
  83. util::fail('请60秒后再操作');
  84. }
  85. $todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $uniqueId . '_' . $ip;
  86. $num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
  87. $num = is_numeric($num) ? $num : 0;
  88. if ($num > self::LIMIT) {
  89. util::fail('您今天申请的短信条数达到上限');
  90. }
  91. Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
  92. Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
  93. $sms->send($mobile, $msg);
  94. }
  95. }