新增订阅消息发送模版
This commit is contained in:
+73
-51
@@ -73,64 +73,86 @@ class Dysms
|
||||
*/
|
||||
public static function sendSms(string $phone_numbers,array $template_param,string $template_code,string $scene_desc = ""): void
|
||||
{
|
||||
$config = config("alibaba.dysms");
|
||||
try {
|
||||
$config = config("alibaba.dysms");
|
||||
|
||||
$client = self::createClient($config['accessKey'], $config['accessKeySecret']);
|
||||
$params = self::createApiInfo();
|
||||
$client = self::createClient($config['accessKey'], $config['accessKeySecret']);
|
||||
$params = self::createApiInfo();
|
||||
|
||||
// query params
|
||||
$queries = [];
|
||||
$queries["PhoneNumbers"] = $phone_numbers;
|
||||
$queries["SignName"] = "肝胆相照";
|
||||
$queries["TemplateCode"] = $template_code;
|
||||
$queries["TemplateParam"] = json_encode($template_param,JSON_UNESCAPED_UNICODE);
|
||||
// runtime options
|
||||
$runtime = new RuntimeOptions([]);
|
||||
$request = new OpenApiRequest([
|
||||
"query" => OpenApiUtilClient::query($queries)
|
||||
]);
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode
|
||||
$result = $client->callApi($params, $request, $runtime);
|
||||
// query params
|
||||
$queries = [];
|
||||
$queries["PhoneNumbers"] = $phone_numbers;
|
||||
$queries["SignName"] = "肝胆相照";
|
||||
$queries["TemplateCode"] = $template_code;
|
||||
$queries["TemplateParam"] = json_encode($template_param,JSON_UNESCAPED_UNICODE);
|
||||
// runtime options
|
||||
$runtime = new RuntimeOptions([]);
|
||||
$request = new OpenApiRequest([
|
||||
"query" => OpenApiUtilClient::query($queries)
|
||||
]);
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
// 返回值为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode
|
||||
$result = $client->callApi($params, $request, $runtime);
|
||||
|
||||
if (empty($result)){
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::CODE_FAIL));
|
||||
}
|
||||
|
||||
if (empty($result['body'])){
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::CODE_FAIL));
|
||||
}
|
||||
|
||||
Log::getInstance()->info("发送短信日志:" . json_encode($result['body'],JSON_UNESCAPED_UNICODE));
|
||||
|
||||
if ($result['body']['Code'] != "OK"){
|
||||
if (isset($result['body']['MessageIm'])){
|
||||
throw new BusinessException($result['body']['MessageIm'],HttpEnumCode::CODE_FAIL);
|
||||
if (empty($result)){
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::CODE_FAIL));
|
||||
}
|
||||
throw new BusinessException("短信发送失败",HttpEnumCode::CODE_FAIL);
|
||||
}
|
||||
|
||||
if (empty($result['body']['RequestId'])){
|
||||
// 无唯一值
|
||||
if (isset($result['body']['MessageIm'])){
|
||||
throw new BusinessException($result['body']['MessageIm'],HttpEnumCode::CODE_FAIL);
|
||||
if (empty($result['body'])){
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::CODE_FAIL));
|
||||
}
|
||||
throw new BusinessException("短信发送失败",HttpEnumCode::CODE_FAIL);
|
||||
}
|
||||
|
||||
// 记录log
|
||||
$data = array();
|
||||
$data['type'] = 1;
|
||||
$data['status'] = 1;
|
||||
$data['phone'] = $phone_numbers;
|
||||
$data['template_code'] = $template_code;
|
||||
$data['third_code'] = $result['body']['RequestId'];
|
||||
$data['scene_desc'] = $scene_desc ?: "";
|
||||
$data['remarks'] = json_encode($template_param,JSON_UNESCAPED_UNICODE);
|
||||
$res = LogSms::LogSms($data);
|
||||
if (empty($res)){
|
||||
// 发送成功,记录失败
|
||||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::CODE_FAIL));
|
||||
Log::getInstance()->info("发送短信日志:" . json_encode($result['body'],JSON_UNESCAPED_UNICODE));
|
||||
|
||||
if ($result['body']['Code'] != "OK"){
|
||||
if (isset($result['body']['MessageIm'])){
|
||||
throw new BusinessException($result['body']['MessageIm'],HttpEnumCode::CODE_FAIL);
|
||||
}
|
||||
throw new BusinessException("短信发送失败",HttpEnumCode::CODE_FAIL);
|
||||
}
|
||||
|
||||
if (empty($result['body']['RequestId'])){
|
||||
// 无唯一值
|
||||
if (isset($result['body']['MessageIm'])){
|
||||
throw new BusinessException($result['body']['MessageIm'],HttpEnumCode::CODE_FAIL);
|
||||
}
|
||||
throw new BusinessException("短信发送失败",HttpEnumCode::CODE_FAIL);
|
||||
}
|
||||
|
||||
// 记录log
|
||||
$data = array();
|
||||
$data['type'] = 1;
|
||||
$data['status'] = 1;
|
||||
$data['phone'] = $phone_numbers;
|
||||
$data['template_code'] = $template_code;
|
||||
$data['third_code'] = $result['body']['RequestId'];
|
||||
$data['scene_desc'] = $scene_desc ?: "";
|
||||
$data['remarks'] = json_encode($template_param,JSON_UNESCAPED_UNICODE);
|
||||
$res = LogSms::LogSms($data);
|
||||
if (empty($res)){
|
||||
// 发送成功,记录失败
|
||||
Log::getInstance()->info("发送短信成功,记录log失败" . json_encode($data,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
}catch (\Exception $e) {
|
||||
Log::getInstance()->error("发送短信失败:" . $e->getMessage());
|
||||
|
||||
// 记录log
|
||||
$data = array();
|
||||
$data['type'] = 1;
|
||||
$data['status'] = 2;
|
||||
$data['phone'] = $phone_numbers;
|
||||
$data['template_code'] = $template_code;
|
||||
$data['third_code'] = $result['body']['RequestId'];
|
||||
$data['scene_desc'] = $scene_desc ?: "";
|
||||
$data['remarks'] = json_encode($template_param,JSON_UNESCAPED_UNICODE);
|
||||
$res = LogSms::LogSms($data);
|
||||
if (empty($res)){
|
||||
// 发送成功,记录失败
|
||||
Log::getInstance()->info("发送短信失败,记录log失败" . json_encode($data,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
throw new BusinessException($e->getMessage(),HttpEnumCode::CODE_FAIL);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user