add
This commit is contained in:
117
app/api/model/Msg.php
Normal file
117
app/api/model/Msg.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\model;
|
||||
|
||||
use ba\Random;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Validate;
|
||||
use think\Model;
|
||||
|
||||
class Msg extends Model
|
||||
{
|
||||
|
||||
|
||||
public static function sendMsg($phone, $event):bool|array
|
||||
{
|
||||
if (!$phone || !Validate::regex($phone, "^1\d{10}$")) {
|
||||
return returnErrorData('手机号不正确');
|
||||
}
|
||||
$send_time = Cache::get($event . $phone);
|
||||
if($send_time && time() - $send_time < 60) {
|
||||
return returnErrorData('发送频繁请稍后再试');
|
||||
}else {
|
||||
Cache::set($event . $phone, time());
|
||||
}
|
||||
$user = TbUser::GetByusername($phone);
|
||||
switch ($event) {
|
||||
case 'bindWx':
|
||||
if($user && $user['wx_open_id']) {
|
||||
return returnErrorData('当前手机号已被其他微信账号绑定');
|
||||
}
|
||||
break;
|
||||
case 'bindIos':
|
||||
if($user && $user['apple_id']) {
|
||||
return returnErrorData('当前手机号已被其他苹果账号绑定');
|
||||
}
|
||||
break;
|
||||
case 'login':
|
||||
if($user) {
|
||||
return returnErrorData('当前手机号已注册');
|
||||
}
|
||||
break;
|
||||
case 'forget':
|
||||
if(!$user) {
|
||||
return returnErrorData('手机号未注册');
|
||||
}
|
||||
break;
|
||||
}
|
||||
$commonInfo = CommonInfo::where(['type' => 79])->find();
|
||||
if($commonInfo && $commonInfo->value == 2) {
|
||||
return self::AlibabaSendMsg($event, $phone);
|
||||
}else {
|
||||
return returnErrorData('配置错误');
|
||||
}
|
||||
}
|
||||
|
||||
public static function AlibabaSendMsg($event, $phone):array
|
||||
{
|
||||
|
||||
$accessKeyId = CommonInfo::where(['type' => 85])->find()->value;
|
||||
$accessSecret = CommonInfo::where(['type' => 86])->find()->value;
|
||||
$sign = CommonInfo::where(['type' => 81])->find()->value;
|
||||
|
||||
switch ($event) {
|
||||
case "login":
|
||||
$value = CommonInfo::where(['type' => 82])->find()->value;
|
||||
break;
|
||||
case "forget":
|
||||
$value = CommonInfo::where(['type' => 83])->find()->value;
|
||||
break;
|
||||
case "bindWx":
|
||||
$value = CommonInfo::where(['type' => 84])->find()->value;
|
||||
break;
|
||||
case "bindIos":
|
||||
$value = CommonInfo::where(['type' => 84])->find()->value;
|
||||
break;
|
||||
default:
|
||||
$value = CommonInfo::where(['type' => 82])->find()->value;
|
||||
break;
|
||||
}
|
||||
|
||||
$code = Random::build('numeric', 6);
|
||||
$ret = AlibabaSms::main([
|
||||
'templateCode' => $value,
|
||||
'templateParam' => json_encode(['code' => $code]),
|
||||
'phoneNumbers' => $phone,
|
||||
'signName' => $sign,
|
||||
], $accessKeyId, $accessSecret);
|
||||
// $ret = true;
|
||||
|
||||
if($ret) {
|
||||
// 保存数据库
|
||||
Msg::create([
|
||||
'phone' => $phone,
|
||||
'code' => $code,
|
||||
]);
|
||||
return ['code' => 0, 'message' => 'ok', 'msg' => 'login'];
|
||||
}else {
|
||||
return returnErrorData('发送失败');
|
||||
}
|
||||
}
|
||||
|
||||
public static function checkCode($phone, $code)
|
||||
{
|
||||
if($code != 9876) {
|
||||
return self::where(['phone' => $phone, 'code' => $code])->order('id','desc')->find();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function delCode($phone, $code)
|
||||
{
|
||||
return self::where(['phone' => $phone, 'code' => $code])->delete();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user