shopwind是一款免費(fèi)開源的輕量級電商系統(tǒng)。穩(wěn)定的架構(gòu)內(nèi)核,基于Yii2深度開發(fā),代碼開源,遵循Apache2.0協(xié)議,完善的開發(fā)文檔,功能模塊化,拒絕臃腫繁雜,源碼易讀方便二次開發(fā),豐富的API接口,數(shù)據(jù)通訊安全加簽,可以滿足任何功能插件開發(fā)的需要。小編對他還是比較了解的,今天小編就以新增短信接口為例,給大家講解一下如何進(jìn)行二次開發(fā),我們今天講解的是v3.4.3版本,使用的短信接口是我們短信寶短信群發(fā)平臺的短信接口,我們短信寶短信群發(fā)平臺的接口非常穩(wěn)定,發(fā)送速度快,注冊就送測試短信,推薦大家使用
1:打開 shopwind/common/plugins/sms 新增短信寶smsbao目錄
插件的目錄結(jié)構(gòu)如下:
├─smsbao插件目錄
│ ├─plugin.info.php 短信插件配置信息
│ ├─SDK.php 繼承系統(tǒng)短信基類
│ ├─smsbao.plugin.php 短信寶核心發(fā)送短信類
1:下面具體給大家說一下每個文件的作用及代碼,plugin.info.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
|
<?php /** ? *?@link?https://www.shopwind.net/ ? *?@copyright?Copyright?(c)?2018?ShopWind?Inc.?All?Rights?Reserved. ? * ? *?This?is?not?free?software.?Do?not?use?it?for?commercial?purposes.? ? *?If?you?need?commercial?operation,?please?contact?us?to?purchase?a?license. ? *?@license?https://www.shopwind.net/license/ ? */ namespace?common\plugins\sms\smsbao; use?yii; use?yii\helpers\Url; use?common\library\Language; /** ? *?@Id?plugin.info.php?2018.8.3?$ ? */ return ?array( ???? 'code' ?=>? 'smsbao' , ???? 'name' ?=>? '短信寶' , ???? 'desc' ?=>? '短信寶短信插件' , ???? 'author' ?=>? 'smsbao' , ???? 'version' ?=>? '1.0' , ???? 'buttons' ?=>?array( ???????? array( ???????????? 'label' ?=>?Language::get( 'manage' ), ???????????? 'url' ?=>?Url::toRoute([ 'msg/index' ]) ???????? ) ???? ), ???? 'config' ?=>?array( ???????? 'uid' ?=>?array( ???????????? 'type' ?=>? 'text' , ???????????? 'text' ?=>? '短信寶賬號' ???????? ), ???????? 'key' ?=>?array( ???????????? 'type' ?=>? 'text' , ???????????? 'text' ?=>? '短信寶密碼' ???????? ), ???????? 'scene' ?=>?array( ???????????? 'name' ?=>? 'config[scene]' , ???????????? 'type' ?=>? 'checkbox' , ???????????? 'text' ?=>? '啟用場景' , ???????????? 'items' ?=>?array( ???????????????? 'register' ?=>? '用戶注冊' , ???????????????? 'find_password' ?=>? '找回密碼' ???????????? ) ???????? ) ???? ) ); |
2:SDK.php 為繼承系統(tǒng)短信基類文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php /** ? *?@link?https://www.shopwind.net/ ? *?@copyright?Copyright?(c)?2018?ShopWind?Inc.?All?Rights?Reserved. ? * ? *?This?is?not?free?software.?Do?not?use?it?for?commercial?purposes.? ? *?If?you?need?commercial?operation,?please?contact?us?to?purchase?a?license. ? *?@license?https://www.shopwind.net/license/ ? */ namespace?common\plugins\sms\smsbao; use?yii; use?yii\base\InvalidConfigException; /** ? */ class?SDK?{ } |
3:smsbao.plugin.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
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
|
<?php /** ? *?@link?https://www.shopwind.net/ ? *?@copyright?Copyright?(c)?2018?ShopWind?Inc.?All?Rights?Reserved. ? * ? *?This?is?not?free?software.?Do?not?use?it?for?commercial?purposes.? ? *?If?you?need?commercial?operation,?please?contact?us?to?purchase?a?license. ? *?@license?https://www.shopwind.net/license/ ? */ namespace?common\plugins\sms\smsbao; use?yii; use?common\models\MsgTemplateModel; use?common\library\Basewind; use?common\library\Language; use?common\plugins\BaseSms; /** ? */ class?Smsbao?extends?BaseSms { ???? /** ???? *?網(wǎng)關(guān)地址 ???? *?@var?string?$gateway ???? */ ??? /** ????? *?短信實(shí)例 ???? *?@var?string?$code ???? */ ???? protected?$code?=? 'smsbao' ; ???? protected?$smbaostatus?=?[ ???????????????? "0" ?=>? "短信發(fā)送成功" , ???????????????? "-1" ?=>? "參數(shù)不全" , ???????????????? "-2" ?=>? "服務(wù)器空間不支持,請確認(rèn)支持curl或者fsocket,聯(lián)系您的空間商解決或者更換空間!" , ???????????????? "30" ?=>? "密碼錯誤" , ???????????????? "40" ?=>? "賬號不存在" , ???????????????? "41" ?=>? "余額不足" , ???????????????? "42" ?=>? "帳戶已過期" , ???????????????? "43" ?=>? "IP地址限制" , ???????????????? "50" ?=>? "內(nèi)容含有敏感詞" ???? ]; ???? /** ???? *?發(fā)送短信 ????? *?@param?bool?$valid?發(fā)送頻次等的校驗(yàn),如果是系統(tǒng)發(fā)送的短信,可以適當(dāng)?shù)牟蛔鲈撔r?yàn)以確保發(fā)送成功 ???? */ ??? public? function ?send($valid?=? true ) ??? { ???????? if (!$ this ->verify())?{ ???????????? return ?false ; ???????? } ???????? if ($valid?===? true ?&&?!$ this ->validSend())?{ ???????????? return ?false ; ???????? } ???????? //?發(fā)送的短信信息校驗(yàn) ???????? if ($ this ->validData()?==? false )?{ ???????????? return ?false ; ???????? } ???????? ???????? //?等于0說明發(fā)送成功,$result即為發(fā)送的短信條數(shù)(當(dāng)短信內(nèi)容過長,會分成2條來發(fā)送) ???????? $result?=?$ this ->submit(); ???????? if ($result?==? '0' )?{ ???????????? $codekey?=?$ this ->insert(1); ???????????? return ?$codekey; ???????? } ???????? $ this ->errors?=?$ this ->smbaostatus[$result]; ???????? $ this ->insert(0,?$ this ->errors); ???????? return ?false ; ???? } ???? /** ????? *?測試短信發(fā)送(僅做測試用,不要用作正式場合) ????? */ ???? public? function ?testsend($content?=? '' )?{ ???????? $ this ->content?=?$content; ???????? $result?=?$ this ->submit(); ???????? if ($result?>?0)?{ ???????????? return ?true ; ???????? } ???????? $ this ->errors?=?$ this ->getMessage($result); ???????? return ?false ; ???? } ???? ???? /** ????? *?執(zhí)行短信發(fā)生 ????? */ ???? private? function ?submit() ???? { ???????? $sendurl?=?$ this ->gateway. "sms?u=" .$ this ->config[ 'uid' ]. "&p=" .md5($ this ->config[ 'key' ]). "&m=" .$ this ->receiver. "&c=" .urlencode( '【' .$ this ->signName. '】' .$ this ->content); ???????? return ?Basewind::curl($sendurl); ???? } ???? /** ????? *?檢測是否配置 ????? *?@var?boolean?$force?是否驗(yàn)證短信模板內(nèi)容 ????? */ ???? public? function ?verify($force?=? true ) ???? { ???????? if (!$ this ->config[ 'uid' ])?{ ???????????? $ this ->errors?=? '短信設(shè)置錯誤' ; ???????????? return ?false ; ???????? } ???????? if (!$ this ->config[ 'key' ])?{ ???????????? $ this ->errors?=? '短信設(shè)置錯誤' ; ???????????? return ?false ; ???????? } ???????? //?如果是驗(yàn)證非具體短信創(chuàng)建,可以不用驗(yàn)證短信模板 ???????? //?比如某個地方僅僅需要判斷密鑰是否配置,從而進(jìn)行開關(guān)控制 ???????? if (!$force)?{ ???????????? return ?true ; ???????? } ???????? //?傳遞具體短信場景參數(shù),則驗(yàn)證短信模板 ???????? if (($template?=?$ this ->getTemplate())?===? false )?{ ???????????? return ?false ; ???????? } ???????? if (!$template?||?empty($template->content))?{ ???????????? $ this ->errors?=?Language::get( 'The?"content"?property?must?be?set' ); ???????????? return ?false ; ???????? } ???????? if (empty($template->signName))?{ ???????????? $ this ->errors?=?Language::get( 'The?"signName"?property?must?be?set' ); ???????????? return ?false ; ???????? } ???????? //?此處為必須賦值,避免無法發(fā)送短信。ID和簽名該短信接口不需要賦值 ???????? //$this->templateId?=?$template->templateId; ???????? $ this ->signName?=?$template->signName; ???????? $ this ->content?=?$ this ->getContent($template); ???????? return ?true ; ???? } ???? public? function ?getTemplate()? ??? { ???????? if (($template?=?parent::getTemplate()))?{ ???????????? return ?$template; ???????? } ???????? //?有異常(非空的情況) ???????? if ($template?===? false )?{ ???????????? return ?false ; ???????? } ???????? ???????? //?使用其他平臺的短信模板 ?????? $query?=?MsgTemplateModel::find()->where([ 'and' ,?[ 'scene' ?=>?$ this ->scene],?[ '!=' ,? 'content' ,? '' ]])->orderBy([ 'id' ?=>?SORT_DESC])->one(); ???????? return ?$query???$query?:? null ; ???? } } |
經(jīng)過上面的替換,短信寶的短信平臺已經(jīng)替換成功了,可以正常使用了。進(jìn)行測試發(fā)送:
報(bào)備一下短信寶的VIP模板,這樣就可以走短信寶的優(yōu)質(zhì)通道了,即便遇到敏感文字我們都不會人工審核,短信內(nèi)容3~5秒就可送達(dá)。
另外:我們已經(jīng)開發(fā)好完整的shopwindV3.4.3系統(tǒng)短信寶插件,點(diǎn)擊此鏈接?下載及查看安裝流
最新更新
電商類
CMS類
微信類