<?php

    /**
     * 状态码说明
     * 200 成功
     * 201 未登录
     * 202 失败
     * 203 空值
     * 204 无结果
     */

	// 页面编码
	header("Content-type:application/json");
	
	// 判断登录状态
    session_start();
    if(isset($_SESSION["yinliubao"])){
        
        // 已登录
        $chatroom_id = trim($_GET['chatroom_id']);
        
        // 过滤参数
        if(empty($chatroom_id) || !isset($chatroom_id)){
            
            // 非法请求
            $result = array(
			    'code' => 203,
                'msg' => '非法请求'
		    );
        }else{
            
            // 数据库配置
        	include '../../../../Db.php';
        
        	// 实例化类
        	$db = new DB_API($config);
        	
        	// 验证操作权限
            $checkAdmin = $db->set_table('huoma_user')->find(['user_name' => $_SESSION["yinliubao"]]);
            $user_admin = $checkAdmin['user_admin'];
            
            if($user_admin == 2) {
                
                $result = array(
                    'code' => 202,
                    'msg' => '无操作权限'
                );
                echo json_encode($result,JSON_UNESCAPED_UNICODE);
                exit;
            }
        	
            // 获取详情
            $getChatRoomInfo = $db->set_table('ylb_ChatroomPay')->find(['chatroom_id' => $chatroom_id]);
            
            // 返回数据
            if($getChatRoomInfo){
                
                // 域名
                $chatroom_domain = $getChatRoomInfo['chatroom_domain'];
                
                // 生成longUrl
                // 获取当前请求的URI
                $currentUri = $_SERVER["REQUEST_URI"];
                
                // 解析URI并构建根路径
                $parsedUrl = parse_url($currentUri);
                $pathComponents = explode('/', $parsedUrl['path']);
                
                // 去掉最后六个路径部分
                $rootPathComponents = array_slice($pathComponents, 0, -6);
                $rootPath = implode('/', $rootPathComponents);
                
                // 构建完整的URL
                $longUrl = $chatroom_domain . $rootPath . '/common/addchatroombyinvite/?crid=' . base64_encode($chatroom_id) . '#wechat_redirect';
                
                // 有结果
                $result = array(
        		    'code' => 200,
        		    'msg' => '获取成功',
        		    'longUrl' => $longUrl,
        		    'qrcodeUrl' => $longUrl.'&t='.time(),
    		    );
            }else{
                
                // 无结果
                $result = array(
        		    'code' => 204,
        		    'msg' => '获取失败'
    		    );
            }
        }
    	
    }else{
        
        // 未登录
        $result = array(
			'code' => 201,
            'msg' => '未登录'
		);
    }

	// 输出JSON
	echo json_encode($result, JSON_UNESCAPED_UNICODE);
	
?>