<?php
session_start();
$expire = 1800;
if (empty($_SESSION['user_id']) || (time() - $_SESSION['login_time'] > $expire)) {
    echo json_encode(["code"=>401,"msg"=>"未登录"]);
    exit;
}
header("Content-Type:application/json;charset=utf-8");
$userFile = "./user_info.json";
if(!file_exists($userFile)){
    echo json_encode(["code"=>500,"msg"=>"账号配置文件缺失"]);
    exit;
}
$userData = json_decode(file_get_contents($userFile),true);
$act = $_POST['act'] ?? '';

if($act == "change_pwd"){
    $old = $_POST['old_pwd'];
    $new = $_POST['new_pwd'];
    $confirm = $_POST['confirm_pwd'];
    if($userData['password'] != md5($old)){
        echo json_encode(["code"=>400,"msg"=>"原密码错误"]);
        exit;
    }
    if($new != $confirm){
        echo json_encode(["code"=>400,"msg"=>"两次新密码不一致"]);
        exit;
    }
    $userData['password'] = md5($new);
    file_put_contents($userFile,json_encode($userData,JSON_UNESCAPED_UNICODE));
    echo json_encode(["code"=>0,"msg"=>"密码修改成功，请重新登录"]);
    exit;
}
echo json_encode(["code"=>400,"msg"=>"非法操作"]);
?>
