2025-06-25 10:53:11 +08:00

404 lines
16 KiB
PHP
Raw Permalink 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 美天智能科技
* @author 李志强
* @link http://www.meteteme.com
*/
declare(strict_types=1);
namespace app\business\controller;
use app\base\BaseController;
use app\model\Business as BusinessList;
use app\model\Businesscontact;
use app\business\validate\BusinessCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Index extends BaseController
{
public function nav()
{
return view();
}
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$map1 = [
['admin_id', '=', $this->uid],
];
$map2 = [
['director_uid', '=', $this->uid],
];
$map4 = [
['', 'exp', Db::raw("FIND_IN_SET({$this->uid},check_admin_ids)")],
];
$map5 = [
['is_open', '=', 2],
];
$rows = empty($param['limit']) ? get_md_contentconfig('app.page_size') : $param['limit'];
// 判断是否有筛选条件传递,如果有则使用筛选条件,否则使用默认条件
$list = BusinessList::withoutField('')
->where(function ($query) use ($param, $map1, $map2, $map4, $map5) {
if (!empty($param['contact']) && !empty($param['name'])) {
// 同时存在联系人和名称的情况
$result = $query->where('contact', $param['contact'])->where('name', 'like', '%' . $param['name'] . '%')->find();
if ($result === null) {
return null;
}
} elseif (!empty($param['contact'])) {
// 存在联系人的情况
$query->where('contact', $param['contact']);
} elseif (!empty($param['name'])) {
// 存在名称的情况
$query->where('name', 'like', '%' . $param['name'] . '%');
} elseif (!empty($param['name_short'])) {
// 存在名称的情况
$query->where('name_short', 'like', '%' . $param['name_short'] . '%');
} else {
// 其他情况
$query->where($map1)->whereor($map2)->whereor($map4)->whereor($map5);
}
})
->where('delete_time', 0)
->order('id desc')
->paginate($rows, false, ['query' => $param])
->each(function ($item, $key) {
$item->director_name = Db::name('Admin')->where(['id' => $item->director_uid])->value('name');
$item->admin_name = Db::name('Admin')->where(['id' => $item->admin_id])->value('name');
$item->business = Db::name('Business')->where(['delete_time' => 0, 'id' => $item->id])->count();
$item->content = Db::name('Business')->where(['delete_time' => 0, 'id' => $item->id])->value('content');
$item->contact = Db::name('Businesscontact')->where(['business_id' => $item->id])->value('contact');
$item->post = Db::name('Businesscontact')->where(['business_id' => $item->id])->value('post');
$ids = Db::name('Business')->where(['delete_time' => 0, 'id' => $item->id])->column('id');
});
return table_assign(0, '', $list);
} else {
View::assign('user_info', get_login_admin());
return view();
}
}
//获取客户列表
public function getlists()
{
try {
// 查询数据分页设置每页10条
$list = Db::name('Business')
->alias('b')
->field('b.id, b.name, b.name_short, b.company_address, a.nickname as director_uid') // 映射字段
->leftJoin('Admin a', 'b.director_uid = a.id') // 联表 Admin 表
->order('b.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' => [] // 空数据
]);
}
}
//获取客户列表(无页码)
public function getlistsmore()
{
try {
// 查询数据
$list = Db::name('Business')
->alias('b')
->field('b.id, b.name, b.name_short, a.nickname as director_uid') // 映射字段
->leftJoin('Admin a', 'b.director_uid = a.id') // 联表 Admin 表
->order('b.id desc') // 按 id 倒序排序
->select();
// 返回成功响应
return json([
'code' => 1, // 成功状态码
'msg' => '加载成功', // 提示信息
'data' => $list, // 当前页数据
]);
} catch (\Exception $e) {
// 捕获异常并返回错误信息
return json([
'code' => 0, // 失败状态码
'msg' => '获取失败: ' . $e->getMessage(), // 错误信息
'data' => [] // 空数据
]);
}
}
//新增客户app端
public function addcustomer()
{
$param = get_params();
if (request()->isPost()) {
// 新增逻辑
$param['create_time'] = time();
$sid = BusinessList::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) {
$businessLists = BusinessList::where('id', $id)
->field('id, name, email, phone, message, product, company, create_time')
->find();
if (empty($businessLists)) {
return json(["code" => 1, "msg" => "信息不存在"]); // 返回JSON格式数据
}
}
json(["code" => 0, "msg" => "编辑成功"]); // 返回JSON格式数据
}
}
//新增联系人app端
public function addcontact()
{
$param = get_params();
if (request()->isPost()) {
// 新增逻辑
$param['create_time'] = time();
$sid = Businesscontact::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) {
$businesscontactLists = Businesscontact::where('id', $id)
->field('id, name, email, phone, message, product, company, create_time')
->find();
if (empty($businesscontactLists)) {
return json(["code" => 1, "msg" => "信息不存在"]); // 返回JSON格式数据
}
}
json(["code" => 0, "msg" => "编辑成功"]); // 返回JSON格式数据
}
}
//获取联系人列表
public function getcontactlist()
{
try {
// 查询数据分页设置每页10条
$list = Db::name('Businesscontact')
->alias('b')
->field('b.id, b.contact, b.charge, b.post, b.phone, b.email, b.wechat, b.qq, a.name as business_id') // 映射字段
->leftJoin('Business a', 'b.business_id = a.id') // 联表 Admin 表
->order('b.id desc') // 按 id 倒序排序
->select(); // 每页10条记录
// 返回成功响应
return json([
'code' => 1, // 成功状态码
'msg' => '加载成功', // 提示信息
'data' => $list,
]);
} catch (\Exception $e) {
// 捕获异常并返回错误信息
return json([
'code' => 0, // 失败状态码
'msg' => '获取失败: ' . $e->getMessage(), // 错误信息
'data' => [] // 空数据
]);
}
}
//添加
public function add()
{
$param = get_params();
if (request()->isPost()) {
// markdown数据处理
if (isset($param['table-align'])) {
unset($param['table-align']);
}
if (isset($param['docContent-html-code'])) {
$param['content'] = $param['docContent-html-code'];
$param['md_content'] = $param['docContent-markdown-doc'];
unset($param['docContent-html-code']);
unset($param['docContent-markdown-doc']);
}
if (isset($param['ueditorcontent'])) {
$param['content'] = $param['ueditorcontent'];
$param['md_content'] = '';
}
try {
if (!empty($param['id'])) {
validate(BusinessCheck::class)->scene('edit')->check($param);
$business = (new BusinessList())->detail($param['id']);
if ($this->uid == $business['admin_id'] || $this->uid == $business['director_uid']) {
$param['update_time'] = time();
$res = BusinessList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $business);
}
return to_assign();
} else {
return to_assign(1, '只有创建人或者负责人才有权限编辑');
}
} else {
validate(BusinessCheck::class)->scene('add')->check($param);
$param['create_time'] = time();
$param['admin_id'] = $this->uid;
$param['director_uid'] = $this->uid;
$sid = BusinessList::strict(false)->insertGetId($param);
// if ($sid) {
// add_log('add', $sid, $param);
// $log_data = array(
// 'module' => 'business',
// '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();
}
} catch (ValidateException $e) {
return to_assign(1, $e->getError());
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new BusinessList())->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 BusinessList())->detail($id);
// print_r($id);
if (empty($detail)) {
return to_assign(1, '2.信息不存在');
} else {
$role_uid = [$detail['admin_id'], $detail['director_uid']];
$role_edit = 'view';
if (in_array($this->uid, $role_uid)) {
$role_edit = 'edit';
}
$view_uid = $role_uid;
if (!empty($detail['check_admin_ids'])) {
$view_uid = array_merge($role_uid, explode(",", $detail['check_admin_ids']));
}
if (!in_array($this->uid, $view_uid) && $detail['is_open'] == 1) {
return to_assign(1, '您没权限查看该信息');
}
$contact_array = Db::name('Businesscontact')
->field('i.id,i.business_id,i.contact,i.post,i.phone,i.email,i.wechat,i.qq,a.name as admin_name')
->alias('i')
->join('Admin a', 'i.admin_id = a.id', 'LEFT')
->join('Business b', 'i.business_id = b.id', 'LEFT')
->order('i.create_time desc')
->where(array('i.business_id' => $id))
->where('i.delete_time', null)
->select()->toArray();
$file_array = Db::name('FileInterfix')
->field('i.id,i.topic_id,i.admin_id,f.name,f.filesize,f.filepath,a.name as admin_name')
->alias('i')
->join('File f', 'i.file_id = f.id', 'LEFT')
->join('Admin a', 'i.admin_id = a.id', 'LEFT')
->order('i.create_time desc')
->where(array('i.topic_id' => $id, 'i.module' => 'business'))
->select()->toArray();
$link_array = Db::name('LinkInterfix')
->field('i.id,i.topic_id,i.admin_id,i.desc,i.url,a.name as admin_name')
->alias('i')
->join('Admin a', 'i.admin_id = a.id', 'LEFT')
->order('i.create_time desc')
->where(array('i.topic_id' => $id, 'i.module' => 'business', 'delete_time' => 0))
->select()->toArray();
View::assign('detail', $detail);
View::assign('contact_array', $contact_array);
View::assign('file_array', $file_array);
View::assign('link_array', $link_array);
View::assign('role_edit', $role_edit);
View::assign('id', $id);
return view();
}
}
//删除
public function delete()
{
if (request()->isDelete()) {
$id = get_params("id");
$detail = Db::name('Business')->where('id', $id)->find();
if ($detail['admin_id'] != $this->uid) {
return to_assign(1, "你不是该客户的创建人,无权限删除");
}
if (Db::name('Business')->where('id', $id)->update(['delete_time' => time()]) !== false) {
$log_data = array(
'module' => 'business',
'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, "错误的请求");
}
}
}