| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace common\components;
- use biz\merchant\services\MerchantAssetService;
- use biz\wx\services\WxOpenService;
- use Yii;
- /**
- * 短信接口
- *
- *
- * 平台系统发短信
- * 考虑短信总数是否足够
- * 平台客户发短信需要限制IP和数量(user_id)
- *
- * 商家发短信
- * 考虑总条数
- * 考虑商家短信是否充值
- *
- * 商家系统发短信息不需要限制IP和数量
- *
- * 商家的客户发短信需要限制IP和数量
- * 如果平台余额充足,分不限制和根据ip数量限制二种
- * 如果商家余额充足,分不限制和根据ip数量限制二种
- * 如果不足就记录一下,不要发送
- */
- class sms
- {
- //短信服务帐号
- const ACCOUNT = 'N2890342';
- //短信服务密码
- const PASSWORD = 'abc178c7';
- //每个用户每天可以申请的短信数量
- const LIMIT = 15;
- //商家发短信 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);
- }
- //自由发短,无ip和数量限制 shish 2019.12.24
- public static function freeSend($mobile, $msg, $merchant = null)
- {
- $open = WxOpenService::getById(1);
- $name = isset($open['name']) ? $open['name'] : '花卉宝';
- $sms = new chuanglanSMS(self::ACCOUNT, self::PASSWORD);
- $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$name}】";
- $msg .= $sign;
- $sms->send($mobile, $msg);
- }
- //发短信,有ip和用户数量限制 shish 2020.2.27
- public static function send($mobile, $msg, $merchant = null)
- {
- $open = WxOpenService::getOpen();
- $name = isset($open['name']) ? $open['name'] : '花卉宝';
- $sms = new chuanglanSMS(self::ACCOUNT, self::PASSWORD);
- $sign = isset($merchant) == true ? "【{$merchant['merchantName']}】" : "【{$name}】";
- $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 > self::LIMIT) {
- util::fail('您今天申请的短信条数达到上限');
- }
- Yii::$app->redis->executeCommand('SETEX', [$statusKey, 60, 'hasSend']);
- Yii::$app->redis->executeCommand('SETEX', [$todayKey, 86400, ++$num]);
- $sms->send($mobile, $msg);
- }
- }
|