| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\CityModel;
- /**
- * 城市管理-服务类
- * @author wesmiler
- * @since 2020/11/11
- * Class CityService
- * @package App\Services
- */
- class CityService extends BaseService
- {
- protected static $instance;
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/11
- * CityService constructor.
- */
- public function __construct()
- {
- $this->model = new CityModel();
- }
- /**
- * @return CityService
- */
- public static function make(){
- if(!self::$instance){
- self::$instance = new CityService();
- }
- return self::$instance;
- }
- /**
- * 获取城市列表
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function getList()
- {
- $param = request()->all();
- // 查询条件
- $map = [];
- // 上级ID
- $pid = intval(getter($param, 'pid', 0));
- if (!$pid) {
- $map[] = ['pid', '=', 0];
- } else {
- $map[] = ['pid', '=', $pid];
- }
- // 城市名称
- $name = getter($param, "name");
- if ($name) {
- $map[] = ['name', 'like', "%{$name}%"];
- }
- $list = $this->model->getList($map, [['sort', 'asc']]);
- if (!empty($list)) {
- foreach ($list as &$val) {
- if ($val['level'] <= 2) {
- $val['hasChildren'] = true;
- }
- }
- }
- return message("操作成功", true, $list);
- }
- /**
- * 获取城市列表
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function getPickerList()
- {
- $cacheKey = "caches:citys";
- $cityList = RedisService::get($cacheKey);
- if($cityList){
- return message("操作成功", true, $cityList);
- }
- $datas = $this->model::where(['mark'=> 1])
- ->select(['id','pid','level','name','citycode as code'])
- ->orderBy('sort')
- ->get()
- ->keyBy('id');
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- $cityList = $this->formatCity($datas);
- if($cityList){
- RedisService::set($cacheKey, $cityList, 3600);
- }
- }
- return message("操作成功", true, $cityList);
- }
- /*
- * 格式化
- */
- public function formatCity($datas, $parentId=0){
- $cityList = [];
- foreach ($datas as $key => $v){
- $id = isset($v['id'])? $v['id'] : 0;
- $pid = isset($v['pid'])? $v['pid'] : 0;
- if($pid == $parentId){
- $children = $this->formatCity($datas, $id);
- //if($children){
- $v['children'] = $children;
- //}
- $cityList[] = $v;
- }
- }
- return $cityList;
- }
- /**
- * 获取城市名称
- * @param $id
- * @return mixed
- */
- public function getName($id){
- return $this->model::where(['citycode'=> $id])->value('name');
- }
- }
|