128 lines
4.6 KiB
PHP
128 lines
4.6 KiB
PHP
<?php
|
||
namespace app\tools;
|
||
|
||
use AlibabaCloud\Client\AlibabaCloud;
|
||
use AlibabaCloud\Client\Exception\ClientException;
|
||
use AlibabaCloud\Client\Exception\ServerException;
|
||
|
||
// Download:https://github.com/aliyun/openapi-sdk-php
|
||
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
|
||
|
||
class Sms
|
||
{
|
||
/**
|
||
* 发送短信验证码,本接只适合发送单个验证码
|
||
*
|
||
* @param integer $number 目标用户手机号
|
||
* @param int $code 短信验证码
|
||
* @param string $signName 签名如:Knowpia
|
||
* @param string $template 使用的短信模板
|
||
* @return array
|
||
*/
|
||
public static function sendmsg($number, $code, $signName='', $template='')
|
||
{
|
||
if(empty($signName)) $signName = env('aliyunsms.signName');
|
||
if(empty($template)) $template = env('aliyunsms.TemplateCode');
|
||
|
||
if (intval($number) == 0 || $code == "" || $signName == "" || $template == "") {
|
||
return ['code' => 0, 'message' => '参数不全'];
|
||
}
|
||
AlibabaCloud::accessKeyClient(env('aliyunsms.accessKeyId'), env('aliyunsms.accesskey'))
|
||
->regionId(env('aliyunsms.regionid'))
|
||
->asDefaultClient();
|
||
try {
|
||
$result = AlibabaCloud::rpc()
|
||
->product('Dysmsapi')
|
||
->version('2017-05-25')
|
||
->action('SendSms')
|
||
->method('POST')
|
||
->host('dysmsapi.aliyuncs.com')
|
||
->options([
|
||
'query' => [
|
||
'RegionId' => env('aliyunsms.regionid'),
|
||
'PhoneNumbers' => $number,
|
||
'SignName' => $signName,
|
||
'TemplateCode' => $template,
|
||
'TemplateParam' => "{\"code\":\"" . $code . "\"}",
|
||
],
|
||
])
|
||
->request();
|
||
$result = $result->toArray();
|
||
return ['code' => 1, 'info' => $result];
|
||
} catch (ClientException $e) {
|
||
return ['code' => 0, 'message' => $e->getErrorMessage()];
|
||
} catch (ServerException $e) {
|
||
return ['code' => 0, 'message' => $e->getErrorMessage()];
|
||
}
|
||
}
|
||
/**
|
||
* 获得用户的短信验证码
|
||
*
|
||
* @param string $mobile [手机号]
|
||
* @return array
|
||
*/
|
||
public static function getMobileCode($mobile)
|
||
{
|
||
$smsExpiration = env('system.smsExpiration');
|
||
$codeInfo = \app\tools\model\MobileCode::where('state=0 and mobile="'.$mobile.'"')->order('id desc')->find();
|
||
$notMessage = '请先发送短信再验证';
|
||
if ($codeInfo) {
|
||
if ((time() - $codeInfo['create_at']) <= $smsExpiration) {
|
||
return ['code' => 1, 'MobileCode' => $codeInfo['code'],'check_id'=>$codeInfo['id']];
|
||
}
|
||
if ((time() - $codeInfo['create_at']) > $smsExpiration && (time() - $codeInfo['create_at']) <= 60 * 30) {
|
||
return ['code' => 0, 'message' => '验证码已过期'];
|
||
}
|
||
return ['code' => 0, 'message' => $notMessage];
|
||
}
|
||
return ['code' => 0, 'message' => $notMessage];
|
||
}
|
||
|
||
/**
|
||
* 号码认证服务,利用一键登录TOKEN获取手机号
|
||
*
|
||
* @param string $token
|
||
* @return array
|
||
*/
|
||
public static function getMobileNumber($token = ''): array
|
||
{
|
||
/** back array
|
||
* {
|
||
* "GetMobileResultDTO": {
|
||
* "Mobile": "18620725473"
|
||
* },
|
||
* "Message": "OK",
|
||
* "RequestId": "098CC43B-8006-4127-9DC5-2B30CA741745",
|
||
* "Code": "OK"
|
||
* }
|
||
*/
|
||
if ($token == '') {
|
||
return ['code' => 0, 'message' => '参数不全'];
|
||
}
|
||
AlibabaCloud::accessKeyClient(env('aliyunsms.accessKeyId'), env('aliyunsms.accesskey'))
|
||
->regionId('cn-hangzhou')
|
||
->asDefaultClient();
|
||
try {
|
||
$result = AlibabaCloud::rpc()
|
||
->product('Dypnsapi')
|
||
->scheme('https')
|
||
->version('2017-05-25')
|
||
->action('GetMobile')
|
||
->method('POST')
|
||
->host('dypnsapi.aliyuncs.com')
|
||
->options([
|
||
'query' => [
|
||
'RegionId' => "cn-hangzhou",
|
||
'AccessToken' => $token,
|
||
],
|
||
])->request();
|
||
return ['code' => 1, 'info' => $result->toArray()];
|
||
} catch (ClientException $e) {
|
||
return ['code' => 0, 'message' => $e->getErrorMessage()];
|
||
} catch (ServerException $e) {
|
||
return ['code' => 0, 'message' => $e->getErrorMessage()];
|
||
}
|
||
|
||
}
|
||
}
|