配置修改
This commit is contained in:
178
app/common/logic/CityLogic.php
Normal file
178
app/common/logic/CityLogic.php
Normal file
@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\basics\Logic;
|
||||
use app\common\model\DevRegion;
|
||||
use app\common\model\distribution\DistributionMemberApply;
|
||||
use app\common\model\shop\Shop;
|
||||
use app\common\model\user\User;
|
||||
use app\common\server\ConfigServer;
|
||||
|
||||
class CityLogic extends Logic
|
||||
{
|
||||
/**
|
||||
* 根据进入方式获取应展示的城市商城
|
||||
* 未扫码进入:上海总部;扫码进入:码主人所属城市
|
||||
*/
|
||||
public static function getEntryCity(array $params = []): array
|
||||
{
|
||||
if (!empty($params['invite_code'])) {
|
||||
return self::getCityByInviteCode($params['invite_code']);
|
||||
}
|
||||
|
||||
if (!empty($params['user_id'])) {
|
||||
return self::getCityByUserId((int)$params['user_id']);
|
||||
}
|
||||
|
||||
return self::getHeadquartersCity();
|
||||
}
|
||||
|
||||
/**
|
||||
* 上海总部城市(默认商城)
|
||||
*/
|
||||
public static function getHeadquartersCity(): array
|
||||
{
|
||||
$cityId = self::resolveHeadquartersCityId();
|
||||
if ($cityId <= 0) {
|
||||
return self::formatCityInfo(0, true);
|
||||
}
|
||||
|
||||
return self::formatCityInfo($cityId, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据邀请码获取码主人所属城市
|
||||
*/
|
||||
public static function getCityByInviteCode(string $code): array
|
||||
{
|
||||
$user = User::where(['distribution_code' => $code, 'del' => 0])
|
||||
->field('id,city_id')
|
||||
->findOrEmpty();
|
||||
|
||||
if ($user->isEmpty()) {
|
||||
return self::getHeadquartersCity();
|
||||
}
|
||||
|
||||
$cityId = self::resolveUserCityId((int)$user['id'], (int)$user['city_id']);
|
||||
if ($cityId <= 0) {
|
||||
return self::getHeadquartersCity();
|
||||
}
|
||||
|
||||
return self::formatCityInfo($cityId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID获取所属城市(分销扫码等场景)
|
||||
*/
|
||||
public static function getCityByUserId(int $userId): array
|
||||
{
|
||||
$user = User::where(['id' => $userId, 'del' => 0])
|
||||
->field('id,city_id')
|
||||
->findOrEmpty();
|
||||
|
||||
if ($user->isEmpty()) {
|
||||
return self::getHeadquartersCity();
|
||||
}
|
||||
|
||||
$cityId = self::resolveUserCityId((int)$user['id'], (int)$user['city_id']);
|
||||
if ($cityId <= 0) {
|
||||
return self::getHeadquartersCity();
|
||||
}
|
||||
|
||||
return self::formatCityInfo($cityId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析用户所属城市:优先用户表,其次分销申请地区
|
||||
*/
|
||||
protected static function resolveUserCityId(int $userId, int $cityId = 0): int
|
||||
{
|
||||
if ($cityId > 0) {
|
||||
return $cityId;
|
||||
}
|
||||
|
||||
if ($userId <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int)DistributionMemberApply::where([
|
||||
['user_id', '=', $userId],
|
||||
['status', '=', 1],
|
||||
])->order('id desc')->value('city');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户所属城市ID,扫码绑定时继承上级地区
|
||||
*/
|
||||
public static function getUserCityId(int $userId): int
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$cityId = (int)User::where(['id' => $userId, 'del' => 0])->value('city_id');
|
||||
|
||||
return self::resolveUserCityId($userId, $cityId);
|
||||
}
|
||||
|
||||
protected static function formatCityInfo(int $cityId, bool $isHeadquarters = false): array
|
||||
{
|
||||
if ($cityId <= 0) {
|
||||
return [
|
||||
'id' => 0,
|
||||
'name' => '上海总部',
|
||||
'gcj02_lat' => 0,
|
||||
'gcj02_lng' => 0,
|
||||
'shop_id' => 1,
|
||||
'is_headquarters' => 1,
|
||||
];
|
||||
}
|
||||
|
||||
$city = DevRegion::where('id', $cityId)
|
||||
->field('id,name,gcj02_lat,gcj02_lng,db09_lat,db09_lng')
|
||||
->findOrEmpty();
|
||||
|
||||
if ($city->isEmpty()) {
|
||||
return [
|
||||
'id' => 0,
|
||||
'name' => '上海总部',
|
||||
'gcj02_lat' => 0,
|
||||
'gcj02_lng' => 0,
|
||||
'shop_id' => 1,
|
||||
'is_headquarters' => 1,
|
||||
];
|
||||
}
|
||||
|
||||
$shop = Shop::where([
|
||||
['city_id', '=', $cityId],
|
||||
['is_run', '=', 1],
|
||||
['is_freeze', '=', 0],
|
||||
['del', '=', 0],
|
||||
])->field('id')->findOrEmpty();
|
||||
|
||||
return [
|
||||
'id' => (int)$city['id'],
|
||||
'name' => $city['name'],
|
||||
'gcj02_lat' => (float)($city['gcj02_lat'] ?: 0),
|
||||
'gcj02_lng' => (float)($city['gcj02_lng'] ?: 0),
|
||||
'shop_id' => $shop->isEmpty() ? 1 : (int)$shop['id'],
|
||||
'is_headquarters' => $isHeadquarters ? 1 : 0,
|
||||
];
|
||||
}
|
||||
|
||||
protected static function resolveHeadquartersCityId(): int
|
||||
{
|
||||
$cityId = (int)ConfigServer::get('shop', 'headquarters_city_id', 0);
|
||||
if ($cityId <= 0) {
|
||||
$cityId = (int)Shop::where(['id' => 1, 'del' => 0])->value('city_id');
|
||||
}
|
||||
if ($cityId <= 0) {
|
||||
$cityId = (int)DevRegion::where(['level' => 2])
|
||||
->whereLike('name', '上海%')
|
||||
->value('id');
|
||||
}
|
||||
|
||||
return $cityId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user