增加短信验证

main
yanzai 1 year ago
parent 4319f6dd58
commit 6e52359291

@ -0,0 +1,175 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Login;
use Yo;
class SmsController extends Controller
{
public $accessKeyId = 'LTAI5t9b1Wb2vyqLvzYbDnMs';
public $accessKeySecret = 'I8eBmBa9ysd0U641NYqI0Y7twSplme';
public $signName = '北京国康';
public $TemplateCode = 'SMS_162522586';
public $Lifespan=1;//验证码有效期单位分钟
public function SendSms(Request $request)
{
Login::user();
// $lifespan=10;//验证码有效期单位分钟
$phone = $request->post('phone');
if(strlen($phone)!==11) Yo::error_echo(200093);
$code=$this->generateCode();
if(strlen($code)===4){
$cha=DB::table('sms')->where(['phone'=>$phone])->orderBy('id','desc')->first();
if(!!$cha){
$specificTimeTimestamp = strtotime($cha->created_at);
// 计算一分钟之后的时间戳
$oneMinuteLaterTimestamp = strtotime('+1 minute', $specificTimeTimestamp);
if($oneMinuteLaterTimestamp>time()){
Yo::error_echo(200094);
}
}
$end_time = strtotime('+'.$this->Lifespan.' minute', time());
$in=DB::table('sms')->insert([
'code'=>$code,
'phone'=>$phone,
'status'=>0,
'lifespan'=>$this->Lifespan,
'end_time'=>date('Y-m-d H:i:s', $end_time)
]);
if($in){
//调用发送短信接口
$templateParam = json_encode(['code' => $code], JSON_UNESCAPED_UNICODE);
$send= self::sendSms2($phone,$templateParam);
if($send->Code=="OK"){
return Yo::echo(['status' => true]);
}else{
Yo::error_echo(200096);
}
}
}
}
//校验短信验证码
public function CheckCode($phone,$code)
{
$nowtime=date('Y-m-d H:i:s');
$cha=DB::table('sms')->where(['phone'=>$phone,'code'=>$code,'status'=>0,['end_time','>',$nowtime]])->first();
if(!!$cha){
$u=DB::table('sms')->where(['id'=>$cha->id])->update([
'status'=>1
]);
if($u){
return true;
}
}else{
return false;
}
}
// 生成随机验证码
function generateCode($length = 4) {
$characters = '0123456789';
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $characters[rand(0, strlen($characters) - 1)];
}
return $code;
}
public function testSms(){
$templateParam = json_encode(['code' => '0245'], JSON_UNESCAPED_UNICODE);
$send= self::sendSms2('19933509886',$templateParam);
return $send;
}
public function sendSms2($mobile,$paramString) {
//$mobile='19933509886';
// $paramString='';
$params = array ();
$accessKeyId = $this->accessKeyId;
$accessKeySecret = $this->accessKeySecret;
$params["PhoneNumbers"] = $mobile;
$params["SignName"] = $this->signName;
$params["TemplateCode"] = $this->TemplateCode;
$params['TemplateParam'] = $paramString;
$content = $this->request(
$accessKeyId,
$accessKeySecret,
"dysmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SendSms",
"Version" => "2017-05-25",
))
);
return $content;
}
public function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false) {
$apiParams = array_merge(array (
"SignatureMethod" => "HMAC-SHA1",
"SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
"SignatureVersion" => "1.0",
"AccessKeyId" => $accessKeyId,
"Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
"Format" => "JSON",
), $params);
ksort($apiParams);
$sortedQueryStringTmp = "";
foreach ($apiParams as $key => $value) {
$sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
}
$stringToSign = "GET&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
$sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
$signature = $this->encode($sign);
$url = ($security ? 'https' : 'http')."://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}";
try {
$content = $this->fetchContent($url);
return json_decode($content);
} catch( \Exception $e) {
return false;
}
}
private function encode($str)
{
$res = urlencode($str);
$res = preg_replace("/\+/", "%20", $res);
$res = preg_replace("/\*/", "%2A", $res);
$res = preg_replace("/%7E/", "~", $res);
return $res;
}
private function fetchContent($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"x-sdk-client" => "php/2.0.0"
));
if(substr($url, 0,5) == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$rtn = curl_exec($ch);
if($rtn === false) {
trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
}
curl_close($ch);
return $rtn;
}
}

@ -36,9 +36,11 @@ class UserPersonController extends Controller
$birthday = $request->post('birthday');
$sex = $request->post('sex');
$phone = $request->post('phone');
$code = $request->post('code');
$relationship = $request->post('relationship');
$marriage = $request->post('marriage');
$default = $request->post('default');
$user_person_default_count = UserPerson::where('user', Login::$info->id)
->where('del', 2)->where('default', 1)->count();
if ($user_person_default_count == 0) $default = 1;
@ -46,6 +48,12 @@ class UserPersonController extends Controller
UserPerson::where('user', Login::$info->id)
->where('del', 2)->where('default', 1)->update(['default' => 2]);
}
if(!isset($birthday)) Yo::error_echo(200097);
//校验短信验证码
$Sms=new SmsController();
$checkCode= $Sms->CheckCode($phone,$code);
if(!$checkCode) Yo::error_echo(200095);
$default = $user_person_default_count > 0 ? 2 : 1;
$user_person = new UserPerson();
$user_person->user = Login::$info->id;

@ -142,4 +142,9 @@ return [
200091 => '调用分诊接口出错',
200092 => '团检登记时间范围过期',
200093 => '手机号码格式不正确',
200094 => '请稍后再发送',
200095 => '验证码无效',
200096 => '短信发送失败',
200097 => '请选择生日',
];

@ -175,3 +175,5 @@ Route::post("api/$admin_api/Chat/changeWorkOrder", [\App\Http\Controllers\ChatCo
Route::post("api/$mp_api/Fenzhen/Dengji", [\App\Http\Controllers\FenZhenController::class, 'FenZhenDengJI']);
Route::any("api/$mp_api/Fenzhen/ChaXun", [\App\Http\Controllers\FenZhenController::class, 'FenZhenChaXun']);
Route::any("api/$mp_api/Appointment/SelectToday", [\App\Http\Controllers\AppointmentController::class, 'SelectToday']);//选择当日
Route::any("api/$mp_api/sms/SendSms", [\App\Http\Controllers\SmsController::class, 'SendSms']);//发送验证码
Route::any("api/$mp_api/sms/testSendSms", [\App\Http\Controllers\SmsController::class, 'testSms']);//ceshi发送验证码

@ -2,7 +2,7 @@ let url_ = "https://bjgk-api.sixinyun.com";
let report_url_ = "https://bjgk-api.sixinyun.com";
//let report_url_ = "http://192.168.31.106:5173";
let h5_url_ = "https://bjgk-api.sixinyun.com";
const dev =1;
const dev =0;
if (dev === 1) {
url_ = "http://beijingguokang";
report_url_ = "http://192.168.31.106:5173";
@ -42,6 +42,7 @@ url_array['Carousel/list'] = `${url_}/api/Mp/Carousel/list`;
url_array['Config/get'] = `${url_}/api/Mp/Config/get`;
url_array['Fenzhen/chaxun'] = `${url_}/api/Mp/Fenzhen/ChaXun`;
url_array['Appointment/SelectToday'] = `${url_}/api/Mp/Appointment/SelectToday`;
url_array['Sms/SendSms'] = `${url_}/api/Mp/sms/SendSms`;
url_array['YO'] = `${url_}/api/yo`;
const api = (mark) => {
if (mark === '') return url_;

@ -4,7 +4,7 @@ import {
import $api from './api.js'
let url_ = "https://bjgk-api.sixinyun.com";
let chat_url = "https://bjgk-api.sixinyun.com"
const dev = 1
const dev = 0
if (dev === 1) {
url_ = "http://beijingguokang"
chat_url = "http://192.168.31.106:5173"
@ -133,6 +133,11 @@ export const AppointmentSelectToday = async (data) => await $post({
url: 'Appointment/SelectToday',
data
})
export const SmsSendSmsAction = async (data) => await $post({
url: 'Sms/SendSms',
data
})
export const yo = async (data) => await $post({
url: 'YO',
data

@ -5,13 +5,15 @@
* date2023年4月12日 21:39:00
*/
import {
ref,watch
ref,
watch,nextTick
} from 'vue'
import {
UserPersonCreateAction,
UserPersonUpdateAction,
UserPersonCountAction,
UserPersonInfoAction,
SmsSendSmsAction,
$image,
$response
} from '@/api'
@ -33,14 +35,14 @@
setTitle()
})
//
const SysGreyType=ref(0)
const GetGreySet=()=>{
const SysGreyType = ref(0)
const GetGreySet = () => {
uni.getStorage({
key: 'SysGreytype',
success: function (res) {
success: function(res) {
console.log(res.data);
if(res.data==1){
SysGreyType.value=1
if (res.data == 1) {
SysGreyType.value = 1
}
}
});
@ -54,7 +56,7 @@
title: title
})
}
const default_user_person_info = {
id: 0,
name: '',
@ -172,17 +174,19 @@
const birthdayChange = (e) => {
console.log(e)
}
//
watch(()=>user_person_info.value.id_number, (newVal, oldVal) => {
if(Number($props.id) === 0){
if(newVal.length==18){
user_person_info.value.birthday= newVal.substring(6,10)+'-'+newVal.substring(10,12)+'-'+newVal.substring(12,14)
}else{
user_person_info.value.birthday=''
}
}
watch(() => user_person_info.value.id_number, (newVal, oldVal) => {
if (Number($props.id) === 0) {
if (newVal.length == 18) {
user_person_info.value.birthday = newVal.substring(6, 10) + '-' + newVal.substring(10, 12) + '-' +
newVal.substring(12, 14)
} else {
user_person_info.value.birthday = ''
}
}
});
const birthday_ref = ref(null)
const birthdayRef = (e) => {
@ -212,13 +216,13 @@
}
const saveClick = async () => {
if(tongyi.value!='tongyi'){
uni.showToast({
title: "请阅读并同意用户协议",
icon:'none'
})
return false;
}
if (tongyi.value != 'tongyi') {
uni.showToast({
title: "请阅读并同意用户协议",
icon: 'none'
})
return false;
}
const response = !user_person_info.value.id ?
await UserPersonCreateAction(user_person_info.value) :
await UserPersonUpdateAction(user_person_info.value)
@ -228,15 +232,45 @@
})
})
}
let tongyi=ref(false)
const tongyiclick=(e)=>{
tongyi.value=e.detail.value
let tongyi = ref(false)
const tongyiclick = (e) => {
tongyi.value = e.detail.value
}
//
const sendSmsFunc = async () => {
if(user_person_info.value.birthday.length<1){
uni.showToast({
title: '请选择生日',
icon:"none"
});
return false
}
uni.showLoading()
const response = await SmsSendSmsAction({
'phone': user_person_info.value.phone
})
uni.hideLoading();
$response(response, () => {
if(response.data.status===true){
uni.showToast({
title: '短信发送成功',
duration: 2000
});
}
})
}
const filterInput=(event)=> {
// 使
const filteredValue = event.target.value.replace(/[^a-zA-Z0-9]/g, '');
nextTick(()=>{
user_person_info.value.id_number = filteredValue;
})
}
</script>
<template>
<view :class="SysGreyType==1? 'grey' :''">
<uni-datetime-picker :ref="birthdayRef" class="input_line_datetime_picker_wrapper" type="date" :clear-icon="false"
v-model="user_person_info.birthday" @change="birthdayChange" />
<uni-datetime-picker :ref="birthdayRef" class="input_line_datetime_picker_wrapper" type="date"
:clear-icon="false" v-model="user_person_info.birthday" @change="birthdayChange" />
<view class="input_wrapper">
<view class="input_line_wrapper">
<view class="input_line_title_wrapper">姓名</view>
@ -270,7 +304,7 @@
<view class="input_line_wrapper">
<view class="input_line_title_wrapper">证件号</view>
<view class="input_line_input_wrapper">
<input type="text" v-model="user_person_info.id_number" placeholder="请输入">
<input @input="filterInput" type="text" v-model="user_person_info.id_number" placeholder="请输入">
</view>
</view>
<view class="input_line_wrapper">
@ -282,17 +316,29 @@
<image :src="$image('/storage/assets/mp/user/箭头@2x.png')"></image>
</view>
</view>
<view class="input_line_wrapper">
<view class="input_line_wrapper2">
<view class="input_line_title_wrapper">手机号</view>
<view class="input_line_input_wrapper" style="width: 350rpx;">
<input type="text" v-model="user_person_info.phone" style="width: 350rpx;" placeholder="请输入">
</view>
<view v-if="Number($props.id) === 0" class="send_button" @click="sendSmsFunc()">
获取验证码
</view>
</view>
<view class="input_line_wrapper" v-if="Number($props.id) === 0">
<view class="input_line_title_wrapper">验证码</view>
<view class="input_line_input_wrapper">
<input type="text" v-model="user_person_info.phone" placeholder="请输入">
<input type="text" v-model="user_person_info.code" placeholder="请输入">
</view>
</view>
<view class="input_line_wrapper">
<view class="input_line_title_wrapper">关系</view>
<view class="input_line_input_wrapper">
<picker @change="relationshipChange" :value="user_person_info.relationship_select" :range="relationship_arr"
range-key="label">
<picker @change="relationshipChange" :value="user_person_info.relationship_select"
:range="relationship_arr" range-key="label">
<view>{{ user_person_info.relationship_name || '请选择' }}</view>
</picker>
</view>
@ -309,7 +355,7 @@
</view>
</view>
<view class="break_block_wrapper"></view>
<!-- <view @click="readmeClick()" class="readme_wrapper">添加或修改体检人信息</view>
<!-- <view @click="readmeClick()" class="readme_wrapper">添加或修改体检人信息</view>
<view @click="readmeClick()" class="readme_wrapper">
即代表您已同意
<text class="blue_text_wrapper">用户服务协议</text>
@ -317,20 +363,20 @@
<text class="blue_text_wrapper">隐私协议</text>
</view> -->
<view style="display: flex; padding-left: 30rpx; align-items: center;">
<view>
<checkbox-group @change="tongyiclick">
<label>
<checkbox value="tongyi" />已阅读并同意
</label>
</checkbox-group>
</view>
<view @click="readmeClick()" class="readme_wrapper">
<text class="blue_text_wrapper">用户服务协议</text>
<text class="blue_text_wrapper">隐私协议</text>
</view>
<view>
<checkbox-group @change="tongyiclick">
<label>
<checkbox value="tongyi" />已阅读并同意
</label>
</checkbox-group>
</view>
<view @click="readmeClick()" class="readme_wrapper">
<text class="blue_text_wrapper">用户服务协议</text>
<text class="blue_text_wrapper">隐私协议</text>
</view>
</view>
<view @click="saveClick()" class="add_button_wrapper">保存</view>
<view class="blank_wrapper"></view>
@ -440,7 +486,30 @@
border-bottom: 1rpx #EBEBEB solid;
position: relative;
}
.grey{
filter: grayscale(100%);
}
.input_line_wrapper2 {
display: flex;
height: 110rpx;
align-items: center;
line-height: 110rpx;
width: 690rpx;
margin: 0 auto;
border-bottom: 1rpx #EBEBEB solid;
position: relative;
}
.grey {
filter: grayscale(100%);
}
.send_button {
border: 1rpx solid #ccc;
font-size: 24rpx;
height: 60rpx;
line-height: 60rpx;
padding-left: 10rpx;
padding-right: 10rpx;
border-radius: 10rpx;
margin-left: 20rpx;
}
</style>
Loading…
Cancel
Save