下載安裝composer
composer使用國內鏡像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
通過composer安裝ThinkPHP5.1
composer create-project topthink/think 項目名稱
通過composer安裝jwt
composer require firebase/php-jwt
ThinkPHP5.1配置
// config/app.php
<?php
return [
// 開啟調試
'app_debug' => true,
// 禁止訪問模塊
'deny_module_list' => [],
];
// config/log.php
<?php
return [
'type' => 'File',
'path' => '../logs/',
'level' => [],
'single' => false,
'apart_level' => [],
'max_files' => 0,
'close' => false,
'json' => true,
];
// application/common.php
// 此文件的意義,個人理解是定義全局變量和全局函數的
<?php
const ERRNO_MAP = [
'OK' => '成功',
'DBERR' => '數據庫查詢錯誤',
'NODATA' => '無數據',
'DATAEXIST' => '數據已存在',
'DATAERR' => '數據錯誤',
'SESSIONERR' => '用戶未登錄',
'LOGINERR' => '用戶登錄失敗',
'PARAMERR' => '參數錯誤',
'USERERR' => '用戶不存在或未激活',
'ROLEERR' => '用戶身份錯誤',
'PWDERR' => '密碼錯誤',
'REQERR' => '非法請求或請求次數受限',
'IPERR' => 'IP受限',
'THIRDERR' => '第三方系統(tǒng)錯誤',
'IOERR' => '文件讀寫錯誤',
'SERVERERR' => '內部錯誤',
'UNKOWNERR' => '未知錯誤',
];
const ERRNO = [
'OK' => '0',
'DBERR' => '4001',
'NODATA' => '4002',
'DATAEXIST' => '4003',
'DATAERR' => '4004',
'SESSIONERR' => '4101',
'LOGINERR' => '4102',
'PARAMERR' => '4103',
'USERERR' => '4104',
'ROLEERR' => '4105',
'PWDERR' => '4106',
'REQERR' => '4201',
'IPERR' => '4202',
'THIRDERR' => '4301',
'IOERR' => '4302',
'SERVERERR' => '4500',
'UNKOWNERR' => '4501',
];
// 向前端返回JSON數據
function ajaxReturn() {
// 形參個數
$args_num = func_num_args();
// 形參列表
$args = func_get_args();
if (1 === $args_num) {
return \json([
'errno' => ERRNO['OK'],
'msg' => '成功',
'data' => $args[0]]);
}
if (2 === $args_num) {
return \json([
'errno' => $args[0],
'msg' => $args[1]]);
}
if (3 === $args_num) {
return \json([
'errno' => $args[0],
'msg' => $args[1],
'data' => $args[2]]);
}
throw new Exception("Error The number of parameters can be one or two or three");
}
use \Firebase\JWT\JWT;
// 設置JWT
function setJWT($data) {
$jwt = new JWT();
$token = array(
// "iss" => "http://example.org", // 簽發(fā)者
// "aud" => "http://example.com", // 認證者
'iat' => time(), // 簽發(fā)時間
'nbf' => time(), // 生效時間
'exp' => (time() + 60 * 60 * 24 * 7), // 過期時間 7天后的時間戳
'data' => $data,
);
$jwt = $jwt::encode($token, \config('jwt_key'), 'HS256');
return $jwt;
}
// 獲取JWT內容
function getJWT($token) {
$jwt = new JWT();
$data = null;
try {
$jwt_data = $jwt::decode($token, \config('jwt_key'), array('HS256'));
$data = (array) ($jwt_data->data);
} catch (\Throwable $e) {
Log::write($e->getMessage(), 'error');
return null;
}
return $data;
}
// application/common/controller/Common.php
<?php
namespace app\common\controller;
class Common {
public function miss() {
return \json([
'errno' => \ERRNO['PARAMERR'],
'msg' => '訪問接口不存在或參數錯誤']);
}
}
// application/common/controller/Authen.php
<?php
namespace app\common\controller;
use app\common\controller\Common;
class Authen extends Common {
// 用戶信息
protected $user_info;
public function initialize() {
$token = \input('server.http_token');
// 驗證是否登錄
if (is_null($token)) {
header('Content-Type:application/json; charset=utf-8');
exit(json_encode([
'code' => ERRNO['SESSIONERR'],
'error' => '用戶未登陸']));
}
// 驗證登錄是否過期
$user_info = \getJWT($token);
if (is_null($user_info)) {
header('Content-Type:application/json; charset=utf-8');
exit(json_encode([
'code' => ERRNO['SESSIONERR'],
'error' => '登錄已過期']));
}
// 存儲用戶信息
$this->user_info = $user_info;
}
}
// application/test[應用名稱]/config/database.php
<?php
return [
'type' => 'mysql',
'hostname' => '127.0.0.1',
'database' => 'test',
'username' => 'root',
'password' => 'root',
'hostport' => '3306',
];
// application/test[應用名稱]/controller/v1/Teacher.php
<?php
namespace app\test\controller\v1;
// 不需要認證的話繼承Common
use app\common\controller\Common;
// 需要登錄驗證的繼承Authen
// use app\common\controller\Authen;
use Log;
class Teacher extends Common {
// 查 getTest
// 增 postTest
// 改 putTest
// 刪 deleteTest
public function getStudents() {
return \ajaxReturn(ERRNO['OK'],'查詢成功',['list'=>[]]);
}
}
// route/route.php
<?php
// 定義miss路由
Route::miss('common/Common/miss');
// route/test[應用名稱].php
<?php
Route::group('test/v1.0', function () {
Route::group('teacher', function () {
// /test/v1.0/teacher/students
Route::get('students', 'getStudents');
})->prefix('test/v1.teacher/');
})
->ext(false)
->header('Access-Control-Allow-Headers', 'token')
->allowCrossDomain()
->pattern(['id' => '\d+']);
部署到linux上的問題
// 刪除.user.ini文件
chattr -i ~/public/.user.ini
rm -f ~/public/.user.ini
// 改變目錄權限
chmod -R 777 ~