Laravel6 使用騰訊雲簡訊-簡訊模板狀態查詢

yunli發表於2020-07-09

簡訊模板狀態查詢

SmsTemplateRequestModel.php增加describeSmsTemplates方法

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

use TencentCloud\Sms\V20190711\Models\AddSmsTemplateRequest;

use TencentCloud\Sms\V20190711\Models\ModifySmsTemplateRequest;

use TencentCloud\Sms\V20190711\Models\DeleteSmsTemplateRequest;

use TencentCloud\Sms\V20190711\Models\DescribeSmsTemplateListRequest;

class SmsTemplateRequest

{

    ·
    ·
    ·

    /**
     * 簡訊模板狀態查詢 DescribeSmsTemplateList
     *
     * @param Array $templateIds 模板 ID 陣列。
     * @param integer $international 是否國際/港澳臺簡訊:
     *                               0:表示國內簡訊。
     *                               1:表示國際/港澳臺簡訊。
     * @return void
     */
    public function describeSmsTemplates(Array $templateIds, int $international = 0)
    {
        $req = new DescribeSmsTemplateListRequest();

        $req->TemplateIdSet = $templateIds;

        $req->International = $international;

        return $req;
    }

}

簡訊模板驗證類增加簡訊模板狀態查詢的驗證規則

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

use Illuminate\Http\Request;

class SmsTemplateRequest extends FormRequest

{

    ·
    ·
    ·

    protected $describeTemplatesRules = [
        'templateIds' => 'required|array',
        'international' => 'required|integer'
    ];

    /**

     * Get the validation rules that apply to the request.

     *

     * @return array

     */

    public function rules(Request $request)
    {
        switch ($request->method) {
            case 'GET':
                $rules = $this->describeTemplatesRules;
                break;

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

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

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

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

            if (count($DescribeTemplateStatusSet) >= 1) {
                return response()->json([
                    'key' => $RequestId,
                    'code' => 0,
                    'message' => 'Ok',
                    'templates' => $DescribeTemplateStatusSet
                ]);
            }

        } 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');
    // 簡訊模板狀態查詢
    Route::get('smsTemplate', 'SmsTemplateController@describeTemplates')
    ->name('sms.template.describe');

});

測試

通過 PostMan 測試一下:

Laravel6 使用騰訊雲簡訊-簡訊模板狀態查詢

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

相關文章