# 1.文件上传类 >[info]FileClass 目录位于/libraries/class/FileClass 主要方法uploadFile方法,文件上传,上传图片后的路径位于public/attachment中 headUpload方法,主要用于上传头像,生成头像大小分辨率的文件 ## 1.1使用方法 * 文件上传控制 * @param Request $request * @return \Illuminate\Http\JsonResponse */ public function fileUpload(Request $request) { $file = $request->file('file'); //将文件上传的数据存入到attachment表中 $attachment = \FileClass::uploadFile($file, 'task'); $attachment = json_decode($attachment, true); //判断文件是否上传 if ($attachment['code'] != 200) { return response()->json(['errCode' => 0, 'errMsg' => $attachment['message']]); } $attachment_data = array_add($attachment['data'], 'status', 1); $attachment_data['created_at'] = date('Y-m-d H:i:s', time()); //将记录写入到attchement表中 $result = AttachmentModel::create($attachment_data); $result = json_decode($result, true); if (!$result) { return response()->json(['errCode' => 0, 'errMsg' => '文件上传失败!']); } //回传附件id return response()->json(['id' => $result['id']]); } # 2.极验验证GeetestLib ## 2.1后端代码 * 滑块请求图片生成 */ public function StartCaptchaServlet() { $GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY); session_start(); $data = array( "user_id" => "test", # 网站用户id "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式 "ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP ); $status = $GtSdk->pre_process($data, 1); $_SESSION['gtserver'] = $status; $_SESSION['user_id'] = $data['user_id']; echo $GtSdk->get_response_str(); } * 滑块请求二次验证 */ public function check() { session_start(); $GtSdk = new GeetestLib(CAPTCHA_ID, PRIVATE_KEY); $data = array( "user_id" => $_SESSION['user_id'], # 网站用户id "client_type" => "web", #web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生SDK植入APP应用的方式 "ip_address" => "127.0.0.1" # 请在此处传输用户请求验证时所携带的IP ); if ($_SESSION['gtserver'] == 1) { //服务器正常 $result = $GtSdk->success_validate($_POST['geetest_challenge'], $_POST['geetest_validate'], $_POST['geetest_seccode'], $data); if ($result) { echo '{"status":"success"}'; } else{ echo '{"status":"fail"}'; } }else{ //服务器宕机,走failback模式 if ($GtSdk->fail_validate($_POST['geetest_challenge'],$_POST['geetest_validate'],$_POST['geetest_seccode'])) { echo '{"status":"success"}'; }else{ echo '{"status":"fail"}'; } } } 2.2.前端js用法 var handlerEmbed = function (captchaObj) { $("#embed-submit").click(function (e) { var validate = captchaObj.getValidate(); if (!validate) { $("#notice")[0].className = "show"; setTimeout(function () { $("#notice")[0].className = "hide"; }, 2000); e.preventDefault(); } }); // 将验证码加到id为captcha的元素里,同时会有三个input的值:geetest_challenge, geetest_validate, geetest_seccode captchaObj.appendTo("#embed-captcha"); captchaObj.onReady(function () { $("#wait")[0].className = "hide"; }); // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html }; $.ajax({ // 获取id,challenge,success(是否启用failback) url: "../web/StartCaptchaServlet.php?t=" + (new Date()).getTime(), // 加随机数防止缓存 type: "get", dataType: "json", success: function (data) { console.log(data); // 使用initGeetest接口 // 参数1:配置参数 // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件 initGeetest({ gt: data.gt, challenge: data.challenge, new_captcha: data.new_captcha, product: "embed", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效 offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注 // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config }, handlerEmbed); } }); # 3.发送短信SmsClass类 * 发送短信 * * @param $mobile * @param array $templates ['服务商' => 'temp_id'] * @param array $data ['变量' => '值'] * @param string $content * @return string */ static function sendSms($mobile, $templates, $data, $content = '') { $to = $mobile; $status = Sms::make()->to($to)->template($templates)->data($data) ->content($content)->send(); return $status; } * 发送手机注册验证码 * * @param Request $request * @return string */ public function sendMobileApiCode(Request $request) { $arr = $request->except('_token'); $status = UserModel::where('mobile', $arr['mobile'])->first(); $detail = UserDetailModel::where('mobile', $arr['mobile'])->first(); if (isset($status) || isset($detail)) { return ['code' => 1001, 'msg' => '短信发送失败']; } //发送注册短信 $code = rand(1000, 9999); $templates = [ 'YunTongXun' => '152075', ]; $tempData = [ 'code' => $code, ]; $content = '【客客信息】你注册的验证码为' . $code; $status = \SmsClass::sendSms($arr['mobile'], $templates, $tempData, $content); if ($status['success'] == true) { $data = [ 'code' => $code, 'mobile' => $arr['mobile'] ]; Session::put('auth_mobile_info', $data); return ['code' => 1000, 'msg' => '短信发送成功']; } else { return ['code' => 1001, 'msg' => '短信发送失败']; } }