修改簡訊模板
SmsTemplateRequestModel.php
增加modifySmsTemplate
方法
/* app/Http/Requests/Api/SmsTemplateRequest.php */
<?php
namespace App\Http\Models\TencentCloud;
use TencentCloud\Sms\V20190711\Models\AddSmsTemplateRequest;
use TencentCloud\Sms\V20190711\Models\ModifySmsTemplateRequest;
class SmsTemplateRequest
{
·
·
·
/**
* 修改簡訊模板 ModifySmsTemplate
*
* @param integer $templateId 待修改的模板的模板 ID。
* @param String $templateContent 新的模板內容。
* @param String $templateName 新的模板名稱。
* @param integer $smsType 簡訊型別,0表示普通簡訊, 1表示營銷簡訊。
* @param integer $international 是否國際/港澳臺簡訊:
* 0:表示國內簡訊。
* 1:表示國際/港澳臺簡訊。
* @param String $remark 模板備註,例如申請原因,使用場景等。
* @return void
*/
public function modifySmsTemplate(int $templateId, String $templateContent = '',String $templateName = '', int $smsType = 0, int $international = 0, String $remark = '')
{
// 例項化一個 sms 新增簽名請求物件,每個介面都會對應一個request物件。
$req = new ModifySmsTemplateRequest();
$req->TemplateId = $templateId;
$req->TemplateContent = $templateContent;
$req->TemplateName = $templateName;
$req->SmsType = $smsType;
$req->International = $international;
$req->Remark = $remark;
return $req;
}
}
簡訊模板驗證類增加修改模板的驗證規則
/* app/Http/Requests/Api/SmsTemplateRequest.php */
<?php
namespace App\Http\Requests\Api;
use Illuminate\Http\Request;
class SmsTemplateRequest extends FormRequest
{
·
·
·
protected $modifyTemplateRules = [
'templateId' => 'required|integer',
'templateContent' => 'required|String',
'templateName' => 'required|String',
'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;
case 'PUT':
$rules = $this->modifyTemplateRules;
break;
default:
$rules = $this->addTemplateRules;
break;
}
return $rules;
}
}
SmsTemplateController.php
增加修改模板方法
/* 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 modifyTemplate(SmsTemplateRequest $request,SmsTemplateRequestModel $SmsTemplateRequestModel)
{
try {
$sms = app('tencentcloudSms');
$req = $SmsTemplateRequestModel->modifySmsTemplate($request->templateId, $request->templateContent, $request->templateName, $request->smsType, $request->international, $request->remark);
// 通過client物件呼叫DescribeInstances方法發起請求。注意請求方法名與請求物件是對應的
// 返回的resp是一個DescribeInstancesResponse類的例項,與請求物件對應
$resp = $sms->ModifySmsTemplate($req);
list($ModifyTemplateStatus, $RequestId) = array_values(json_decode($resp->toJsonString(), true));
if ($ModifyTemplateStatus['TemplateId']) {
return response()->json([
'key' => $RequestId,
'code' => 0,
'message' => 'Ok',
'templateId' => $ModifyTemplateStatus['TemplateId']
]);
}
} 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');
// 修改簡訊模板
Route::put('smsTemplate', 'SmsTemplateController@modifyTemplate')
->name('sms.template.modify');
});
測試
通過 PostMan 測試一下:
本作品採用《CC 協議》,轉載必須註明作者和本文連結