yunzer/app/admin/controller/YunzeradminController.php

960 lines
30 KiB
PHP

<?php
/**
* 商业使用授权协议
*
* Copyright (c) 2025 [云泽网]. 保留所有权利.
*
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
*
* 授权购买请联系: 357099073@qq.com
* 官方网站: https://www.yunzer.cn
*
* 评估用户须知:
* 1. 禁止移除版权声明
* 2. 禁止用于生产环境
* 3. 禁止转售或分发
*/
namespace app\admin\controller;
use app\admin\controller\Base;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\LogController as Log;
use app\admin\model\AdminSysMenu;
use app\admin\model\AdminUserGroup;
use app\admin\model\AdminUser;
use app\admin\model\User\Users;
use app\admin\model\User\UsersGroup;
use app\admin\model\Banner;
use app\admin\model\ContentPush;
class YunzeradminController extends Base
{
// 角色列表
public function groupinfo()
{
$group = AdminUserGroup::select();
View::assign([
'group' => $group
]);
return View::fetch();
}
// 角色添加
public function groupadd()
{
if (Request::isPost()) {
$data['group_name'] = trim(input('post.group_name'));
if (!$data['group_name']) {
Log::record('添加角色', 0, '角色名称不能为空', '角色管理');
return json(['code' => 1, 'msg' => '角色名称不能为空']);
}
$data['status'] = intval(trim(input('post.status')));
$data['create_time'] = time();
$menus = input('post.menu/a');
if ($menus) {
$data['rights'] = json_encode(array_keys($menus));
}
$res = AdminUserGroup::insert($data);
if (!$res) {
Log::record('添加角色', 0, '添加角色失败', '角色管理');
return json(['code' => 1, 'msg' => '添加角色失败']);
}
Log::record('添加角色', 1, '', '角色管理');
return json(['code' => 0, 'msg' => '添加成功']);
} else {
$menus = AdminSysMenu::order('type,sort desc')->where('status', '=', 1)->select();
$menu = [];
// 先处理所有父菜单
foreach ($menus as $menus_v) {
if ($menus_v['parent_id'] == 0) {
$menu[$menus_v['smid']] = $menus_v;
$menu[$menus_v['smid']]['children'] = []; // 初始化 children 数组
}
}
// 再处理子菜单
foreach ($menus as $menus_v) {
if ($menus_v['parent_id'] != 0 && isset($menu[$menus_v['parent_id']])) {
$menu[$menus_v['parent_id']]['children'][] = $menus_v;
}
}
View::assign([
'menus' => $menu
]);
return View::fetch();
}
}
// 角色编辑
public function groupedit()
{
if (Request::isPost()) {
$group_id = (int) trim(input('post.group_id'));
$data['group_name'] = trim(input('post.group_name'));
if (!$data['group_name']) {
Log::record('编辑角色', 0, '角色名称不能为空', '角色管理');
return json(['code' => 1, 'msg' => '角色名称不能为空']);
}
$data['status'] = (int) trim(input('post.status'));
$menus = input('post.menu/a');
if ($menus) {
$data['rights'] = json_encode(array_keys($menus));
} else {
$data['rights'] = '';
}
$res = AdminUserGroup::where('group_id', $group_id)->update($data);
if (!$res) {
Log::record('编辑角色', 0, '更新角色失败', '角色管理');
return json(['code' => 1, 'msg' => '更新角色失败']);
}
Log::record('编辑角色', 1, '', '角色管理');
return json(['code' => 0, 'msg' => '更新成功']);
} else {
$group_id = (int) input('get.group_id');
$group = AdminUserGroup::where('group_id', $group_id)->find();
if ($group && $group['rights']) {
$group['rights'] = json_decode($group['rights']);
}
// 使用模型中的 getMenuTree 方法获取菜单树
$menu = AdminSysMenu::getMenuTree();
View::assign([
'group' => $group,
'menus' => $menu
]);
return View::fetch();
}
}
// 角色删除
public function groupdel()
{
$group_id = (int) input('post.group_id');
$res = AdminUserGroup::where('group_id', $group_id)->delete();
if (empty($res)) {
Log::record('删除角色', 0, '删除角色失败', '角色管理');
return json(['code' => 1, 'msg' => '删除角色失败']);
}
Log::record('删除角色', 1, '', '角色管理');
return json(['code' => 0, 'msg' => '删除成功']);
}
// 管理员列表
public function userinfo()
{
$lists = AdminUser::select();
$group = [];
$groups = AdminUserGroup::select();
foreach ($groups as $key => $value) {
$group[$value['group_id']] = $value;
}
View::assign([
'lists' => $lists,
'group' => $group
]);
return View::fetch();
}
// 管理员添加
public function useradd()
{
if (Request::isPost()) {
$data['account'] = trim(input('post.account'));
if (empty($data['account'])) {
Log::record('添加管理员', 0, '账号不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '账号不能为空']);
}
$pattern = "/^([0-9A-Za-z-_.]+)@([0-9a-z]+.[a-z]{2,3}(.[a-z]{2})?)$/i";
if (!preg_match($pattern, $data['account'])) {
Log::record('添加管理员', 0, '邮箱格式不正确', '管理员管理');
return json(['code' => 1, 'msg' => '邮箱格式不正确']);
}
$item = AdminUser::where('account', $data['account'])->find();
if ($item) {
Log::record('添加管理员', 0, '该账号已存在', '管理员管理');
return json(['code' => 1, 'msg' => '该账号已存在']);
}
$data['name'] = trim(input('post.name'));
$data['phone'] = trim(input('post.phone'));
$data['qq'] = (int) trim(input('post.qq'));
$data['group_id'] = (int) input('post.group_id');
$data['sex'] = (int) (input('post.sex'));
$data['status'] = (int) (input('post.status'));
$password = trim(input('post.password'));
if (empty($data['name'])) {
Log::record('添加管理员', 0, '姓名不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '姓名不能为空']);
}
if (empty($data['phone'])) {
Log::record('添加管理员', 0, '手机号不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '手机号不能为空']);
}
if (empty($data['group_id'])) {
Log::record('添加管理员', 0, '请选择角色', '管理员管理');
return json(['code' => 1, 'msg' => '请选择角色']);
}
if (empty($password)) {
Log::record('添加管理员', 0, '密码不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '密码不能为空']);
} else {
$data['password'] = md5($password);
}
$data['create_time'] = time();
$data['update_time'] = time();
$res = AdminUser::insert($data);
if (!$res) {
Log::record('添加管理员', 0, '添加管理员失败', '管理员管理');
return json(['code' => 1, 'msg' => '添加管理员失败']);
}
Log::record('添加管理员', 1, '', '管理员管理');
return json(['code' => 0, 'msg' => '添加成功']);
} else {
$group = [];
$groups = AdminUserGroup::select();
foreach ($groups as $key => $value) {
$group[$value['group_id']] = $value;
}
View::assign([
'group' => $group
]);
return View::fetch();
}
}
// 管理员编辑
public function useredit()
{
if (Request::isPost()) {
$uid = (int) trim(input('post.uid'));
$data['name'] = trim(input('post.name'));
$data['phone'] = trim(input('post.phone'));
$data['qq'] = (int) trim(input('post.qq'));
$data['group_id'] = (int) input('post.group_id');
$data['sex'] = (int) (input('post.sex'));
$data['status'] = (int) (input('post.status'));
if (empty($data['name'])) {
Log::record('编辑管理员', 0, '姓名不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '姓名不能为空']);
}
if (empty($data['phone'])) {
Log::record('编辑管理员', 0, '手机号不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '手机号不能为空']);
}
if (empty($data['group_id'])) {
Log::record('编辑管理员', 0, '请选择角色', '管理员管理');
return json(['code' => 1, 'msg' => '请选择角色']);
}
$res = AdminUser::where('uid', $uid)->update($data);
if (!$res) {
Log::record('编辑管理员', 0, '更新管理员信息失败', '管理员管理');
return json(['code' => 1, 'msg' => '更新管理员信息失败']);
}
Log::record('编辑管理员', 1, '', '管理员管理');
return json(['code' => 0, 'msg' => '更新成功']);
} else {
$uid = (int) input('get.uid');
// 加载管理员
$lists = AdminUser::where('uid', $uid)->find();
// 加载角色
$group = [];
$groups = AdminUserGroup::select();
foreach ($groups as $key => $value) {
$group[$value['group_id']] = $value;
}
View::assign([
'lists' => $lists,
'group' => $group
]);
return View::fetch();
}
}
// 管理员删除
public function userdel()
{
$uid = (int) input('post.uid');
$res = AdminUser::where('uid', $uid)->delete();
if (empty($res)) {
Log::record('删除管理员', 0, '删除管理员失败', '管理员管理');
return json(['code' => 1, 'msg' => '删除管理员失败']);
}
Log::record('删除管理员', 1, '', '管理员管理');
return json(['code' => 0, 'msg' => '删除成功']);
}
// 管理员信息
public function admininfo()
{
if (Request::isPost()) {
$find = AdminUser::where('uid', $this->adminId)->find();
if (empty($find)) {
Log::record('修改个人信息', 0, '当前账户不存在', '个人信息');
return json(['code' => 1, 'msg' => '当前账户不存在']);
}
$data['name'] = trim(input('post.name'));
$data['phone'] = trim(input('post.phone'));
$data['qq'] = (int) trim(input('post.qq'));
$data['sex'] = (int) (input('post.sex'));
if (empty($data['name'])) {
Log::record('修改个人信息', 0, '姓名不能为空', '个人信息');
return json(['code' => 1, 'msg' => '姓名不能为空']);
}
if (empty($data['phone'])) {
Log::record('修改个人信息', 0, '手机号不能为空', '个人信息');
return json(['code' => 1, 'msg' => '手机号不能为空']);
}
// 处理密码修改
$old_pw = trim(input('post.old_pw'));
$new_pw = trim(input('post.new_pw'));
if (!empty($old_pw) && !empty($new_pw)) {
if (md5($old_pw) != $find['password']) {
Log::record('修改个人信息', 0, '原密码错误', '个人信息');
return json(['code' => 1, 'msg' => '原密码错误']);
}
$data['password'] = md5($new_pw);
}
$res = AdminUser::where('uid', $this->adminId)->update($data);
if (!$res) {
Log::record('修改个人信息', 0, '更新管理员信息失败', '个人信息');
return json(['code' => 1, 'msg' => '更新管理员信息失败']);
}
Log::record('修改个人信息', 1, '', '个人信息');
return json(['code' => 0, 'msg' => '更新成功']);
} else {
return View::fetch();
}
}
// 前端会员列表
public function frontuser()
{
$lists = Users::select();
$group = [];
$groups = UsersGroup::select();
foreach ($groups as $key => $value) {
$group[$value['group_id']] = $value['group_name'];
}
// 替换 group_id 为 group_name
foreach ($lists as &$user) {
$gid = $user['group_id'] ?? 0;
$user['group_id'] = isset($group[$gid]) ? $group[$gid] : '';
}
unset($user);
if (request()->isAjax()) {
return json([
'code' => 0,
'msg' => '',
'count' => count($lists),
'data' => $lists
]);
}
View::assign([
'lists' => $lists,
'group' => $group
]);
return View::fetch();
}
// 前端会员添加
public function frontuseradd()
{
if (Request::isPost()) {
$data['account'] = trim(input('post.account'));
if (empty($data['account'])) {
Log::record('添加管理员', 0, '账号不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '账号不能为空']);
}
$pattern = "/^([0-9A-Za-z-_.]+)@([0-9a-z]+.[a-z]{2,3}(.[a-z]{2})?)$/i";
if (!preg_match($pattern, $data['account'])) {
Log::record('添加管理员', 0, '邮箱格式不正确', '管理员管理');
return json(['code' => 1, 'msg' => '邮箱格式不正确']);
}
$item = Users::where('account', $data['account'])->find();
if ($item) {
Log::record('添加管理员', 0, '该账号已存在', '管理员管理');
return json(['code' => 1, 'msg' => '该账号已存在']);
}
$data['name'] = trim(input('post.name'));
$data['phone'] = trim(input('post.phone'));
$data['qq'] = (int) trim(input('post.qq'));
$data['group_id'] = (int) input('post.group_id');
$data['sex'] = (int) (input('post.sex'));
$data['status'] = (int) (input('post.status'));
$password = trim(input('post.password'));
if (empty($data['name'])) {
Log::record('添加管理员', 0, '姓名不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '姓名不能为空']);
}
if (empty($data['phone'])) {
Log::record('添加管理员', 0, '手机号不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '手机号不能为空']);
}
if (empty($data['group_id'])) {
Log::record('添加管理员', 0, '请选择角色', '管理员管理');
return json(['code' => 1, 'msg' => '请选择角色']);
}
if (empty($password)) {
Log::record('添加管理员', 0, '密码不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '密码不能为空']);
} else {
$data['password'] = md5($password);
}
$data['create_time'] = time();
$data['update_time'] = time();
$res = Users::insert($data);
if (!$res) {
Log::record('添加管理员', 0, '添加管理员失败', '管理员管理');
return json(['code' => 1, 'msg' => '添加管理员失败']);
}
Log::record('添加管理员', 1, '', '管理员管理');
return json(['code' => 0, 'msg' => '添加成功']);
} else {
$group = [];
$groups = UsersGroup::select();
foreach ($groups as $key => $value) {
$group[$value['group_id']] = $value;
}
View::assign([
'group' => $group
]);
return View::fetch();
}
}
// 前端会员编辑
public function frontuseredit()
{
if (Request::isPost()) {
$uid = (int) trim(input('post.uid'));
$data['name'] = trim(input('post.name'));
$data['phone'] = trim(input('post.phone'));
$data['qq'] = (int) trim(input('post.qq'));
$data['group_id'] = (int) input('post.group_id');
$data['sex'] = (int) (input('post.sex'));
$data['status'] = (int) (input('post.status'));
if (empty($data['name'])) {
Log::record('编辑管理员', 0, '姓名不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '姓名不能为空']);
}
if (empty($data['phone'])) {
Log::record('编辑管理员', 0, '手机号不能为空', '管理员管理');
return json(['code' => 1, 'msg' => '手机号不能为空']);
}
if (empty($data['group_id'])) {
Log::record('编辑管理员', 0, '请选择角色', '管理员管理');
return json(['code' => 1, 'msg' => '请选择角色']);
}
$res = Users::where('uid', $uid)->update($data);
if (!$res) {
Log::record('编辑管理员', 0, '更新管理员信息失败', '管理员管理');
return json(['code' => 1, 'msg' => '更新管理员信息失败']);
}
Log::record('编辑管理员', 1, '', '管理员管理');
return json(['code' => 0, 'msg' => '更新成功']);
} else {
$uid = (int) input('get.uid');
// 加载前端会员
$lists = Users::where('uid', $uid)->find();
// 加载角色
$group = [];
$groups = UsersGroup::select();
foreach ($groups as $key => $value) {
$group[$value['group_id']] = $value;
}
View::assign([
'lists' => $lists,
'group' => $group
]);
return View::fetch();
}
}
// 前端会员删除
public function frontuserdel()
{
$uid = (int) input('post.uid');
$res = Users::where('uid', $uid)->delete();
if (empty($res)) {
Log::record('删除管理员', 0, '删除管理员失败', '管理员管理');
return json(['code' => 1, 'msg' => '删除管理员失败']);
}
Log::record('删除管理员', 1, '', '管理员管理');
return json(['code' => 0, 'msg' => '删除成功']);
}
// 前端会员信息
public function frontuserdetail()
{
if (Request::isPost()) {
$find = Users::where('uid', $this->adminId)->find();
if (empty($find)) {
Log::record('修改个人信息', 0, '当前账户不存在', '个人信息');
return json(['code' => 1, 'msg' => '当前账户不存在']);
}
$data['name'] = trim(input('post.name'));
$data['phone'] = trim(input('post.phone'));
$data['qq'] = (int) trim(input('post.qq'));
$data['sex'] = (int) (input('post.sex'));
if (empty($data['name'])) {
Log::record('修改个人信息', 0, '姓名不能为空', '个人信息');
return json(['code' => 1, 'msg' => '姓名不能为空']);
}
if (empty($data['phone'])) {
Log::record('修改个人信息', 0, '手机号不能为空', '个人信息');
return json(['code' => 1, 'msg' => '手机号不能为空']);
}
// 处理密码修改
$old_pw = trim(input('post.old_pw'));
$new_pw = trim(input('post.new_pw'));
if (!empty($old_pw) && !empty($new_pw)) {
if (md5($old_pw) != $find['password']) {
Log::record('修改个人信息', 0, '原密码错误', '个人信息');
return json(['code' => 1, 'msg' => '原密码错误']);
}
$data['password'] = md5($new_pw);
}
$res = Users::where('uid', $this->adminId)->update($data);
if (!$res) {
Log::record('修改个人信息', 0, '更新管理员信息失败', '个人信息');
return json(['code' => 1, 'msg' => '更新管理员信息失败']);
}
Log::record('修改个人信息', 1, '', '个人信息');
return json(['code' => 0, 'msg' => '更新成功']);
} else {
return View::fetch();
}
}
//banner管理
public function banner()
{
return View::fetch();
}
// banner列表
public function bannerlist()
{
if (Request::isGet()) {
$page = intval(input('post.page', 1));
$limit = intval(input('post.limit', 10));
$query = Banner::where('delete_time', null)
->field('id, title, image, url, sort, create_time, update_time');
// 获取总记录数
$count = $query->count();
// 获取分页数据
$lists = $query->order(['sort DESC', 'id DESC'])
->page($page, $limit)
->select()
->toArray();
// 处理数据
foreach ($lists as &$item) {
$item['create_time'] = is_numeric($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : $item['create_time'];
$item['update_time'] = is_numeric($item['update_time']) ? date('Y-m-d H:i:s', $item['update_time']) : $item['update_time'];
}
return json([
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $lists
]);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 添加banner
public function banneradd()
{
if (Request::isPost()) {
$data = [
'title' => input('post.title'),
'image' => input('post.image'),
'url' => input('post.url'),
'sort' => input('post.sort', 0),
'status' => 1,
'create_time' => time()
];
$res = Banner::insert($data);
if (!$res) {
Log::record('添加Banner', 0, '添加Banner失败', 'Banner管理');
return json(['code' => 1, 'msg' => '添加Banner失败']);
}
Log::record('添加Banner', 1, '', 'Banner管理');
return json(['code' => 0, 'msg' => '添加成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 编辑banner
public function banneredit()
{
if (Request::isPost()) {
$id = input('post.id');
if (empty($id)) {
Log::record('编辑Banner', 0, 'ID不能为空', 'Banner管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$data = [
'title' => input('post.title'),
'image' => input('post.image'),
'url' => input('post.url'),
'sort' => input('post.sort', 0),
'update_time' => time()
];
$res = Banner::where('id', $id)->update($data);
if ($res === false) {
Log::record('编辑Banner', 0, '更新Banner失败', 'Banner管理');
return json(['code' => 1, 'msg' => '更新Banner失败']);
}
Log::record('编辑Banner', 1, '', 'Banner管理');
return json(['code' => 0, 'msg' => '更新成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 删除banner
public function bannerdel()
{
if (Request::isPost()) {
$id = input('post.id');
if (empty($id)) {
Log::record('删除Banner', 0, 'ID不能为空', 'Banner管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$res = Banner::where('id', $id)->update(['delete_time' => time()]);
if (!$res) {
Log::record('删除Banner', 0, '删除Banner失败', 'Banner管理');
return json(['code' => 1, 'msg' => '删除Banner失败']);
}
Log::record('删除Banner', 1, '', 'Banner管理');
return json(['code' => 0, 'msg' => '删除成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 修改banner状态
public function bannerstatus()
{
if (Request::isPost()) {
$id = input('post.id');
$status = input('post.status');
if (empty($id)) {
Log::record('修改Banner状态', 0, 'ID不能为空', 'Banner管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$res = Banner::where('id', $id)->update(['status' => $status]);
if ($res === false) {
Log::record('修改Banner状态', 0, '更新状态失败', 'Banner管理');
return json(['code' => 1, 'msg' => '更新状态失败']);
}
Log::record('修改Banner状态', 1, '', 'Banner管理');
return json(['code' => 0, 'msg' => '更新成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
//内容推送
public function contentpush()
{
return View::fetch();
}
// 内容推送列表
public function contentpushlist()
{
if (Request::isGet()) {
$page = intval(input('post.page', 1));
$limit = intval(input('post.limit', 10));
$query = ContentPush::where('delete_time', null)
->field('id, title, type, status, sort, create_time, update_time');
// 获取总记录数
$count = $query->count();
// 获取分页数据
$lists = $query->order(['sort DESC', 'id DESC'])
->page($page, $limit)
->select()
->toArray();
// 处理数据
foreach ($lists as &$item) {
$item['create_time'] = is_numeric($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : $item['create_time'];
$item['update_time'] = is_numeric($item['update_time']) ? date('Y-m-d H:i:s', $item['update_time']) : $item['update_time'];
}
return json([
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $lists
]);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 添加内容推送
public function contentpushadd()
{
if (Request::isPost()) {
$data = [
'title' => input('post.title'),
'content' => input('post.content'),
'image' => input('post.image'),
'url' => input('post.url'),
'type' => input('post.type', 1),
'status' => input('post.status', 1),
'sort' => input('post.sort', 0),
'create_time' => time()
];
$res = ContentPush::insert($data);
if (!$res) {
Log::record('添加内容推送', 0, '添加内容推送失败', '内容推送管理');
return json(['code' => 1, 'msg' => '添加内容推送失败']);
}
Log::record('添加内容推送', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '添加成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 编辑内容推送
public function contentpushedit()
{
if (Request::isPost()) {
$id = input('post.id');
if (empty($id)) {
Log::record('编辑内容推送', 0, 'ID不能为空', '内容推送管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$data = [
'title' => input('post.title'),
'content' => input('post.content'),
'image' => input('post.image'),
'url' => input('post.url'),
'type' => input('post.type', 1),
'status' => input('post.status', 1),
'sort' => input('post.sort', 0),
'update_time' => time()
];
$res = ContentPush::where('id', $id)->update($data);
if ($res === false) {
Log::record('编辑内容推送', 0, '更新内容推送失败', '内容推送管理');
return json(['code' => 1, 'msg' => '更新内容推送失败']);
}
Log::record('编辑内容推送', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '更新成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 删除内容推送
public function contentpushdel()
{
if (Request::isPost()) {
$id = input('post.id');
if (empty($id)) {
Log::record('删除内容推送', 0, 'ID不能为空', '内容推送管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$res = ContentPush::where('id', $id)->update(['delete_time' => time()]);
if (!$res) {
Log::record('删除内容推送', 0, '删除内容推送失败', '内容推送管理');
return json(['code' => 1, 'msg' => '删除内容推送失败']);
}
Log::record('删除内容推送', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '删除成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 修改内容推送状态
public function contentpushstatus()
{
if (Request::isPost()) {
$id = input('post.id');
$status = input('post.status');
if (empty($id)) {
Log::record('修改内容推送状态', 0, 'ID不能为空', '内容推送管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$res = ContentPush::where('id', $id)->update(['status' => $status]);
if ($res === false) {
Log::record('修改内容推送状态', 0, '更新状态失败', '内容推送管理');
return json(['code' => 1, 'msg' => '更新状态失败']);
}
Log::record('修改内容推送状态', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '更新成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
//素材中心
public function materialcenter()
{
return View::fetch();
}
// 内容推送列表
public function materialcenterlist()
{
if (Request::isGet()) {
$page = intval(input('post.page', 1));
$limit = intval(input('post.limit', 10));
$query = ContentPush::where('delete_time', null)
->field('id, title, type, status, sort, create_time, update_time');
// 获取总记录数
$count = $query->count();
// 获取分页数据
$lists = $query->order(['sort DESC', 'id DESC'])
->page($page, $limit)
->select()
->toArray();
// 处理数据
foreach ($lists as &$item) {
$item['create_time'] = is_numeric($item['create_time']) ? date('Y-m-d H:i:s', $item['create_time']) : $item['create_time'];
$item['update_time'] = is_numeric($item['update_time']) ? date('Y-m-d H:i:s', $item['update_time']) : $item['update_time'];
}
return json([
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $lists
]);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 添加内容推送
public function materialcenteradd()
{
if (Request::isPost()) {
$data = [
'title' => input('post.title'),
'content' => input('post.content'),
'image' => input('post.image'),
'url' => input('post.url'),
'type' => input('post.type', 1),
'status' => input('post.status', 1),
'sort' => input('post.sort', 0),
'create_time' => time()
];
$res = ContentPush::insert($data);
if (!$res) {
Log::record('添加内容推送', 0, '添加内容推送失败', '内容推送管理');
return json(['code' => 1, 'msg' => '添加内容推送失败']);
}
Log::record('添加内容推送', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '添加成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 编辑内容推送
public function materialcenteredit()
{
if (Request::isPost()) {
$id = input('post.id');
if (empty($id)) {
Log::record('编辑内容推送', 0, 'ID不能为空', '内容推送管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$data = [
'title' => input('post.title'),
'content' => input('post.content'),
'image' => input('post.image'),
'url' => input('post.url'),
'type' => input('post.type', 1),
'status' => input('post.status', 1),
'sort' => input('post.sort', 0),
'update_time' => time()
];
$res = ContentPush::where('id', $id)->update($data);
if ($res === false) {
Log::record('编辑内容推送', 0, '更新内容推送失败', '内容推送管理');
return json(['code' => 1, 'msg' => '更新内容推送失败']);
}
Log::record('编辑内容推送', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '更新成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 删除内容推送
public function materialcenterdel()
{
if (Request::isPost()) {
$id = input('post.id');
if (empty($id)) {
Log::record('删除内容推送', 0, 'ID不能为空', '内容推送管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$res = ContentPush::where('id', $id)->update(['delete_time' => time()]);
if (!$res) {
Log::record('删除内容推送', 0, '删除内容推送失败', '内容推送管理');
return json(['code' => 1, 'msg' => '删除内容推送失败']);
}
Log::record('删除内容推送', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '删除成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
// 修改内容推送状态
public function materialcenterstatus()
{
if (Request::isPost()) {
$id = input('post.id');
$status = input('post.status');
if (empty($id)) {
Log::record('修改内容推送状态', 0, 'ID不能为空', '内容推送管理');
return json(['code' => 1, 'msg' => 'ID不能为空']);
}
$res = ContentPush::where('id', $id)->update(['status' => $status]);
if ($res === false) {
Log::record('修改内容推送状态', 0, '更新状态失败', '内容推送管理');
return json(['code' => 1, 'msg' => '更新状态失败']);
}
Log::record('修改内容推送状态', 1, '', '内容推送管理');
return json(['code' => 0, 'msg' => '更新成功']);
}
return json(['code' => 1, 'msg' => '请求方法无效']);
}
}