likeadmin是一款能快速開發通用管理后臺的開源程序。基于Vue3、elementPlus,結合PHP、Java、Python、Go、NodeJS等主流后端語言搭建,集成用戶權限、代碼生成器、表單設計、崗位部門、云存儲、素材中心、微信配置、API模塊等一系列開箱即用功能。小編對他還是比較了解的,今天小編就以新增短信接口為例,給大家講解一下如何進行二次開發,我們今天講解的是v1.4.2版本,使用的短信接口是我們短信寶短信群發平臺的短信接口,我們短信寶短信群發平臺的接口非常穩定,發送速度快,注冊就送測試短信,推薦大家使用。
1:打開項目: app\adminapi\logic\notice\SmsConfigLogic.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
|
public static function getConfig() { $config = [ ConfigService::get( 'sms' , 'ali' , [ 'type' => 'ali' , 'name' => '短信寶短信' , 'status' => 1]), ConfigService::get( 'sms' , 'tencent' , [ 'type' => 'tencent' , 'name' => '騰訊云短信' , 'status' => 0]), ]; return $config; } public static function setConfig($params) { $type = $params[ 'type' ]; $params[ 'name' ] = self::getNameDesc(strtoupper($type)); ConfigService::set( 'sms' , $type, $params); $ default = ConfigService::get( 'sms' , 'engine' , false ); if ($params[ 'status' ] == 1 && $ default === false ) { // 啟用當前短信配置 并 設置當前短信配置為默認 ConfigService::set( 'sms' , 'engine' , strtoupper($type)); return true ; } if ($params[ 'status' ] == 1 && $ default != strtoupper($type)) { // 找到默認短信配置 $defaultConfig = ConfigService::get( 'sms' , strtolower($ default )); // 狀態置為禁用 并 更新 $defaultConfig[ 'status' ] = 0; ConfigService::set( 'sms' , strtolower($ default ), $defaultConfig); // 設置當前短信配置為默認 ConfigService::set( 'sms' , 'engine' , strtoupper($type)); return true ; } } public static function getNameDesc($value) { $desc = [ 'ALI' => '短信寶短信' , 'TENCENT' => '騰訊云短信' , ]; return $desc[$value] ?? '' ; } |
2:打開項目: app\common\service\sms\engine\AliSms.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
|
protected $content; public function setContent($content) { $ this ->content = $content; return $ this ; } public function send() { try { $statusStr = array( "0" => "短信發送成功" , "-1" => "參數不全" , "-2" => "服務器空間不支持,請確認支持curl或者fsocket,聯系您的空間商解決或者更換空間!" , "30" => "密碼錯誤" , "40" => "賬號不存在" , "41" => "余額不足" , "42" => "帳戶已過期" , "43" => "IP地址限制" , "50" => "內容含有敏感詞" ); $user = $ this ->config[ 'app_key' ]; //短信平臺帳號 $pass = md5($ this ->config[ 'secret_key' ]); //短信平臺密碼 $phone = $ this ->mobile; //要發送短信的手機號碼 $content = '【' .$ this ->config[ 'sign' ]. '】' .$ this ->content; $sendurl = $smsapi. "sms?u=" .$user. "&p=" .$pass. "&m=" .$phone. "&c=" .urlencode($content); $result =file_get_contents($sendurl); if ($result == '0' ) { return [ 'code' =>0 ]; } throw new \Exception( '發送失敗:' . $statusStr[$result]); } catch (\Exception $e) { $ this ->error = $e->getMessage(); return false ; } } |
3:打開項目:app\common\service\sms\SmsDriver.php 修改短信驅動中的send函數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public function send($mobile, $data) { try { // 發送頻率限制 $ this ->sendLimit($mobile); // 開始發送 $result = $ this ->engine ->setMobile($mobile) ->setTemplateId($data[ 'template_id' ]) ->setTemplateParams($data[ 'params' ]) ->setContent($data[ 'content' ]) ->send(); if ( false === $result) { throw new \Exception($ this ->engine->getError()); } return $result; } catch (\Exception $e) { $ this ->error = $e->getMessage(); return false ; } } |
4:打開項目:app\common\service\sms\SmsMessageService.php 修改短信服務類的send函數
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
|
public function send($params) { try { // 通知設置 $noticeSetting = NoticeSetting::where( 'scene_id' , $params[ 'scene_id' ])->findOrEmpty()->toArray(); // 添加短信記錄 $content = $ this ->contentFormat($noticeSetting, $params); $ this ->smsLog = $ this ->addSmsLog($params, $content); // 添加通知記錄 $ this ->notice = NoticeLogic::addNotice($params, $noticeSetting, NoticeEnum::SMS, $content); // 發送短信 $smsDriver = new SmsDriver(); if (!is_null($smsDriver->getError())) { throw new \Exception($smsDriver->getError()); } $result = $smsDriver->send($params[ 'params' ][ 'mobile' ], [ 'template_id' => $noticeSetting[ 'sms_notice' ][ 'template_id' ], 'params' => $ this ->setSmsParams($noticeSetting, $params), 'content' =>$content ]); if ($result === false ) { // 發送失敗更新短信記錄 $ this ->updateSmsLog($ this ->smsLog[ 'id' ], SmsEnum::SEND_FAIL, $smsDriver->getError()); throw new \Exception($smsDriver->getError()); } // 發送成功更新短信記錄 $ this ->updateSmsLog($ this ->smsLog[ 'id' ], SmsEnum::SEND_SUCCESS, $result); return true ; } catch (\Exception $e) { throw new \Exception($e->getMessage()); } } |
好了經過以上的添加,短信寶的短信平臺已經替換成功了,可以正常使用了
報備一下短信寶的VIP模板,這樣就可以走短信寶的優質通道了,即便遇到敏感文字我們都不會人工審核,短信內容3~5秒就可送達。
另外:我們已經開發好完整的likeadmin_1.4.2系統短信寶插件,點擊此鏈接 下載及查看安裝流程。
最新更新
電商類
CMS類
微信類