124 行
4.0 KiB
PHP
124 行
4.0 KiB
PHP
<?php
|
||
// 使用curl模拟完整的登录和接口调用流程
|
||
|
||
// 1. 先获取登录页面的session
|
||
$ch = curl_init();
|
||
curl_setopt($ch, CURLOPT_URL, 'http://mss.nahaichuan.com/index.php/Retail/Login/index');
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
|
||
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
|
||
$response = curl_exec($ch);
|
||
curl_close($ch);
|
||
|
||
echo "获取登录页面完成\n";
|
||
|
||
// 2. 执行登录
|
||
$ch = curl_init();
|
||
curl_setopt($ch, CURLOPT_URL, 'http://mss.nahaichuan.com/index.php/Retail/Login/doLogin_v1');
|
||
curl_setopt($ch, CURLOPT_POST, true);
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
|
||
'username' => '13888888888',
|
||
'password' => 'test123'
|
||
)));
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
|
||
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
||
'Content-Type: application/x-www-form-urlencoded'
|
||
));
|
||
$login_response = curl_exec($ch);
|
||
curl_close($ch);
|
||
|
||
echo "登录响应: " . $login_response . "\n\n";
|
||
|
||
// 解析登录响应,获取用户数据
|
||
$login_data = json_decode($login_response, true);
|
||
echo "解析后的登录数据: " . print_r($login_data, true) . "\n";
|
||
|
||
if ($login_data && $login_data['status'] == 1 && isset($login_data['list']) && !empty($login_data['list'])) {
|
||
// 选择第一个用户进行登录
|
||
$user = $login_data['list'][0];
|
||
echo "选择用户: " . $user['username'] . " (ID: " . $user['id'] . ")\n";
|
||
|
||
// 3. 模拟选择用户并设置session(直接调用PHP代码)
|
||
echo "\n=== 设置用户Session ===\n";
|
||
|
||
// 启动session
|
||
session_start();
|
||
|
||
// 模拟doUserLogin方法的逻辑
|
||
if ($user['user_type'] == 0) {
|
||
// 新建角色 - 这里简化处理
|
||
$user['login_type'] = 3; // 假设是商户
|
||
} else {
|
||
$user['login_type'] = $user['user_type'];
|
||
}
|
||
|
||
// 设置jurisdiction_user session
|
||
$_SESSION['jurisdiction_user'] = $user;
|
||
echo "已设置 jurisdiction_user session\n";
|
||
|
||
// 根据用户类型设置其他session
|
||
switch ($user['login_type']) {
|
||
case 3:
|
||
// 商户
|
||
$_SESSION['merchant_userinfo'] = array('id' => $user['system_user_id']);
|
||
$_SESSION['identity'] = 1;
|
||
echo "已设置商户相关session\n";
|
||
break;
|
||
case 4:
|
||
// 店长
|
||
$_SESSION['store_userinfo'] = array('id' => $user['system_user_id']);
|
||
$_SESSION['storeid'] = $user['storeid'] ?? 1;
|
||
$_SESSION['identity'] = 2;
|
||
echo "已设置店长相关session\n";
|
||
break;
|
||
}
|
||
|
||
echo "Session设置完成\n\n";
|
||
} else {
|
||
echo "登录失败或无用户数据\n";
|
||
exit;
|
||
}
|
||
|
||
// 4. 直接调用storeSet方法测试权限
|
||
echo "\n=== 权限调试信息 ===\n";
|
||
echo "当前请求路径: /index.php/Retail/Xcx/storeSet\n";
|
||
echo "Session信息检查...\n";
|
||
|
||
// 模拟POST数据
|
||
$_POST['store_name'] = '测试门店名称';
|
||
$_POST['store_address'] = '测试门店地址';
|
||
$_POST['store_phone'] = '13800138000';
|
||
|
||
// 包含必要的框架文件
|
||
define('APP_PATH', './Application/');
|
||
define('RUNTIME_PATH', './Runtime/');
|
||
require './ThinkPHP/ThinkPHP.php';
|
||
|
||
// 直接实例化控制器并调用方法
|
||
try {
|
||
$controller = new \Retail\Controller\XcxController();
|
||
|
||
// 检查session状态
|
||
echo "jurisdiction_user session: " . (isset($_SESSION['jurisdiction_user']) ? 'EXISTS' : 'NOT SET') . "\n";
|
||
if (isset($_SESSION['jurisdiction_user'])) {
|
||
echo "用户ID: " . $_SESSION['jurisdiction_user']['id'] . "\n";
|
||
echo "用户类型: " . $_SESSION['jurisdiction_user']['user_type'] . "\n";
|
||
}
|
||
|
||
echo "\n=== 调用storeSet方法 ===\n";
|
||
|
||
// 捕获输出
|
||
ob_start();
|
||
$controller->storeSet();
|
||
$output = ob_get_clean();
|
||
|
||
echo "storeSet方法输出: " . $output . "\n";
|
||
|
||
} catch (Exception $e) {
|
||
echo "调用出错: " . $e->getMessage() . "\n";
|
||
echo "错误文件: " . $e->getFile() . ":" . $e->getLine() . "\n";
|
||
}
|
||
|
||
?>
|