待发短信

在线客服
产品支持 短信宝客服
合作渠道 渠道合作
服务咨询

4001-021-502

工作时间

9:00-21:00

likeadmin_v1.4.2新增短信宝短信接口

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" => "内容含有敏感词"
        );
        $smsapi = "http://api.smsbao.com/";
        $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类

微信类

文章标签