From 6e523592918eb7588cddffe895ad106d12c1b208 Mon Sep 17 00:00:00 2001 From: yanzai Date: Sat, 24 Aug 2024 22:14:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=9F=AD=E4=BF=A1=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/SmsController.php | 175 ++++++++++++++++++ app/Http/Controllers/UserPersonController.php | 8 + config/code.php | 5 + routes/web.php | 2 + 北京国康小程序/api/api.js | 3 +- 北京国康小程序/api/index.js | 7 +- 北京国康小程序/pages/user/edit/edit.vue | 173 +++++++++++------ 7 files changed, 319 insertions(+), 54 deletions(-) create mode 100644 app/Http/Controllers/SmsController.php diff --git a/app/Http/Controllers/SmsController.php b/app/Http/Controllers/SmsController.php new file mode 100644 index 0000000..0fb2e58 --- /dev/null +++ b/app/Http/Controllers/SmsController.php @@ -0,0 +1,175 @@ +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; + } + +} diff --git a/app/Http/Controllers/UserPersonController.php b/app/Http/Controllers/UserPersonController.php index db33a38..9828eb3 100644 --- a/app/Http/Controllers/UserPersonController.php +++ b/app/Http/Controllers/UserPersonController.php @@ -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; diff --git a/config/code.php b/config/code.php index ea4b96a..b3e5c7c 100644 --- a/config/code.php +++ b/config/code.php @@ -142,4 +142,9 @@ return [ 200091 => '调用分诊接口出错', 200092 => '团检登记时间范围过期', + 200093 => '手机号码格式不正确', + 200094 => '请稍后再发送', + 200095 => '验证码无效', + 200096 => '短信发送失败', + 200097 => '请选择生日', ]; diff --git a/routes/web.php b/routes/web.php index 8fb5aec..9231506 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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发送验证码 diff --git a/北京国康小程序/api/api.js b/北京国康小程序/api/api.js index f0534c4..7c9478c 100644 --- a/北京国康小程序/api/api.js +++ b/北京国康小程序/api/api.js @@ -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_; diff --git a/北京国康小程序/api/index.js b/北京国康小程序/api/index.js index 7f8432d..6b76c97 100644 --- a/北京国康小程序/api/index.js +++ b/北京国康小程序/api/index.js @@ -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 diff --git a/北京国康小程序/pages/user/edit/edit.vue b/北京国康小程序/pages/user/edit/edit.vue index 7bd2b48..5447715 100644 --- a/北京国康小程序/pages/user/edit/edit.vue +++ b/北京国康小程序/pages/user/edit/edit.vue @@ -5,13 +5,15 @@ * date:2023年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; + }) + }