From 1bc80113e29330f3ca9f8d1ec02cfa71a88469a8 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Fri, 20 Jun 2025 13:27:35 +0800 Subject: [PATCH] update --- .../Admin/SupplyDemandTypeController.php | 186 -------------- .../Mobile/SupplyDemandController.php | 237 ++++++++++++++++++ routes/api.php | 6 + 3 files changed, 243 insertions(+), 186 deletions(-) delete mode 100755 app/Http/Controllers/Admin/SupplyDemandTypeController.php create mode 100755 app/Http/Controllers/Mobile/SupplyDemandController.php diff --git a/app/Http/Controllers/Admin/SupplyDemandTypeController.php b/app/Http/Controllers/Admin/SupplyDemandTypeController.php deleted file mode 100755 index 53d0a22..0000000 --- a/app/Http/Controllers/Admin/SupplyDemandTypeController.php +++ /dev/null @@ -1,186 +0,0 @@ -all(); - $list = $this->model->with('icon')->where(function ($query) use ($all) { - if (isset($all['filter']) && !empty($all['filter'])) { - foreach ($all['filter'] as $condition) { - $key = $condition['key'] ?? null; - $op = $condition['op'] ?? null; - $value = $condition['value'] ?? null; - if (!isset($key) || !isset($op) || !isset($value)) { - continue; - } - // 等于 - if ($op == 'eq') { - $query->where($key, $value); - } - // 不等于 - if ($op == 'neq') { - $query->where($key, '!=', $value); - } - // 大于 - if ($op == 'gt') { - $query->where($key, '>', $value); - } - // 大于等于 - if ($op == 'egt') { - $query->where($key, '>=', $value); - } - // 小于 - if ($op == 'lt') { - $query->where($key, '<', $value); - } - // 小于等于 - if ($op == 'elt') { - $query->where($key, '<=', $value); - } - // 模糊搜索 - if ($op == 'like') { - $query->where($key, 'like', '%' . $value . '%'); - } - // 否定模糊搜索 - if ($op == 'notlike') { - $query->where($key, 'not like', '%' . $value . '%'); - } - // 范围搜索 - if ($op == 'range') { - list($from, $to) = explode(',', $value); - if (empty($from) || empty($to)) { - continue; - } - $query->whereBetween($key, [$from, $to]); - } - } - } - })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc'); - if (isset($all['is_export']) && !empty($all['is_export'])) { - $list = $list->get()->toArray(); - $export_fields = $all['export_fields'] ?? []; - // 导出文件名字 - $tableName = $this->model->getTable(); - $filename = (new CustomForm())->getTableComment($tableName); - return Excel::download(new BaseExport($export_fields, $list, $tableName), $filename . date('YmdHis') . '.xlsx'); - } else { - // 输出 - $list = $list->paginate($all['page_size'] ?? 20); - } - return $this->success($list); - } - - /** - * @OA\Get( - * path="/api/admin/supply-demand-type/show", - * tags={"供需类型管理"}, - * summary="详情", - * description="", - * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), - * @OA\Parameter(name="show_relation", in="query", @OA\Schema(type="string"), required=false, description="需要输出的关联关系数组,填写输出指定数据"), - * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), - * @OA\Response( - * response="200", - * description="暂无" - * ) - * ) - */ - public function show() - { - $all = \request()->all(); - $messages = [ - 'id.required' => 'Id必填', - ]; - $validator = Validator::make($all, [ - 'id' => 'required' - ], $messages); - if ($validator->fails()) { - return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); - } - $detail = $this->model->with(underlineToHump($all['show_relation'] ?? []))->find($all['id']); - return $this->success($detail); - } - - /** - * @OA\Post( - * path="/api/admin/supply-demand-type/save", - * tags={"供需类型管理"}, - * summary="更新或新增", - * @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=true, description="id"), - * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="验证token"), - * @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), description="名字"), - * @OA\Parameter(name="icon_id", in="query", @OA\Schema(type="string", format="date"), description="icon的id"), - * @OA\Parameter(name="status", in="query", @OA\Schema(type="string", format="date"), description="状态0禁用1启用"), - * @OA\Response( - * response=200, - * description="操作成功" - * ) - * ) - */ - public function save() - { - return parent::save(); - } - - /** - * @OA\Get( - * path="/api/admin/supply-demand-type/destroy", - * tags={"供需类型管理"}, - * summary="删除", - * description="", - * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), - * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), - * @OA\Response( - * response="200", - * description="暂无" - * ) - * ) - */ - public function destroy() - { - return parent::destroy(); - } - - -} diff --git a/app/Http/Controllers/Mobile/SupplyDemandController.php b/app/Http/Controllers/Mobile/SupplyDemandController.php new file mode 100755 index 0000000..3f5eec9 --- /dev/null +++ b/app/Http/Controllers/Mobile/SupplyDemandController.php @@ -0,0 +1,237 @@ +all(); + $supplyDemands = SupplyDemand::where(function ($query) use ($all) { + if (isset($all['type'])) { + $query->where('type', $all['type']); + } + if (isset($all['status'])) { + $query->where('status', $all['status']); + } + if (isset($all['keyword'])) { + $query->where('content', 'like', '%' . $all['keyword'] . '%'); + } + if (isset($all['myself']) && $all['myself'] == 1) { + $query->where('user_id', $this->getUserId()); + } + })->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc') + ->paginate($all['page_size'] ?? 20); + return $this->success(compact('supplyDemands')); + } + + /** + * @OA\Get( + * path="/api/mobile/supply-demand/detail", + * tags={"小程序-供需"}, + * summary="详情", + * @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=true, description="id"), + * @OA\Response( + * response=200, + * description="操作成功" + * ) + * ) + */ + public function detail() + { + $all = \request()->all(); + $messages = [ + 'id.required' => 'Id必填', + ]; + $validator = Validator::make($all, [ + 'id' => 'required' + ], $messages); + if ($validator->fails()) { + return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); + } + $detail = SupplyDemand::with(['user' => function ($query) { + $query->select('id', 'nickname', 'name', 'headimgurl'); + }])->find($all['id']); + return $this->success($detail); + } + + /** + * @OA\Post( + * path="/api/mobile/supply-demand/save", + * tags={"小程序-供需"}, + * summary="更新", + * description="", + * @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=false, description="需求供应表ID(存在则更新,不存在则新增)"), + * @OA\Parameter(name="title", in="query", @OA\Schema(type="string"), required=false, description="标题"), + * @OA\Parameter(name="supply_demand_type_id", in="query", @OA\Schema(type="integer"), required=false, description="分类ID"), + * @OA\Parameter(name="content", in="query", @OA\Schema(type="string"), required=false, description="内容"), + * @OA\Parameter(name="tag", in="query", @OA\Schema(type="string"), required=false, description="标签"), + * @OA\Parameter(name="wechat", in="query", @OA\Schema(type="string"), required=false, description="微信号"), + * @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=false, description="电话"), + * @OA\Parameter(name="email", in="query", @OA\Schema(type="string"), required=false, description="邮箱"), + * @OA\Parameter(name="status", in="query", @OA\Schema(type="integer"), required=false, description="审核状态(0:待审核;1:通过;2:拒绝)"), + * @OA\Response( + * response="200", + * description="暂无" + * ) + * ) + */ + public function save() + { + $all = \request()->all(); + DB::beginTransaction(); + try { + if (isset($all['id'])) { + $model = SupplyDemand::where('user_id', $this->getUserId())->find($all['id']); + if (empty($model)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']); + } + } else { + $model = new SupplyDemand(); + $all['user_id'] = $this->getUserId(); + } + $model->fill($all); + $model->save(); + DB::commit(); + return $this->success($model); + } catch (\Exception $exception) { + DB::rollBack(); + return $this->fail([$exception->getCode(), $exception->getMessage()]); + } + } + + /** + * @OA\Get( + * path="/api/mobile/supply-demand/destroy", + * tags={"小程序-供需"}, + * summary="删除", + * description="", + * @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"), + * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), + * @OA\Response( + * response="200", + * description="暂无" + * ) + * ) + */ + public function destroy() + { + $all = \request()->all(); + $messages = [ + 'id.required' => 'Id必填', + ]; + $validator = Validator::make($all, [ + 'id' => 'required' + ], $messages); + if ($validator->fails()) { + return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); + } + $model = SupplyDemand::find($all['id']); + if (empty($model)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']); + } + $model->delete(); + return $this->success('删除成功'); + } + + /** + * @OA\Get( + * path="/api/mobile/supply-demand/send-message", + * tags={"小程序-供需"}, + * summary="发送消息", + * description="", + * @OA\Parameter(name="supply_demand_id", in="query", @OA\Schema(type="string"), required=true, description="供需信息id"), + * @OA\Parameter(name="content", in="query", @OA\Schema(type="string"), required=true, description="内容"), + * @OA\Parameter(name="to_user_id", in="query", @OA\Schema(type="string"), required=true, description="接收人用户id"), + * @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"), + * @OA\Response( + * response="200", + * description="暂无" + * ) + * ) + */ + public function sendMessage() + { + $all = \request()->all(); + $messages = [ + 'supply_demand_id.required' => '供需信息id必填', + 'content.required' => '内容必填', + 'to_user_id.required' => '接收人必填', + ]; + $validator = Validator::make($all, [ + 'supply_demand_id' => 'required', + 'content' => 'required', + 'to_user_id' => 'required' + ], $messages); + if ($validator->fails()) { + return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]); + } + $model = SupplyDemand::find($all['id']); + if (empty($model)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '供需数据不存在']); + } + // 每天限制私信次数限制 + $message_limit = Config::getValueByKey('message_limit'); + $messageToday = Message::where('user_id', $this->getUserId()) + ->where('created_at', 'like', '%' . date('Y-m-d') . '%') + ->get(); + if ($messageToday > $message_limit) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '今天私信次数已达上限']); + } + // 有且仅有自己发的信息,则不能发再 + $myMessage = Message::where('user_id', $this->getUserId()) + ->where('supply_demand_id', $all['supply_demand_id']) + ->first(); + // 对方的信息 + $otherMessage = Message::where('user_id', $all['to_user_id']) + ->where('supply_demand_id', $all['supply_demand_id']) + ->first(); + if ($myMessage && empty($otherMessage)) { + return $this->fail([ResponseCode::ERROR_BUSINESS, '对方回复以后才可以再次发送消息']); + } + // 创建消息 + Message::create([ + 'user_id' => $this->getUserId(), + 'to_user_id' => $all['to_user_id'], + 'supply_demand_id' => $all['supply_demand_id'], + 'content' => $all['content'], + 'is_read' => 0 + ]); + return $this->success('删除成功'); + } + + +} diff --git a/routes/api.php b/routes/api.php index 3748582..a336aca 100755 --- a/routes/api.php +++ b/routes/api.php @@ -225,6 +225,12 @@ Route::group(["namespace" => "Mobile", "prefix" => "mobile"], function () { Route::post('schedule/save', [\App\Http\Controllers\Mobile\ScheduleController::class, "save"]); Route::get('schedule/cancel', [\App\Http\Controllers\Mobile\ScheduleController::class, "cancel"]); + // 供需信息 + Route::get('supply-demand/index', [\App\Http\Controllers\Mobile\SupplyDemandController::class, "index"]); + Route::get('supply-demand/detail', [\App\Http\Controllers\Mobile\SupplyDemandController::class, "detail"]); + Route::get('supply-demand/save', [\App\Http\Controllers\Mobile\SupplyDemandController::class, "save"]); + Route::get('supply-demand/destroy', [\App\Http\Controllers\Mobile\SupplyDemandController::class, "destroy"]); + Route::get('supply-demand/send-message', [\App\Http\Controllers\Mobile\SupplyDemandController::class, "sendMessage"]); });