Files
projectmanager/app/account/controller/Index.php
T
2025-06-25 10:53:11 +08:00

400 lines
15 KiB
PHP

<?php
/**
* @copyright Copyright (c) 2023-2024 美天智能科技
* @link http://www.meteteme.com
*/
declare(strict_types=1);
namespace app\account\controller;
use app\base\BaseController;
use app\model\Account as AccountList;
use app\model\AccountCategory as AccountCategoryList;
use app\model\AccountChannel as AccountChannelList;
use app\account\validate\AccountCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
class Index extends BaseController
{
public function index()
{
if (request()->isAjax()) {
$param = get_params();
$rows = empty($param['limit']) ? get_md_contentconfig('app.page_size') : $param['limit'];
$list = AccountList::withoutField('')
->where('delete_time', null)
->order('id desc')
->paginate($rows, false)
->filter(function ($item) {
return $item->delete_time === null;
});
// 获取相应的类别和频道
$list = $list->map(function ($item) {
$item['category'] = AccountCategoryList::where('id', $item['category'])->value('category');
$item['channel'] = AccountChannelList::where('id', $item['channel'])->value('channel');
return $item;
});
return table_assign(0, '', $list->toArray());
} else {
return view();
}
}
//添加
public function add()
{
$param = get_params();
if (request()->isPost()) {
if (!empty($param['id']) && $param['id'] > 0) {
$account = (new AccountList())->detail($param['id']);
try {
validate(AccountCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['update_time'] = time();
$res = AccountList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $account);
}
return to_assign();
} else {
try {
validate(AccountCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
// 检查条目是否已经存在
$exist = AccountList::where('id', $param['id'])->find();
if ($exist) {
return to_assign(1, '该信息已存在');
}
// 插入条目的其余代码
$param['create_time'] = time();
$param['admin_id'] = $this->uid;
$sid = AccountList::strict(false)->field(true)->insertGetId($param);
if ($sid) {
add_log('add', $sid, $param);
$log_data = array(
'module' => 'account',
'id' => $sid,
'new_content' => '录入账号',
'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 AccountList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
//添加账号类型
public function add_category()
{
$param = get_params();
if (request()->isPost()) {
if (!empty($param['id']) && $param['id'] > 0) {
$account = (new AccountCategoryList())->detail($param['id']);
try {
validate(AccountCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$param['update_time'] = time();
$res = AccountCategoryList::where('id', $param['id'])->strict(false)->field(true)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $account);
}
return to_assign();
} else {
try {
validate(AccountCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$exist = AccountCategoryList::where('id', $param['id'])
->where('id', '<>', $param['id'])->find();
if ($exist) {
return to_assign(1, '该信息已存在');
}
$param['create_time'] = time();
$param['admin_id'] = $this->uid;
AccountCategoryList::strict(false)->field(true)->insertGetId($param);
return to_assign(2, '添加成功!');
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new AccountCategoryList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
// 获取账号类型
public function getaccountcategory()
{
$pageNumber = $_GET['page'] ?? 1; // 获取当前页码,默认为第1页
$pageSize = 10; // 每页显示的数据条数
$totalRecords = AccountCategoryList::where('delete_time', null)->count(); // 获取总记录数
$totalPages = ceil($totalRecords / $pageSize); // 计算总页数
$offset = ($pageNumber - 1) * $pageSize; // 计算偏移量
$category = AccountCategoryList::where('delete_time', null)->limit($offset, $pageSize)->order('id', 'desc')->column('category', 'id');
$formattedCategorys = [];
foreach ($category as $id => $category) {
$formattedCategorys[] = ["id" => $id, "category" => $category];
}
return json(["code" => 0, "count" => $totalRecords, "data" => $formattedCategorys, "pages" => $totalPages]);
}
//获取账号类型-添加界面
public function getaddcategory()
{
$categories = AccountCategoryList::where('delete_time', null)->column('category', 'id');
$formattedCategories = array_map(function ($id, $category) {
return array("id" => $id, "category" => $category);
}, array_keys($categories), $categories);
return json_encode($formattedCategories);
}
//删除账号类型
public function del_account_category()
{
$id = $_POST['id'];
$res = Db::name('Account_category')->where('id', $id)->update(['delete_time' => time()]);
if ($res) {
return to_assign(1, '删除成功!');
} else {
return to_assign(2, '删除失败!');
}
}
//添加账号渠道
public function add_channel()
{
$param = get_params();
if (request()->isPost()) {
if (!empty($param['id']) && $param['id'] > 0) {
$account = (new AccountChannelList())->detail($param['id']);
try {
validate(AccountCheck::class)->scene('edit')->check($param);
} catch (ValidateException $e) {
return to_assign(1, $e->getError());
}
$param['update_time'] = time();
$res = AccountChannelList::where('id', $param['id'])->strict(false)->update($param);
if ($res) {
add_log('edit', $param['id'], $param, $account);
}
return to_assign();
} else {
try {
validate(AccountCheck::class)->scene('add')->check($param);
} catch (ValidateException $e) {
return to_assign(1, $e->getError());
}
$param['create_time'] = time();
$param['admin_id'] = $this->uid;
AccountChannelList::strict(false)->field(true)->insertGetId($param);
return to_assign(2, '添加成功!');
}
} else {
$id = isset($param['id']) ? $param['id'] : 0;
if ($id > 0) {
$detail = (new AccountChannelList())->detail($id);
if (empty($detail)) {
return to_assign(1, '1.信息不存在');
}
View::assign('detail', $detail);
}
View::assign('id', $id);
return view();
}
}
//删除账号渠道
public function del_account_channel()
{
$id = $_POST['id'];
$res = Db::name('Account_channel')->where('id', $id)->update(['delete_time' => time()]);
if ($res) {
return to_assign(1, '删除成功!');
} else {
return to_assign(2, '删除失败!');
}
}
// 获取账号渠道
public function getaccountchannel()
{
$pageNumber = $_GET['page'] ?? 1; // 获取当前页码,默认为第1页
$pageSize = 10; // 每页显示的数据条数
$totalRecords = AccountChannelList::where('delete_time', null)->count(); // 获取总记录数
$totalPages = ceil($totalRecords / $pageSize); // 计算总页数
$offset = ($pageNumber - 1) * $pageSize; // 计算偏移量
$channels = AccountChannelList::where('delete_time', null)->limit($offset, $pageSize)->order('id', 'desc')->column('channel', 'id');
$formattedChannels = [];
foreach ($channels as $id => $channel) {
$formattedChannels[] = ["id" => $id, "channel" => $channel];
}
return json(["code" => 0, "count" => $totalRecords, "data" => $formattedChannels, "pages" => $totalPages]);
}
// 获取账号渠道all
public function getaccountchannelall()
{
$channels = AccountChannelList::where('delete_time', null)->order('id', 'desc')->column('channel', 'id');
$formattedChannels = [];
foreach ($channels as $id => $channel) {
$formattedChannels[] = ["id" => $id, "channel" => $channel];
}
return json(["code" => 0, "data" => $formattedChannels]);
}
//获取账号渠道-添加界面
public function getaddchannel()
{
$channels = AccountChannelList::where('delete_time', null)->column('channel', 'id');
$formattedChannels = array_map(function ($id, $channel) {
return array("id" => $id, "channel" => $channel);
}, array_keys($channels), $channels);
return json_encode($formattedChannels);
}
//编辑
public function edit()
{
$param = get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$account = AccountList::where('id', $id)
->field('id, category, channel, creater, email, regphone, account, pwd, remark, create_time')
->find();
if ($account) {
View::assign('detail', $account);
} else {
View::assign('message', '没有详细数据');
}
if (request()->isPost()) {
$data = [
'category' => $param['category'],
'channel' => $param['channel'],
'email' => $param['email'],
'regphone' => $param['regphone'],
'account' => $param['account'],
'pwd' => $param['pwd'],
'remark' => $param['remark'],
'update_time' => time(),
];
$res = AccountList::where('id', $id)->update($data);
if ($res) {
return to_assign(2, '更新成功!');
} else {
return to_assign(1, '更新失败');
}
}
return view();
}
//删除
public function delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$id = get_params("id");
$detail = Db::name('Account')->where('id', $id)->find();
if (!isset($detail['id'])) {
return to_assign(1, "账号不存在");
}
if (Db::name('Account')->where('id', $id)->update(['delete_time' => time()]) !== false) {
$log_data = array(
'module' => 'account',
'field' => 'delete',
'action' => 'delete',
'admin_id' => $this->uid,
'old_content' => '',
'new_content' => '',
'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, "错误的请求");
}
}
//批量删除
public function batch_delete()
{
$request = request();
if ($request->isDelete() || $request->isAjax()) {
$ids = get_params("ids");
foreach ($ids as $id) {
$detail = Db::name('Plan')->where('id', $id)->find();
if (Db::name('Plan')->where('id', $id)->update(['delete_time' => date('Y-m-d H:i:s')]) !== false) {
$log_data = array(
'module' => 'plan',
'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(1, "错误的请求");
}
}
}