支付宝接口
This commit is contained in:
@@ -24,6 +24,7 @@ class Fastphp
|
||||
public function run(){
|
||||
global $is_script;
|
||||
spl_autoload_register(array($this,'loadClass'));
|
||||
$this->origin();
|
||||
$this->setReporting();
|
||||
$this->unregisterGlobals();
|
||||
$this->setDbConfig();
|
||||
@@ -32,6 +33,20 @@ class Fastphp
|
||||
}
|
||||
}
|
||||
|
||||
//跨域请求处理
|
||||
public function origin(){
|
||||
$allow_origin = $this->config['origin'];//跨域访问的时候才会存在此字段
|
||||
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
|
||||
if (in_array($origin, $allow_origin)) {
|
||||
header('Access-Control-Allow-Origin:' . $origin);
|
||||
header("Access-Control-Allow-Methods: *");
|
||||
header('Access-Control-Allow-Headers:*');
|
||||
header("Access-Control-Allow-Credentials: true");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//路由处理
|
||||
public function route(){
|
||||
$controllerName = $this->config['defaultController'];
|
||||
|
||||
@@ -9,25 +9,35 @@ namespace fastphp\base;
|
||||
*/
|
||||
class Controller
|
||||
{
|
||||
protected $_controller;
|
||||
protected $_action;
|
||||
// protected $_view;
|
||||
public $userinfo;
|
||||
|
||||
//初始化属性,实例化对应模型
|
||||
public function __construct($controller,$action)
|
||||
{
|
||||
$this->_controller = $controller;
|
||||
$this->_action = $action;
|
||||
// $this->_view = new View($controller,$action);
|
||||
$data = json_decode(file_get_contents("php://input"),true);
|
||||
$getPayload = false;
|
||||
if(isset($data['cookie'])){
|
||||
$cookie = explode('; ',$data['cookie']);
|
||||
$token = '';
|
||||
foreach($cookie as $k => $v){
|
||||
$find_token = explode('=',$v);
|
||||
if($find_token[0] == 'token'){
|
||||
$token = $find_token[1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
$jwt = new Jwt('hncore_yh_lzh_20f_2020_READY');
|
||||
$getPayload=$jwt->verifyToken($token);
|
||||
|
||||
} else {
|
||||
$jwt = new Jwt('etor_yh_lzh_20f_2020_YES');
|
||||
$getPayload=$jwt->verifyToken($_SERVER['HTTP_TOKEN']);
|
||||
}
|
||||
|
||||
|
||||
if(!$getPayload){
|
||||
die;
|
||||
}
|
||||
$this->userinfo = $getPayload;
|
||||
}
|
||||
|
||||
// //分配变量
|
||||
// public function assign($name,$value){
|
||||
// $this->_view->assign($name,$value);
|
||||
// }
|
||||
//
|
||||
// //渲染视图
|
||||
// public function render(){
|
||||
// $this->_view->render();
|
||||
// }
|
||||
}
|
||||
132
fastphp/base/Jwt.php
Normal file
132
fastphp/base/Jwt.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace fastphp\base;
|
||||
|
||||
/**
|
||||
* PHP实现jwt
|
||||
*/
|
||||
class Jwt {
|
||||
|
||||
//头部
|
||||
private static $header=array(
|
||||
'alg'=>'HS256', //生成signature的算法
|
||||
'typ'=>'JWT' //类型
|
||||
);
|
||||
|
||||
//使用HMAC生成信息摘要时所使用的密钥
|
||||
private static $key;
|
||||
|
||||
public function __construct($key){
|
||||
self::$key = $key;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取jwt token
|
||||
* @param array $payload jwt载荷 格式如下非必须
|
||||
* [
|
||||
* 'iss'=>'jwt_admin', //该JWT的签发者
|
||||
* 'iat'=>time(), //签发时间
|
||||
* 'exp'=>time()+7200, //过期时间
|
||||
* 'nbf'=>time()+60, //该时间之前不接收处理该Token
|
||||
* 'sub'=>'www.admin.com', //面向的用户
|
||||
* 'jti'=>md5(uniqid('JWT').time()) //该Token唯一标识
|
||||
* ]
|
||||
* @return bool|string
|
||||
*/
|
||||
public static function getToken(array $payload)
|
||||
{
|
||||
if(is_array($payload))
|
||||
{
|
||||
$base64header=self::base64UrlEncode(json_encode(self::$header,JSON_UNESCAPED_UNICODE));
|
||||
$base64payload=self::base64UrlEncode(json_encode($payload,JSON_UNESCAPED_UNICODE));
|
||||
$token=$base64header.'.'.$base64payload.'.'.self::signature($base64header.'.'.$base64payload,self::$key);
|
||||
return $token;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证token是否有效,默认验证exp,nbf,iat时间
|
||||
* @param string $Token 需要验证的token
|
||||
* @return bool|string
|
||||
*/
|
||||
public static function verifyToken(string $Token)
|
||||
{
|
||||
$tokens = explode('.', $Token);
|
||||
if (count($tokens) != 3)
|
||||
return false;
|
||||
|
||||
list($base64header, $base64payload, $sign) = $tokens;
|
||||
|
||||
//获取jwt算法
|
||||
$base64decodeheader = json_decode(self::base64UrlDecode($base64header), JSON_OBJECT_AS_ARRAY);
|
||||
if (empty($base64decodeheader['alg']))
|
||||
return false;
|
||||
|
||||
//签名验证
|
||||
if (self::signature($base64header . '.' . $base64payload, self::$key, $base64decodeheader['alg']) !== $sign)
|
||||
return false;
|
||||
|
||||
$payload = json_decode(self::base64UrlDecode($base64payload), JSON_OBJECT_AS_ARRAY);
|
||||
|
||||
//签发时间大于当前服务器时间验证失败
|
||||
if (isset($payload['iat']) && $payload['iat'] > time())
|
||||
return false;
|
||||
|
||||
//过期时间小宇当前服务器时间验证失败
|
||||
if (isset($payload['exp']) && $payload['exp'] < time())
|
||||
return false;
|
||||
|
||||
//该nbf时间之前不接收处理该Token
|
||||
if (isset($payload['nbf']) && $payload['nbf'] > time())
|
||||
return false;
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* base64UrlEncode https://jwt.io/ 中base64UrlEncode编码实现
|
||||
* @param string $input 需要编码的字符串
|
||||
* @return string
|
||||
*/
|
||||
private static function base64UrlEncode(string $input)
|
||||
{
|
||||
return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
|
||||
}
|
||||
|
||||
/**
|
||||
* base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现
|
||||
* @param string $input 需要解码的字符串
|
||||
* @return bool|string
|
||||
*/
|
||||
private static function base64UrlDecode(string $input)
|
||||
{
|
||||
$remainder = strlen($input) % 4;
|
||||
if ($remainder) {
|
||||
$addlen = 4 - $remainder;
|
||||
$input .= str_repeat('=', $addlen);
|
||||
}
|
||||
return base64_decode(strtr($input, '-_', '+/'));
|
||||
}
|
||||
|
||||
/**
|
||||
* HMACSHA256签名 https://jwt.io/ 中HMACSHA256签名实现
|
||||
* @param string $input 为base64UrlEncode(header).".".base64UrlEncode(payload)
|
||||
* @param string $key
|
||||
* @param string $alg 算法方式
|
||||
* @return mixed
|
||||
*/
|
||||
private static function signature(string $input, string $key, string $alg = 'HS256')
|
||||
{
|
||||
$alg_config=array(
|
||||
'HS256'=>'sha256'
|
||||
);
|
||||
return self::base64UrlEncode(hash_hmac($alg_config[$alg], $input, $key,true));
|
||||
}
|
||||
}
|
||||
@@ -23,4 +23,25 @@ class Model extends Sql
|
||||
$this->table = strtolower($this->model);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取总数目
|
||||
*
|
||||
*/
|
||||
public function getCount(){
|
||||
return $this->field('count(1) as count')->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照页数获取数据
|
||||
* @param $fields 'id,count(1)...'
|
||||
* @param $order 'id desc'/'id asc'
|
||||
* @param $limit = '100' 限制查询100条
|
||||
* $limit = '2,100' 查询第二页 100条数据
|
||||
*/
|
||||
public function getListPage($fields = '*',$order = 'id desc', $page = '50')
|
||||
{
|
||||
return $this->field($fields)->order($order)->limit($page)->fetchAll();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,7 +23,9 @@ class Sql
|
||||
*
|
||||
* @param $where 条件
|
||||
* @return $this
|
||||
* ['id'=>1] 或者 '`id`=1'
|
||||
* ['id'=>1] 或者 '`id`=1' 尽量不要使用string 因为没有做防sql注入
|
||||
* 多条件查询
|
||||
* ['id'=>['<',100]]
|
||||
*/
|
||||
public function where($where){
|
||||
$this->param = [];
|
||||
@@ -63,10 +65,11 @@ class Sql
|
||||
* 拼装排序条件
|
||||
* @param array $order 排序条件
|
||||
* @return $this
|
||||
* $order='id desc'
|
||||
*/
|
||||
public function order($order = 'id',$type = 'desc'){
|
||||
public function order($order = 'id desc'){
|
||||
if($order){
|
||||
$this->filter .= ' ORDER BY '.$order.' '.$type.' ';
|
||||
$this->filter .= ' ORDER BY '.$order.' ';
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
@@ -76,11 +79,12 @@ class Sql
|
||||
* group by
|
||||
* @param array $order
|
||||
* @return $this
|
||||
* $group=['sex','name']
|
||||
*/
|
||||
public function group($order = []){
|
||||
public function group($group = []){
|
||||
if($order){
|
||||
$this->filter .= ' GROUP BY ';
|
||||
$this->filter .= ' '.implode(' ,',$order).' ';
|
||||
$this->filter .= ' '.implode(' ,',$group).' ';
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
@@ -89,6 +93,8 @@ class Sql
|
||||
* 查询limit
|
||||
* @param string
|
||||
* @return $this
|
||||
* $limit = '100' 限制查询100条
|
||||
* $limit = '2,100' 查询第二页 100条数据
|
||||
*/
|
||||
public function limit($limit = '100'){
|
||||
$this->filter .= ' LIMIT '.$limit.' ';
|
||||
@@ -99,6 +105,7 @@ class Sql
|
||||
* 查询字段
|
||||
* @param string $field
|
||||
* @return $this
|
||||
* $field = 'id,count(1),sub(num)'
|
||||
*/
|
||||
public function field($field = '*'){
|
||||
$this->field = $field;
|
||||
|
||||
@@ -43,4 +43,8 @@ function echoBase($var, $echo=true, $label=null, $strict=true) {
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
function JWTDecode($token,$_secret,$verify = true){
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user