檢測接口加密類

namespace Api\Controller;
//API基礎控制層
use Think\Controller;
class BaseController extends Controller
{
protected accesstoken;privatestatic secret = “pxpkmm”;
public static $code = array(
0 => “成功”,

public function __construct()
{
    parent::__construct();
    self::checkEncrypt()? : self::response(-32003);
    self::checkIsPost()? :self::response(-32009);
}

/* 
* 檢測接口加密的access_token是否合法
 */
public static function checkEncrypt(){
    $request_data['access_token'] = isset($_REQUEST['access_token']) && trim($_REQUEST['access_token']) ? trim($_REQUEST['access_token']) : "";
    $request_data['time'] = isset($_REQUEST['time']) && trim($_REQUEST['time']) ? intval($_REQUEST['time']) : 0;
    if( $request_data['access_token'] != md5(self::$secret.$request_data['time']) ){
        $flag = false;
    }
    if( !$request_data['access_token'] || !$request_data['time']){
        $flag = false;
    }else{
        $flag = ture;
    }
     return $flag;
}


/*
 * 檢測接口有限期是否超時
 */
public static function checkValidTime(){
    $request_data['time'] = isset($_REQUEST['time']) && trim($_REQUEST['time']) ? intval($_REQUEST['time']) : 0;
    // var_dump($request_data['time'] - time());
    $flag = ture;
    if( $request_data['time'] - time() > 2 || $request_data['time'] - time() < 0){//每次接口 2 秒有效
        $flag = false;
    }
    return $flag;
}
public static function check($request_data){
    self::checkPara($request_data)? :self::response(-32001);
}

/**
 * 驗證是否是Post請求
 */
protected function checkIsPost()
{
    if (IS_POST) {
        return true;
    } else {
        return false;
    }
}

/**
 * 計算時間差/兩個時間日期相隔的天數,時,分,秒
 * $begin_time 爲第一個時間戳
 * $end_time 爲第二個時間戳
 */
static function timediff($begin_time, $end_time)
{
    if ($begin_time < $end_time) {
        $starttime = $begin_time;
        $endtime = $end_time;
    } else {
        $starttime = $end_time;
        $endtime = $begin_time;
    }
    $timediff = $endtime - $starttime;
    $days = intval($timediff / 86400);
    $remain = $timediff % 86400;
    $hours = intval($remain / 3600);
    $remain = $remain % 3600;
    $mins = intval($remain / 60);
    $secs = $remain % 60;
    return array(
        "day" => $days,
        "hour" => $hours,
        "min" => $mins,
        "sec" => $secs
    );
}
/*
 * 檢測接口參數是否有效
 */
public static function checkPara($request_data){
    $flag = true;
    foreach ($request_data as $key => $value) {
        if(!isset($value)){
            $flag = false;
        }
    }
    //var_dump($request_data);
    return $flag;
}
public static function response($code,$data=null,$message=null){
    $json_array["code"] = intval($code);
    $json_array["message"] = $message? $message : self::$code[$code];
    $json_array["time"] = time();
    if($data){
        $json_array["data"] = $data;
    }
    echo json_encode($json_array);
    exit;
}
//獲取用戶信息
public  function getUser($token){
    $MemberInfo=M('User')->field('id,nickname,head_img as img,source,money,userno,birthday')->where(array("token"=>$token))->find();
    return $MemberInfo?$MemberInfo:array();
}

//獲取上傳的任務圖片
// public function getTaskImg($info){
//      $imgarray=json_decode($info['img'],true);
//       foreach ($imgarray as $key => $value){
//          $upinfo='';
//          $base64=$imgarray[$key]['image'];
//          $upinfo = uploadImgBase($base64,$floder='TaskApp');
//          if($upinfo==-1){
//               $this->response(-32022);
//             }else if($upinfo==-2){
//               $this->response(-1);
//             }
//           $image.=trim(str_replace('./Uploads/','',$upinfo)).',';
//       }
//       $taskimg=rtrim($image,",");
//       return $taskimg;
// }

//獲取上傳的任務圖片
public function getTaskImg($info){
     $imgarray=json_decode($info['img'],true);
    // var_dump($imgarray);exit;
     $taskimg='';
      foreach ($imgarray as $key => $value){
         $upinfo='';
         $base64=$imgarray[$key]['image'];
         $upinfo = uploadImgBase($base64,$floder='TaskApp');
         if($upinfo==-1){
              $this->response(-32022);
            }else if($upinfo==-2){
              $this->response(-1);
            }
          $taskimg.=trim(str_replace('./Uploads/','',$upinfo)).',';
      }
     $taskimg=rtrim($taskimg,",");
      return $taskimg;
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章