get('page', 1); $limit = (int)$request->get('limit', 15); if ($page < 1) $page = 1; if ($limit < 1) $limit = 15; $query = WechatApp::query(); if (($name = trim((string)$request->get('name', ''))) !== '') { $query->where('name', 'like', "%{$name}%"); } if (($appId = trim((string)$request->get('app_id', ''))) !== '') { $query->where('app_id', 'like', "%{$appId}%"); } if (($type = trim((string)$request->get('type', ''))) !== '') { $query->where('type', $type); } if ($request->get('status') !== null && $request->get('status') !== '') { $query->where('status', (int)$request->get('status')); } $total = $query->count(); $list = $query->select([ 'id', 'name', 'type', 'app_id', 'status', 'remark', 'created_at', ]) ->selectRaw("IF(app_secret IS NULL OR app_secret = '', 0, 1) as has_secret") ->orderByDesc('id') ->offset(($page - 1) * $limit) ->limit($limit) ->get(); return jsonResponse([ 'total' => $total, 'list' => $list, ]); } public function create(Request $request) { $name = trim((string)$request->post('name', '')); $type = trim((string)$request->post('type', 'h5')); $appId = trim((string)$request->post('app_id', '')); $appSecret = trim((string)$request->post('app_secret', '')); $status = (int)$request->post('status', 1); $remark = trim((string)$request->post('remark', '')); if ($name === '' || $appId === '') { return jsonResponse(null, '名称和AppID必填', 400); } if (!in_array($type, ['h5', 'mini'], true)) { return jsonResponse(null, '类型不合法', 400); } try { $row = WechatApp::create([ 'name' => $name, 'type' => $type, 'app_id' => $appId, 'app_secret' => $appSecret ?: null, 'status' => $status ? 1 : 0, 'remark' => $remark ?: null, ]); return jsonResponse($row, '创建成功'); } catch (\Throwable $e) { return jsonResponse(null, '创建失败: ' . $e->getMessage(), 500); } } public function update(Request $request) { $id = (int)$request->post('id'); $row = WechatApp::find($id); if (!$row) { return jsonResponse(null, '应用不存在', 404); } $name = trim((string)$request->post('name', $row->name)); $type = trim((string)$request->post('type', $row->type)); $appId = trim((string)$request->post('app_id', $row->app_id)); $appSecret = trim((string)$request->post('app_secret', '')); $status = (int)$request->post('status', $row->status); $remark = trim((string)$request->post('remark', $row->remark ?? '')); if ($name === '' || $appId === '') { return jsonResponse(null, '名称和AppID必填', 400); } if (!in_array($type, ['h5', 'mini'], true)) { return jsonResponse(null, '类型不合法', 400); } DB::beginTransaction(); try { $row->name = $name; $row->type = $type; $row->app_id = $appId; if ($appSecret !== '') { $row->app_secret = $appSecret; } $row->status = $status ? 1 : 0; $row->remark = $remark ?: null; $row->save(); DB::commit(); return jsonResponse(null, '更新成功'); } catch (\Throwable $e) { DB::rollBack(); return jsonResponse(null, '更新失败: ' . $e->getMessage(), 500); } } public function delete(Request $request) { $id = (int)$request->post('id'); $row = WechatApp::find($id); if (!$row) { return jsonResponse(null, '应用不存在', 404); } $row->delete(); return jsonResponse(null, '删除成功'); } }