Browse Source

短信

develop
lemon 4 years ago
parent
commit
803de4c5e9
  1. 38
      app/Providers/DemandBiddingServiceProvider.php
  2. 115
      app/Service/SmsService.php
  3. 3
      composer.json
  4. 154
      composer.lock
  5. 16
      config/sms.php

38
app/Providers/DemandBiddingServiceProvider.php

@ -0,0 +1,38 @@
<?php
namespace App\Providers;
use App\Models\Demand;
use App\Models\DemandProduct;
use App\Service\SmsService;
use Illuminate\Support\ServiceProvider;
class DemandBiddingServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
DemandProduct::created(function ($order) {
$demand = Demand::query()->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);
}
});
}
}

115
app/Service/SmsService.php

@ -0,0 +1,115 @@
<?php
namespace App\Service;
use \Illuminate\Support\Facades\Log;
use TencentCloud\Sms\V20210111\SmsClient;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
class SmsService
{
private $client;
private $cfg;
function __construct()
{
/* 必要步骤:
* 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey。
* 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
* 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
* 以免泄露密钥对危及你的财产安全。
* CAM密匙查询: https://console.cloud.tencent.com/cam/capi
*/
$this->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;
}
}

3
composer.json

@ -12,7 +12,8 @@
"guzzlehttp/guzzle": "^7.0.1", "guzzlehttp/guzzle": "^7.0.1",
"laravel/framework": "^8.40", "laravel/framework": "^8.40",
"laravel/tinker": "^2.5", "laravel/tinker": "^2.5",
"overtrue/wechat": "~5.0"
"overtrue/wechat": "~5.0",
"tencentcloud/tencentcloud-sdk-php": "^3.0"
}, },
"require-dev": { "require-dev": {
"facade/ignition": "^2.5", "facade/ignition": "^2.5",

154
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "7a150183291d25e55b752a22e9bd8d62",
"content-hash": "5dd2556c6bb5b2e049435c53c47893ff",
"packages": [ "packages": [
{ {
"name": "asm89/stack-cors", "name": "asm89/stack-cors",
@ -1426,16 +1426,16 @@
}, },
{ {
"name": "guzzlehttp/psr7", "name": "guzzlehttp/psr7",
"version": "2.0.0",
"version": "1.8.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/guzzle/psr7.git", "url": "https://github.com/guzzle/psr7.git",
"reference": "1dc8d9cba3897165e16d12bb13d813afb1eb3fe7"
"reference": "dc960a912984efb74d0a90222870c72c87f10c91"
}, },
"dist": { "dist": {
"type": "zip", "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": "", "shasum": "",
"mirrors": [ "mirrors": [
{ {
@ -1445,19 +1445,16 @@
] ]
}, },
"require": { "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": { "provide": {
"psr/http-factory-implementation": "1.0",
"psr/http-message-implementation": "1.0" "psr/http-message-implementation": "1.0"
}, },
"require-dev": { "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": { "suggest": {
"laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses"
@ -1465,13 +1462,16 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "2.0-dev"
"dev-master": "1.7-dev"
} }
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"GuzzleHttp\\Psr7\\": "src/" "GuzzleHttp\\Psr7\\": "src/"
}
},
"files": [
"src/functions_include.php"
]
}, },
"notification-url": "https://packagist.org/downloads/", "notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
@ -1486,11 +1486,6 @@
{ {
"name": "Tobias Schultze", "name": "Tobias Schultze",
"homepage": "https://github.com/Tobion" "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", "description": "PSR-7 message implementation that also provides common utility methods",
@ -1506,9 +1501,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/guzzle/psr7/issues", "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", "name": "hamcrest/hamcrest-php",
@ -3030,67 +3025,6 @@
}, },
"time": "2020-06-29T06:28:15+00:00" "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", "name": "psr/http-message",
"version": "1.0.1", "version": "1.0.1",
@ -6497,6 +6431,60 @@
], ],
"time": "2021-06-27T09:16:08+00:00" "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", "name": "tijsverkoyen/css-to-inline-styles",
"version": "2.2.3", "version": "2.2.3",

16
config/sms.php

@ -0,0 +1,16 @@
<?php
return [
'sender' => \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}查看。
]
]
];
Loading…
Cancel
Save