标签:pack 杭州 添加模板 code turn 二次 todo oge 研发
作者:廖飞 - CRMEB小程序商城研发项目组长
前言:
cremb小程序商城v4.0版本支持短信平台为云信,但有部分用户有需求对接阿里云短信,这篇文章将对阿里云短信平台如何对接方以及对接流程详细说明.
驱动详细架构流程可参考:http://help.crmeb.net/crmeb-v4/1863575
return [
...
‘stores‘ => [
//云信
‘yunxin‘ => [
...
],
//阿里云 增加阿里云驱动
‘aliyun‘ => [
//这里填写阿里云模板id可和运行的模板名称对应,方便开发
‘template_id‘ => [
]
]
]
];
通过composer
安装SDK
composer require alibabacloud/client
按回车进行安装sdk
注意:如提示composer不是一个命令请先安装composer
crmeb\services\sms\storage\Aliyun.php
<?php
/**
* @author: liaofei<136327134@qq.com>
* @day: 2020/8/19
*/
namespace crmeb\services\sms\storage;
use crmeb\basic\BaseSms;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use think\exception\ValidateException;
/**
* 阿里云短信发送
* Class Aliyun
* @package crmeb\services\sms\storage
*/
class Aliyun extends BaseSms
{
/**
* AccessKeyId
* @var string
*/
protected $accessKeyId;
/**
* AccessKeySecret
* @var string
*/
protected $accessKeySecret;
/**
* 签名
* @var string
*/
protected $signName;
/**
* 区域 默认杭州
* @var string
*/
protected $regionId = ‘cn-hangzhou‘;
/**
* 初始化
* @param array $config
* @return mixed|void
*/
protected function initialize(array $config)
{
parent::initialize($config); // TODO: Change the autogenerated stub
$this->accessKeyId = $config[‘accessKeyId‘] ?? null;
$this->accessKeySecret = $config[‘accessKeySecret‘] ?? null;
$this->signName = $config[‘signName‘] ?? null;
if (isset($config[‘regionId‘])) {
$this->regionId = $config[‘regionId‘];
}
}
/**
* 初始化阿里云短信
*/
protected function app()
{
}
/**
* 发送短信
* @param string $phone
* @param string $templateId
* @param array $data
* @return mixed|void
*/
public function send(string $phone, string $templateId, array $data = [])
{
// TODO: Implement send() method.
}
}
/**
* 初始化阿里云短信
*/
protected function app()
{
// 判断下accessKeyId和accessKeySecret存在
if (!$this->accessKeyId || !$this->accessKeySecret) {
throw new ValidateException(‘请传入阿里云短信配置‘);
}
//调用阿里云SDK初始化
AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessKeySecret)
->regionId($this->regionId)
->asDefaultClient();
}
send()方法主要负责执行发送逻辑的处理
/**
* 发送短信
* @param string $phone
* @param string $templateId
* @param array $data
* @return mixed|void
*/
public function send(string $phone, string $templateId, array $data = [])
{
//参数判断
if (!$phone) {
throw new ValidateException(‘请传入手机号‘);
}
if (!$templateId) {
throw new ValidateException(‘请传入发送模板id‘);
}
//初始化阿里云SDK
$this->app();
try {
//执行发送
$result = AlibabaCloud::rpc()
->product(‘Dysmsapi‘)
->version(‘2017-05-25‘)
->action(‘SendSms‘)
->method(‘POST‘)
->host(‘dysmsapi.aliyuncs.com‘)
->options([
‘query‘ => [
‘RegionId‘ => $this->regionId,
‘PhoneNumbers‘ => $phone,
‘SignName‘ => $this->signName,
‘TemplateCode‘ => $templateId,
‘TemplateParam‘ => json_encode($data),
],
])->request()->toArray();
return $result;
} catch (ClientException $e) {
throw new ValidateException($e->getMessage());
} catch (ServerException $e) {
throw new ValidateException($e->getMessage());
}
}
调用实例:
//实例化短信类
/** @var Sms $services */
$services = app()->make(Sms::class, [
‘aliyun‘,
[
‘accessKeyId‘ => ‘阿里云短信accessKeyId‘,
‘accessKeySecret‘ => ‘阿里云短信accessKeyId‘,
‘signName‘ => ‘阿里云短信签名‘,
]
]);
try {
// 执行发送
$res = $services->send(‘15594500000‘, ‘VERIFICATION_CODE‘, [‘code‘=>1234]);
dump($res);
} catch (\Throwable $e) {
dump($e->getMessage());
}
阿里云短信发送集成还是比较简单的,主要在于二开过程中类的结构和对于php类的设计的理解.
标签:pack 杭州 添加模板 code turn 二次 todo oge 研发
原文地址:https://blog.51cto.com/14887126/2521792