diff --git a/app/Providers/DemandBiddingServiceProvider.php b/app/Providers/DemandBiddingServiceProvider.php new file mode 100644 index 0000000..580d37c --- /dev/null +++ b/app/Providers/DemandBiddingServiceProvider.php @@ -0,0 +1,38 @@ +with('publisher')->where('id',$order->demand_id)->first(); + if(!empty($demand->publisher->contact_phone)) { + $smsConfig = config('sms.tencent'); + $sms = new SmsService(); + $sms->send($smsConfig['templates']['bidding'],['','$demand->id'],$demand->publisher->contact_phone); + } + }); + } +} diff --git a/app/Service/SmsService.php b/app/Service/SmsService.php new file mode 100644 index 0000000..d41555c --- /dev/null +++ b/app/Service/SmsService.php @@ -0,0 +1,115 @@ +cfg = config('sms.tencent'); + $cred = new Credential($this->cfg['secret_id'], $this->cfg['secret_key']); + // $cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"), getenv("TENCENTCLOUD_SECRET_KEY")); + // 实例化一个http选项,可选的,没有特殊需求可以跳过 + $httpProfile = new HttpProfile(); + // 配置代理 + // $httpProfile->setProxy("https://ip:port"); + $httpProfile->setReqMethod("GET"); // post请求(默认为post请求) + $httpProfile->setReqTimeout(30); // 请求超时时间,单位为秒(默认60秒) + $httpProfile->setEndpoint("sms.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入) + // 实例化一个client选项,可选的,没有特殊需求可以跳过 + $clientProfile = new ClientProfile(); + $clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256) + $clientProfile->setHttpProfile($httpProfile); + // 实例化要请求产品(以sms为例)的client对象,clientProfile是可选的 + // 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量 + $this->client = new SmsClient($cred, 'ap-guangzhou', $clientProfile); + } + + public function send($templateId, $templateParamSet, $phoneNumberSet, $signName = '领峰远扬') + { + //if (!config('sms.debug') || config('sms.debug') == 'yes') { + // Log::debug([$templateId, $templateParamSet, $phoneNumberSet, $signName]); + // return true; + //} + foreach ($phoneNumberSet as $key => $phone) { + if (!$phoneNumberSet[$key]) { + unset($phoneNumberSet[$key]); + } else { + $phoneNumberSet[$key] = '+86' . $phoneNumberSet[$key]; + } + } + if (count($phoneNumberSet) == 0) { + return true; + } + if (!$signName) { + $signName = $this->cfg['sign_name']; + } + try { + // 实例化一个 sms 发送短信请求对象,每个接口都会对应一个request对象。 + $req = new SendSmsRequest(); + /* 填充请求参数,这里request对象的成员变量即对应接口的入参 + * 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义 + * 基本类型的设置: + * 帮助链接: + * 短信控制台: https://console.cloud.tencent.com/smsv2 + * sms helper: https://cloud.tencent.com/document/product/382/3773 + */ + /* + * 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 + */ + $req->SmsSdkAppId = $this->cfg['sms_sdk_app_id']; + /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */ + $req->SignName = $signName; + /* 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper] */ + $req->ExtendCode = ""; + /* + 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号] + * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 + */ + $req->PhoneNumberSet = $phoneNumberSet; + /* 国际/港澳台短信 SenderId: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */ + $req->SenderId = ""; + /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */ + $req->SessionContext = "xxx"; + /* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */ + $req->TemplateId = $this->cfg['templates'][$templateId]; + /* 模板参数: 若无模板参数,则设置为空*/ + $req->TemplateParamSet = $templateParamSet; + // 通过client对象调用SendSms方法发起请求。注意请求方法名与请求对象是对应的 + // 返回的resp是一个SendSmsResponse类的实例,与请求对象对应 + $resp = $this->client->SendSms($req); + // 输出json格式的字符串回包 + $res = json_decode($resp->toJsonString()); + foreach ($res->SendStatusSet as $item) { + if ($item->Code != 'Ok') { + $tpl = $this->cfg['templates'][$templateId]; + $param = json_encode($templateParamSet); + Log::error("SMS ERROR $item->Code[$tpl:$item->PhoneNumber:$param]$item->Message"); + } + } + return True; + } catch (TencentCloudSDKException $e) { + Log::error($e); + } + return 0; + } +} diff --git a/composer.json b/composer.json index 4a1348c..2d0673c 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "guzzlehttp/guzzle": "^7.0.1", "laravel/framework": "^8.40", "laravel/tinker": "^2.5", - "overtrue/wechat": "~5.0" + "overtrue/wechat": "~5.0", + "tencentcloud/tencentcloud-sdk-php": "^3.0" }, "require-dev": { "facade/ignition": "^2.5", diff --git a/composer.lock b/composer.lock index 10daf56..6d893f6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7a150183291d25e55b752a22e9bd8d62", + "content-hash": "5dd2556c6bb5b2e049435c53c47893ff", "packages": [ { "name": "asm89/stack-cors", @@ -1426,16 +1426,16 @@ }, { "name": "guzzlehttp/psr7", - "version": "2.0.0", + "version": "1.8.2", "source": { "type": "git", "url": "https://github.com/guzzle/psr7.git", - "reference": "1dc8d9cba3897165e16d12bb13d813afb1eb3fe7" + "reference": "dc960a912984efb74d0a90222870c72c87f10c91" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/psr7/zipball/1dc8d9cba3897165e16d12bb13d813afb1eb3fe7", - "reference": "1dc8d9cba3897165e16d12bb13d813afb1eb3fe7", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/dc960a912984efb74d0a90222870c72c87f10c91", + "reference": "dc960a912984efb74d0a90222870c72c87f10c91", "shasum": "", "mirrors": [ { @@ -1445,19 +1445,16 @@ ] }, "require": { - "php": "^7.2.5 || ^8.0", - "psr/http-factory": "^1.0", - "psr/http-message": "^1.0", - "ralouphie/getallheaders": "^3.0" + "php": ">=5.4.0", + "psr/http-message": "~1.0", + "ralouphie/getallheaders": "^2.0.5 || ^3.0.0" }, "provide": { - "psr/http-factory-implementation": "1.0", "psr/http-message-implementation": "1.0" }, "require-dev": { - "bamarni/composer-bin-plugin": "^1.4.1", - "http-interop/http-factory-tests": "^0.9", - "phpunit/phpunit": "^8.5.8 || ^9.3.10" + "ext-zlib": "*", + "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.8 || ^9.3.10" }, "suggest": { "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" @@ -1465,13 +1462,16 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.0-dev" + "dev-master": "1.7-dev" } }, "autoload": { "psr-4": { "GuzzleHttp\\Psr7\\": "src/" - } + }, + "files": [ + "src/functions_include.php" + ] }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -1486,11 +1486,6 @@ { "name": "Tobias Schultze", "homepage": "https://github.com/Tobion" - }, - { - "name": "Márk Sági-Kazár", - "email": "mark.sagikazar@gmail.com", - "homepage": "https://sagikazarmark.hu" } ], "description": "PSR-7 message implementation that also provides common utility methods", @@ -1506,9 +1501,9 @@ ], "support": { "issues": "https://github.com/guzzle/psr7/issues", - "source": "https://github.com/guzzle/psr7/tree/2.0.0" + "source": "https://github.com/guzzle/psr7/tree/1.8.2" }, - "time": "2021-06-30T20:03:07+00:00" + "time": "2021-04-26T09:17:50+00:00" }, { "name": "hamcrest/hamcrest-php", @@ -3030,67 +3025,6 @@ }, "time": "2020-06-29T06:28:15+00:00" }, - { - "name": "psr/http-factory", - "version": "1.0.1", - "source": { - "type": "git", - "url": "https://github.com/php-fig/http-factory.git", - "reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-fig/http-factory/zipball/12ac7fcd07e5b077433f5f2bee95b3a771bf61be", - "reference": "12ac7fcd07e5b077433f5f2bee95b3a771bf61be", - "shasum": "", - "mirrors": [ - { - "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", - "preferred": true - } - ] - }, - "require": { - "php": ">=7.0.0", - "psr/http-message": "^1.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "Psr\\Http\\Message\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "PHP-FIG", - "homepage": "http://www.php-fig.org/" - } - ], - "description": "Common interfaces for PSR-7 HTTP message factories", - "keywords": [ - "factory", - "http", - "message", - "psr", - "psr-17", - "psr-7", - "request", - "response" - ], - "support": { - "source": "https://github.com/php-fig/http-factory/tree/master" - }, - "time": "2019-04-30T12:38:16+00:00" - }, { "name": "psr/http-message", "version": "1.0.1", @@ -6497,6 +6431,60 @@ ], "time": "2021-06-27T09:16:08+00:00" }, + { + "name": "tencentcloud/tencentcloud-sdk-php", + "version": "3.0.467", + "source": { + "type": "git", + "url": "https://github.com/TencentCloud/tencentcloud-sdk-php.git", + "reference": "11f0a41777f97cdc4226a9d1b0dea78eead42a24" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/TencentCloud/tencentcloud-sdk-php/zipball/11f0a41777f97cdc4226a9d1b0dea78eead42a24", + "reference": "11f0a41777f97cdc4226a9d1b0dea78eead42a24", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "guzzlehttp/guzzle": "^6.3 || ^7.0", + "guzzlehttp/psr7": "^1.8", + "php": ">=5.6.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/QcloudApi/QcloudApi.php" + ], + "psr-4": { + "TencentCloud\\": "./src/TencentCloud" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "coolli", + "email": "tencentcloudapi@tencent.com", + "homepage": "https://cloud.tencent.com/document/sdk/PHP", + "role": "Developer" + } + ], + "description": "TencentCloudApi php sdk", + "homepage": "https://github.com/TencentCloud/tencentcloud-sdk-php", + "support": { + "issues": "https://github.com/TencentCloud/tencentcloud-sdk-php/issues", + "source": "https://github.com/TencentCloud/tencentcloud-sdk-php/tree/3.0.467" + }, + "time": "2021-08-30T01:17:41+00:00" + }, { "name": "tijsverkoyen/css-to-inline-styles", "version": "2.2.3", diff --git a/config/sms.php b/config/sms.php new file mode 100644 index 0000000..cfa0a03 --- /dev/null +++ b/config/sms.php @@ -0,0 +1,16 @@ + \App\Libraries\Sms\Tencent::class, + 'type' => 'tencent', + //'debug' => env('SMS_DEBUG'), + 'tencent' => [ + 'sms_sdk_app_id'=> env('SMS_SDK_APP_ID'), + 'secret_id' => env('SECRET_ID'), + 'secret_key' => env('SECRET_KEY'), + 'sign_name' => '领峰远扬', + 'templates' => [ + 'bidding' => '1100494' //{1}参与了您发起的竞标{2},详情请登录{3}查看。 + ] + ] +];