目录
一、阿里云SDK依赖(需要安装好composer)
二、使用步骤
1.登录/注册接口对接的阿里云发送短信的控制器
2.登录发送短信/登录短信验证
3:注册发送短信/注册短信验证
登录和注册时都需要查询用户是否已存在:
登录是为了查看用户是否是新用户(新用户去注册)
注册是为了查看用户是否已经被注册(被注册去登录)
验证成功后:
登录验证就签发token——登陆成功(前端跳转首页)
注册验证就将注册信息添加到用户表里——注册成功(前端跳转登录)
1:win+R打开命令行进行阿里云换源
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
2:cd 到项目根路径下安装依赖
composer require alibabacloud/client
'Access Key ID',
'accessSecret' => 'Access Key Secret',
'SignName' => '短信签名',
'TemplateCode' => '模板ID',
'regionId' => 'cn-hangzhou' /*不用改这里的地址*/
];
$param = ['code' => $code];
AlibabaCloud::accessKeyClient($config['accessKeyId'], $config['accessSecret'])
->regionId($config['regionId'])
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('Dysmsapi')
->version('2017-05-25')
->action('SendSms')
->method('POST')
->options([
'query' => [
'PhoneNumbers' => $phone,
'SignName' => $config['SignName'],
'TemplateCode' => $config['TemplateCode'],
'TemplateParam' => json_encode($param)
],
])
->request();
/*发送成功存入数据库,将手机号,验证码,请求类型写入短信表*/
$smsModel = new sms();
$saveSms = $smsModel->saveSms($phone, $code, $type);
return $result->toArray();
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
}
}
//短信登陆
/**
* remarks:用户登录-短信发送
*
* phone(手机号)/type(long/register)
*/
public function index()
{
/*用户表中查询手机号是否存在*/
$adminModel = new admin();
$data = request::only(['phone', 'type']);
$regular = preg_match('/^1[3456789]\d{9}$/', $data['phone']);/*验证手机格式*/
if ($regular == 0) {
return json(['code' => 0, 'msg' => '手机号格式不正确']);
} else {
$checkUser = $adminModel->checkphone($data['phone'], 'login');
if ($checkUser == null) {
return json(['code' => 0, 'msg' => '抱歉,该手机号是新户暂时不能使用短信登陆!']);
} else {
$sendsms = new Indexsms();/*实列化发送短信的阿里控制器*/
$send = $sendsms->index($data['phone'], $data['type']);/*带数据过去*/
if ($send) {
return json(['code' => 1, 'msg' => '发送成功,请注意查收短息5分钟内生效!']);
} else {
return json(['code' => 0, 'msg' => '发送失败,网络繁忙请稍后再试!']);
}
}
}
}
/**
* remarks:用户登录-验证短信
*
* phone/type
*/
public function checkLogin()
{
$data = request::only(['phone', 'code', 'type']);
/*查询短信验证码*/
$smsModel = new sms();
$checkCode = $smsModel->checkCode($data);
if ($checkCode == null) {
return json(['code' => 0, 'msg' => '验证码错误,请重新核对!']);
} else {
$uid = new admin();
$key = 'token';
$payload = [
"iat" => time(), /*令牌签发时间*/
"nbf" => time(), /*如果当前时间在nbf里的时间之前,则Token不被接受*/
"exp" => time() + 60 * 60 * 24 * 7, /*过期时间*/
"uid" => $uid->uid($data['phone']),/*验证用户id是否相同*/
];
$smsModel->editCodeState($checkCode['id']);/*验证成功后更改短信的有效性(改无效)*/
$token = JWT::encode($payload, $key, "HS256");
return json(['code' => 1, 'msg' => '登陆成功', 'token' => $token]);
}
}
* remarks:注册
*/
public function register()
{
$data = request::only(['phone', 'type']);
$regular = preg_match('/^1[3456789]\d{9}$/', $data['phone']);
if ($regular == 0) {
return json(['code' => 0, 'msg' => '手机号格式不正确']);
} else {
/*查询手机号是否为空*/
$nullphone = new admin();
if ($nullphone->checkphone($data['phone']) == null) {
$sendsms = new Indexsms();/*实列化发送短信的阿里控制器*/
$send = $sendsms->index($data['phone'], $data['type']);
if ($send) {
return json(['code' => 1, 'msg' => '发送成功,请注意查收短息5分钟内生效!']);
} else {
return json(['code' => 0, 'msg' => '发送失败,网络繁忙请稍后再试!']);
}
} else {
return json(['code' => 0, 'msg' => '抱歉,该手机号已被注册!']);
}
}
}
/**
* remarks:用户注册-验证短信
*
*/
public function checkRegister()
{
$data = request::only(['phone', 'code', 'password', 'type']);
$register = new sms();
$codeinfo = $register->checkCode($data);
if ($codeinfo == null) {
return json(['code' => 0, 'msg' => '验证码错误,请重新核对!']);
} else if (!empty($data['phone']) && !empty($data['password'])) {
/*修改短信有效状态*/
$register->editCodeState($codeinfo['id']);
$password = md5($data['password']);
$adduser = new admin();
$adduser = $adduser->save([
'phone' => $data['phone'],
'password' => $password,
]);
return json(['code' => 1, 'msg' => '注册成功!']);
} else {
/*注册失败*/
return json(['code' => 1, 'msg' => '表单信息不完整!']);
}
}