You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.0 KiB
110 lines
3.0 KiB
<?php
|
|
|
|
namespace app\service\user;
|
|
|
|
use app\model\AdminConfig;
|
|
use fast\FuncException;
|
|
|
|
class SmsService
|
|
{
|
|
protected $smsUsername;
|
|
protected $smsPassword;
|
|
protected $sendUrl;
|
|
protected $content_prefix = '【大工业区水司】';
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
$smsContentJson = (new AdminConfig())->where('title', 'sms')->where('delete_time', 0)->value('content');
|
|
if ($smsContentJson) {
|
|
$smsContent = json_decode($smsContentJson, true);
|
|
if (isset($smsContent[0]['api_url']) && isset($smsContent[0]['username']) && isset($smsContent[0]['password'])) {
|
|
$this->smsUsername = $smsContent[0]['username'];
|
|
$this->smsPassword = $smsContent[0]['password'];
|
|
$this->sendUrl = $smsContent[0]['api_url'];
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param $code
|
|
* @return string
|
|
*/
|
|
public function smsCodeTemplate($code): string
|
|
{
|
|
return '您的短信验证码为:' . $code . ',用于手机号绑定,10分钟内有效。若非本人操作,请勿泄露,谨防被骗。';
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param $email
|
|
* @return string
|
|
*/
|
|
public function smsInvoiceTemplate($email): string
|
|
{
|
|
return '发票已经发送至邮箱' . $email . ',请及时查收';
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param $mobile
|
|
* @param $message
|
|
* @throws FuncException
|
|
*/
|
|
public function sendSms($mobile, $message)
|
|
{
|
|
$content = $this->content_prefix . $message;
|
|
|
|
$param = [
|
|
'userName' => $this->smsUsername,
|
|
'sign' => md5($this->smsUsername . $this->smsPassword . $mobile . $content),
|
|
'mobile' => $mobile,
|
|
'content' => $content,
|
|
];
|
|
|
|
$json = json_encode($param);
|
|
|
|
$result = $this->curl($json);
|
|
if ($result['resultCode'] != 1) {
|
|
throw new FuncException('发送失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param $json
|
|
* @return bool|string
|
|
* @throws FuncException
|
|
*/
|
|
public function curl($json)
|
|
{
|
|
// 初始化cURL会话
|
|
$ch = curl_init();
|
|
|
|
// 设置cURL选项
|
|
curl_setopt($ch, CURLOPT_URL, $this->sendUrl); // 目标URL
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回结果而不是输出
|
|
curl_setopt($ch, CURLOPT_POST, true); // 发送POST请求
|
|
|
|
// 设置POST字段
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); // 设置HTTP头
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //todo::增加改行
|
|
|
|
// 执行cURL会话
|
|
$response = curl_exec($ch);
|
|
|
|
// 检查是否有错误发生
|
|
if (curl_errno($ch)) {
|
|
throw new FuncException('发送失败');
|
|
}
|
|
|
|
// 关闭cURL会话
|
|
curl_close($ch);
|
|
|
|
return json_decode($response, true);
|
|
}
|
|
|
|
|
|
}
|
|
|