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.
 
 
 
 
 
 

115 lines
3.5 KiB

<?php
// phpinfo();
// exit;
class IncludeCommon
{
function CheckToken($token)
{
date_default_timezone_set("Asia/Taipei");
$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]) && !empty($explode[4])) {
$info = $explode[0] . '.' . $explode[1] . '.' . $explode[2] . '.' . $explode[3]; //信息部分
$true_signature = hash_hmac('md5', $info, $encryption_code); //正确的签名
if ($time > $explode[2]) {
$data['user_id'] = '';
$data['user_name'] = '';
$data['code'] = '401';
$data['message'] = 'Token已過期,請重新登入';
return $data;
}
if ($true_signature == $explode[4]) {
$data['user_id'] = $explode[0];
$data['user_name'] = $explode[3];
$data['code'] = '200';
$data['message'] = 'Token合法';
return $data;
} else {
$data['user_id'] = '';
$data['user_name'] = '';
$data['code'] = '400';
$data['message'] = 'Token不合法';
return $data;
}
} else {
$data['user_id'] = '';
$data['user_name'] = '';
$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, $user_name)
{
date_default_timezone_set("Asia/Taipei");
$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 . '.' . $user_name; //设置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];
}
}