145 lines
5.3 KiB
PHP
145 lines
5.3 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
namespace common\services;
|
|||
|
|
|
|||
|
|
use common\core\sms\CaptchaMessage;
|
|||
|
|
use common\core\sms\PrescriptionPassMessage;
|
|||
|
|
use common\core\sms\PrescriptionRefuseMessage;
|
|||
|
|
use common\core\sms\RegisterMessage;
|
|||
|
|
use common\core\sms\WaitApprovalMessage;
|
|||
|
|
use GuzzleHttp\Exception\ClientException;
|
|||
|
|
use Overtrue\EasySms\EasySms;
|
|||
|
|
use Overtrue\EasySms\Exceptions\GatewayErrorException;
|
|||
|
|
use Overtrue\EasySms\Exceptions\NoGatewayAvailableException;
|
|||
|
|
|
|||
|
|
class SmsService
|
|||
|
|
{
|
|||
|
|
public function __construct()
|
|||
|
|
{
|
|||
|
|
$this->sms = [
|
|||
|
|
"status" => "1", //是否开启
|
|||
|
|
"access_key_id" => "LTAI5tSePVz6X1EXi6tBHzVg",
|
|||
|
|
"access_key_secret" => "ppd5xa2o8b9oKalOiUqQFnYLYq4xlX",
|
|||
|
|
"sign_name" => "萧康医药", //签名
|
|||
|
|
"captcha" => [ //验证码格式设置
|
|||
|
|
//'template_code' => 'SMS_274460204', // 模板code
|
|||
|
|
"template_id" => "SMS_275060336", // 模板id
|
|||
|
|
"template_variable" => "code", //模板变量
|
|||
|
|
],
|
|||
|
|
"doctor" => [
|
|||
|
|
"register" => [ //挂号订单提醒
|
|||
|
|
"template_id" => "SMS_462205458",
|
|||
|
|
"template_variable" => ""
|
|||
|
|
],
|
|||
|
|
"prescription_pass" => [
|
|||
|
|
"template_id" => "SMS_462260412",
|
|||
|
|
"template_variable" => "order"
|
|||
|
|
],
|
|||
|
|
"prescription_refuse" => [
|
|||
|
|
"template_id" => "SMS_462230433",
|
|||
|
|
"template_variable" => [
|
|||
|
|
"order",
|
|||
|
|
"cause"
|
|||
|
|
]
|
|||
|
|
]
|
|||
|
|
],
|
|||
|
|
"pharmacist" => [
|
|||
|
|
"wait_approval" => [
|
|||
|
|
"template_id" => "SMS_462255406",
|
|||
|
|
"template_variable" => ""
|
|||
|
|
]
|
|||
|
|
]
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
$this->config = [
|
|||
|
|
// HTTP 请求的超时时间(秒)
|
|||
|
|
'timeout' => 5.0,
|
|||
|
|
|
|||
|
|
// 默认发送配置
|
|||
|
|
'default' => [
|
|||
|
|
// 网关调用策略,默认:顺序调用
|
|||
|
|
'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
|
|||
|
|
|
|||
|
|
// 默认可用的发送网关
|
|||
|
|
'gateways' => [
|
|||
|
|
'aliyun',
|
|||
|
|
],
|
|||
|
|
],
|
|||
|
|
// 可用的网关配置
|
|||
|
|
'gateways' => [
|
|||
|
|
'errorlog' => [
|
|||
|
|
'file' => '/tmp/easy-sms.log',
|
|||
|
|
],
|
|||
|
|
'aliyun' => [
|
|||
|
|
'access_key_id' => $this->sms['access_key_id'],
|
|||
|
|
'access_key_secret' => $this->sms['access_key_secret'],
|
|||
|
|
'sign_name' => $this->sms['sign_name'],
|
|||
|
|
],
|
|||
|
|
],
|
|||
|
|
];
|
|||
|
|
$this->easySms = new EasySms($this->config);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function send($mobile, $message)
|
|||
|
|
{
|
|||
|
|
try {
|
|||
|
|
$this->easySms->send($mobile, $message);
|
|||
|
|
return true;
|
|||
|
|
} catch (NoGatewayAvailableException $exception) {
|
|||
|
|
$es = $exception->getExceptions();
|
|||
|
|
foreach ($es as $e) {
|
|||
|
|
/** @var GatewayErrorException $e */
|
|||
|
|
if (isset($e->raw) && isset($e->raw['Code']) && $e->raw['Code'] == 'isv.MOBILE_NUMBER_ILLEGAL') {
|
|||
|
|
throw new GatewayErrorException("无效的号码 {$mobile}", $e->getCode(), $e->raw);
|
|||
|
|
}
|
|||
|
|
if ($e instanceof ClientException) {
|
|||
|
|
$result = json_decode($e->getResponse()->getBody()->getContents(), true);
|
|||
|
|
if ($result && is_array($result) && isset($result['Message']) && isset($result['Code'])) {
|
|||
|
|
if ($result['Code'] == 'InvalidAccessKeyId.NotFound') {
|
|||
|
|
throw new GatewayErrorException("无效的AccessKeyId", $e->getCode(), $result);
|
|||
|
|
}
|
|||
|
|
if ($result['Code'] == 'SignatureDoesNotMatch') {
|
|||
|
|
throw new GatewayErrorException(
|
|||
|
|
"Signature不匹配,请检查AccessKeySecret是否正确",
|
|||
|
|
$e->getCode(),
|
|||
|
|
$result
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
throw new GatewayErrorException($result['Message'], $e->getCode(), $result);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
throw $e;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public function sendCaptcha($mobile, $captcha)
|
|||
|
|
{
|
|||
|
|
// $captcha = (string)mt_rand(100000, 999999);
|
|||
|
|
$message = new CaptchaMessage($captcha, $this->sms['captcha']);
|
|||
|
|
return $this->send($mobile, $message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function sendRegister($mobile){
|
|||
|
|
$message = new RegisterMessage($this->sms['doctor']['register']);
|
|||
|
|
return $this->send($mobile, $message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function sendWaitApproval($mobile){
|
|||
|
|
$message = new WaitApprovalMessage($this->sms['pharmacist']['wait_approval']);
|
|||
|
|
return $this->send($mobile, $message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function sendPrescriptionPass($mobile,$prescription_no){
|
|||
|
|
$message = new PrescriptionPassMessage($prescription_no, $this->sms['doctor']['prescription_pass']);
|
|||
|
|
return $this->send($mobile, $message);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public function sendPrescriptionRefuse($mobile,$prescription){
|
|||
|
|
$message = new PrescriptionRefuseMessage($prescription,$this->sms['doctor']['prescription_refuse']);
|
|||
|
|
return $this->send($mobile, $message);
|
|||
|
|
}
|
|||
|
|
}
|