<?php
require '../include/common.php';

session_name('user_session');
session_start();


if (isset($_SESSION['user']) && isset($_SESSION['user']['social_uid']) && isset($_SESSION['user']['type'])) {
    $openid = $_SESSION['user']['social_uid']; 
    $type = $_SESSION['user']['type']; 

    
    $field = '';
    if ($type === 'qq') {
        $field = 'qq_uid';
    } elseif ($type === 'wx') {
        $field = 'wx_uid';
    } elseif ($type === 'alipay') {
        $field = 'alipay_uid';
    } else {
        exit("无效的用户类型！");
    }

    
    $stmt = $pdo->prepare("SELECT * FROM " . table('users') . " WHERE $field = :openid");
    $stmt->execute(['openid' => $openid]);
    $user = $stmt->fetch();

    if ($user) {
        
        $_SESSION['user_id'] = $user['id']; 
        $_SESSION['role'] = $user['role']; 

        
        
        
        
        
        
        
        
        

        
        header("Location: ../user/index.php"); 
        exit();
    } else {
        echo "用户不存在或绑定信息不匹配！";
    }
} else {
    echo "会话中没有提供openid或type！";
}
?>
