assign('cate', MallCate::column('title', 'id')); } #[NodeAnnotation(title: '列表', auth: true)] public function index(Request $request): Json|string { if ($request->isAjax()) { if (input('selectFields')) return $this->selectList(); list($page, $limit, $where) = $this->buildTableParams(); $count = self::$model::where($where)->count(); $list = self::$model::with(['cate'])->where($where)->page($page, $limit)->order($this->sort)->select()->toArray(); $data = [ 'code' => 0, 'msg' => '', 'count' => $count, 'data' => $list, ]; return json($data); } return $this->fetch(); } #[NodeAnnotation(title: '入库', auth: true)] public function stock(Request $request, $id): string { $row = self::$model::find($id); empty($row) && $this->error('数据不存在'); if ($request->isPost()) { $post = $request->post(); $rule = []; $this->validate($post, $rule); try { $post['total_stock'] = $row->total_stock + $post['stock']; $post['stock'] = $row->stock + $post['stock']; $save = $row->save($post); }catch (\Exception $e) { $this->error('保存失败'); } $save ? $this->success('保存成功') : $this->error('保存失败'); } $this->assign('row', $row); return $this->fetch(); } #[MiddlewareAnnotation(ignore: MiddlewareAnnotation::IGNORE_LOGIN)] public function no_check_login(Request $request): string { return '这里演示方法不需要经过登录验证'; } #[NodeAnnotation(title: 'AI优化', auth: true)] public function aiOptimization(Request $request): void { $message = $request->post('message'); if (empty($message)) $this->error('请输入内容'); // 演示环境下 默认返回的内容 if ($this->isDemo) { $content = << [ 'role' => 'assistant', 'content' => $content, ]]]; $this->success('success', compact('choices')); } try { $result = AiChatService::instance() // 当使用推理模型时,可能存在超时的情况,所以需要设置超时时间为 0 // ->setTimeLimit(0) // 请替换为您需要的模型类型 ->setAiType(AiType::QWEN) // 如果需要指定模型的 API 地址,可自行设置 // ->setAiUrl('https://xxx.com') // 请替换为您的模型 ->setAiModel('qwen-plus') // 请替换为您的 API KEY ->setAiKey('sk-1234567890') // 此内容会作为系统提示,会影响到回答的内容 当前仅作为测试使用 ->setSystemContent('你现在是一位资深的海外电商产品经理') ->chat($message); $choices = $result['choices']; }catch (\Throwable $exception) { $choices = [['message' => [ 'role' => 'assistant', 'content' => $exception->getMessage(), ]]]; } $this->success('success', compact('choices')); } }