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.

85 lines
2.7 KiB

11 months 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.common.mobile_required'),
'mobile.numeric' => __('mobile.common.mobile_numeric'),
'type.numeric' => __('mobile.common.type_required'),
];
$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, __('mobile.common.send_too_frequent')]);
}
$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(__('mobile.common.send_success'));
}
return $this->fail([StarterResponseCode::START_ERROR_PARAMETER, __('mobile.common.send_failed')]);
}
}