| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace common\components;
- use biz\merchant\services\MerchantAssetService;
- use biz\weixin\services\WxOpenService;
- use Yii;
- /**
- * 短信接口
- */
- class sms
- {
-
- //商家发短信 shish 2019.12.24
- public static function merchantSend($mobile, $msg, $merchant)
- {
- $merchantId = $merchant['id'];
- $asset = MerchantAssetService::getByMerchantId($merchantId, true);
- $remainSmsNum = $asset->remainSmsNum;
- if ($remainSmsNum <= 0) {
- Yii::info($merchant['merchantName'] . '短信余额不足');
- return false;
- }
- $asset->remainSmsNum = --$remainSmsNum;
- $asset->save();
- self::freeSend($mobile, $msg, $merchant);
- }
-
- //自由发送无限制 shish 2019.12.24
- public static function freeSend($mobile, $msg, $merchant = null)
- {
- $open = WxOpenService::getById(1);
- $openName = $open['openName'];
- $sms = new chuanglanSMS('N2890342', 'abc178c7');
- $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$openName}】";
- $msg .= $sign;
- $sms->send($mobile, $msg);
- }
-
- //发短信
- public static function send($mobile, $msg, $merchant = null)
- {
- $open = WxOpenService::getById(1);
- $openName = $open['openName'];
- $sms = new chuanglanSMS('N2890342', 'abc178c7');
- $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$openName}】";
- $msg .= $sign;
-
- //验证是否有权限发短信
- $uniqueId = isset($merchant['id']) ? $merchant['id'] : 1;
- $ip = httpUtil::ip();
-
- $statusKey = 'SMS_STATUS_' . $uniqueId . '_' . $ip;
- $hasSend = Yii::$app->redis->executeCommand('GET', [$statusKey]);
- if (!empty($hasSend)) {
- util::fail('请60秒后再操作');
- }
-
- $todayKey = 'SMS_NUM_' . date("Ymd") . '_' . $uniqueId . '_' . $ip;
- $num = Yii::$app->redis->executeCommand('GET', [$todayKey]);
- $num = is_numeric($num) ? $num : 0;
- if ($num > 10) {
- util::fail('您今天申请的短信条数达到上限');
- }
-
- Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
- Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
- $sms->send($mobile, $msg);
- }
-
- }
|