$value) { $attach_param .= "&{$key}={$value}"; $attach_items .= " " . PHP_EOL; } if (empty($redirect_uri)) $redirect_uri = rawurlencode(getLocaleUrl()); if (empty($error_uri)) $error_uri = rawurlencode(dirname(getLocaleUrl()) . '/error.php'); $data = array( 'market_key' => $market_key, 'logintype' => $login_type, ); if (!is_null($silence)) $data['silence'] = $silence; $api_url = getFullUrl('/api/login/querylist'); $response = SendPost($api_url, $data); // 如果API调用失败,直接从数据库查询登录方式 if (empty($response)) { error_log("Login API Error: Empty response from $api_url, using database fallback"); try { // 直接连接数据库获取登录方式(从环境变量读取) require_once dirname(dirname(dirname(__DIR__))) . '/env_config.php'; $dsn = "mysql:host=" . env('API_DB_HOST', 'rm-bp1btyuwq77591x0jpo.mysql.rds.aliyuncs.com') . ":" . env('API_DB_PORT', '3306') . ";dbname=" . env('API_DB_NAME', 'youlehudong') . ";"; $username = env('API_DB_USER', 'games'); $passwd = env('API_DB_PASSWORD', 'Games0791!!'); $pdo = new PDO($dsn, $username, $passwd, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); if (empty($market_key)) { $condition = 'is_enabled = 1 and is_silence = ' . intval(empty($silence) ? 0 : $silence); if (!empty($login_type)) $condition .= sprintf(' and type_id = %d', intval($login_type)); $stmt = $pdo->prepare("SELECT type_id,type_key,type_name,image,url,component,is_third,third_flag FROM syweb_logintype_base WHERE $condition"); $stmt->execute(); } else { $condition = 'a.type_key = b.type_key and a.is_enabled = 1 and b.is_enabled = 1 and market_key = ? and a.is_silence = ' . intval(empty($silence) ? 0 : $silence); if (!empty($login_type)) $condition .= sprintf(' and a.type_id = %d', intval($login_type)); $stmt = $pdo->prepare("SELECT a.type_id,a.type_key,a.type_name,a.image,a.url,a.component,a.is_third,a.third_flag FROM syweb_logintype_base a, syweb_logintype_market b WHERE $condition"); $stmt->execute(array($market_key)); } $data = $stmt->fetchAll(PDO::FETCH_ASSOC); // 处理URL为完整路径 foreach ($data as $key => &$value) { $value['url'] = getFullUrl($value['url']); } $response = rawurlencode(json_encode($data, JSON_UNESCAPED_UNICODE)); } catch (Exception $e) { error_log("Database fallback error: " . $e->getMessage()); // 最后的备用方案:返回默认微信登录配置 $default_login_types = array( array( 'type_id' => 2, 'type_key' => '0002', 'type_name' => '微信登录', 'image' => 'img/wx.png', 'url' => getFullUrl('api/login/weixin'), 'component' => null, 'is_third' => 1, 'third_flag' => 'weixin' ) ); $response = rawurlencode(json_encode($default_login_types, JSON_UNESCAPED_UNICODE)); } } $result = json_decode(rawurldecode($response)); if (empty($result)) /// 为空表示没有获取到任何登录方式 { $html = << alert('找不到支援的任何登录方式, 请联系相关管理员!'); EOL; } else /// 正常 { $third_array = array(); /// 三方认证方式(不需要输入账号密码的方式) $owner_array = array(); /// 本地认证方式(需要输入账号密码的方式) foreach ($result as $item) { $item = (array)$item; if (0 != $item['is_third']) /// 三方认证方式 array_push($third_array, $item); else /// 本地认证方式 array_push($owner_array, $item); } $third_count = count($third_array); $owner_count = count($owner_array); $third_html = ''; $owner_html = ''; $splitter = ''; $forms = ''; if (0 == $owner_count && 1 == $third_count) /// 只存在一种三方的校验方式则直接跳转到目标连接 { $url = $third_array[0]['url'] . "?appid={$app_id}&devkey={$dev_key}&scode={$scode}&market_key={$market_key}&target={$redirect_uri}&fail_target={$error_uri}{$attach_param}"; header("Location: {$url}"); exit; } /// 如果两种方式都有,则需要分隔元素。 if (0 != $owner_count && 0 != $third_count) $splitter = <<
更多登录方式
EOL; if (0 != $owner_count) /// 如果本地验证方式不为空则需要用于输入账号密码的表单元素。 { $owner_html = '
' . PHP_EOL; $forms = ''; $index = 0; foreach ($owner_array as $owner) { $index++; /// 用来提交数据的表单 $forms .= <<
' . PHP_EOL; } if (0 != $third_count) { $third_html = '
' . PHP_EOL; $index = 0; foreach ($third_array as $third) { $index++; $url = $third['url'] . "?appid={$app_id}&devkey={$dev_key}&scode={$scode}&market_key={$market_key}&target={$redirect_uri}&fail_target={$error_uri}{$attach_param}"; $third_html .= << EOL; } } $html = $owner_html . $splitter . $third_html; } ?>