Files
duolamaojiazhen/application/admin/controller/BulletinBoard.php
gitfjn 6963216a85 添加根据经纬度获取城市
添加后台统计看板
2025-12-26 16:26:15 +08:00

124 lines
3.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\admin\controller;
use app\admin\model\User;
use app\common\server\UrlServer;
use think\facade\Url;
class BulletinBoard extends AdminBase
{
//看板统计
public function index()
{
return $this->fetch();
}
//地图上显示注册用户位置
public function map()
{
return $this->fetch();
}
public function getUserMap()
{
// 查询有经纬度的用户
$userArray = User::where('latitude', '<>', '')
->where('longitude', '<>', '')
->field('id,nickname,mobile,avatar,longitude,latitude')
->select();
// 格式化数据,转换为前端期望的字段名
$result = [];
foreach ($userArray as $user) {
// 处理头像URL使用代理接口避免CORS问题
$avatar = '';
if (!empty($user['avatar'])) {
$originalUrl = UrlServer::getFileUrl($user['avatar']);
// 去掉JSON编码时产生的转义反斜杠
$originalUrl = str_replace('\\/', '/', $originalUrl);
// 如果URL是跨域的使用代理接口
$currentDomain = $this->request->domain();
if (strpos($originalUrl, $currentDomain) === false) {
// 跨域使用代理手动构建URL避免双重编码
$avatar = $this->request->domain() . '/admin/bulletin_board/proxyImage?url=' . rawurlencode($originalUrl);
} else {
// 同域,直接使用
$avatar = $originalUrl;
}
}
$result[] = [
'id' => $user['id'] ?? 0,
'lng' => floatval($user['longitude'] ?? 0),
'lat' => floatval($user['latitude'] ?? 0),
'name' => $user['nickname'] ?? '',
'contact' => $user['nickname'] ?? '', // 兼容两种字段名
'mobile' => $user['mobile'] ?? '',
'phone' => $user['mobile'] ?? '', // 兼容 telephone/phone
'telephone' => $user['mobile'] ?? '',
'avatar' => $avatar
];
}
return $this->_success('获取成功', $result);
}
/**
* 图片代理接口解决CORS跨域问题
*/
public function proxyImage()
{
$url = $this->request->get('url', '');
if (empty($url)) {
header('HTTP/1.1 404 Not Found');
exit;
}
// 解码URL
$url = urldecode($url);
// 验证URL格式
if (!filter_var($url, FILTER_VALIDATE_URL)) {
header('HTTP/1.1 400 Bad Request');
exit;
}
// 只允许图片格式
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
$extension = strtolower(pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION));
if (!in_array($extension, $allowedExtensions)) {
header('HTTP/1.1 403 Forbidden');
exit;
}
// 获取图片
$context = stream_context_create([
'http' => [
'method' => 'GET',
'timeout' => 10,
'header' => [
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
]
]
]);
$imageData = @file_get_contents($url, false, $context);
if ($imageData === false) {
header('HTTP/1.1 404 Not Found');
exit;
}
// 设置响应头
header('Content-Type: image/' . ($extension === 'jpg' ? 'jpeg' : $extension));
header('Cache-Control: public, max-age=3600');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
// 输出图片
echo $imageData;
exit;
}
}