2025-06-25 11:52:01 +08:00

276 lines
9.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @copyright Copyright (c) 2023-2024 美天智能科技
* @link http://www.meteteme.com
*/
declare(strict_types=1);
namespace app\information\controller;
use app\base\BaseController;
use app\model\Information as InformationList;
use app\business_info\validate\InformationCheck;
use Error;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Index extends BaseController
{
// 查询 IP 地址
public function ipquery($ip)
{
$url = "https://cn.apihz.cn/api/ip/chaapi.php?ip={$ip}&id=10001344&key=3cffc3ad20630afffe65c3b908d6ebd4";
$response = @file_get_contents($url);
if ($response) {
$ipData = json_decode($response, true);
if ($ipData && isset($ipData['ip'])) {
$q = "{$ipData['msg']}{$ipData['ip']}";
if (is_string($q)) {
return $q;
}
}
}
return '接口每分钟查询次数用完,请稍后再试';
}
public function translateToChinese($q)
{
$id = 10001344;
$key = '3cffc3ad20630afffe65c3b908d6ebd4 ';
// 构造请求URL
$url = "https://cn.apihz.cn/api/zici/fanyiapihz.php?id=" . $id . "&key=" . $key . "&words=" . $q . "&ytype=1&etype=2&htype=0";
//调试输出语句
var_dump($url);
// 发送请求并获取响应
$response = @file_get_contents($url);
// 处理响应
if ($response) {
$data = json_decode($response, true);
if (isset($data['translation'][0])) {
return $data['translation'][0]; // 返回翻译结果
}
}
// 返回错误信息
return $q;
}
public function index()
{
if (request()->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, "错误的请求");
}
}
}