文件
fenleixinxi/application/admin/controller/fc/Area.php
MiAn 133d425ec0 feat: 新增阿里云OSS适配器及分类图标优化
- 新增阿里云OSS SDK核心类库文件
- 新增OSS插件配置文件及钩子监听
- 新增分类图标SVG资源并优化为轻量化格式
- 替换定位接口为天地图逆地理编码接口
- 新增OSS图片缩略处理工具方法
2026-11-20 11:11:03 +08:00

169 行
4.9 KiB
PHP

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace app\admin\controller\fc;
use app\common\controller\Backend;
use think\Db;
/**
* 同城-服务区域(省 / 市 / 区县平铺,区县为最终服务区域)
*
* @icon fa fa-map-o
*/
class Area extends Backend
{
/**
* FcArea模型对象
* @var \app\admin\model\FcArea
*/
protected $model = null;
/**
* 快速搜索字段
* @var string
*/
protected $searchFields = 'id,province,city,district';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\FcArea;
}
/**
* 新增:保存前校验
*/
public function add()
{
$this->assignAreaOptions();
if ($this->request->isPost()) {
$this->validateArea($this->request->post('row/a'), 0);
}
return parent::add();
}
public function afterAdd($row)
{
\app\common\library\NationalArea::invalidate();
}
/**
* 编辑:保存前校验
* @param string $ids
*/
public function edit($ids = null)
{
$this->assignAreaOptions();
if ($this->request->isPost()) {
$this->validateArea($this->request->post('row/a'), (int)$this->request->param('ids'));
}
return parent::edit($ids);
}
public function afterEdit($row)
{
\app\common\library\NationalArea::invalidate();
}
public function afterDel($ids)
{
\app\common\library\NationalArea::invalidate();
}
public function afterMulti($params)
{
\app\common\library\NationalArea::invalidate();
}
/**
* 省份下拉选项(不重复、enabled=1 的省份列表)
* GET ajax → ['北京市', '天津市', '贵州省', ...]
*/
public function provinces()
{
$rows = Db::name('fc_area')
->where('enabled', 1)
->group('province')
->order('province asc')
->column('province');
$this->success('', null, $rows ?: []);
}
/**
* 恢复全国默认(导入覆盖层)
* 操作:清空 fa_fc_area 覆盖层,从 national_areas.json 导入全国 3600 个区县,全部 enabled=1。
* POST ajax = 确认按钮触发。
*/
public function import()
{
if ($this->request->isPost()) {
$this->checkToken();
try {
$count = \app\common\library\NationalArea::importFromJson();
$this->success("已恢复全国默认,导入 {$count} 条区域记录");
} catch (\Throwable $e) {
$this->error('恢复失败:' . $e->getMessage());
}
}
return $this->view->fetch();
}
/**
* 省 / 市 / 区县已添加记录,供表单级联下拉合并全国行政区划及已添加状态标记使用
*/
protected function assignAreaOptions()
{
$rows = Db::name('fc_area')
->field('id,province,city,district,lat,lng')
->order('province asc, city asc, district asc')
->select();
$this->view->assign('areaOptionsJson', json_encode($rows, JSON_UNESCAPED_UNICODE));
}
/**
* 保存前校验:省 / 市 / 区县均必填,同一组合不可重复
* @param array $params 表单数据
* @param int $id 编辑时的区域ID
*/
protected function validateArea($params, $id = 0)
{
$province = isset($params['province']) ? trim((string)$params['province']) : '';
$city = isset($params['city']) ? trim((string)$params['city']) : '';
$district = isset($params['district']) ? trim((string)$params['district']) : '';
$values = ['省份' => $province, '城市' => $city, '区县' => $district];
foreach ($values as $label => $value) {
if ($value === '') {
$this->error($label . '不能为空');
}
if (mb_strlen($value) > 32) {
$this->error($label . '不能超过 32 个字符');
}
}
$duplicate = Db::name('fc_area')
->where('province', $province)
->where('city', $city)
->where('district', $district)
->where('id', '<>', $id)
->find();
if ($duplicate) {
$this->error('该省市区组合已存在');
}
foreach (['lat' => 90, 'lng' => 180] as $field => $limit) {
if (!isset($params[$field]) || $params[$field] === '' || $params[$field] === null) {
continue;
}
if (!is_numeric($params[$field]) || (float)$params[$field] < -$limit || (float)$params[$field] > $limit) {
$this->error($field === 'lat' ? '纬度格式不正确' : '经度格式不正确');
}
}
if (isset($params['sort']) && $params['sort'] !== '' && (int)$params['sort'] < 0) {
$this->error('排序权重不能为负数');
}
}
}