isAjax()) { $param = get_params(); $rows = empty($param['limit']) ? get_md_contentconfig('app.page_size') : $param['limit']; $list = InformationList::withoutField('') ->where('delete_time', null) ->order('id desc') ->paginate($rows, false, ['query' => $param]) ->each(function ($item, $key) { // 这里简化查询逻辑,避免重复查询 $item->product = Db::name('Product')->where('id', $item->product)->value('name') ?: ''; $item->ip = $this->ipquery($item->ip); // 调用 ipquery 方法查询 IP 数据 switch ($item->status) { case 0: $item->status = '新商机'; break; case 1: $item->status = '已跟进'; break; case 2: $item->status = '已转化'; break; case 3: $item->status = '已关闭'; break; } }) ->filter(function ($item) { return $item->delete_time === null; }); return table_assign(0, '', $list); } else { return view(); } } // 获取信息列表(分页,每页10条) public function list() { try { // 查询数据,分页设置每页10条 $list = Db::name('Information') // ->field('id, title, content, create_time, update_time') // 选择所需字段 ->order('id desc') // 按 id 倒序排序 ->paginate(10); // 每页10条记录 // 返回成功响应 return json([ 'code' => 1, // 成功状态码 'msg' => '获取成功', // 提示信息 'data' => $list->items(), // 当前页数据 'pagination' => [ 'total' => $list->total(), // 总记录数 'per_page' => $list->listRows(), // 每页条数 'current_page' => $list->currentPage(), // 当前页码 'last_page' => $list->lastPage() // 总页数 ] ]); } catch (\Exception $e) { // 捕获异常并返回错误信息 return json([ 'code' => 0, // 失败状态码 'msg' => '获取失败: ' . $e->getMessage(), // 错误信息 'data' => [] // 空数据 ]); } } //增加商机(app端) public function addbusinessinfo() { $param = get_params(); if (request()->isPost()) { // 新增逻辑 $param['create_time'] = time(); $sid = InformationList::strict(false)->field(true)->insertGetId($param); if ($sid) { add_log('add', $sid, $param); return json(["code" => 0, "msg" => "添加成功"]); // 返回JSON格式数据 } else { return json(["code" => 1, "msg" => "添加失败"]); // 返回JSON格式数据 } } else { $id = isset($param['id']) ? $param['id'] : 0; if ($id > 0) { $businessinfoLists = InformationList::where('id', $id) ->field('id, name, email, phone, message, product, company, create_time') ->find(); if (empty($businessinfoLists)) { return json(["code" => 1, "msg" => "信息不存在"]); // 返回JSON格式数据 } } json(["code" => 0, "msg" => "编辑成功"]); // 返回JSON格式数据 } } //添加 public function add() { $param = get_params(); if (request()->isPost()) { if (!empty($param['id']) && $param['id'] > 0) { $information = (new InformationList())->detail($param['id']); try { validate(InformationCheck::class)->scene('edit')->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } $param['update_time'] = time(); $res = InformationList::where('id', $param['id'])->strict(false)->field(true)->update($param); if ($res) { add_log('edit', $param['id'], $param, $information); } return to_assign(); } else { try { validate(InformationCheck::class)->scene('add')->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return to_assign(1, $e->getError()); } $exist = InformationList::where('name', $param['name']) ->where('id', '<>', $param['id'])->find(); if ($exist) { return to_assign(1, '该信息已存在'); } $param['create_time'] = time(); $param['admin_id'] = $this->uid; $sid = InformationList::strict(false)->field(true)->insertGetId($param); if ($sid) { add_log('add', $sid, $param); $log_data = array( 'module' => 'business_info', 'id' => $sid, 'new_content' => $param['name'], 'field' => 'new', 'action' => 'add', 'admin_id' => $this->uid, 'create_time' => time(), ); Db::name('Log')->strict(false)->field(true)->insert($log_data); } return to_assign(2, '添加成功!'); } } else { $id = isset($param['id']) ? $param['id'] : 0; if ($id > 0) { $detail = (new InformationList())->detail($id); if (empty($detail)) { return to_assign(1, '1.信息不存在'); } View::assign('detail', $detail); } View::assign('id', $id); return view(); } } //查看 public function view() { $param = get_params(); $id = isset($param['id']) ? $param['id'] : 0; $detail = (new InformationList())->detail($id); // print_r($id); if (empty($detail)) { return to_assign(1, '无法查询到详情信息'); } else { View::assign('detail', $detail); // print_r($detail); return view(); } } //删除 public function delete() { $request = request(); if ($request->isDelete() || $request->isAjax()) { $id = get_params("id"); $detail = Db::name('Information')->where('id', $id)->find(); // if ($detail['admin_id'] != $this->uid) { // return to_assign(1, "你不是图片的上传人,无权限删除"); // } if (Db::name('Information')->where('id', $id)->update(['delete_time' => time()]) !== false) { $log_data = array( 'module' => 'picbed', 'field' => 'delete', 'action' => 'delete', 'admin_id' => $this->uid, 'old_content' => '', 'new_content' => $detail['name'], 'create_time' => time(), ); Db::name('Log')->strict(false)->field(true)->insert($log_data); return to_assign(0, "删除成功"); } else { return to_assign(0, "删除失败"); } } else { return to_assign(1, "错误的请求"); } } }