| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace common\components;
- use biz\sj\services\MerchantAssetService;
- use bizHd\wx\services\WxOpenService;
- use Yii;
- /**
- * 短信接口
- *
- *
- * 平台系统发短信
- * 考虑短信总数是否足够
- * 平台客户发短信需要限制IP和数量(user_id)
- *
- * 商家发短信
- * 考虑总条数
- * 考虑商家短信是否充值
- *
- * 商家系统发短信息不需要限制IP和数量
- *
- * 商家的客户发短信需要限制IP和数量
- * 如果平台余额充足,分不限制和根据ip数量限制二种
- * 如果商家余额充足,分不限制和根据ip数量限制二种
- * 如果不足就记录一下,不要发送
- */
- class sms
- {
- //短信服务帐号
- const ACCOUNT = 'N2890342';
- //短信服务密码
- const PASSWORD = 'abc178c7';
- //每个用户每天可以申请的短信数量
- const LIMIT = 15;
- //商家发短信 ssh 2019.12.24
- public static function merchantSend($params, $msg, $merchant)
- {
- $sjId = $merchant['id'];
- $asset = MerchantAssetService::getBySjId($sjId, true);
- $remainSmsNum = $asset->remainSmsNum;
- if ($remainSmsNum <= 0) {
- Yii::info('商家id:' . $sjId . ' -- ' . $merchant['name'] . '短信余额不足');
- return false;
- }
- $asset->remainSmsNum = --$remainSmsNum;
- $asset->save();
- self::freeSend($params, $msg, $merchant);
- return true;
- }
- //自由发短,无ip和数量限制 ssh 2019.12.24
- public static function freeSend($params, $msg, $merchant = null, $extend = '')
- {
- $ptStyle = dict::getDict('ptStyle', 'hd');
- if (isset(Yii::$app->params['ptStyle'])) {
- $ptStyle = Yii::$app->params['ptStyle'];
- }
- $open = WxOpenService::getById($ptStyle);
- $name = isset($open['name']) ? $open['name'] : '花卉宝';
- $sign = isset($merchant) == true ? "【{$merchant['name']}】" : "【{$name}】";
- $msg = $sign . $msg;
- $sms = new ChuanglanSmsApi();
- $result = $sms->sendVariableSMS($msg, $params, $extend);
- if (!is_null(json_decode($result))) {
- $output = json_decode($result, true);
- if (isset($output['code']) && $output['code'] == '0') {
- Yii::info("短信发送成功,参数:{$params} 返回:" . $result);
- return true;
- } else {
- Yii::info("短信发送失败,参数:{$params} 返回:" . $result);
- return false;
- }
- } else {
- Yii::info("短信发送失败,参数:{$params} 返回:" . $result);
- return false;
- }
- }
- //发短信,有ip和用户数量限制 ssh 2020.2.27
- public static function send($params, $msg, $merchant = null)
- {
- $ptStyle = Yii::$app->params['ptStyle'];
- $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => $ptStyle]);
- if (empty($open)) {
- util::fail('没有找到平台');
- }
- //$name = isset($open['name']) ? $open['name'] : '花卉宝';
- $name = '厦门中花汇';
- $sign = isset($merchant) == true ? "【{$merchant['name']}】" : "【{$name}】";
- $msg = $sign . $msg;
- //验证是否有权限发短信
- $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 = new ChuanglanSmsApi();
- $sms->sendVariableSMS($msg, $params);
- }
- }
|