likeshop是一款开源的企业级B2C电商系统,前端uni-app架构,极致节省开发成本,一次研发,多端发布。后端ThinkPHP架构,国内最流行的PHP研发框架,易用易学,好二开。小编对他还是比较了解的,今天小编就以likeshop_3.0.5新增短信接口为例,给大家讲解一下如何进行二次开发,使用的短信接口是我们短信宝短信群发平台的短信接口,我们短信宝短信群发平台的接口非常稳定,发送速度快,注册就送测试短信,推荐大家使用。
前提:需先执行以下sql
|
1
|
INSERT INTO `ls_sms_config` ( `name`, `describe`, `sign`, `app_key`, `secret_key`, `status`, `del`) VALUES ('短信宝', '短信宝短信服务', '', '', '', '0', '0'); |
1:打开项目:application\common\behavior\SmsSend.php 替换run方法
|
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
|
public function run($params) { try{ // 设置短信参数 $params['scene'] = $params['key']; $this->sms_key = $params['key']; $this->sms_params = $params['params']; $this->mobile = $params['mobile']; $result = $this->getSmsConfig($params['key']); if ($result !== true) return $result; $this->sms_log = new SmsLog(); $this->setSmsContent(); //短信内容 $this->setSmsCode(); //短信验证码 $this->createSmsLog(); //增加短信记录 //增加通知记录 $this->notice_id = NoticeLogic::addNoticeLog($params, $this->sms, NoticeSetting::SMS_NOTICE, $this->content); if ($this->channel == 1) { // 阿里云短信 $alisms = new Alisms($this->config); $res = $alisms->setMobile($this->mobile) ->setTemplateCode($this->sms['template_code']) ->setTemplateParam($this->sms_params) ->sendSms(); if (isset($res['Code']) && $res['Code'] == 'OK') { $send_status = SmsLog::send_success; $this->updateSmsLog($send_status, $res); return true; } else { $send_status = SmsLog::send_fail; $this->updateSmsLog($send_status, $res); $message = $res['Message'] ?? $res; throw new Exception('短信配置错误:' . $message); } } elseif ($this->channel == 2) { // 腾讯云短信 $res = (new TencentSms($this->config)) ->setMobile($this->mobile) ->setTemplateCode($this->sms['template_code']) ->setTemplateParam($this->handleTcParams()) ->sendSms(); if (isset($res['SendStatusSet']) && $res['SendStatusSet'][0]['Code'] == 'Ok') { $send_status = SmsLog::send_success; $this->updateSmsLog($send_status, $res); return true; } else { $send_status = SmsLog::send_fail; $this->updateSmsLog($send_status, $res); $message = $res['SendStatusSet'][0]['Message'] ?? json_encode($res); throw new Exception('短信配置错误:' . $message); } } throw new Exception('短信渠道不存在'); } catch (\Exception $e) { $this->updateSmsLog(SmsLog::send_fail, $e->getMessage()); NoticeLogic::updateNotice($this->notice_id, $e->getMessage()); } } |
2:打开项目:application\common\serve 新增 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
|
<?phpnamespace app\common\server;use think\Exception;class Smsbao{ private $app_key = ''; private $secret_key = ''; private $sign = ''; private $mobile = ''; private $content= ''; public function __construct($config) { $this->sign = $config['sign']; $this->app_key = $config['app_key']; $this->secret_key = $config['secret_key']; } public function setContent($template_code){ $this->content = $template_code; return $this; } public function setMobile($mobile){ $this->mobile = $mobile; return $this; } public function sendSms(){ try { $statusStr = array( "0" => "短信发送成功", "-1" => "参数不全", "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!", "30" => "密码错误", "40" => "账号不存在", "41" => "余额不足", "42" => "帐户已过期", "43" => "IP地址限制", "50" => "内容含有敏感词" ); $user = $this->app_key; //短信平台帐号 $pass = md5($this->secret_key); //短信平台密码 $sign = '【'.$this->sign.'】'; //短信平台密码 $content=$sign.$this->content;//要发送的短信内容 $phone = $this->mobile;//要发送短信的手机号码 $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content); $result = $this->curl_get($sendurl); $res = []; if ($result == 0){ $res['Code'] ='OK'; }else{ $res['Code'] ='fail'; $res['Message'] =$statusStr[$result]; } return $res; // return $result->toArray(); } catch(Exception $e){ return $e->getMessage() . PHP_EOL; } } private function curl_get($sendurl){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $sendurl); curl_setopt($ch, CURLOPT_TIMEOUT, 1); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书 $result = curl_exec($ch); curl_close($ch); return $result; }} |
经过上面的替换,短信宝的短信平台已经替换成功了,可以正常使用了。
报备一下短信宝的VIP模板,这样就可以走短信宝的优质通道了,即便遇到敏感文字我们都不会人工审核,短信内容3~5秒就可送达。
另外:我们已经开发好完整的likeshop系统短信宝插件,点击此链接 下载及查看安装流程。
最新更新
电商类
CMS类
微信类