待发短信

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

4001-021-502

工作时间

9:00-21:00

CRMEBV4.50新增短信宝短信接口

CRMEB商城系统是基于ThinkPhp6.0+Vue开发的一套新零售移动电商开源系统,包含商城、拼团、砍价、秒杀、优惠券、积分、分销等功能,更适合企业二次开发。今天小编就以新增短信接口为例,给大家讲解一下如何进行二次开发,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台的接口非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
前提:前先执行以下sql语句,增加短信宝相关配置

1
2
3
4
5
6
7
8
9
INSERT INTO `eb_system_config_tab` (`id`, `pid`, `title`, `eng_title`, `status`, `info`, `icon`, `type`, `sort`) VALUES ('999''96''短信宝配置''smsbao''1''0''ios-chatboxes''0''0');
 
INSERT INTO `eb_system_config` ( `menu_name`, `type`, `input_type`, `config_tab_id`, `parameter`, `upload_type`, `required`, `width`, `high`, `value`, `info`, `desc`, `sort`, `status`)
VALUES ('smsbao_user''text''input''999''', 0, '''999', 0, '''短信宝账号''短信宝账号''0''1');
INSERT INTO `eb_system_config` ( `menu_name`, `type`, `input_type`, `config_tab_id`, `parameter`, `upload_type`, `required`, `width`, `high`, `value`, `info`, `desc`, `sort`, `status`)
VALUES ('smsbao_pwd''text''input''999''', 0, '''999', 0, '''短信宝密码''短信宝密码''0''1');
INSERT INTO `eb_system_config` ( `menu_name`, `type`, `input_type`, `config_tab_id`, `parameter`, `upload_type`, `required`, `width`, `high`, `value`, `info`, `desc`, `sort`, `status`)
VALUES ('sms_sign_name''text''input''999''', 0, '''999', 0, '''短信签名''短信签名''0''1');
UPDATE `eb_system_config` SET `parameter`='0=>一号通\r\n1=>阿里云\r\n2=>腾讯云\r\n3=>短信宝' WHERE (`id`='393');

1:打开项目:config\sms.php 新增短信宝配置

1
2
3
4
5
        'smsbao'=>[
            'smsbao_user'=>'',
            'smsbao_pwd'=>'',
            'sign_name'=>'',
        ]

2:打开项目:crmeb\services\sms\storage 新增smsbao.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace crmeb\services\sms\storage;
 
use crmeb\services\sms\BaseSms;
use crmeb\services\HttpService;
use think\exception\ValidateException;
use think\facade\Config;
use app\services\message\SystemNotificationServices;
 
class Smsbao extends BaseSms
{
 
    protected $status;
 
    /**
     * 发送模板id
     * @var array
     */
    protected $templateIds = [];
 
    /**
     * @var string
     */
    protected $accessKeyId = '';
 
    /**
     * @var string
     */
    protected $accessKeySecret = '';
 
    protected $signName = '';
 
    /**
     * @var string
     */
    protected $region = "ap-guangzhou";
 
    /**
     * @param array $config
     * @return mixed|void
     */
    protected function initialize(array $config = [])
    {
        parent::initialize($config);
        $this->accessKeyId = sys_config('smsbao_user''');
        $this->accessKeySecret = sys_config('smsbao_pwd''');
        if ($this->accessKeyId && $this->accessKeySecret) {
            $this->status = true;
        else {
            $this->status = false;
        }
        $this->signName = $config['sign_name'] ?? null;
        if($this->signName == null){
            $this->signName  = sys_config('sms_sign_name''');
        }
    }
 
    /**
     * 提取模板code
     * @param string $templateId
     * @return null
     */
    protected function getTemplateCode(string $templateId)
    {
        $notifyServices = app()->make(SystemNotificationServices::class);
        $template = $notifyServices->value([
            'sms_id' => $templateId,
            ], 'system_text') ?? 0;
        return $template ?? null;
    }
 
    /**
     * @param string $phone
     * @param string $templateId
     * @param array $data
     * @return mixed|void
     */
    public function send(string $phone, string $templateId, array $data)
    {
        $statusStr = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
 
        $smsapi = "http://api.smsbao.com/";
        $user = $this->accessKeyId;
        $pass = md5($this->accessKeySecret);
        $content = $this->getTemplateCode($templateId);
        foreach (array_keys($data) as $k=>$v){
            $search = '{'.$v.'}';
            $content = str_replace($search,$data[$v],$content);
        }
        $content = '【'.$this->signName.'】'.$content;
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result = $this->fetchContent($sendurl,'POST','') ;
        try {
            if($result == '0'){
                return true;
            }else{
                throw new ValidateException($statusStr[$result]);
            }
        }catch (\Exception $e) {
            throw new ValidateException($e->getMessage());
        }
    }
 
    private function fetchContent($url, $method, $body) {
        $ch = curl_init();
 
        if($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        else {
            $url .= '?'.$body;
        }
 
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "x-sdk-client" => "php/2.0.0"
        ));
 
        if(substr($url, 0,5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
 
        $rtn = curl_exec($ch);
 
        if($rtn === false) {
            // 大多由设置等原因引起,一般无法保障后续逻辑正常执行,
            // 所以这里触发的是E_USER_ERROR,会终止脚本执行,无法被try...catch捕获,需要用户排查环境、网络等故障
            trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
        }
        curl_close($ch);
 
        return $rtn;
    }
 
    public function open()
    {
        // TODO: Implement open() method.
    }
 
    public function modify(string $sign = null, string $phone = '', string $code = '')
    {
        // TODO: Implement modify() method.
    }
 
    public function info()
    {
        // TODO: Implement info() method.
    }
 
    public function temps(int $page, int $limit, int $type)
    {
        // TODO: Implement temps() method.
    }
 
    public function apply(string $title, string $content, int $type)
    {
        // TODO: Implement apply() method.
    }
 
    public function applys(int $tempType, int $page, int $limit)
    {
        // TODO: Implement applys() method.
    }
 
    public function record($record_id)
    {
        // TODO: Implement record() method.
    }
}

3:打开项目app\services\message\notice\SmsService.php 增加短信宝网关 大概在45行

1
2
3
4
5
    /**
     * 短信类型
     * @var string[]
     */
    private $smsType = ['yihaotong''aliyun''tencent','smsbao'];

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

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

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

开源插件

最新更新

电商类

CMS类

微信类

文章标签