feat : site

This commit is contained in:
2025-02-27 17:21:21 +08:00
parent a76d360b77
commit 3be21ed1b6
6 changed files with 305 additions and 0 deletions

View File

@@ -53,4 +53,11 @@ class SiteCode
* @var string 站点不删除
*/
const SITE_NO_DEL = 1;
/**
* @var int 用户默认地址 1=否 2=是
*/
CONST INT USER_NO_DEFAULT = 1;
CONST INT USER_DEFAULT = 2;
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Controller\Api;
use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Service\Api\System\SiteListService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
#[Controller(prefix: 'api/system')]
class SystemController extends AbstractController
{
#[RequestMapping(path: 'site/search_list',methods: 'POST')]
#[Scene(scene: 'search_list')]
public function searchSiteList()
{
return (new SiteListService)->handle();
}
}

View File

@@ -8,6 +8,7 @@ use App\Controller\AbstractController;
use App\Middleware\Api\JwtAuthMiddleware;
use App\Request\Api\UserRequest;
use App\Service\Api\User\BindPhoneByWxService;
use App\Service\Api\User\SiteService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
@@ -33,4 +34,38 @@ class UserController extends AbstractController
{
return (new BindPhoneByWxService)->handle();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[RequestMapping(path: 'site/list',methods: 'post')]
#[Scene(scene: 'site_list')]
public function userSiteList()
{
return (new SiteService)->handle();
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[RequestMapping(path: 'site/add',methods: 'post')]
#[Scene(scene: 'add_site')]
public function addSite()
{
return (new SiteService)->add();
}
/**
* @return array
*/
#[RequestMapping(path: 'site/del',methods: 'post')]
#[Scene(scene: 'del_site')]
public function delSite()
{
return (new SiteService)->del();
}
}

77
app/Model/UserSite.php Normal file
View File

@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace App\Model;
use App\Constants\Common\SiteCode;
use Hyperf\Database\Concerns\BuildsQueries;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property int $site_id
* @property int $user_id
* @property int $is_default
* @property string $create_time
* @property string $update_time
*/
class UserSite extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'user_site';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'site_id' => 'integer', 'user_id' => 'integer', 'is_default' => 'integer'];
const string CREATED_AT = 'created_time';
const string UPDATED_AT = 'updated_time';
/**
* @param int $id
* @return UserSite|UserSite[]|Collection|\Hyperf\Database\Model\Model|null
*/
public function getInfoById(int $id): Collection|\Hyperf\Database\Model\Model|UserSite|array|null
{
return $this->find($id);
}
/**
* @param int $userId
* @return array
*/
public function getSiteIdsByUserId(int $userId): array
{
return $this->where('user_id', $userId)->pluck('site_id')->toArray();
}
/**
* @param int $userId
* @return int
*/
public function getUserDefaultIdByUserId(int $userId): int
{
return $this->where('user_id', $userId)->where('is_default',SiteCode::USER_DEFAULT)->value('site_id') ?? 0;
}
/**
* @param int $userId
* @return BuildsQueries|Builder|\Hyperf\Database\Model\Model|object|null
*/
public function getUserNoDefaultIdByUserId(int $userId)
{
return $this->where('user_id', $userId)->where('is_default',SiteCode::USER_NO_DEFAULT)->first();
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\System;
use App\Cache\Redis\Api\SiteCache;
use App\Extend\SystemUtil;
use App\Model\Site;
use App\Service\Api\BaseService;
use Hyperf\Di\Annotation\Inject;
class SiteListService extends BaseService
{
/**
* @var Site
*/
#[Inject]
protected Site $siteModel;
/**
* @return array
*/
public function handle(): array
{
$limit = $this->request->input('limit', 20);
$list = $this->siteModel
->when($name = $this->request->input('search_value'), function ($query) use ($name) {
$query->where('name', 'like', "%$name%");
})
// ->where('name', 'like', '%'.$this->request->input('search_value').'%')
->paginate($limit)
->toArray();
if (empty($list['data'])) return $this->return->success('success', ['list' => $list]);
foreach ($list['data'] as &$item) {
$item['gap'] = SystemUtil::calculateDistance(
['lng' => $this->request->input('lng'),'lat' => $this->request->input('lat')],
['lng' => $item['lng'], 'lat' => $item['lat']]
);
}
return $this->return->success('success', ['list' => $list]);
}
}

View File

@@ -0,0 +1,108 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\User;
use App\Cache\Redis\Api\SiteCache;
use App\Constants\Common\SiteCode;
use App\Exception\ErrException;
use App\Model\UserSite;
use App\Service\Api\BaseService;
use Exception;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class SiteService extends BaseService
{
/**
* @var UserSite
*/
#[Inject]
protected UserSite $userSiteModel;
/**
* @var SiteCache
*/
#[Inject]
protected SiteCache $siteCache;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$siteIds = $this->userSiteModel->where('user_id', $this->userId)->pluck('site_id')->toArray();
$res = [];
foreach ($siteIds as $siteId) {
$res[] = $this->siteCache->getSiteInfo($siteId);
}
return $this->return->success('success', ['list' => $res]);
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function add(): array
{
$id = (int)$this->request->input('site_id');
if (empty($this->siteCache->getSiteInfo($id))) throw new ErrException('该地点不存在,请联系客服');
$siteIds = $this->userSiteModel->getSiteIdsByUserId($this->userId);
if (in_array($id, $siteIds)) throw new ErrException('已添加该地点');
$insertModel = new UserSite();
$insertModel->user_id = $this->userId;
$insertModel->site_id = $id;
$insertModel->is_default = SiteCode::USER_NO_DEFAULT;
if ($this->userSiteModel->getUserDefaultIdByUserId($this->userId) == 0) {
$insertModel->is_default = SiteCode::USER_DEFAULT;
}
if (!$insertModel->save()) throw new ErrException('添加失败');
return $this->return->success();
}
/**
* @return array
*/
public function del(): array
{
$id = (int)$this->request->input('id');
$info = $this->userSiteModel->getInfoById($id);
Db::transaction(function () use ($info) {
if ($info->is_default == SiteCode::USER_DEFAULT) {
$updateInfo = $this->userSiteModel->getUserNoDefaultIdByUserId($this->userId);
$updateInfo->is_default = SiteCode::USER_DEFAULT;
if(!$updateInfo->save()) throw new ErrException('删除失败-修改联动数据失败');
}
if (!$info->delete()) throw new ErrException('删除失败');
});
return $this->return->success();
}
}