1
0
Files
helper/application/common/service/Sms.php
2023-11-16 14:33:38 +08:00

89 lines
2.5 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
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace app\common\service;
use app\common\model\Sms as SmsModel;
use app\common\validate\Sms as SmsValidate;
use think\Config;
use think\Log;
use tools\Str;
class Sms extends _Init
{
public static function send($mobile)
{
$code = Str::number(1, 9999);
$code = 6666;
$data = [
'mobile' => $mobile,
'code' => $code,
];
$validate = new SmsValidate();
if (! $validate->check($data)) {
return $validate->getError();
}
$res = SmsModel::create($data);
if ($res) {
$result = self::ali($mobile, $code);
$result = true;
if ($result) {
return true;
} else {
return '发送失败';
}
} else {
return '验证码发送失败';
}
}
public static function check($mobile, $code)
{
$data = [
'mobile' => $mobile,
'code' => $code,
];
$res = SmsModel::where('mobile', $mobile)->where('status', 0)->order('id desc')->find();
if ($res && $res['code'] == $code) {
$res->save(['status' => 1]);
return true;
} else {
return '验证码有误';
}
}
private static function ali($mobile, $code)
{
$config = Config::get('sms_config');
$params = [
'appid' => $config['appid'],
'timestamp' => time(),
'receive' => $mobile,
'tpl_id' => 'SMS_85730027',
'sign_method' => '身份验证',
'params' => '{"code":"'.$code.'","product":"【超级助手】"}',
];
ksort($params);
$sign = http_build_query($params);
$sign = urldecode($sign).'&secret='.$config['secret'];
$params['sign'] = strtoupper(md5($sign));
$res = json_decode(http('http://api.cjango.com/sms/send?'.http_build_query($params), 'GET'), true);
if ($res && $res['code'] == 0) {
return true;
} else {
return false;
}
}
}