ECJia移動(dòng)商城系統(tǒng)(EC+)是一款基于移動(dòng)互聯(lián)網(wǎng)的商城應(yīng)用服務(wù)產(chǎn)品,重新定義了移動(dòng)商城系統(tǒng)操作方式及用戶體驗(yàn) ,讓用戶輕松將商城開到移動(dòng)設(shè)備中,擁有執(zhí)行效率高、上手輕松、管理便捷等一系列優(yōu)點(diǎn)。只要是商城系統(tǒng)就必不可免的會(huì)用到短信,今天就教大家如何開發(fā)ECJIA到家系統(tǒng)的短信插件,短信接口使用的是我們短信寶短信群發(fā)平臺(tái),我們短信寶短信群發(fā)平臺(tái)極其穩(wěn)定,而且短信發(fā)送速度相當(dāng)快捷,驗(yàn)證碼和訂單通知在3~5秒就能收到,用戶體驗(yàn)非常好,注冊(cè)就送測(cè)試短信。
接下來我就說一下開發(fā)步驟:第一步我們要清楚目錄格式,插件是放在content/plugins目錄下,我們首先在plugins文件夾下面建立一個(gè)文件夾,名字叫sms_smsbao,在sms_smsbao文件夾下面創(chuàng)建下列文件,languages文件夾、config.php文件、sms_smsbao.class.php文件、sms_smsbao.php文件、SMSbao.php文件。languages文件夾下面需要?jiǎng)?chuàng)建zh_CN文件夾,在zh_CN文件夾下面創(chuàng)建plugin.lang.php文件。文件創(chuàng)建完成之后可以正式開發(fā)了。
第二步:打開config.php文件,我們?cè)谶@個(gè)文件中配置短信參數(shù):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php return array ( 'sms_code' => 'sms_smsbao' , 'check_balance' => true, 'forms' => array ( array ( 'name' => 'app_key' , 'type' => 'text' , 'value' => '' ), array ( 'name' => 'app_secret' , 'type' => 'text' , 'value' => '' ), array ( 'name' => 'app_sign' , 'type' => 'text' , 'value' => '' ) ), ); |
第三步:打開sms_smsbao.class.php文件,這個(gè)文件是加載短信寶短信插件的配置信息的。
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
|
<?php defined( 'IN_ECJIA' ) or exit ( 'No permission resources.' ); use Ecjia\App\Sms\SmsAbstract; class sms_smsbao extends SmsAbstract { public function setConfig( array $config ) { parent::setConfig( $config ); $this ->setAgentConfig(); $this ->agent = royalcms( 'sms' )->driver( 'smsbao' ); } public function setAgentConfig() { RC_Config::set( 'sms::sms.agents.smsbao.credentials' , [ 'appKey' => $this ->config[ 'app_key' ], 'appSecret' => $this ->config[ 'app_secret' ], 'appsign' => $this ->config[ 'app_sign' ] ]); } /** * 獲取插件代號(hào) * * @see \Ecjia\System\Plugin\PluginInterface::getCode() */ public function getCode() { return $this ->loadConfig( 'sms_code' ); } /** * 加載配置文件 * * @see \Ecjia\System\Plugin\PluginInterface::loadConfig() */ public function loadConfig( $key = null, $default = null) { return $this ->loadPluginData(RC_Plugin::plugin_dir_path( __FILE__ ) . 'config.php' , $key , $default ); } /** * 加載語(yǔ)言包 * * @see \Ecjia\System\Plugin\PluginInterface::loadLanguage() */ public function loadLanguage( $key = null, $default = null) { $locale = RC_Config::get( 'system.locale' ); return $this ->loadPluginData(RC_Plugin::plugin_dir_path( __FILE__ ) . '/languages/' . $locale . '/plugin.lang.php' , $key , $default ); } } |
第四步:打開sms_smsbao.php文件,這個(gè)文件是作為安裝卸載及使用插件的文件。
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
|
<?php /* Plugin Name: 短信寶短信 Plugin URI: http://www.gjrencai.com Description: 使用短信寶短信渠道,發(fā)送驗(yàn)證碼短信、訂單通知等。 Author: ECJIA TEAM Version: 1.0.0 Author URI: http://www.ecjia.com/ Plugin App: sms */ defined( 'IN_ECJIA' ) or exit ( 'No permission resources.' ); class plugin_sms_smsbao { public static function install() { $config = include (RC_Plugin::plugin_dir_path( __FILE__ ) . 'config.php' ); $param = array ( 'file' => __FILE__ , 'config' => $config ); return RC_Api::api( 'sms' , 'plugin_install' , $param ); } public static function uninstall() { $config = include (RC_Plugin::plugin_dir_path( __FILE__ ) . 'config.php' ); $param = array ( 'file' => __FILE__ , 'config' => $config ); return RC_Api::api( 'sms' , 'plugin_uninstall' , $param ); } public static function royalcms_sms_agent_filter( $factories ) { require_once RC_Plugin::plugin_dir_path( __FILE__ ) . 'SMSbao.php' ; $factories [ 'smsbao' ] = 'SMSbao' ; return $factories ; } } Ecjia_PluginManager::extend( 'sms_smsbao' , function () { require_once RC_Plugin::plugin_dir_path( __FILE__ ) . 'sms_smsbao.class.php' ; return new sms_smsbao(); }); RC_Plugin::register_activation_hook( __FILE__ , array ( 'plugin_sms_smsbao' , 'install' )); RC_Plugin::register_deactivation_hook( __FILE__ , array ( 'plugin_sms_smsbao' , 'uninstall' )); RC_Hook::add_filter( 'royalcms_sms_agent_filter' , array ( 'plugin_sms_smsbao' , 'royalcms_sms_agent_filter' )); |
第五步:打開SMSbao.php文件,這個(gè)文件是短信發(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
143
|
<?php use Royalcms\Component\Support\Arr; use Royalcms\Component\Sms\Sms; use Royalcms\Component\Sms\Contracts\SmsAgent; class SMSbao extends Sms implements SmsAgent { private $appKey ; private $appSecret ; private $appSign ; private $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)容含有敏感詞" ); public function __construct( $config ) { $this ->config = $config ; $this ->transformConfig(); } public function transformConfig() { $credentials = Arr::pull( $this ->config, 'credentials' ); $this ->appKey = Arr::pull( $credentials , 'appKey' ); $this ->appSecret = Arr::pull( $credentials , 'appSecret' ); $this ->appSign = Arr::pull( $credentials , 'appsign' ); } protected function authParams() { return [ 'u' => $this ->appKey, 'p' => $this ->appSecret, 'k' => $this ->appSign ]; } /** * 發(fā)送信息 * * @see \Royalcms\Component\Sms\Contracts\SmsAgent::send() */ public function send( $mobile ) { $url = self::HOST. 'u=' . $this ->appKey. '&p=' .md5( $this ->appSecret). '&m=' . $mobile . '&c=【' . $this ->appSign. '】' . $this ->content; $ret = file_get_contents ( $url ); return $ret ; } /** * 查詢賬戶余額 */ public function balance() { $ret = file_get_contents ( $url ); $rest = explode ( "," , $ret ); $res [ 'data' ][ 'num' ] = $rest [ '1' ]; return $res ; } /** * @param $url * @param array $body * @return array $result * @return int $result[].code 返回0則成功,返回其它則錯(cuò)誤 * @return string $result[].msg 返回消息 * @return string $result[].raw 接口返回的原生信息 * @return array $result[].data 數(shù)據(jù)信息 */ public function httpRequest( $url , array $body ) { $data = [ 'body' => $body ]; $response = $this ->sendWithRetry( $url , $data ); $result = $this ->transformerResponse( $response ); return $result ; } /** * 轉(zhuǎn)換返回的信息處理 * @param array $response * @return array $result * @return int $result[].code 返回0則成功,返回其它則錯(cuò)誤 * @return string $result[].msg 返回消息 * @return string $result[].raw 接口返回的原生信息 * @return array $result[].data 數(shù)據(jù)信息 */ public function transformerResponse( $response ) { $body = $response [ 'body' ]; $result_arr = RC_Xml::to_array( $body ); $data = array (); if (isset( $result_arr [ 'smsid' ])) { $data [ 'smsid' ] = $result_arr [ 'smsid' ][0]; $data [ 'msgid' ] = $result_arr [ 'smsid' ][0]; } if (isset( $result_arr [ 'num' ])) { $data [ 'num' ] = $result_arr [ 'num' ][0]; } $result = [ 'raw' => $body , 'data' => $data , 'code' => $result_arr [ 'code' ][0], 'description' => $result_arr [ 'msg' ][0], ]; if ( $result [ 'code' ] != '2' ) { return new ecjia_error( 'ihuyi_error_' . $result [ 'code' ], $result [ 'description' ], $result ); } return $result ; } } |
第六步:找到項(xiàng)目/wendor/royalcms/sms/config/sms.php,在agents中添加
1
2
3
4
5
6
7
8
|
'smsbao' => [ 'credentials' => [ 'appKey' => env( 'SMSBAO_APPKEY' ), 'appSecret' => env( 'SMSBAO_APPSECRET' ), 'appsign' => env( 'SMSBAO_APPSIGN' ) ], 'executableFile' => 'SMSbao' , ], |
第七步:在項(xiàng)目/vendor/royalcms/sms/Royalcms/Component/Sms/Agents下新建一個(gè)文件,取名為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
|
<?php namespace Royalcms\Component\Sms\Agents; use Royalcms\Component\Support\Arr; use Royalcms\Component\Sms\Sms; use Royalcms\Component\Sms\Contracts\SmsAgent; class SMSbao extends Sms implements SmsAgent { private $appKey ; private $appSecret ; private $appSign ; private $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)容含有敏感詞" ); public function __construct( $config ) { $this ->config = $config ; $this ->transformConfig(); } public function transformConfig() { $credentials = Arr::pull( $this ->config, 'credentials' ); $this ->appKey = Arr::pull( $credentials , 'appKey' ); $this ->appSecret = Arr::pull( $credentials , 'appSecret' ); } protected function authParams() { return [ 'u' => $this ->appKey, 'p' => $this ->appSecret, 'k' => $this ->appSign ]; } /** * 發(fā)送信息 * * @see \Royalcms\Component\Sms\Contracts\SmsAgent::send() */ public function send( $mobile ) { $url = self::HOST; $requestParams = array ( 'content' => $this ->content, 'mobile' => $mobile , ); $requestParams = array_merge ( $this ->authParams(), $requestParams ); return $this ->httpRequest( $url , $requestParams ); } /** * 查詢賬戶余額 */ public function balance() { $ret = file_get_contents ( $url ); $retArr = split( "\n" , $ret ); $balanceArr = split( "," , $retArr [1]); $rest = $retArr [0] == 0 ? $balanceArr [1] : $ret ; return $rest ; } /** * @param $url * @param array $body * @return array $result * @return int $result[].code 返回0則成功,返回其它則錯(cuò)誤 * @return string $result[].msg 返回消息 * @return string $result[].raw 接口返回的原生信息 * @return array $result[].data 數(shù)據(jù)信息 */ public function httpRequest( $url , array $body ) { $data = [ 'body' => $body ]; $response = $this ->sendWithRetry( $url , $data ); $result = $this ->transformerResponse( $response ); return $result ; } /** * 轉(zhuǎn)換返回的信息處理 * @param array $response * @return array $result * @return int $result[].code 返回0則成功,返回其它則錯(cuò)誤 * @return string $result[].msg 返回消息 * @return string $result[].raw 接口返回的原生信息 * @return array $result[].data 數(shù)據(jù)信息 */ public function transformerResponse( $response ) { $body = $response [ 'body' ]; $result_arr = RC_Xml::to_array( $body ); $data = array (); if (isset( $result_arr [ 'smsid' ])) { $data [ 'smsid' ] = $result_arr [ 'smsid' ][0]; $data [ 'msgid' ] = $result_arr [ 'smsid' ][0]; } if (isset( $result_arr [ 'num' ])) { $data [ 'num' ] = $result_arr [ 'num' ][0]; } $result = [ 'raw' => $body , 'data' => $data , 'code' => $result_arr [ 'code' ][0], 'description' => $result_arr [ 'msg' ][0], ]; if ( $result [ 'code' ] != '2' ) { return new ecjia_error( 'ihuyi_error_' . $result [ 'code' ], $result [ 'description' ], $result ); } return $result ; } } |
經(jīng)過上面的替換,短信寶的短信平臺(tái)已經(jīng)替換成功了,可以正常使用了。進(jìn)行測(cè)試發(fā)送:
報(bào)備一下短信寶的VIP模版,這樣就可以走短信寶的優(yōu)質(zhì)通道,并且免審核了,短信內(nèi)容3~5秒就可送達(dá)。
最新更新
電商類
CMS類
微信類