CRMEB商城系統(tǒng)是基于ThinkPhp6.0+Vue開發(fā)的一套新零售移動(dòng)電商開源系統(tǒng),包含商城、拼團(tuán)、砍價(jià)、秒殺、優(yōu)惠券、積分、分銷等功能,更適合企業(yè)二次開發(fā)。今天小編就以新增短信接口為例,給大家講解一下如何進(jìn)行二次開發(fā),使用的短信接口是我們短信寶短信群發(fā)平臺(tái)的短信接口,我們短信寶短信群發(fā)平臺(tái)的接口非常穩(wěn)定,發(fā)送速度快,注冊(cè)就送測(cè)試短信,推薦大家使用。
前提:執(zhí)行以下sql語(yǔ)句增加短信寶配置
|
1
2
3
4
5
6
7
|
INSERT INTO `eb_system_config_tab` (`id`, `pid`, `title`, `eng_title`, `status`, `info`, `icon`, `type`, `sort`) VALUES ('100', '0', '短信配置', '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', '100', '', 0, '', '100', 0, '', '短信寶賬號(hào)', '短信寶賬號(hào)', '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', '100', '', 0, '', '100', 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', '100', '', 0, '', '100', 0, '', '短信簽名', '短信簽名', '0', '1'); |
1:打開項(xiàng)目:config\sms.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
|
'smsbao'=>[ 'template_id' => [ //驗(yàn)證碼 'VERIFICATION_CODE_TIME' => '您的驗(yàn)證碼是code,有效期為time分鐘。如非本人操作,可不用理會(huì)。', //支付成功 'PAY_SUCCESS_CODE' => '您購(gòu)買的商品已支付成功,支付金額pay_price元,訂單號(hào)order_id,感謝您的光臨!', //發(fā)貨提醒 'DELIVER_GOODS_CODE' => '親愛的用戶nickname您的商品store_name,訂單號(hào)order_id已發(fā)貨,請(qǐng)注意查收', //確認(rèn)收貨提醒 'TAKE_DELIVERY_CODE' => '親,您的訂單order_id,商品store_name已確認(rèn)收貨,感謝您的光臨!', //管理員下單提醒 'ADMIN_PLACE_ORDER_CODE' => 'admin_name管理員,您有一筆已支付的訂單待處理,訂單號(hào)為order_id!', //管理員退貨提醒 'ADMIN_RETURN_GOODS_CODE' => 'admin_name管理員,您有一筆支付成功的訂單待處理,訂單號(hào)order_id!', //管理員支付成功提醒 'ADMIN_PAY_SUCCESS_CODE' => 'admin_name管理員,您有一筆支付成功的訂單待處理,訂單號(hào)order_id!', //管理員確認(rèn)收貨 'ADMIN_TAKE_DELIVERY_CODE' => 'admin_name管理員,您有一筆訂單已經(jīng)確認(rèn)收貨,訂單號(hào)order_id!', //改價(jià)提醒 'PRICE_REVISION_CODE' => '您的訂單order_id,實(shí)際支付金額已被修改為pay_price', //訂單未支付 'ORDER_PAY_FALSE' => '您有未付款訂單,訂單號(hào)為:order_id,商品數(shù)量有限,請(qǐng)及時(shí)付款。', ], ] |
2:打開項(xiàng)目:crmeb\services\sms\Sms.php 屏蔽掉invokeClass方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// protected function invokeClass($class) // { // if (!class_exists($class)) { // throw new \RuntimeException('class not exists: ' . $class); // } // $this->getConfigFile(); // if (!$this->config) { // $this->config = Config::get($this->configFile . '.stores.' . $this->name, []); // } // $handleAccessToken = new AccessTokenServeService($this->config['account'] ?? '', $this->config['secret'] ?? ''); // $handle = Container::getInstance()->invokeClass($class, [$this->name, $handleAccessToken, $this->configFile]); // $this->config = []; // return $handle; // } |
3:打開項(xiàng)目: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
|
<?phpnamespace crmeb\services\sms\storage;use crmeb\basic\BaseSms;use think\exception\ValidateException;use think\facade\Config;class SmsBao extends BaseSms{ /** * accessKeyId * @var string */ protected $accessKeyId; /** * accessKeySecret * @var string */ protected $accessKeySecret; /** * 簽名 * @var string */ protected $signName = ''; protected $name = 'smsbao'; /** * 狀態(tài) * @var bool */ protected $status; protected $templateIds = []; 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', ''); } $this->templateIds = Config::get($this->configFile . '.stores.' . $this->name . '.template_id', []); } /** * 提取模板code * @param string $templateId * @return null */ protected function getTemplateCode(string $templateId) { return $this->templateIds[$templateId] ?? null; } /** * 發(fā)送短信 * @param $phone * @param $templateId * @param $data * @return bool|string */ public function send($phone, $templateId, array $data = []) { $statusStr = array( "0" => "短信發(fā)送成功", "-1" => "參數(shù)不全", "-2" => "服務(wù)器空間不支持,請(qǐng)確認(rèn)支持curl或者fsocket,聯(lián)系您的空間商解決或者更換空間!", "30" => "密碼錯(cuò)誤", "40" => "賬號(hào)不存在", "41" => "余額不足", "42" => "帳戶已過(guò)期", "43" => "IP地址限制", "50" => "內(nèi)容含有敏感詞" ); $user = $this->accessKeyId; $pass = md5($this->accessKeySecret); $tmp = $this->getTemplateCode($templateId); $content = str_replace(array_keys($data),array_values($data),$tmp); $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) { // 大多由設(shè)置等原因引起,一般無(wú)法保障后續(xù)邏輯正常執(zhí)行, // 所以這里觸發(fā)的是E_USER_ERROR,會(huì)終止腳本執(zhí)行,無(wú)法被try...catch捕獲,需要用戶排查環(huán)境、網(wǎng)絡(luò)等故障 trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR); } curl_close($ch); return $rtn; }} |
4:打開項(xiàng)目:app\services\serve\ServeServices.php 修改驅(qū)動(dòng)配置 大概在60行左右
|
1
2
3
4
5
6
7
8
9
10
|
public function sms(array $config = []) { return app()->make(Sms::class, [ 'smsbao', [ 'accessKeyId' => sys_config('smsbao_user'), 'accessKeySecret' => sys_config('smsbao_pwd'), 'signName' => sys_config('sms_sign_name'), ]]); } |
經(jīng)過(guò)上面的替換,短信寶的短信平臺(tái)已經(jīng)替換成功了,可以正常使用了。進(jìn)行測(cè)試發(fā)送:
報(bào)備一下短信寶的VIP模板,這樣就可以走短信寶的優(yōu)質(zhì)通道了,即便遇到敏感文字我們都不會(huì)人工審核,短信內(nèi)容3~5秒就可送達(dá)。
另外:我們已經(jīng)開發(fā)好完整的CRMEB_V4.3.2系統(tǒng)短信寶插件,點(diǎn)擊此鏈接?下載及查看安裝流程。
最新更新
電商類
CMS類
微信類