NIUSHOP V6 使用NIUCLOUD-ADMIN底層框架設(shè)計(jì), 國內(nèi)首家唯一支持TP8框架 ,前端采用市面最流行的技術(shù)棧 Vite+TypeScript+Vue3+ElementPlus ,后端采用 THINKPHP8、PHP8 語言搭建。今天小編以新增短信接口為例告訴大家如何進(jìn)行二次開發(fā),使用的短信接口是我們短信寶短信群發(fā)平臺(tái)的接口,我們短信寶短信群發(fā)平臺(tái)非常穩(wěn)定,發(fā)送速度快,注冊(cè)就送測(cè)試短信,推薦大家使用。
下面具體給大家說一下每個(gè)文件的作用及代碼。
1:在niucloud\app\dict\sys\SmsDict.php大約25行左右增加短信寶枚舉類,代碼如下:
|
1
2
|
//短信寶短信 public const SMSBAO = 'smsbao'; |
接著在67行左右增加短信寶配置參數(shù),代碼如下:
|
1
2
3
4
5
6
7
8
9
10
11
|
self::SMSBAO => [ 'name' => '短信寶', //配置參數(shù) 'params' => [ 'sign' => '短信簽名', 'username' => '短信寶賬號(hào)', 'secret_key' => 'APP_KEY' ], 'encrypt_params' => [], 'component' => '/src/app/views/setting/components/sms-smsbao.vue', ], |
2:在niucloud\core\sms 目錄下 新增Smsbao.php 短信寶核心發(fā)送短信類
|
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
|
<?phpnamespace core\sms;use core\exception\NoticeException;use Exception;class Smsbao extends BaseSms{ protected $username = ''; protected $secret_key = ''; protected $sign = ''; protected $statusStr = array( "0" => "短信發(fā)送成功", "-1" => "參數(shù)不全", "-2" => "服務(wù)器空間不支持,請(qǐng)確認(rèn)支持curl或者fsocket,聯(lián)系您的空間商解決或者更換空間!", "30" => "密碼錯(cuò)誤", "40" => "賬號(hào)不存在", "41" => "余額不足", "42" => "帳戶已過期", "43" => "IP地址限制", "50" => "內(nèi)容含有敏感詞" ); /** * @param array $config * @return void */ protected function initialize(array $config = []) { parent::initialize($config); $this->username = $config[ 'username' ] ?? ''; $this->secret_key = $config[ 'secret_key' ] ?? ''; $this->sign = $config[ 'sign' ] ?? ''; } /** * 發(fā)送短信 * @param string $mobile * @param string $template_id * @param array $data * @return array */ public function send(string $mobile, string $template_id, array $data = []) { try { $search = array_map(function($key) { return '{' . $key . '}'; // 將鍵轉(zhuǎn)換為 {key} 形式 }, array_keys($data)); $replace = array_values($data); // 替換內(nèi)容為數(shù)組的值 // 執(zhí)行替換 $content = str_replace($search, $replace, $template_id); $url = $this->smsapi.'?u='.$this->username.'&p='.$this->secret_key.'&m='.$mobile.'&c=【'.$this->sign.'】'.$content; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30 ); $response = curl_exec($ch); curl_close($ch); throw new NoticeException($this->statusStr[$response]); } catch (Exception $e) { throw new NoticeException($e->getMessage()); } } public function modify(string $sign, string $mobile, string $code) { } public function template(int $page = 0, int $limit = 10, int $type = 1) { } public function apply(string $title, string $content, int $type) { } public function localTemplate(int $type, int $page, int $limit) { } public function record($id) { }} |
3:在admin\src\app\lang\zh-cn\setting.sms.json 文件中增加前端國際化變量參數(shù)
|
1
2
3
4
5
6
7
8
9
|
{ "smsbao":"短信寶", "smsbaoSign":"短信簽名", "smsbaoUsername": "短信寶賬號(hào)", "smsbaoApiKey": "API_KEY", "smsbaoSignPlaceholder":"請(qǐng)輸入短信簽名", "smsbaoUsernamePlaceholder":"請(qǐng)輸入短信寶賬號(hào)", "smsbaoApiKeyPlaceholder":"請(qǐng)輸入API_KEY"} |
4:在admin\src\app\views\setting\components 目錄下新增 sms-smsbao.vue 短信寶配置文件
好了經(jīng)過以上的添加,短信寶的短信平臺(tái)已經(jīng)替換成功了,可以正常使用了

報(bào)備一下短信寶的VIP模板,這樣就可以走短信寶的優(yōu)質(zhì)通道了,即便遇到敏感文字我們都不會(huì)人工審核,短信內(nèi)容3~5秒就可送達(dá)。
另外:我們已經(jīng)開發(fā)好完整的niushopV6版系統(tǒng)短信寶插件,點(diǎn)擊此鏈接 下載及查看安裝流程。
最新更新
電商類
CMS類
微信類