Laravel6 使用騰訊雲簡訊-刪除簡訊模板

yunli發表於2020-07-09

刪除簡訊模板

SmsTemplateRequestModel.php增加deleteSmsTemplate方法

/* app/Http/Requests/Api/SmsTemplateRequest.php */
<?php
namespace App\Http\Models\TencentCloud;

use TencentCloud\Sms\V20190711\Models\AddSmsSignRequest;

use TencentCloud\Sms\V20190711\Models\ModifySmsSignRequest;

use TencentCloud\Sms\V20190711\Models\DeleteSmsTemplateRequest;

class SmsTemplateRequest

{

    ·
    ·
    ·

    /**
     * 刪除簡訊模板 DeleteSmsTemplate
     *
     * @param integer $templateId 待刪除的模板 ID。
     * @return void
     */
    public function deleteSmsTemplate(int $templateId)
    {
        $req = new DeleteSmsTemplateRequest();

        $req->TemplateId = $templateId;

        return $req;
    }

}

簡訊簽名驗證類增加刪除簽名的驗證規則

/* app/Http/Requests/Api/SmsSignRequest.php */
<?php
namespace App\Http\Requests\Api;

use Illuminate\Http\Request;

class SmsSignRequest extends FormRequest

{

    ·
    ·
    ·

    protected $deleteTemplateRules = [
        'templateId' => 'required|integer'
    ];

    /**

     * 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;

            case 'DELETE':

                $rules = $this->deleteTemplateRules;

                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 deleteTemplate(SmsTemplateRequest $request,SmsTemplateRequestModel $SmsTemplateRequestModel)
    {
        try {
            $sms = app('tencentcloudSms');

            $req = $SmsTemplateRequestModel->deleteSmsTemplate($request->templateId);
            // 通過client物件呼叫DescribeInstances方法發起請求。注意請求方法名與請求物件是對應的
            // 返回的resp是一個DescribeInstancesResponse類的例項,與請求物件對應

            $resp = $sms->DeleteSmsTemplate($req);

            list($DeleteTemplateStatus, $RequestId) = array_values(json_decode($resp->toJsonString(), true));

            if ($DeleteTemplateStatus['DeleteTime'] && $DeleteTemplateStatus['DeleteStatus']) {
                return response()->json([
                    'key' => $RequestId,
                    'code' => 0,
                    'message' => 'Ok',
                    'deleteTime' => $DeleteTemplateStatus['DeleteTime'],
                    'deleteStatus' => $DeleteTemplateStatus['DeleteStatus']
                ]);
            }

        } 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');
    // 刪除簡訊模板
    Route::delete('smsTemplate', 'SmsTemplateController@deleteTemplate')
    ->name('sms.template.delete');

});

測試

通過 PostMan 測試一下:

Laravel6 使用騰訊雲簡訊-刪除簡訊模板

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章