diff --git a/app/Aspect/Admin/AdminLoginLogAspect.php b/app/Aspect/Admin/AdminLoginLogAspect.php index c45f963..a17387e 100644 --- a/app/Aspect/Admin/AdminLoginLogAspect.php +++ b/app/Aspect/Admin/AdminLoginLogAspect.php @@ -2,7 +2,7 @@ namespace App\Aspect\Admin; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Service\Admin\Login\LoginService; use Hyperf\Di\Annotation\Aspect; use Hyperf\Di\Aop\AbstractAspect; @@ -43,9 +43,9 @@ class AdminLoginLogAspect extends AbstractAspect //todo 登录日志是否需要 return $result; - } catch (AdminException $e) { + } catch (ErrException $e) { // var_dump($e->getMessage()); - throw new AdminException($e->getMessage()); + throw new ErrException($e->getMessage()); } } } \ No newline at end of file diff --git a/app/Aspect/Admin/AdminOperationAspect.php b/app/Aspect/Admin/AdminOperationAspect.php index 051e960..e12e8ad 100644 --- a/app/Aspect/Admin/AdminOperationAspect.php +++ b/app/Aspect/Admin/AdminOperationAspect.php @@ -2,7 +2,7 @@ namespace App\Aspect\Admin; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Lib\Log; use Hyperf\Context\Context; use Hyperf\Di\Annotation\Aspect; @@ -65,7 +65,7 @@ class AdminOperationAspect extends AbstractAspect // // //如果没有id 说明没有登录 抛出异常 // if (empty($adminId) || empty($roleId)) { -// throw new AdminException('请先登录'); +// throw new ErrException('请先登录'); // } // // //超级管理员不需要鉴权 diff --git a/app/Exception/AdminException.php b/app/Exception/AdminException.php deleted file mode 100644 index 202ea3b..0000000 --- a/app/Exception/AdminException.php +++ /dev/null @@ -1,10 +0,0 @@ -return->error($throwable->getMessage(),$throwable->getCode()); - - // 阻止异常冒泡 - $this->stopPropagation(); - - return $response->withHeader("Content-Type", "application/json") - ->withStatus(200) - ->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE))); - } - - // 交给下一个异常处理器 - return $response; - } - - public function isValid(Throwable $throwable): bool - { - return true; - } -} \ No newline at end of file diff --git a/app/Exception/Handler/ApiExceptionHandler.php b/app/Exception/Handler/ApiExceptionHandler.php deleted file mode 100644 index 84776d6..0000000 --- a/app/Exception/Handler/ApiExceptionHandler.php +++ /dev/null @@ -1,46 +0,0 @@ -return->error($throwable->getMessage(),$throwable->getCode()); - - // 阻止异常冒泡 - $this->stopPropagation(); - - return $response->withHeader("Content-Type", "application/json") - ->withStatus(200) - ->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE))); - } - - // 交给下一个异常处理器 - return $response; - } - - public function isValid(Throwable $throwable): bool - { - return true; - } -} \ No newline at end of file diff --git a/app/Exception/Handler/ErrExceptionHandler.php b/app/Exception/Handler/ErrExceptionHandler.php new file mode 100644 index 0000000..0277e64 --- /dev/null +++ b/app/Exception/Handler/ErrExceptionHandler.php @@ -0,0 +1,64 @@ +request->path(); + $urlArr = explode('/',$url); + + $result = match ($urlArr[0]) { + 'api' => $this->apiReturn->error($throwable->getMessage(),$throwable->getCode()), + 'admin' => $this->adminReturn->error($throwable->getMessage(),$throwable->getCode()), + default => null, + }; + + // 阻止异常冒泡 + $this->stopPropagation(); + + if (!empty($result)) { + return $response->withHeader("Content-Type", "application/json") + ->withStatus(200) + ->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE))); + } + } + + // 交给下一个异常处理器 + return $response; + } + + public function isValid(Throwable $throwable): bool + { + return true; + } +} \ No newline at end of file diff --git a/app/Request/Admin/SiteRequest.php b/app/Request/Admin/SiteRequest.php index 28d61a5..8b4f774 100644 --- a/app/Request/Admin/SiteRequest.php +++ b/app/Request/Admin/SiteRequest.php @@ -22,7 +22,30 @@ class SiteRequest extends FormRequest public function rules(): array { return [ + 'name' => 'required|string', + 'city_id' => 'required|integer|exists:system_city,id', +// 'driver_id' => 'required|integer|exists:admin_user,id', +// 'kitchen_id' => 'required|integer|exists:kitchen,id', + 'status' => 'required|integer|in:1,2', + 'expected_delivery_time' => 'required|string|date_format:H:i', + 'remark' => 'sometimes|string', + 'address' => 'required|string', + 'lng' => 'required|string', + 'lat' => 'required|string', +// 'expected_spend_time' => 'required|string|date_format:i:s', +// 'image_id' => 'sometimes|integer|exists:oss_object,id', + ]; + } + public function messages(): array + { + return []; + } + + public function scenes(): array + { + return [ + 'add' => ['name', 'city_id', 'driver_id', 'kitchen_id', 'status','expected_delivery_time', 'remark', 'address', 'lng', 'lat','expected_spend_time','image_id'], ]; } } diff --git a/app/Service/Admin/Login/LoginService.php b/app/Service/Admin/Login/LoginService.php index 5f9fe8e..9b38224 100644 --- a/app/Service/Admin/Login/LoginService.php +++ b/app/Service/Admin/Login/LoginService.php @@ -13,7 +13,7 @@ namespace App\Service\Admin\Login; use App\Cache\Redis\Admin\UserCache; use App\Constants\Admin\UserCode; use App\Constants\AdminCode; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Extend\SystemUtil; use App\Lib\Crypto\CryptoFactory; use App\Model\AdminUser; @@ -59,19 +59,19 @@ class LoginService extends BaseService public function handle(): array { $userInfo = $this->adminUserModel->getAdminInfoByAccount($this->request->input('account')); - if (!$userInfo) throw new AdminException('账号不存在'); + if (!$userInfo) throw new ErrException('账号不存在'); - if ($userInfo->status == UserCode::DISABLE) throw new AdminException(UserCode::getMessage($userInfo->status),AdminCode::LOGIN_ERROR); + if ($userInfo->status == UserCode::DISABLE) throw new ErrException(UserCode::getMessage($userInfo->status),AdminCode::LOGIN_ERROR); // pass加密跟数据库做判断 $password = $this->cryptoFactory->cryptoClass('admin-password',$this->request->input('password'),$userInfo->salt)->encrypt(); - if ($password != $userInfo->password) throw new AdminException('密码错误!'); + if ($password != $userInfo->password) throw new ErrException('密码错误!'); $userInfo->last_login_time = date('Y-m-d H:i:s'); $userInfo->last_login_ip = SystemUtil::getClientIp(); $userInfo->save(); - if (!$userInfo->save()) throw new AdminException('登录失败'); + if (!$userInfo->save()) throw new ErrException('登录失败'); $cityId = $this->getCityById($userInfo->id); //生成 token diff --git a/app/Service/Admin/System/CityService.php b/app/Service/Admin/System/CityService.php index 13c729f..4f1cbaa 100644 --- a/app/Service/Admin/System/CityService.php +++ b/app/Service/Admin/System/CityService.php @@ -12,7 +12,7 @@ namespace App\Service\Admin\System; use App\Cache\Redis\Common\CityCache; use App\Constants\Common\CityCode; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Model\SystemCity; use App\Model\SystemCityConfig; use App\Service\Admin\BaseService; @@ -78,9 +78,9 @@ class CityService extends BaseService $cityId = (int)$this->request->input('city_id'); $info = $this->systemCityConfigModel->getAddressByIdAndPid($cityId,$provinceId); - if (empty($info)) throw new AdminException('城市选择错误'); + if (empty($info)) throw new ErrException('城市选择错误'); - if ($this->systemCityModel->getInfoByCityId($cityId)) throw new AdminException('城市已存在'); + if ($this->systemCityModel->getInfoByCityId($cityId)) throw new ErrException('城市已存在'); $model = new SystemCity(); @@ -90,7 +90,7 @@ class CityService extends BaseService $model->status = $this->request->input('status', 1); $model->is_del = 1; - if (!$model->save()) throw new AdminException('添加失败'); + if (!$model->save()) throw new ErrException('添加失败'); return $this->return->success(); } @@ -101,11 +101,11 @@ class CityService extends BaseService $model = $this->systemCityModel->getInfoById($id); - if (!$model) throw new AdminException('数据不存在'); + if (!$model) throw new ErrException('数据不存在'); $model->status = $model->status == CityCode::STATUS_ENABLE ? CityCode::STATUS_DISABLE : CityCode::STATUS_ENABLE; - if (!$model->save()) throw new AdminException('修改失败'); + if (!$model->save()) throw new ErrException('修改失败'); return $this->return->success(); } @@ -119,11 +119,11 @@ class CityService extends BaseService $model = $this->systemCityModel->getInfoById($id); - if (!$model) throw new AdminException('数据不存在'); + if (!$model) throw new ErrException('数据不存在'); $model->is_del = CityCode::IS_DELETE; - if (!$model->save()) throw new AdminException('删除失败'); + if (!$model->save()) throw new ErrException('删除失败'); return $this->return->success(); } @@ -140,7 +140,7 @@ class CityService extends BaseService $info = $this->systemCityModel->getInfoById($id); - if (!$info) throw new AdminException('数据不存在'); + if (!$info) throw new ErrException('数据不存在'); $allInfo = $this->cityCache->getCityList(); $allInfo = array_column($allInfo,'title','id'); diff --git a/app/Service/Admin/System/ConfigService.php b/app/Service/Admin/System/ConfigService.php index 44b9385..f823de3 100644 --- a/app/Service/Admin/System/ConfigService.php +++ b/app/Service/Admin/System/ConfigService.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace App\Service\Admin\System; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Model\Config; use App\Service\Admin\BaseService; use Hyperf\Di\Annotation\Inject; @@ -42,7 +42,7 @@ class ConfigService extends BaseService $res = (new Config)->where('key', $editKey)->update(['value' => $editValue]); - if (!$res) throw new AdminException('配置更新失败'); + if (!$res) throw new ErrException('配置更新失败'); return $this->return->success(); } diff --git a/app/Service/Admin/System/KitchenService.php b/app/Service/Admin/System/KitchenService.php index 828b712..dc2610a 100644 --- a/app/Service/Admin/System/KitchenService.php +++ b/app/Service/Admin/System/KitchenService.php @@ -13,7 +13,7 @@ namespace App\Service\Admin\System; use App\Cache\Redis\Common\CommonRedisKey; use App\Cache\Redis\RedisCache; use App\Constants\Common\SiteCode; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Model\Kitchen; use App\Model\Site; use App\Model\SystemCity; @@ -94,7 +94,7 @@ class KitchenService extends BaseService $name = $this->request->input('name'); $info = $this->kitchenModel->getInfoByName($name); - if (!empty($info)) throw new AdminException('数据已存在'); + if (!empty($info)) throw new ErrException('数据已存在'); $model = new Kitchen(); @@ -105,7 +105,7 @@ class KitchenService extends BaseService $model->lat = $this->request->input('lat'); $model->status = $this->request->input('status'); - if (!$model->save()) throw new AdminException('添加失败'); + if (!$model->save()) throw new ErrException('添加失败'); // if ($model->status == SiteCode::KITCHEN_ENABLE) { // $this->setSiteCache(SiteCode::KITCHEN_REDIS_PREFIX.$model->id,$model->lng,$model->lat); @@ -124,10 +124,10 @@ class KitchenService extends BaseService $status = (int)$this->request->input('status'); $info = $this->kitchenModel->getInfoById($id); - if (empty($info)) throw new AdminException('数据不存在'); + if (empty($info)) throw new ErrException('数据不存在'); $name = $this->kitchenModel->getInfoByName($name); - if ($name->id != $info->id) throw new AdminException('数据已存在'); + if ($name->id != $info->id) throw new ErrException('数据已存在'); if ($info->status == SiteCode::KITCHEN_ENABLE && $status == SiteCode::KITCHEN_DISABLE) { $this->siteModel->disableStatusByKitchenId($info->id); @@ -139,7 +139,7 @@ class KitchenService extends BaseService $info->lat = $this->request->input('lat'); $info->status = $status; - if (!$info->save()) throw new AdminException('修改失败'); + if (!$info->save()) throw new ErrException('修改失败'); return $this->return->success(); } @@ -153,13 +153,13 @@ class KitchenService extends BaseService $id = (int)$this->request->input('id'); $info = $this->kitchenModel->getInfoById($id); - if (empty($info)) throw new AdminException('数据不存在'); + if (empty($info)) throw new ErrException('数据不存在'); $this->siteModel->disableStatusByKitchenId($info->id); $info->is_del = SiteCode::SITE_DEL; - if (!$info->save()) throw new AdminException('删除失败'); + if (!$info->save()) throw new ErrException('删除失败'); return $this->return->success(); } @@ -171,7 +171,7 @@ class KitchenService extends BaseService { $data = $this->kitchenModel ->getInfoById((int)$this->request->input('id')); - if (empty($data)) throw new AdminException('数据不存在'); + if (empty($data)) throw new ErrException('数据不存在'); $res = [ 'id' => $data->id, diff --git a/app/Service/Admin/System/SiteService.php b/app/Service/Admin/System/SiteService.php index f6241a1..a81a2ee 100644 --- a/app/Service/Admin/System/SiteService.php +++ b/app/Service/Admin/System/SiteService.php @@ -10,15 +10,76 @@ declare(strict_types=1); namespace App\Service\Admin\System; +use App\Constants\Admin\AuthCode; use App\Constants\Admin\UserCode; +use App\Constants\Common\CityCode; use App\Constants\Common\RoleCode; +use App\Constants\Common\SiteCode; +use App\Exception\ErrException; use App\Model\AdminSection; use App\Model\AdminUser; +use App\Model\Kitchen; +use App\Model\Site; +use App\Model\SystemCity; use App\Service\Admin\BaseService; +use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; class SiteService extends BaseService { + /** + * @var Site + */ + #[Inject] + protected readonly Site $siteModel; + + /** + * @var AdminUser + */ + #[Inject] + protected readonly AdminUser $adminUserModel; + + /** + * @var SystemCity + */ + #[Inject] + protected readonly SystemCity $systemCityModel; + + /** + * @var Kitchen + */ + #[Inject] + protected readonly Kitchen $kitchenModel; + + /** + * 注入部门类 + * @var AdminSection + */ + #[Inject] + protected readonly AdminSection $adminSectionModel; + + + /** + * @var int + */ + protected int $cityId = 0; + /** + * @var int + */ + protected int $driverId = 0; + /** + * @var int + */ + protected int $kitchenId = 0; + /** + * @var int + */ + protected int $imageId = 0; + + protected mixed $kitchenInfo; + protected mixed $driverInfo; + protected mixed $cityInfo; + public function handle() { return $this->return->success(); @@ -26,9 +87,57 @@ class SiteService extends BaseService public function add() { + $this->checkData(); + + Db::transaction(function () { + $model = new Site(); + + $model->name = $this->request->input('name'); + $model->city_id = $this->cityId; + $model->delivered_id = $this->driverId; + $model->kitchen_id = $this->kitchenId; + $model->address = $this->request->input('address'); + $model->lng = $this->request->input('lng'); + $model->lat = $this->request->input('lat'); + $model->remark = $this->request->input('remark'); + $model->status = $this->request->input('status'); + $model->expected_delivery_time = $this->request->input('expected_delivery_time'); + $model->expected_spend_time = $this->request->input('expected_spend_time'); + $model->distance = 0; + + if (!$model->save()) throw new ErrException('添加失败'); + }); + return $this->return->success(); } + private function checkData(): void + { + $this->cityId = (int)$this->request->input('city_id'); + $this->driverId = (int)$this->request->input('driver_id'); + $this->kitchenId = (int)$this->request->input('kitchen_id'); + $this->imageId = (int)$this->request->input('image_id'); + + $this->cityInfo = $this->systemCityModel->getInfoByCityId($this->cityId); + if ( + empty($this->cityInfo) || + $this->cityInfo->status == CityCode::STATUS_DISABLE + ) throw new ErrException('该城市已被禁用'); + + $this->driverInfo = $this->adminUserModel->getAdminInfoById($this->driverId); + if ( + empty($this->driverInfo) || + $this->driverInfo->status == UserCode::DISABLE || + $this->driverInfo->role_id != RoleCode::DRIVER + ) throw new ErrException('该司机已被禁用'); + + $this->kitchenInfo = $this->kitchenModel->getInfoById($this->kitchenId); + if ( + empty($this->kitchenInfo) || + $this->kitchenInfo->status == SiteCode::KITCHEN_DISABLE + ) throw new ErrException('该厨房已被禁用'); + } + public function edit() { return $this->return->success(); @@ -44,19 +153,6 @@ class SiteService extends BaseService return $this->return->success(); } - /** - * 注入用户类 - * @var AdminUser - */ - #[Inject] - protected AdminUser $adminUserModel; - - /** - * 注入部门类 - * @var AdminSection - */ - #[Inject] - protected AdminSection $adminSectionModel; /** * @return array diff --git a/app/Service/Admin/Third/AliStsService.php b/app/Service/Admin/Third/AliStsService.php index 644d1f3..80b9ed9 100644 --- a/app/Service/Admin/Third/AliStsService.php +++ b/app/Service/Admin/Third/AliStsService.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace App\Service\Admin\Third; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Service\Admin\BaseService; use App\Service\ServiceTrait\Common\AliStsTrait; use Psr\Container\ContainerExceptionInterface; @@ -97,7 +97,7 @@ class AliStsService extends BaseService $this->log->info(__CLASS__.':'.__FUNCTION__.':授权oss信息:'.json_encode($res)); $aliResponse = $res->body->credentials; - if (empty($aliResponse)) throw new AdminException('授权失败'); + if (empty($aliResponse)) throw new ErrException('授权失败'); return $this->return->success('success',[ 'access_key_id' => $aliResponse->accessKeyId, diff --git a/app/Service/Admin/User/EmployeeService.php b/app/Service/Admin/User/EmployeeService.php index 4fc1b85..a23fab8 100644 --- a/app/Service/Admin/User/EmployeeService.php +++ b/app/Service/Admin/User/EmployeeService.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace App\Service\Admin\User; use App\Constants\Admin\UserCode; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Extend\StringUtil; use App\Lib\Crypto\CryptoFactory; use App\Model\AdminRole; @@ -81,7 +81,7 @@ class EmployeeService extends BaseService $oldAccount = $this->adminUserModel->getAdminInfoByAccount($account); $oldName = $this->adminUserModel->getAdminInfoByName($name); - if (!empty($oldName) && !empty($oldAccount)) throw new AdminException('账号或者员工已存在'); + if (!empty($oldName) && !empty($oldAccount)) throw new ErrException('账号或者员工已存在'); $salt = StringUtil::randStr(6); $defaultPassword = config('system.admin_default_password'); @@ -98,7 +98,7 @@ class EmployeeService extends BaseService $model->role_id = $this->request->input('role_id', 0); $model->section_id = $this->request->input('section_id', 0); - if (!$model->save()) throw new AdminException('账号添加失败'); + if (!$model->save()) throw new ErrException('账号添加失败'); return $this->return->success(); } @@ -114,13 +114,13 @@ class EmployeeService extends BaseService $account = $this->request->input('account'); $info = $this->adminUserModel->getAdminInfoById($id); - if (empty($info)) throw new AdminException('数据不存在'); + if (empty($info)) throw new ErrException('数据不存在'); $oldAccount = $this->adminUserModel->getAdminInfoByAccount($account); $oldName = $this->adminUserModel->getAdminInfoByName($name); - if (!empty($oldName) && $oldName->id != $info->id) throw new AdminException('员工已存在'); - if (!empty($oldAccount) && $oldAccount->id != $info->id) throw new AdminException('账号已存在'); + if (!empty($oldName) && $oldName->id != $info->id) throw new ErrException('员工已存在'); + if (!empty($oldAccount) && $oldAccount->id != $info->id) throw new ErrException('账号已存在'); $info->username = $account; $info->mobile = $account; @@ -130,7 +130,7 @@ class EmployeeService extends BaseService $info->role_id = $this->request->input('role_id', 0); $info->section_id = $this->request->input('section_id', 0); - if (!$info->save()) throw new AdminException('账号修改失败'); + if (!$info->save()) throw new ErrException('账号修改失败'); return $this->return->success(); } @@ -143,15 +143,15 @@ class EmployeeService extends BaseService { $id = (int)$this->request->input('id'); - if ($this->adminId == $id) throw new AdminException('不可删除自己'); + if ($this->adminId == $id) throw new ErrException('不可删除自己'); $info = $this->adminUserModel->getAdminInfoById($id); - if (empty($info)) throw new AdminException('员工不存在'); + if (empty($info)) throw new ErrException('员工不存在'); $info->is_del = UserCode::IS_DEL; - if (!$info->save()) throw new AdminException('账号删除失败'); + if (!$info->save()) throw new ErrException('账号删除失败'); return $this->return->success(); } @@ -166,7 +166,7 @@ class EmployeeService extends BaseService $info = $this->adminUserModel->where('id', $id)->where('is_del',UserCode::ENABLE)->first($this->filed); - if (empty($info)) throw new AdminException('员工不存在'); + if (empty($info)) throw new ErrException('员工不存在'); $roleName = $this->adminRoleModel->where('id', $info->role_id)->value('name'); @@ -187,7 +187,7 @@ class EmployeeService extends BaseService $info = $this->adminUserModel->getAdminInfoById($id); - if (empty($info)) throw new AdminException('员工不存在'); + if (empty($info)) throw new ErrException('员工不存在'); $salt = StringUtil::randStr(6); $defaultPassword = config('system.admin_default_password'); @@ -195,7 +195,7 @@ class EmployeeService extends BaseService $info->salt = $salt; $info->password = $this->cryptoFactory->cryptoClass('admin-password',$defaultPassword,$salt)->encrypt(); - if (!$info->save()) throw new AdminException('密码重置失败'); + if (!$info->save()) throw new ErrException('密码重置失败'); return $this->return->success(); } diff --git a/app/Service/Admin/User/RoleMenuService.php b/app/Service/Admin/User/RoleMenuService.php index 7e2e715..83812ab 100644 --- a/app/Service/Admin/User/RoleMenuService.php +++ b/app/Service/Admin/User/RoleMenuService.php @@ -12,7 +12,7 @@ namespace App\Service\Admin\User; use App\Cache\Redis\Admin\MenuCache; use App\Constants\Admin\AuthCode; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Model\AdminMenu; use App\Service\Admin\BaseService; use Exception; @@ -60,14 +60,14 @@ class RoleMenuService extends BaseService public function add(): array { $url = $this->request->input('menu_url'); - if (!empty($this->adminMenuModel->getMenuByUrl($url))) throw new AdminException('已存在相同路由的权限菜单'); + if (!empty($this->adminMenuModel->getMenuByUrl($url))) throw new ErrException('已存在相同路由的权限菜单'); $insertButtonArr = []; if (!empty($permissionList = $this->request->input('permission_list'))) { $permissionList = json_decode($permissionList, true); foreach ($permissionList as $one) { - if (empty($one['value'])) throw new AdminException('按钮权限值错误'); - if (empty($one['menu_url'])) throw new AdminException('按钮路由值错误'); + if (empty($one['value'])) throw new ErrException('按钮权限值错误'); + if (empty($one['menu_url'])) throw new ErrException('按钮路由值错误'); $insertButtonArr[] = [ 'url' => $one['menu_url'], 'value' => $one['value'], @@ -104,7 +104,7 @@ class RoleMenuService extends BaseService $this->reconfigurationCache(); } catch (Exception $exception) { Db::rollBack(); - throw new AdminException($exception->getMessage()); + throw new ErrException($exception->getMessage()); } return $this->return->success(); @@ -122,8 +122,8 @@ class RoleMenuService extends BaseService $url = $this->request->input('menu_url'); $oldUrlInfo = $this->adminMenuModel->getMenuByUrl($url); $menuInfo = $this->adminMenuModel->where('id',$menuId)->first(); - if (!empty($oldUrlInfo) && $oldUrlInfo->id != $menuId) throw new AdminException('已存在相同路由的权限菜单'); - if (empty($menuInfo)) throw new AdminException('路由不存在'); + if (!empty($oldUrlInfo) && $oldUrlInfo->id != $menuId) throw new ErrException('已存在相同路由的权限菜单'); + if (empty($menuInfo)) throw new ErrException('路由不存在'); $childrenType = $this->adminMenuModel->getChildMenuType($menuId); $permissionList = $this->request->input('permission_list'); @@ -131,8 +131,8 @@ class RoleMenuService extends BaseService if (!empty($permissionList) && $childrenType != AuthCode::MENU_TYPE_LIST) { $permissionList = json_decode($permissionList, true); foreach ($permissionList as $one) { - if (empty($one['value'])) throw new AdminException('按钮权限值错误'); - if (empty($one['menu_url'])) throw new AdminException('按钮路由值错误'); + if (empty($one['value'])) throw new ErrException('按钮权限值错误'); + if (empty($one['menu_url'])) throw new ErrException('按钮路由值错误'); $insertButtonArr[] = [ 'parent_id' => $menuId, 'url' => $one['menu_url'], @@ -168,7 +168,7 @@ class RoleMenuService extends BaseService $this->reconfigurationCache(); } catch (Exception $exception) { Db::rollBack(); - throw new AdminException($exception->getMessage()); + throw new ErrException($exception->getMessage()); } return $this->return->success(); @@ -185,7 +185,7 @@ class RoleMenuService extends BaseService { $menuId = $this->request->input('menu_id'); $res = $this->adminMenuModel->where('id',$menuId)->first(); - if (!$res) throw new AdminException('路由不存在'); + if (!$res) throw new ErrException('路由不存在'); $ids = [$res->id]; $topIds = [$res->id]; @@ -210,7 +210,7 @@ class RoleMenuService extends BaseService { $menuId = $this->request->input('menu_id'); $res = $this->adminMenuModel->where('id',$menuId)->first(); - if (!$res) throw new AdminException('路由不存在'); + if (!$res) throw new ErrException('路由不存在'); $res = $res->toArray(); //获取子集 diff --git a/app/Service/Admin/User/RoleService.php b/app/Service/Admin/User/RoleService.php index d201a3e..1296b30 100644 --- a/app/Service/Admin/User/RoleService.php +++ b/app/Service/Admin/User/RoleService.php @@ -14,7 +14,7 @@ use App\Cache\Redis\Admin\MenuCache; use App\Cache\Redis\Admin\RoleCache; use App\Constants\Admin\AuthCode; use App\Constants\Common\RoleCode; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Model\AdminRole; use App\Model\AdminRoleMenu; use App\Service\Admin\BaseService; @@ -80,7 +80,7 @@ class RoleService extends BaseService { $name = $this->request->input('role_name'); - if ($this->adminRoleModel->getInfoByName($name)) throw new AdminException('角色已存在'); + if ($this->adminRoleModel->getInfoByName($name)) throw new ErrException('角色已存在'); Db::beginTransaction(); try { @@ -112,7 +112,7 @@ class RoleService extends BaseService $this->roleCache->getRoleCache($model->id); } catch (Exception $e) { Db::rollBack(); - throw new AdminException($e->getMessage()); + throw new ErrException($e->getMessage()); } return $this->return->success(); @@ -128,11 +128,11 @@ class RoleService extends BaseService { $id = (int)$this->request->input('role_id'); - if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); + if (!$info = $this->adminRoleModel->getInfoById($id)) throw new ErrException('角色不存在'); $name = $this->request->input('role_name'); $oldInfo = $this->adminRoleModel->getInfoByName($name); - if ($oldInfo->id != $id) throw new AdminException('角色已存在'); + if ($oldInfo->id != $id) throw new ErrException('角色已存在'); Db::beginTransaction(); try { @@ -165,7 +165,7 @@ class RoleService extends BaseService $this->roleCache->getRoleCache($info->id); } catch (Exception $e) { Db::rollBack(); - throw new AdminException($e->getMessage()); + throw new ErrException($e->getMessage()); } return $this->return->success(); @@ -179,13 +179,13 @@ class RoleService extends BaseService public function changeStatus(): array { $id = (int)$this->request->input('role_id'); - if ($id == RoleCode::SUPER_ADMIN) throw new AdminException('超级管理员不可关闭'); + if ($id == RoleCode::SUPER_ADMIN) throw new ErrException('超级管理员不可关闭'); - if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); + if (!$info = $this->adminRoleModel->getInfoById($id)) throw new ErrException('角色不存在'); $info->status = $this->request->input('role_status', 0); - if (!$info->save()) throw new AdminException('修改失败'); + if (!$info->save()) throw new ErrException('修改失败'); return $this->return->success(); } @@ -201,7 +201,7 @@ class RoleService extends BaseService { $roleId = $this->request->input('role_id'); $res = $this->adminRoleModel->where('id',$roleId)->first($this->field); - if (!$res) throw new AdminException('角色不存在'); + if (!$res) throw new ErrException('角色不存在'); $data = $this->roleCache->getRoleCache((int)$roleId); diff --git a/app/Service/Admin/User/SectionService.php b/app/Service/Admin/User/SectionService.php index e336e9f..08a29af 100644 --- a/app/Service/Admin/User/SectionService.php +++ b/app/Service/Admin/User/SectionService.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace App\Service\Admin\User; use App\Cache\Redis\Admin\SectionCache; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Model\AdminSection; use App\Model\AdminUser; use App\Service\Admin\BaseService; @@ -76,7 +76,7 @@ class SectionService extends BaseService $model->remark = $this->request->input('remark'); $model->status = $this->request->input('status',1); - if (!$model->save()) throw new AdminException('添加失败'); + if (!$model->save()) throw new ErrException('添加失败'); $this->sectionCache->delList(); @@ -94,7 +94,7 @@ class SectionService extends BaseService $id = (int)$this->request->input('id'); $info = $this->adminSectionModel->getInfoById($id); - if (empty($info)) throw new AdminException('数据不存在'); + if (empty($info)) throw new ErrException('数据不存在'); $info->name = $this->request->input('name'); $info->pid = $this->request->input('pid',0); @@ -102,7 +102,7 @@ class SectionService extends BaseService $info->remark = $this->request->input('remark'); $info->status = $this->request->input('status',1); - if (!$info->save()) throw new AdminException('修改失败'); + if (!$info->save()) throw new ErrException('修改失败'); $this->sectionCache->delList(); return $this->return->success(); } @@ -118,10 +118,10 @@ class SectionService extends BaseService $id = (int)$this->request->input('id'); $info = $this->adminSectionModel->getInfoById($id); - if (empty($info)) throw new AdminException('数据不存在'); + if (empty($info)) throw new ErrException('数据不存在'); $children = $this->adminSectionModel->getInfoByPid($info->id); - if (empty($children)) throw new AdminException('请先删除下级数据'); + if (empty($children)) throw new ErrException('请先删除下级数据'); $this->adminUserModel->where('section_id',$id)->update(['section_id'=>0]); @@ -138,7 +138,7 @@ class SectionService extends BaseService $id = (int)$this->request->input('id'); $info = $this->adminSectionModel->getInfoById($id); - if (empty($info)) throw new AdminException('数据不存在'); + if (empty($info)) throw new ErrException('数据不存在'); return $this->return->success('success',$info->toArray()); } diff --git a/app/Service/Api/Login/LoginBaseService.php b/app/Service/Api/Login/LoginBaseService.php index efaa822..de50d2b 100644 --- a/app/Service/Api/Login/LoginBaseService.php +++ b/app/Service/Api/Login/LoginBaseService.php @@ -13,7 +13,7 @@ namespace App\Service\Api\Login; use App\Cache\Redis\Api\ApiRedisKey; use App\Cache\Redis\RedisCache; use App\Constants\Common\UserCode; -use App\Exception\ApiException; +use App\Exception\ErrException; use App\Extend\StringUtil; use App\Extend\SystemUtil; use App\Lib\Crypto\CryptoFactory; @@ -91,7 +91,7 @@ abstract class LoginBaseService extends BaseService $this->userId = empty($this->userId) ? $this->userInfo->id : $this->userId; if (empty($this->userId)) { - throw new ApiException('登录失败'); + throw new ErrException('登录失败'); } //todo 判断注销 判断封号 @@ -137,7 +137,7 @@ abstract class LoginBaseService extends BaseService }; if (0 == ($this->redis->addLock($this->lockKey))){ - throw new ApiException('请勿重复点击'); + throw new ErrException('请勿重复点击'); } } @@ -154,7 +154,7 @@ abstract class LoginBaseService extends BaseService $model->avatar_id = 0; $model->reg_ip = SystemUtil::getClientIp(); - if (!$model->save()) throw new ApiException('数据保存失败-注册失败'); + if (!$model->save()) throw new ErrException('数据保存失败-注册失败'); $this->userId = $model->id; $this->userInfo = $model; diff --git a/app/Service/Api/Login/LoginService.php b/app/Service/Api/Login/LoginService.php index 8acec16..7395646 100644 --- a/app/Service/Api/Login/LoginService.php +++ b/app/Service/Api/Login/LoginService.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace App\Service\Api\Login; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Service\Api\BaseService; class LoginService extends BaseService @@ -26,7 +26,7 @@ class LoginService extends BaseService { 'wx_login' => $factory->wxFastLogin(), 'mobile_code' => $factory->mobileCodeLogin(), - default => throw new AdminException('登录类型错误'), + default => throw new ErrException('登录类型错误'), }; $loginInfo = $service->handle(); diff --git a/app/Service/Api/Login/WxFastLoginService.php b/app/Service/Api/Login/WxFastLoginService.php index 7f5e3da..d6800b5 100644 --- a/app/Service/Api/Login/WxFastLoginService.php +++ b/app/Service/Api/Login/WxFastLoginService.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace App\Service\Api\Login; -use App\Exception\ApiException; +use App\Exception\ErrException; use App\Model\UserThird; use App\Service\ServiceTrait\Api\WxMiniTrait; use Hyperf\DbConnection\Db; @@ -37,7 +37,7 @@ class WxFastLoginService extends LoginBaseService { $this->jsCode = $this->request->input('js_code'); - if (empty($this->jsCode)) throw new ApiException('登录参数错误'); + if (empty($this->jsCode)) throw new ErrException('登录参数错误'); $wxInfo = $this->jsCodeGetOpenId($this->jsCode); $this->openId = $wxInfo['openid']; @@ -88,6 +88,6 @@ class WxFastLoginService extends LoginBaseService $model->open_id = $this->openId; $model->union_id = $this->unionId; - if (!$model->save()) throw new ApiException('注册失败-00001'); + if (!$model->save()) throw new ErrException('注册失败-00001'); } } \ No newline at end of file diff --git a/app/Service/Common/OssCallbackService.php b/app/Service/Common/OssCallbackService.php index a1260a9..9216d40 100644 --- a/app/Service/Common/OssCallbackService.php +++ b/app/Service/Common/OssCallbackService.php @@ -12,7 +12,7 @@ namespace App\Service\Common; use App\Cache\Redis\Common\CommonRedisKey; use App\Cache\Redis\RedisCache; -use App\Exception\AdminException; +use App\Exception\ErrException; use App\Lib\AdminReturn; use App\Lib\Log; use App\Model\OssObject; @@ -158,7 +158,7 @@ class OssCallbackService }catch (Exception $exception){ $this->deleteOssObject(); - throw new AdminException($exception->getMessage()); + throw new ErrException($exception->getMessage()); } $this->log->callbackLog(__CLASS__.':oss回调完成'.json_encode($this->data),'oss'); @@ -289,7 +289,7 @@ class OssCallbackService $ossObjectModel->type = $type; if (!$ossObjectModel->save()){ - throw new AdminException('保存图片失败'); + throw new ErrException('保存图片失败'); } $this->newId = $ossObjectModel->id; diff --git a/app/Service/ServiceTrait/Api/WxMiniTrait.php b/app/Service/ServiceTrait/Api/WxMiniTrait.php index 42a413e..d847848 100644 --- a/app/Service/ServiceTrait/Api/WxMiniTrait.php +++ b/app/Service/ServiceTrait/Api/WxMiniTrait.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace App\Service\ServiceTrait\Api; -use App\Exception\ApiException; +use App\Exception\ErrException; use App\Lib\Log; use GuzzleHttp\Exception\GuzzleException; use Hyperf\Di\Annotation\Inject; @@ -68,12 +68,12 @@ trait WxMiniTrait $res = json_decode($contents,true); - if (empty($res['errcode']) || $res['errcode'] != 0) throw new ApiException($res['errmsg'] ?? '系统繁忙'); + if (empty($res['errcode']) || $res['errcode'] != 0) throw new ErrException($res['errmsg'] ?? '系统繁忙'); return $res; }catch (GuzzleException $e) { $this->log->debug(__CLASS__.':debug:'.$e->getMessage()); - throw new ApiException($e->getMessage()); + throw new ErrException($e->getMessage()); } } } \ No newline at end of file diff --git a/app/Service/ServiceTrait/Common/OssTrait.php b/app/Service/ServiceTrait/Common/OssTrait.php new file mode 100644 index 0000000..61948b0 --- /dev/null +++ b/app/Service/ServiceTrait/Common/OssTrait.php @@ -0,0 +1,46 @@ +ossObjectModel->getIdListByIds($ossIds); + if (empty($data)){ + throw new ErrException('资源不存在'); + } + + $data = $data->toArray(); + + if (count($data) != count($ossIds)) { + throw new ErrException('资源不存在【' . implode('|', array_diff($ossIds, $data)) . '】'); + } + } + +} \ No newline at end of file diff --git a/config/autoload/exceptions.php b/config/autoload/exceptions.php index 360d55c..f244163 100644 --- a/config/autoload/exceptions.php +++ b/config/autoload/exceptions.php @@ -13,8 +13,7 @@ return [ 'handler' => [ 'http' => [ Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class, - App\Exception\Handler\ApiExceptionHandler::class, - App\Exception\Handler\AdminExceptionHandler::class, + App\Exception\Handler\ErrExceptionHandler::class, App\Exception\Handler\ValidationDataExceptionHandler::class, App\Exception\Handler\AppExceptionHandler::class, ], diff --git a/sync/http/admin/auth.http b/sync/http/admin/auth.http index aec1eba..9899a1d 100644 --- a/sync/http/admin/auth.http +++ b/sync/http/admin/auth.http @@ -175,4 +175,12 @@ Authorization: Bearer {{admin_token}} ### 厨房删除 GET {{host}}/admin/kitchen/del?id=1 content-type: application/json -Authorization: Bearer {{admin_token}} \ No newline at end of file +Authorization: Bearer {{admin_token}} + + +### 添加配送点 +POST {{host}}/admin/site/add +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +name=华润大厦&address=华润大厦&lng=113.26452099999997&lat=23.12916&status=1&kitchen_id=1&driver_id=1&expected_delivery_time=11:40&city_id=1 \ No newline at end of file