CRMEB小程序商城v4.0二次开发对接集成阿里云短信
发布于 4 年前 作者 yangfan 3544 次浏览 来自 分享
  • 前言

cremb小程序商城v4.0版本支持短信平台为云信,但有部分用户有需求对接阿里云短信,这篇文章将对阿里云短信平台如何对接方以及对接流程详细说明.>

开通阿里云短信

1. 首先登陆阿里云后台找到短信服务,点击控制台 - 进入短信服务




2. 点击国内消息右侧得添加签名按钮,填写必填项申请签名




3. 申请模板



  • 3.1 点击添加模板进入添加模板页面












  • 3.2 选择模板类型,填写模板名称,模板类型可选择常用模板库内得内容。











  • 3.3 等待签名和模板审核通过。

增加阿里云短信驱动

1. 修改文件CRMEB小程序商城/config/sms.php文件第44行增加阿里云对应的驱动方式和模板id

return [
    ...
    'stores' => [
        //云信
        'yunxin' => [
            ...
        ],
        //阿里云 增加阿里云驱动
        'aliyun' => [
            //这里填写阿里云模板id可和运行的模板名称对应,方便开发
            'template_id' => [

            ]
        ]
    ]
];

通过composer安装SDK

2. 在项目根目录下打开命令行输入:composer require alibabacloud/client按回车进行安装sdk




注意:如提示composer不是一个命令请先安装composer

增加阿里云短信发送类

1. 新建文件crmeb\services\sms\storage\Aliyun.php

<?php
/**
 * [@author](/user/author): liaofei<136327134@qq.com>
 * [@day](/user/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](/user/package) crmeb\services\sms\storage
 */
class Aliyun extends BaseSms
{

    /**
     * AccessKeyId
     * [@var](/user/var) string
     */
    protected $accessKeyId;

    /**
     * AccessKeySecret
     * [@var](/user/var) string
     */
    protected $accessKeySecret;

    /**
     * 签名
     * [@var](/user/var) string
     */
    protected $signName;

    /**
     * 区域 默认杭州
     * [@var](/user/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.
    }

}

2. 完成本类的app()方法,app()方法主要处理初始化阿里云短信的逻辑

/**
     * 初始化阿里云短信
     */
    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](/user/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类的设计的理解.



作者:CRMEB

链接:https://www.jianshu.com/p/2607ac4e2941

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

回到顶部