待发短信

在线客服
产品支持 短信宝客服
合作渠道 渠道合作
服务咨询

4001-021-502

工作时间

9:00-21:00

yoshopV2.0新增短信宝接口

萤火小程序商城,是一款开源的电商系统,为中小企业提供最佳的新零售解决方案。采用稳定的MVC框架开发,执行效率、扩展性、稳定性值得信赖。永久更新维护,界面美观大方。小编对他还是比较了解的,今天小编就以新增短信接口为例,给大家讲解一下如何进行二次开发,我们今天讲解的是2.0版本,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台的接口非常稳定,发送速度快,注册就送测试短信,推荐大家使用
1:打开项目:app\common\library\sms\Config.php 新增短信宝配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public static function getEasySmsConfig(array $smsConfig): array
{
    return [
        // HTTP 请求的超时时间(秒)
        'timeout' => 5.0,
        // 默认发送配置
        'default' => [
            // 网关调用策略,默认:顺序调用
            'strategy' => OrderStrategy::class,
            // 默认可用的发送网关
            'gateways' => [$smsConfig['default']],
        ],
        // 可用的网关配置
        'gateways' => [
            'aliyun' => [
                'access_key_id' => $smsConfig['engine']['aliyun']['AccessKeyId'],
                'access_key_secret' => $smsConfig['engine']['aliyun']['AccessKeySecret'],
                'sign_name' => $smsConfig['engine']['aliyun']['sign'],
            ],
            'qcloud' => [
                'sdk_app_id' => $smsConfig['engine']['qcloud']['SdkAppID'],
                'secret_id' => $smsConfig['engine']['qcloud']['AccessKeyId'],
                'secret_key' => $smsConfig['engine']['qcloud']['AccessKeySecret'],
                'sign_name' => $smsConfig['engine']['qcloud']['sign'],
            ],
            'qiniu' => [
                'access_key' => $smsConfig['engine']['qiniu']['AccessKey'],
                'secret_key' => $smsConfig['engine']['qiniu']['SecretKey'],
            ],
            'smsbao' => [
                'user' => $smsConfig['engine']['smsbao']['user'],
                'password' => $smsConfig['engine']['smsbao']['password'],
            ],
        ]
    ];
}

2:打开项目:app\common\library\sms\Driver.php 修改发送配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public function sendSms(string $acceptPhone, string $templateCode, array $templateParams): bool
{
    // 实例化EasySms
    $easySmsConfig = Config::getEasySmsConfig($this->smsConfig);
    $easySms = new EasySms($easySmsConfig);
    try {
        // 执行发送短信
        $result = $easySms->send($acceptPhone, [
            'content'  => function($gateway)use($templateParams,$templateCode){
                return str_replace('${'.array_keys($templateParams)[0].'}',array_values($templateParams)[0],$templateCode);
            },
            'template' => $templateCode,
            'data' => $this->getSmsTemplateData($templateParams),
        ]);
        // 短信发送成功的错误处理
        $sendStatus = $this->resultHandle($result);
    catch (NoGatewayAvailableException $e) {
        // 短信发送异常的错误处理
        $sendStatus = false;
        $this->exceptionHandle($e);
    }
    // 记录日志
    helper::logInfo('发送短信', [
        'gateway' => $this->smsConfig['default'],
        'acceptPhone' => $acceptPhone,
        'templateCode' => $templateCode,
        'templateParams' => $templateParams,
        'sendStatus' => $sendStatus,
    ]);
    // 存在异常时抛错
    $sendStatus === false && throwError($this->getError());
    return $sendStatus;
}

3:打开项目:app\common\model\store\Setting.php 修改短信通知配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
SettingEnum::SMS => [
                'key' => SettingEnum::SMS,
                'describe' => '短信通知',
                'values' => [
                    'default' => 'aliyun',
                    // 短信服务渠道
                    'engine' => [
                        // 阿里云
                        'aliyun' => [
                            'name' => '阿里云短信',
                            'website' => 'https://dysms.console.aliyun.com/dysms.htm',
                            'AccessKeyId' => '',
                            'AccessKeySecret' => '',
                            'sign' => '萤火商城'   // 短信签名
                        ],
                        // 腾讯云
                        'qcloud' => [
                            'name' => '腾讯云短信',
                            'website' => 'https://console.cloud.tencent.com/smsv2',
                            'SdkAppID' => '',
                            'AccessKeyId' => '',
                            'AccessKeySecret' => '',
                            'sign' => '萤火商城'   // 短信签名
                        ],
                        // 七牛云
                        'qiniu' => [
                            'name' => '七牛云短信',
                            'website' => 'https://portal.qiniu.com/sms/dashboard',
                            'AccessKey' => '',
                            'SecretKey' => '',
                            'sign' => '萤火商城'   // 短信签名
                        ],
                         // 短信宝
                        'smsbao' => [
                            'name' => '短信宝短信',
                            'website' => 'http://www.smsbao.com',
                            'user' => '',
                            'password' => '',
                        ],
                    ],
                    // 短信通知场景
                    'scene' => [
                        // 短信验证码
                        SettingSmsSceneEnum::CAPTCHA => [
                            'name' => '短信验证码 (通知用户)',    // 场景名称
                            'isEnable' => false,     // 是否开启
                            'templateCode' => '',    // 模板ID
                            'content' => '验证码%s,您正在进行身份验证,打死不要告诉别人哦!',
                            'variables' => [
                                'aliyun' => ['${code}'],
                                'qiniu' => ['${code}'],
                                'smsbao' => ['${code}'],
                                'qcloud' => ['{1}'],
                            ]
                        ],
                        // 新付款订单
                        SettingSmsSceneEnum::ORDER_PAY => [
                            'name' => '新付款订单 (通知商家)',   // 场景名称
                            'isEnable' => false,    // 是否开启
                            'templateCode' => '',   // 模板ID
                            'acceptPhone' => '',    // 接收手机号
                            'content' => '您有一条新订单,订单号为:%s,请注意查看',
                            'variables' => [
                                'aliyun' => ['${order_no}'],
                                'qiniu' => ['${order_no}'],
                                'smsbao' => ['${order_no}'],
                                'qcloud' => ['{1}'],
                            ]
                        ],
                    ]
                ],
            ],

4:打开前端项目:src\views\setting\Sms.vue 大概72行增加短信宝页面(此处修改后需要编译)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        <div v-show="form.getFieldValue('default') === 'qcloud'">
          <a-form-item label="SdkAppID" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.qcloud.SdkAppID`]" />
          </a-form-item>
          <a-form-item label="AccessKeyId" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.qcloud.AccessKeyId`]" />
          </a-form-item>
          <a-form-item
            label="AccessKeySecret"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            required
          >
            <a-input v-decorator="[`engine.qcloud.AccessKeySecret`]" />
          </a-form-item>
          <a-form-item label="短信签名 Sign" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.qcloud.sign`]" />
          </a-form-item>
        </div>
        <!-- 七牛云配置 -->
        <div v-show="form.getFieldValue('default') === 'qiniu'">
          <a-form-item label="AccessKey" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.qiniu.AccessKey`]" />
          </a-form-item>
          <a-form-item label="SecretKey" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.qiniu.SecretKey`]" />
          </a-form-item>
        </div>
        <!-- 短信宝配置 -->
        <div v-show="form.getFieldValue('default') === 'smsbao'">
          <a-form-item label="user" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.smsbao.user`]" />
          </a-form-item>
          <a-form-item label="password" :labelCol="labelCol" :wrapperCol="wrapperCol" required>
            <a-input v-decorator="[`engine.smsbao.password`]" />
          </a-form-item>
        </div>

经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。进行测试发送:

报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。

另外:我们已经开发好完整的yoshopV2.0系统短信宝插件,点击此链接 下载及查看安装流程。

开源插件

最新更新

电商类

CMS类

微信类

文章标签