You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.6 KiB

3 years ago
<?php
namespace App\Http\Controllers\Mobile;
use App\Helpers\ResponseCode;
use App\Helpers\StarterResponseCode;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
class CommonController extends Controller
{
public $guardName = "mobile";
public function guard()
{
return auth()->guard($this->guardName);
}
public function getUser()
{
return $this->guard()->user();
}
public function getUserId()
{
return $this->guard()->id();
}
/**
* @OA\Get (
* path="/api/mobile/common/send-sms",
* tags={"小程序-通用"},
* summary="短信发送",
* description="",
* @OA\Parameter(name="type", in="query", @OA\Schema(type="int"), required=true, description="类型1参观预约验证码 2活动预约验证码"),
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=true, description="手机号"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description=""
* )
* )
*/
public function sendSms(){
$all = \request()->all();
$messages = [
'mobile.required' => '手机号必填',
'mobile.numeric' => '手机号格式错误',
'type.numeric' => '类型必填',
];
$validator = Validator::make($all, [
'mobile' => 'required|numeric',
'type' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$key = $all['mobile'];
if($all['type'] == 1){
// 参观预约验证码
$key = 'visit_'.$key;
}
if($all['type'] == 2){
// 参观预约验证码
$key = 'activity_'.$key;
}
$check = Cache::get($key);
if(isset($check) && time() - $check['time'] <= 60){
return $this->fail([ResponseCode::ERROR_BUSINESS, '请勿频繁发送']);
}
$code = randStr(4,true);
$template_id = 'Pat5Z3';
$result = sms($all['mobile'], ['code'=>$code,'minutes'=>5], $template_id);
if($result){
// 缓存
Cache::put($key,['code'=>$code,'time'=>time()],5);
return $this->success("发送成功");
}
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER,"发送失败"]);
}
}