tp6 openid获取 JWT中间件
生活随笔
收集整理的這篇文章主要介紹了
tp6 openid获取 JWT中间件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里寫自定義目錄標題
- 安裝tp6 和配置
- wx login登錄
- token使用
- 中間件使用
- 阿里云上傳
- 小程序
安裝tp6 和配置
1安裝tp6:命令: composer create-project topthink/think tp
2 創建多應用模式安裝擴展
composer require topthink/think-multi-app3 快速生成模塊應用 php think build demo
4 創建模塊下的 控制器類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();//獲取微信授權url$url = sprintf(config('wx.url'),config('wx.AppID'),config('wx.AppSecret'),$code);//獲取openid$data = curlGet($url);// var_dump($data);die();//進行查詢數據庫里面是否有該用戶,如果沒有,則進行新增$user = UserModel::where('openid',$data['openid'])->find();//如果沒有用戶進行創建if (empty($user)){$user = UserModel::create(['openid'=>$data['openid']]);}//生成token,保存用戶登錄狀態$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 文件里配置 系統自動生成的公共文件
<?php // 這是系統自動生成的公共文件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.在模塊下創建server服務層:下的Token控制器:
創建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 進行校驗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);} }路由:
//分組路由 加中間件驗證 Route::group(function (){//輪播圖Route::get('slideshow','Slideshow/index');//文件上傳wxuploadRoute::post('wxupload','Slideshow/upload');//查詢房屋朝向屬性Route::get('getFangAttr','Publish/getFangAttr');//發布出租的合租表單 AddSharedRoute::post('AddShared','Publish/AddShared'); })->middleware('check');//中間件驗簽創建config 的配置文件
oss.php
middleware.php
<?php // 中間件配置 return [// 別名或分組'alias' => ['check' => [app\middleware\CheckToken::class],],// 優先級設置,此數組中的中間件會按照數組中的順序優先執行'priority' => [], ];阿里云上傳
1 先安裝,我使用composer安裝
在項目的根目錄運行 composer require aliyuncs/oss-sdk-php
2.在模塊下創建server服務層:下的Oss控制器:
<?phpnamespace app\api\server;use OSS\OssClient; use OSS\Core\OssException;class Oss {public function uploadFile($filePath){// 阿里云主賬號AccessKey擁有所有API的訪問權限,風險很高。強烈建議您創建并使用RAM賬號進行API訪問或日常運維,請登錄 https://ram.console.aliyun.com 創建RAM賬號。$accessKeyId = config('oss.accessKeyId');$accessKeySecret = config('oss.accessKeySecret');// Endpoint以杭州為例,其它Region請按實際情況填寫。$endpoint = config('oss.endpoint');// 存儲空間名稱$bucket = config('oss.bucket');// <yourObjectName>上傳文件到OSS時需要指定包含文件后綴在內的完整路徑,例如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();//要上傳文件的臨時路徑$filePath = $_FILES['file']['tmp_name'];//阿里云上傳對象存儲$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發送請求
wx.request({url: 'http://www.lianxi.com/api/slideshow',header:{'token':wx.getStorageSync('token')},success:res=>{this.setData({slideshow:res.data})}})沒了
總結
以上是生活随笔為你收集整理的tp6 openid获取 JWT中间件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 随机选择list或nump
- 下一篇: Java--Integer的常量缓存池(