Files
CompanyProject/芳林公司项目/www.zhiyuanhelp.com/application/api/service/SendMessage.php
2024-09-27 01:32:49 +08:00

81 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/4/13
* Time: 17:25
*/
namespace app\api\service;
use app\api\controller\Basecontroller;
use app\common\controller\SignatureHelper;
class SendMessage extends Basecontroller
{
/**
* 短信发送api
* @param int $phoneNums 接收短信的手机号
* @param string $SignName 短信签名
* @param string $TemplateCode 短信模板Code
* @param string $code 短信变量
* @return \think\response\Json
*/
public static function sendAliMessage($phoneNums,$SignName,$TemplateCode,$code="")
{
$params = array ();
// *** 需用户填写部分 ***
// fixme 必填是否启用https
$security = false;
// fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
$accessKeyId = config('alidayu.AccessKeyID');
$accessKeySecret = config('alidayu.AccessKeySecret');
// fixme 必填: 短信接收号码
$params["PhoneNumbers"] = $phoneNums;
// fixme 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
$params["SignName"] = $SignName;
// fixme 必填: 短信模板Code应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
$params["TemplateCode"] = $TemplateCode;
// fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
if($code){
$params['TemplateParam'] = $code;
}
// fixme 可选: 设置发送短信流水号
//$params['OutId'] = "12345";
// fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下无特殊需求用户请忽略此字段
//$params['SmsUpExtendCode'] = "1234567";
// *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
}
// 初始化SignatureHelper实例用于设置参数签名以及发送请求
$helper = new SignatureHelper();
// 此处可能会抛出异常注意catch
$content = $helper->request(
$accessKeyId,
$accessKeySecret,
"dysmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SendSms",
"Version" => "2017-05-25",
)),
$security
);
if($content->Message == 'OK')
return ['status'=>1];
else
return ['status'=>0];
}
}