From ae6553d7b1ab3e6be8535af120d9c9211b4b7b78 Mon Sep 17 00:00:00 2001 From: weizong song Date: Fri, 14 Jul 2023 10:11:41 +0800 Subject: [PATCH 1/2] up --- .../Commands/SyncAlipayRechargeState.php | 2 +- .../Admin/StatisticsController.php | 10 ++ .../Controllers/Worker/AuthController.php | 138 ++++++++++++++++++ .../Controllers/Worker/CommonController.php | 27 ++++ app/Http/Middleware/VerifyCsrfToken.php | 3 +- app/Worker.php | 88 +++++++++++ config/app.php | 4 +- config/auth.php | 10 ++ ...93851_update_paramedic_add_auth_fields.php | 31 ++++ .../views/admin/statistics/finance.blade.php | 10 ++ routes/web.php | 10 ++ 11 files changed, 330 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/Worker/AuthController.php create mode 100644 app/Http/Controllers/Worker/CommonController.php create mode 100644 app/Worker.php create mode 100644 database/migrations/2023_07_14_093851_update_paramedic_add_auth_fields.php diff --git a/app/Console/Commands/SyncAlipayRechargeState.php b/app/Console/Commands/SyncAlipayRechargeState.php index 1f7e9b6..5695cac 100644 --- a/app/Console/Commands/SyncAlipayRechargeState.php +++ b/app/Console/Commands/SyncAlipayRechargeState.php @@ -42,7 +42,7 @@ class SyncAlipayRechargeState extends Command { $threshold = 5; $offset_seconds = 40; //支付发起后多少秒开始轮询 - $due_minutes = 10; //支付发起后第一次开始轮询的时间往后延迟5分钟后不再轮询 + $due_minutes = 10; //支付发起后第一次开始轮询的时间往后延迟若干分钟后不再轮询 $last_id = cache("last_sync_alipay_recharge_id", 0); DB::enableQueryLog(); diff --git a/app/Http/Controllers/Admin/StatisticsController.php b/app/Http/Controllers/Admin/StatisticsController.php index aee76c3..8066377 100755 --- a/app/Http/Controllers/Admin/StatisticsController.php +++ b/app/Http/Controllers/Admin/StatisticsController.php @@ -39,6 +39,16 @@ class StatisticsController extends CommonController return $projects; } + public function getYears() + { + $start_year = config("start_year"); + $years = []; + for ($i = date("Y"); $i >= $start_year; $i--) { + $years[] = $i; + } + view()->share(compact("years")); + } + public function _getMonths() { $months = []; diff --git a/app/Http/Controllers/Worker/AuthController.php b/app/Http/Controllers/Worker/AuthController.php new file mode 100644 index 0000000..5c8c509 --- /dev/null +++ b/app/Http/Controllers/Worker/AuthController.php @@ -0,0 +1,138 @@ +authModel = new Worker(); + } + + /** + * Create a new AuthController instance. + * + * @return void + */ + + public function guard() + { + return auth()->guard($this->guardName); + } + + public function guardName() + { + return $this->guardName; + } + + /** + * @OA\Post( + * path="/worker/login-by-username", + * tags={"护工端用户相关"}, + * summary="V2-通过用户名密码登录", + * description="", + * @OA\Parameter(name="username", in="query", @OA\Schema(type="string"), required=true, description="用户名(身份证号)"), + * @OA\Parameter(name="password", in="query", @OA\Schema(type="string"), required=true, description="密码"), + * @OA\Response( + * response="200", + * description="护工通过用户名(身份证号)密码登录" + * ) + * ) + */ + public function loginByUsername() + { + $credentials = [ + "id_card_number" => request()->username, + "password" => request()->password + ]; + if (!$token = $this->guard()->attempt($credentials)) { + return response()->json([ + 'errorcode' => '401', + 'errormsg' => '登录失败' + ], 401); + } + + return $this->respondWithToken($token); + } + + /** + * @OA\Post( + * path="/worker/me", + * tags={"护工端用户相关"}, + * summary="V2-获取登录者信息", + * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), + * description="", + * @OA\Response( + * response="200", + * description="获取登录者信息" + * ) + * ) + */ + public function me() + { + $id = $this->guard()->id(); + $paramedic = (new Paramedic())->find($id); + return response()->json($paramedic->toArray()); + } + + /** + * @OA\Post( + * path="/worker/logout", + * tags={"护工端用户相关"}, + * summary="V2 退出登录", + * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), + * description="", + * @OA\Response( + * response="200", + * description="退出登录" + * ) + * ) + */ + public function logout() + { + DB::beginTransaction(); + try { + $this->guard()->logout(); + DB::commit(); + return response()->json([ + 'errormsg' => "退出登录成功!" + ]); + } catch (\Exception $exception) { + DB::rollBack(); + return response()->json([ + 'errorcode' => '402', + 'errormsg' => $exception->getMessage() + ]); + } + } + + + /** + * Get the token array structure. + * + * @param string $token + * + * @return \Illuminate\Http\JsonResponse + */ + protected function respondWithToken($token) + { + $user = $this->guard()->user(); + $user = $user->toArray(); + + return response()->json([ + 'access_token' => $token, + 'token_type' => 'bearer', + 'expires_in' => $this->guard()->factory()->getTTL() * 60, + 'user_info' => $user + ]); + } +} diff --git a/app/Http/Controllers/Worker/CommonController.php b/app/Http/Controllers/Worker/CommonController.php new file mode 100644 index 0000000..df24d7e --- /dev/null +++ b/app/Http/Controllers/Worker/CommonController.php @@ -0,0 +1,27 @@ +guard()->user(); + $this->worker = $worker; + } + + public function guard() + { + return auth()->guard($this->guardName); + } + + public function guardName() + { + return $this->guardName; + } +} diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index 454152c..fbf5dfc 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -13,6 +13,7 @@ class VerifyCsrfToken extends Middleware */ protected $except = [ 'customer/*', - 'manager/*' + 'manager/*', + 'worker/*', ]; } diff --git a/app/Worker.php b/app/Worker.php new file mode 100644 index 0000000..d9a7115 --- /dev/null +++ b/app/Worker.php @@ -0,0 +1,88 @@ +secure() ? "https" : "http"; + if (!$this->avatar) { + switch ($this->sex) { + case "男": + $this->avatar = "/images/male.png"; + break; + case "女": + $this->avatar = "/images/female.png"; + break; + } + } + return $this->avatar ? $protocol . "://" . request()->getHost() . $this->avatar : $this->avatar; + } + + public function guardName() + { + return self::GUARD_NAME; + } + + // Rest omitted for brevity + + /** + * Get the identifier that will be stored in the subject claim of the JWT. + * + * @return mixed + */ + public function getJWTIdentifier() + { + return $this->getKey(); + } + + /** + * Return a key value array, containing any custom claims to be added to the JWT. + * + * @return array + */ + public function getJWTCustomClaims() + { + return []; + } + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = []; + + /** + * The attributes that should be hidden for arrays. + * + * @var array + */ + protected $hidden = [ + 'password', 'remember_token', + ]; + + /** + * The attributes that should be cast to native types. + * + * @var array + */ + protected $casts = [ + 'verified_at' => 'datetime', + ]; +} diff --git a/config/app.php b/config/app.php index 4c2cf5f..99d7613 100644 --- a/config/app.php +++ b/config/app.php @@ -39,7 +39,7 @@ return [ | */ - 'debug' => (bool) env('APP_DEBUG', false), + 'debug' => (bool)env('APP_DEBUG', false), /* |-------------------------------------------------------------------------- @@ -56,6 +56,8 @@ return [ 'asset_url' => env('ASSET_URL', null), + 'start_year' => env('START_YEAR', 2017), + /* |-------------------------------------------------------------------------- | Application Timezone diff --git a/config/auth.php b/config/auth.php index 287c850..a6b5d38 100644 --- a/config/auth.php +++ b/config/auth.php @@ -50,6 +50,11 @@ return [ 'provider' => 'manager', ], + 'worker' => [ + 'driver' => 'jwt', + 'provider' => 'worker', + ], + // 'api' => [ // 'driver' => 'token', // 'provider' => 'users', @@ -90,6 +95,11 @@ return [ 'model' => App\Manager::class, ], + 'worker' => [ + 'driver' => 'eloquent', + 'model' => App\Worker::class, + ], + // 'users' => [ // 'driver' => 'database', // 'table' => 'users', diff --git a/database/migrations/2023_07_14_093851_update_paramedic_add_auth_fields.php b/database/migrations/2023_07_14_093851_update_paramedic_add_auth_fields.php new file mode 100644 index 0000000..2618294 --- /dev/null +++ b/database/migrations/2023_07_14_093851_update_paramedic_add_auth_fields.php @@ -0,0 +1,31 @@ +string("remember_token")->nullable(); + $table->string("password")->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +} diff --git a/resources/views/admin/statistics/finance.blade.php b/resources/views/admin/statistics/finance.blade.php index 9f6d32b..9f62fdd 100755 --- a/resources/views/admin/statistics/finance.blade.php +++ b/resources/views/admin/statistics/finance.blade.php @@ -33,6 +33,16 @@ @endforeach +{{-- --}} +{{-- --}}
收款明细
diff --git a/routes/web.php b/routes/web.php index dfd27c2..92ce0d6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -188,3 +188,13 @@ Route::group(["namespace" => "Manager", "prefix" => "manager"], function () { }); }); +Route::group(["namespace" => "Worker", "prefix" => "worker"], function () { + Route::post('logout', 'AuthController@logout'); + Route::post('login-by-username', 'AuthController@loginByUsername'); + + Route::group(['middleware' => ['authorize.jwt:manager']], function () { + Route::post('me', 'AuthController@me'); + + }); +}); + From 38b484000017f88abd38414d399eb3a87cbf78bf Mon Sep 17 00:00:00 2001 From: weizong song Date: Fri, 14 Jul 2023 10:58:14 +0800 Subject: [PATCH 2/2] up --- .../Controllers/Worker/AuthController.php | 2 +- .../Controllers/Worker/OrdersController.php | 183 ++++++++++++++++++ routes/web.php | 7 +- 3 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 app/Http/Controllers/Worker/OrdersController.php diff --git a/app/Http/Controllers/Worker/AuthController.php b/app/Http/Controllers/Worker/AuthController.php index 5c8c509..b40b4f3 100644 --- a/app/Http/Controllers/Worker/AuthController.php +++ b/app/Http/Controllers/Worker/AuthController.php @@ -65,7 +65,7 @@ class AuthController extends Controller } /** - * @OA\Post( + * @OA\Get( * path="/worker/me", * tags={"护工端用户相关"}, * summary="V2-获取登录者信息", diff --git a/app/Http/Controllers/Worker/OrdersController.php b/app/Http/Controllers/Worker/OrdersController.php new file mode 100644 index 0000000..67ece84 --- /dev/null +++ b/app/Http/Controllers/Worker/OrdersController.php @@ -0,0 +1,183 @@ +待处理,ongoing=>进行中,finished=>已完成]"), + * @OA\Response( + * response="200", + * description="获取订单列表" + * ) + * ) + */ + + public function list() + { + $worker = $this->guard()->user(); + $model = $this->_getOrderModel(); + + $ids = OrderItems::where("paramedic_id", $worker->id)->where("total", ">", 0)->pluck("order_id")->toArray(); + $model = $model->whereIn("id", $ids); + + if (request()->keyword) { + $keyword = request()->keyword; + $model = $model->where(function ($query) use ($keyword) { + $query + ->where("serial", "like", "%{$keyword}%") + ->orWhereHas("patient", function ($query) use ($keyword) { + $query->where("name", "like", "%{$keyword}%"); + })->orWhereHas("customer", function ($query) use ($keyword) { + $query->where("mobile", "like", "%{$keyword}%"); + })->orWhereHas("paramedic", function ($query) use ($keyword) { + $query->where("name", "like", "%{$keyword}%"); + }); + }); + } + + if (request()->start_date_from && request()->start_date_to) { + $model = $model->whereRaw(DB::raw("UNIX_TIMESTAMP(`from_date`) between " . strtotime(request()->start_date_from) . " and " . strtotime(request()->start_date_to))); + } + + switch (request()->status) { + case "pending": + $model = $model->whereIn("status", [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED]); + break; + case "ongoing": + case "finished": + $model = $model->where("status", constant(Orders::class . "::STATUS_" . strtoupper(request()->status))); + break; + default: + //do nothing + } + + $page_size = request()->page_size ?? 5; + $data = $model->orderBy("id", "desc")->paginate($page_size); + + return response()->json($data->toArray()); + } + + /** + * @OA\Get( + * path="/worker/get-order/{id}", + * tags={"护工端订单处理"}, + * summary="V2-获取订单详情", + * description="获取订单详情", + * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), + * @OA\Parameter(name="id", in="path", @OA\Schema(type="integer"), required=true, description="id"), + * @OA\Response( + * response="200", + * description="获取订单详情" + * ) + * ) + */ + + public function getOrder($id) + { + $worker = $this->guard()->user(); + $model = $this->_getOrderModel(); + $ids = OrderItems::where("order_id", $id)->where("paramedic_id", $worker->id)->where("total", ">", 0)->pluck("order_id")->toArray(); + if (!$ids) { + return response()->json([ + "errorcode" => "40004", + "errormsg" => "没找到订单" + ]); + } + + $order = $model->with([ + "orderItems" => function ($query) use ($worker) { + $query->where("paramedic_id", $worker->id)->where("total", ">", 0)->orderBy("service_date", "desc"); + } + ])->find($id); + + return response()->json($order->toArray()); + } + + public function _getOrderModel() + { + $model = $order = (new Orders()) + ->select( + "orders.id", + "orders.serial", + "orders.customer_id", + "orders.manager_id", + "orders.bed_id", + "orders.patient_id", + "orders.project_id", + "orders.product_id", + "orders.product_item_id", + "orders.product_paramedic_level_id", + "orders.from_date", + "orders.to_date", + "orders.status", + "orders.total", + "orders.paid_total", + "orders.contact", + "orders.mobile", + "orders.paramedic_id", + "orders.price", + "orders.factors", + "orders.patient_quantity", + "orders.created_at" + ) + ->with([ + "project" => function ($query) { + $query->select("id", "name"); + }, + "bed" => function ($query) { + $query->select("bed.id", "bed.name", "bed.building_id", "bed.area_id") + ->leftJoin("building", "building.id", "=", "bed.building_id") + ->leftJoin("area", "area.id", "=", "bed.area_id") + ->leftJoin("room", "room.id", "=", "bed.room_id") + ->addSelect("room.name as room_name", "area.name as area_name", "building.name as building_name"); + }, + "customer" => function ($query) { + $query->select("id", "name", "balance"); + }, + "patient" => function ($query) { + $query->select("id", "name", "sex", "age", "mobile"); + } + ]); + return $model; + } +} diff --git a/routes/web.php b/routes/web.php index 92ce0d6..9d8068f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -192,9 +192,10 @@ Route::group(["namespace" => "Worker", "prefix" => "worker"], function () { Route::post('logout', 'AuthController@logout'); Route::post('login-by-username', 'AuthController@loginByUsername'); - Route::group(['middleware' => ['authorize.jwt:manager']], function () { - Route::post('me', 'AuthController@me'); - + Route::group(['middleware' => ['authorize.jwt:worker']], function () { + Route::get('me', 'AuthController@me'); + Route::get('get-orders', 'OrdersController@list'); + Route::get('get-order/{id}', 'OrdersController@getOrder'); }); });