作者:廖飞 - CRMEB小程序商城研发我的项目组长

  • 前言
cremb小程序商城v4.0版本反对短信平台为云信,但有局部用户有需要对接阿里云短信,这篇文章将对阿里云短信平台如何对接方以及对接流程具体阐明.

开明阿里云短信

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

2. 点击国内音讯右侧得增加签名按钮,填写必填项申请签名

3. 申请模板

  • 3.1 点击增加模板进入增加模板页面
  • 3.2 抉择模板类型,填写模板名称,模板类型可抉择罕用模板库内得内容。
  • 3.3 期待签名和模板审核通过。

减少阿里云短信驱动

  • 驱动具体架构流程可参考:http://help.crmeb.net/crmeb-v...

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: 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.    }}

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 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类的设计的了解.