新增簡訊模板
新建 SmsTemplateRequestModel.php
touch app/Models/TencentCloud/SmsTemplateRequest.php # win 系統請將 "/" 更改成 "\", 或直接手動建立檔案
/* app/Http/Requests/Api/SmsTemplateRequest.php */
<?php
namespace App\Http\Models\TencentCloud;
use TencentCloud\Sms\V20190711\Models\AddSmsTemplateRequest;
class SmsTemplateRequest
{
/**
* 新增簡訊模板 AddSmsTemplate
*
* @param String $templateContent 模板內容。
* @param String $templateName 模板名稱。
* @param integer $smsType 簡訊型別,0表示普通簡訊, 1表示營銷簡訊。
* @param integer $international 是否國際/港澳臺簡訊:
* 0:表示國內簡訊。
* 1:表示國際/港澳臺簡訊。
* @param String $remark 模板備註,例如申請原因,使用場景等。
* @return void
*/
public function addSmsTemplate(String $templateContent = '',String $templateName = '', int $smsType = 0, int $international = 0, String $remark = '')
{
// 例項化一個 sms 新增簽名請求物件,每個介面都會對應一個request物件。
$req = new AddSmsTemplateRequest();
$req->TemplateContent = $templateContent;
$req->TemplateName = $templateName;
$req->SmsType = $smsType;
$req->International = $international;
$req->International = $international;
$req->Remark = $remark;
return $req;
}
}
建立新增簡訊簽名的驗證類
php artisan make:request Api/SmsTemplateRequest
/* app/Http/Requests/Api/SmsTemplateRequest.php */
<?php
namespace App\Http\Requests\Api;
use Illuminate\Http\Request;
class SmsTemplateRequest extends FormRequest
{
protected $addTemplateRules = [
'templateContent' => 'required|String',
'templateName' => 'required|integer',
'smsType' => 'required|integer',
'international' => 'required|integer',
'remark' => 'required|String'
];
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(Request $request)
{
switch ($request->method) {
case 'POST':
$rules = $this->addTemplateRules;
break;
default:
$rules = $this->addTemplateRules;
break;
}
return $rules;
}
}
新建簡訊簽名控制器
php artisan make:controller Api/SmsTemplateController
/* app/Http/Controllers/Api/SmsTemplateController.php */
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\SmsTemplateRequest;
use App\Http\Models\TencentCloud\SmsTemplateRequest as SmsTemplateRequestModel;
use TencentCloud\Common\Exception\TencentCloudSDKException;
class SmsTemplateController extends Controller
{
public function addTemplate(SmsTemplateRequest $request,SmsTemplateRequestModel $SmsTemplateRequestModel)
{
try {
$sms = app('tencentcloudSms');
$req = $SmsTemplateRequestModel->addSmsTemplate($request->templateContent, $request->templateName, $request->smsType, $request->international, $request->remark);
// 通過client物件呼叫DescribeInstances方法發起請求。注意請求方法名與請求物件是對應的
// 返回的resp是一個DescribeInstancesResponse類的例項,與請求物件對應
$resp = $sms->addSmsTemplate($req);
list($AddTemplateStatus, $RequestId) = array_values(json_decode($resp->toJsonString(), true));
if ($AddTemplateStatus['TemplateId']) {
return response()->json([
'key' => $RequestId,
'code' => 0,
'message' => 'Ok',
'templateId' => $AddTemplateStatus['TemplateId']
])->setStatusCode(201);
}
} catch (TencentCloudSDKException $e) {
return response()->json([
'key' => $e->getRequestId(),
'code' => $e->getErrorCode(),
'message' => $e->getMessage()
])->setStatusCode(500);
}
}
}
新增路由
/* routes/api.php */
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::prefix('v1')->namespace('Api')->name('api.v1.')->group(function () {
// 新增簡訊模板
Route::post('smsTemplate', 'SmsTemplateController@addTemplate')
->name('sms.template.add');
});
測試
通過 PostMan 測試一下:
本作品採用《CC 協議》,轉載必須註明作者和本文連結