vr-shopxo-plugin/shopxo/app/plugins/vr_ticket/service/CityService.php

172 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\plugins\vr_ticket\service;
use think\facade\Db;
/**
* 城市服务层 - VR票务插件
* 用于获取和管理城市列表数据,支撑城市过滤功能
*/
class CityService
{
/**
* 获取所有可用城市列表(叶子节点城市,即区县级别)
* @param array $params
* @return array
*/
public static function getCityList($params = [])
{
// 查询可用城市
// 优先使用已配置的城市过滤条件
$where = [
['is_enable', '=', 1],
];
// 如果指定了父级ID
if (!empty($params['pid'])) {
$where[] = ['pid', '=', intval($params['pid'])];
}
// 如果指定了层级
if (isset($params['level']) && is_numeric($params['level'])) {
$where[] = ['level', '=', intval($params['level'])];
}
// 字段
$field = empty($params['field']) ? 'id,pid,name,level,letters' : $params['field'];
$orderBy = empty($params['order_by']) ? 'sort asc,id asc' : $params['order_by'];
return Db::name('Region')
->field($field)
->where($where)
->order($orderBy)
->select()
->toArray();
}
/**
* 获取热门城市列表
* @param int $limit
* @return array
*/
public static function getHotCities($limit = 10)
{
// 预设热门城市ID可根据实际业务配置
$hotCityIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // 默认北京、上海、广州、深圳等
return Db::name('Region')
->field('id,pid,name,level,letters')
->where(['id' => $hotCityIds, 'is_enable' => 1])
->order('sort asc,id asc')
->select()
->toArray();
}
/**
* 获取省份列表
* @return array
*/
public static function getProvinces()
{
return self::getCityList(['level' => 1]);
}
/**
* 根据父级ID获取子城市列表
* @param int $pid
* @return array
*/
public static function getCitiesByPid($pid)
{
return self::getCityList(['pid' => intval($pid)]);
}
/**
* 获取城市名称
* @param int $cityId
* @return string|null
*/
public static function getCityName($cityId)
{
if (empty($cityId)) {
return null;
}
$name = Db::name('Region')
->where(['id' => intval($cityId), 'is_enable' => 1])
->value('name');
return $name ?: null;
}
/**
* 获取当前选中的城市信息
* @param int $cityId
* @return array|null
*/
public static function getSelectedCity($cityId)
{
if (empty($cityId)) {
return null;
}
$city = Db::name('Region')
->field('id,pid,name,level,letters')
->where(['id' => intval($cityId), 'is_enable' => 1])
->find();
return $city ?: null;
}
/**
* 构建城市选择器所需的三级联动数据结构
* @return array
*/
public static function getCitySelectorData()
{
// 省份(一级)
$provinces = self::getProvinces();
// 标记有子城市的城市为非叶子节点
if (!empty($provinces)) {
foreach ($provinces as &$province) {
// 查询是否有子城市
$hasChildren = Db::name('Region')
->where(['pid' => $province['id'], 'is_enable' => 1])
->count();
$province['has_children'] = $hasChildren > 0 ? 1 : 0;
}
}
return [
'provinces' => $provinces ?: [],
];
}
/**
* 获取城市树形结构(用于级联选择)
* @param int $pid
* @return array
*/
public static function getRegionTree($pid = 0)
{
$pid = intval($pid);
$regions = Db::name('Region')
->field('id,pid,name,level,letters')
->where(['pid' => $pid, 'is_enable' => 1])
->order('sort asc,id asc')
->select()
->toArray();
if (!empty($regions)) {
foreach ($regions as &$region) {
// 递归获取子节点
$children = self::getRegionTree($region['id']);
$region['children'] = $children ?: [];
}
}
return $regions;
}
}