Laravel6 使用騰訊雲簡訊-刪除簡訊簽名

yunli發表於2020-06-12

刪除簡訊簽名

SmsSignRequestModel.php增加deleteSmsSign方法

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

use TencentCloud\Sms\V20190711\Models\AddSmsSignRequest;

use TencentCloud\Sms\V20190711\Models\ModifySmsSignRequest;

use TencentCloud\Sms\V20190711\Models\DeleteSmsSignRequest;

class SmsSignRequest

{

    ·
    ·
    ·

    /**

     * 刪除簡訊簽名 DeleteSmsSign

     *

     * @param integer $signId 待刪除的簽名 ID。

     * @return void

     */
    public function deleteSmsSign(int $signId)

    {

        // 例項化一個 sms 修改簽名請求物件,每個介面都會對應一個request物件。

        $req = new DeleteSmsSignRequest();

        $req->SignId = $signId;

        return $req;

    }

}

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

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

use Illuminate\Http\Request;

class SmsSignRequest extends FormRequest

{

    ·
    ·
    ·

    protected $deleteSignRules = [

        'signId' => '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->addSignRules;

                break;

            case 'PUT':

                $rules = $this->modifySignRules;

                break;

            case 'DELETE':

                $rules = $this->deleteSignRules;

                break;

            default:

                $rules = $this->addSignRules;

                break;

        }

        return $rules;

    }

}

SmsSignController.php增加刪除簽名方法

/* app/Http/Controllers/Api/SmsSignController.php */
<?php
namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\SmsSignRequest;

use App\Http\Models\TencentCloud\SmsSignRequest as SmsSignRequestModel;

use TencentCloud\Common\Exception\TencentCloudSDKException;

class SmsSignController extends Controller

{

    ·
    ·
    ·

    public function deleteSign(SmsSignRequest $request,SmsSignRequestModel $deleteSmsSignRequestModel)

    {

        try {

            $sms = app('tencentcloudSms');

            $req = $deleteSmsSignRequestModel->deleteSmsSign($request->signId);

            // 通過client物件呼叫DescribeInstances方法發起請求。注意請求方法名與請求物件是對應的

            // 返回的resp是一個DescribeInstancesResponse類的例項,與請求物件對應

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

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

            if ($DeleteSignStatus['DeleteTime'] && $DeleteSignStatus['DeleteStatus']) {

                return response()->json([

                    'key' => $RequestId,

                    'code' => 0,

                    'message' => 'Ok',

                    'deleteTime' => $DeleteSignStatus['DeleteTime'],

                    'deleteStatus' => $DeleteSignStatus['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('sendsms', 'SmsController@send')

        ->name('sms.send');

    // 新增簡訊簽名

    Route::post('smsSign', 'SmsSignController@addSign')

        ->name('sms.sign.add');

    // 修改簡訊簽名

    Route::put('smsSign', 'SmsSignController@modifySign')

        ->name('sms.sign.modify');

    // 刪除簡訊簽名

    Route::delete('smsSign', 'SmsSignController@deleteSign')

    ->name('sms.sign.delete');

});

測試

通過 PostMan 測試一下:

Laravel6 使用騰訊雲簡訊-刪除簡訊簽名

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

相關文章