162 lines
4.1 KiB
PHP
162 lines
4.1 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | 开源版本可自由商用,可去除界面版权logo
|
||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||
// | 访问官网:https://www.likeadmin.cn
|
||
// | likeadmin团队 版权所有 拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeadminTeam
|
||
// +----------------------------------------------------------------------
|
||
namespace app\common\service\sms\engine;
|
||
|
||
|
||
/**
|
||
* 短信宝短信
|
||
* Class SmsbaoSms
|
||
* @package app\common\service\sms\engine
|
||
*/
|
||
class SmsbaoSms
|
||
{
|
||
protected $error = null;
|
||
protected $config;
|
||
protected $mobile;
|
||
protected $templateId;
|
||
protected $templateParams;
|
||
|
||
public function __construct($config)
|
||
{
|
||
if(empty($config)) {
|
||
$this->error = '请联系管理员配置参数';
|
||
return false;
|
||
}
|
||
$this->config = $config;
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 设置手机号
|
||
* @param $mobile
|
||
* @return $this
|
||
* @author 段誉
|
||
* @date 2022/9/15 16:28
|
||
*/
|
||
public function setMobile($mobile)
|
||
{
|
||
$this->mobile = $mobile;
|
||
return $this;
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 设置模板id
|
||
* @param $templateId
|
||
* @return $this
|
||
* @author 段誉
|
||
* @date 2022/9/15 16:28
|
||
*/
|
||
public function setTemplateId($templateId)
|
||
{
|
||
$this->templateId = $templateId;
|
||
return $this;
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 设置模板参数
|
||
* @param $templateParams
|
||
* @return $this
|
||
* @author 段誉
|
||
* @date 2022/9/15 16:28
|
||
*/
|
||
public function setTemplateParams($templateParams)
|
||
{
|
||
$this->templateParams = json_encode($templateParams, JSON_UNESCAPED_UNICODE);
|
||
return $this;
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 短信内容
|
||
* @param $sceneContent
|
||
* @return $this
|
||
* @author 段誉
|
||
* @date 2022/9/15 16:28
|
||
*/
|
||
public function setSceneContent($sceneContent)
|
||
{
|
||
$this->sceneContent = $sceneContent;
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* @notes 国家区号
|
||
* @param $sceneContent
|
||
* @return $this
|
||
* @author 段誉
|
||
* @date 2022/9/15 16:28
|
||
*/
|
||
public function setCountryCode($countryCode)
|
||
{
|
||
$this->countryCode = $countryCode;
|
||
return $this;
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 错误信息
|
||
* @return string|null
|
||
* @author 段誉
|
||
* @date 2022/9/15 16:27
|
||
*/
|
||
public function getError()
|
||
{
|
||
return $this->error;
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 发送短信
|
||
* @return array|false
|
||
* @author 段誉
|
||
* @date 2022/9/15 16:27
|
||
*/
|
||
public function send()
|
||
{
|
||
try {
|
||
|
||
$smsapi = $this->config['url_cn'];
|
||
$user = $this->config['username']; //短信平台帐号
|
||
$pass = md5($this->config['api_key']); //短信平台密码
|
||
$content = $this->sceneContent;//要发送的短信内容
|
||
|
||
$sendPhone = urlencode($this->mobile);//要发送短信的手机号码
|
||
|
||
|
||
$countryCode = $this->countryCode;//国家区号
|
||
if('+86' != $countryCode){
|
||
$sendPhone = urlencode($this->countryCode.$this->mobile);
|
||
$smsapi = $this->config['url_go'];
|
||
}
|
||
|
||
|
||
$sendurl = $smsapi."?u=".$user."&p=".$pass."&m=".$sendPhone."&c=".urlencode($content);
|
||
|
||
$result = file_get_contents($sendurl);
|
||
|
||
if($result !=0){
|
||
if($result ==51){
|
||
throw new \Exception('pwd.mobileError');
|
||
}else{
|
||
throw new \Exception('network.networkAnomaly');
|
||
}
|
||
}
|
||
} catch(\Exception $e) {
|
||
$this->error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
} |