增加地图自定义图标并显示客列表

This commit is contained in:
2026-08-03 16:01:36 +08:00
parent 841295da9e
commit 696c613b80
5 changed files with 214 additions and 30 deletions
+1 -1
View File
@@ -20,6 +20,6 @@ class CustomerLocationAdmin(admin.ModelAdmin):
'fields': ('address_zh', 'address_en', 'description_zh', 'description_en')
}),
('显示', {
'fields': ('marker_color', 'sort_order', 'is_active')
'fields': ('marker_color', 'marker_icon', 'sort_order', 'is_active')
}),
)
@@ -0,0 +1,18 @@
# Generated by Django 5.0.14 on 2026-08-03 08:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('customers', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='customerlocation',
name='marker_icon',
field=models.ImageField(blank=True, help_text='建议尺寸 32x32 或 48x48 像素的 PNG 图片,留空则使用默认蓝色圆点', null=True, upload_to='customers/markers/', verbose_name='自定义标记图标'),
),
]
+2
View File
@@ -11,6 +11,8 @@ class CustomerLocation(models.Model):
description_zh = models.TextField('描述说明(中文)', blank=True, default='')
description_en = models.TextField('描述说明(英文)', blank=True, default='')
marker_color = models.CharField('标记点颜色', max_length=20, default='#165DFF', help_text='十六进制颜色值')
marker_icon = models.ImageField('自定义标记图标', upload_to='customers/markers/', blank=True, null=True,
help_text='建议尺寸 32x32 或 48x48 像素的 PNG 图片,留空则使用默认蓝色圆点')
sort_order = models.IntegerField('排序', default=0)
is_active = models.BooleanField('启用', default=True)
created_at = models.DateTimeField('创建时间', auto_now_add=True)
+15 -12
View File
@@ -2,6 +2,7 @@ from django.shortcuts import render
from django.http import JsonResponse
from .models import CustomerLocation
from django.utils.translation import get_language
from django.conf import settings
def customer_map(request):
@@ -12,19 +13,21 @@ def customer_map(request):
def customer_locations_api(request):
lang = get_language()
suffix = 'en' if lang.startswith('en') else 'zh'
locations = CustomerLocation.objects.filter(is_active=True).values(
'id', f'name_{suffix}', f'address_{suffix}', f'description_{suffix}',
'latitude', 'longitude', 'marker_color'
)
locations = CustomerLocation.objects.filter(is_active=True).order_by('sort_order', '-id')
data = []
for loc in locations:
for index, loc in enumerate(locations, start=1):
icon_url = None
if loc.marker_icon:
icon_url = settings.MEDIA_URL + str(loc.marker_icon)
data.append({
'id': loc['id'],
'name': loc[f'name_{suffix}'],
'address': loc[f'address_{suffix}'],
'description': loc[f'description_{suffix}'],
'lat': loc['latitude'],
'lng': loc['longitude'],
'color': loc['marker_color'] or '#165DFF',
'id': loc.id,
'index': index,
'name': getattr(loc, f'name_{suffix}'),
'address': getattr(loc, f'address_{suffix}'),
'description': getattr(loc, f'description_{suffix}'),
'lat': loc.latitude,
'lng': loc.longitude,
'color': loc.marker_color or '#165DFF',
'icon': icon_url,
})
return JsonResponse({'locations': data})