tp6 openid获取 JWT中间件
這里寫自定義目錄標(biāo)題
- 安裝tp6 和配置
- wx login登錄
- token使用
- 中間件使用
- 阿里云上傳
- 小程序
安裝tp6 和配置
1安裝tp6:命令: composer create-project topthink/think tp
2 創(chuàng)建多應(yīng)用模式安裝擴(kuò)展
composer require topthink/think-multi-app3 快速生成模塊應(yīng)用 php think build demo
4 創(chuàng)建模塊下的 控制器類php think make:controller index@Blog
wx login登錄
1 控制器登錄層
<?php declare (strict_types = 1);namespace app\api\controller;use app\api\model\user as UserModel; use app\api\server\Token as TokenServer; use think\Request;class Login {public function wxLogin(Request $request){//echo 1234;die();//獲取code碼$code = $request->get('code'); //var_dump($code);die();//獲取微信授權(quán)url$url = sprintf(config('wx.url'),config('wx.AppID'),config('wx.AppSecret'),$code);//獲取openid$data = curlGet($url);// var_dump($data);die();//進(jìn)行查詢數(shù)據(jù)庫里面是否有該用戶,如果沒有,則進(jìn)行新增$user = UserModel::where('openid',$data['openid'])->find();//如果沒有用戶進(jìn)行創(chuàng)建if (empty($user)){$user = UserModel::create(['openid'=>$data['openid']]);}//生成token,保存用戶登錄狀態(tài)$token = (new TokenServer())->generateToken($user->id);return json(['token'=>$token,'error_code'=>0,'msg'=>'登錄成功']);//return json(['token'=>$token,'error_code'=>0,'msg'=>'登錄成功','openid'=>$user['openid']]);}}在模塊下的common.php 文件里配置 系統(tǒng)自動(dòng)生成的公共文件
<?php // 這是系統(tǒng)自動(dòng)生成的公共文件function curlGet($url){$headerArray =array("Content-type:application/json;","Accept:application/json");$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);$output = curl_exec($ch);curl_close($ch);$output = json_decode($output,true);return $output; }token使用
1安裝jwt命令composer require firebase/php-jwt
2.在模塊下創(chuàng)建server服務(wù)層:下的Token控制器:
創(chuàng)建config 的配置文件
jwt.php
<?php return [ 'salt'=>'a426bcc5f31e42527755afa13cc5f191' ];
wx.php
中間件使用
1, 安裝中間命令:
php think make:middleware Check2middleware目錄下的CheckToken.php文件下 配置
<?php declare (strict_types = 1);namespace app\middleware;use app\api\server\Token as TokenServer; use think\response\Json;class CheckToken {/*** 處理請求** @param \think\Request $request* @param \Closure $next* @return Response*/public function handle($request, \Closure $next){//第一步先取token$token=$request->header('token');//jwt 進(jìn)行校驗(yàn)token$res=(new TokenServer())->chekToken($token);if ($res['code']!=1){return json(['error_code'=>999,'msg'=>$res['msg'],'data'=>''],400);}$request->uid=$res['data']->uid;return $next($request);} }路由:
//分組路由 加中間件驗(yàn)證 Route::group(function (){//輪播圖Route::get('slideshow','Slideshow/index');//文件上傳wxuploadRoute::post('wxupload','Slideshow/upload');//查詢房屋朝向?qū)傩?/span>Route::get('getFangAttr','Publish/getFangAttr');//發(fā)布出租的合租表單 AddSharedRoute::post('AddShared','Publish/AddShared'); })->middleware('check');//中間件驗(yàn)簽創(chuàng)建config 的配置文件
oss.php
middleware.php
<?php // 中間件配置 return [// 別名或分組'alias' => ['check' => [app\middleware\CheckToken::class],],// 優(yōu)先級(jí)設(shè)置,此數(shù)組中的中間件會(huì)按照數(shù)組中的順序優(yōu)先執(zhí)行'priority' => [], ];阿里云上傳
1 先安裝,我使用composer安裝
在項(xiàng)目的根目錄運(yùn)行 composer require aliyuncs/oss-sdk-php
2.在模塊下創(chuàng)建server服務(wù)層:下的Oss控制器:
<?phpnamespace app\api\server;use OSS\OssClient; use OSS\Core\OssException;class Oss {public function uploadFile($filePath){// 阿里云主賬號(hào)AccessKey擁有所有API的訪問權(quán)限,風(fēng)險(xiǎn)很高。強(qiáng)烈建議您創(chuàng)建并使用RAM賬號(hào)進(jìn)行API訪問或日常運(yùn)維,請登錄 https://ram.console.aliyun.com 創(chuàng)建RAM賬號(hào)。$accessKeyId = config('oss.accessKeyId');$accessKeySecret = config('oss.accessKeySecret');// Endpoint以杭州為例,其它Region請按實(shí)際情況填寫。$endpoint = config('oss.endpoint');// 存儲(chǔ)空間名稱$bucket = config('oss.bucket');// <yourObjectName>上傳文件到OSS時(shí)需要指定包含文件后綴在內(nèi)的完整路徑,例如abc/efg/123.jpg$fileName = date('Y-m-d', time()) . '/' . md5(time() . rand(1111, 9999999)) . '.png';try {$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);$result = $ossClient->putObject($bucket, $fileName, file_get_contents($filePath));} catch (OssException $e) {print $e->getMessage();}return $result['info']['url'];} }3,在控制器中;
<?php declare (strict_types = 1);namespace app\api\controller;use app\api\model\Slideshow as SlideshowModel; use app\api\server\Oss;class Slideshow {//文件上傳public function upload(){//echo 133;die();//要上傳文件的臨時(shí)路徑$filePath = $_FILES['file']['tmp_name'];//阿里云上傳對(duì)象存儲(chǔ)$fileName = (new Oss())->uploadFile($filePath);return json(['code'=>200,'msg'=>'上傳成功','url'=>$fileName]);}//查詢輪播圖public function index(){$data = SlideshowModel::select()->toArray();return json_encode($data);}}小程序
存取Token
//緩存 wx.setStorageSync('token', token) //取值 var tonken2 = wx.getStorageSync('token')console.log(tonken2);帶Token發(fā)送請求
wx.request({url: 'http://www.lianxi.com/api/slideshow',header:{'token':wx.getStorageSync('token')},success:res=>{this.setData({slideshow:res.data})}})沒了
總結(jié)
以上是生活随笔為你收集整理的tp6 openid获取 JWT中间件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 随机选择list或nump
- 下一篇: minSdk deviceSdk的问题