first commit
This commit is contained in:
85
application/common/service/Sms.php
Normal file
85
application/common/service/Sms.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?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 tools\Str;
|
||||
|
||||
class Sms extends _Init
|
||||
{
|
||||
|
||||
public static function send($mobile)
|
||||
{
|
||||
$code = Str::number(1, 9999);
|
||||
$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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user