You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

106 lines
3.2 KiB

<?php
class IncludeCommon
{
function CheckToken($token) {
date_default_timezone_set("Asia/Shanghai");
$encryption_code = "MASADA!^***";
$time = date("Y-m-d H:i:s");
if (!isset($token) || empty($token)) {
$data['code'] = '400';
$data['message'] = '非法請求';
return $data;
}
//對比token
$explode = explode('.', $token); //以.分割token為數組
if (!empty($explode[0]) && !empty($explode[1]) && !empty($explode[2]) && !empty($explode[3])) {
$info = $explode[0] . '.' . $explode[1] . '.' . $explode[2]; //信息部分
$true_signature = hash_hmac('md5', $info, $encryption_code); //正確的簽名
if ($time > $explode[2]) {
$data['user_id'] = '';
$data['code'] = '401';
$data['message'] = 'Token已過期,請重新登錄';
return $data;
}
if ($true_signature == $explode[3]) {
$data['user_id'] = $explode[0];
$data['code'] = '200';
$data['message'] = 'Token合法';
return $data;
} else {
$data['user_id'] = '';
$data['code'] = '400';
$data['message'] = 'Token不合法';
return $data;
}
} else {
$data['user_id'] = '';
$data['code'] = '400';
$data['message'] = 'Token不合法';
return $data;
}
}
//生成登錄記錄
function save_log_response($source_flag='web', $account, $response_result){
# 儲存紀錄並輸出
#echo date("Y-m-d");
$file_path = "account_log/" . $account . "-" . date("Y-m-d") . ".ini";
$data = serialize($response_result). date("H:i:s") . "\n";
$fp = fopen($file_path,"a"); # 如果檔案不存在擇嘗試建立再寫入
fwrite($fp, $data);
fclose($fp);
if ($source_flag == "web"){
// header("Location: custom-create.php?function_list=" . serialize($response_result) .
// "&token=" . $response_result["token"]); #請輸入使用者名稱密碼
// exit();
return true;
}else{
// echo json_encode($response_result, JSON_UNESCAPED_UNICODE);
return false;
}
/*
if ($source_flag == "app"){
echo json_encode($response_result, JSON_UNESCAPED_UNICODE);
}elseif ($source_flag == "web"){
header("Location: api-account-reply-functions.php?function_list=" . serialize($response_result) .
"&token=" . $response_result["token"]); #請輸入使用者名稱密碼
exit();
}
*/
}
//生成token
function CreateToken($user_id) {
date_default_timezone_set("Asia/Shanghai");
$start_time = date("Y-m-d H:i:s");
$end_time = date('Y-m-d H:i:s',strtotime('+24 hour')); #設定24小時
$info = $user_id . '.' . $start_time . '.' . $end_time; //設置token過期時間為一天
$encryption_code = "MASADA!^***";
//根據以上信息信息生成簽名(密鑰為 SIGNATURE 自定義全局常量)
$signature = hash_hmac('md5', $info, $encryption_code);
//最後將這兩部分拼接起來,得到最終的Token字符串
return $token = $info . '.' . $signature;
}
//生成菜單
function menu ($from='tw',$type){
$function_type = array(
"tw" => array(
# F角色菜單
"H" => array(
"language" => "tw",
"type" => "manage",
"list" => array(
array("name" => "批量導入", "path" => "http://192.168.0.12/wms/excel-upload"),
),
"note" => "none",
"token" => ""
),
)
);
return $function_type[$from][$type];
}
}