<?php
|
|
namespace app\admin\controller;
|
|
|
use think\Exception;
|
use think\facade\Config;
|
use think\facade\Db;
|
use think\facade\View;
|
use think\facade\Request;
|
|
//商品零售价限制
|
|
class Notice extends Common {
|
|
/**
|
* 公告管理
|
* @return string
|
*/
|
public function index() {
|
////////
|
return View::fetch();
|
}
|
/*
|
* 获取公告列表
|
*/
|
public function getNoticeList() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$condition = [];
|
$kw = input('kw');
|
if ($kw) {
|
$condition[] = ['title', 'like', '%' . $kw . '%'];
|
}
|
$p = input('p', 1);
|
$other['page_size'] = input('page_size', 20);
|
$order = 'id asc';
|
$count = Db::name('notice')->where($condition)->count();
|
$list = Db::name('notice')
|
->field("*")->where($condition)->order($order)->withAttr('publish_time', function ($value) {
|
return date('Y-m-d H:i:s', $value);
|
})->page($p, $other['page_size'])->select()->toArray();
|
$other['count'] = $count;
|
if ($list) {
|
$result['list'] = $list;
|
$result['other'] = $other;
|
return $this->successResponse($result);
|
} else {
|
throw new Exception(lang('no_data_found'));
|
}
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 创建公告
|
*/
|
public function create() {
|
////////
|
View::assign('menuitem', 'notice-index');
|
$id = input('id');
|
$other['id'] = $id;
|
View::assign('other', $other);
|
return View::fetch();
|
}
|
|
public function getNoticeDetail() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$id = input('notice_id');
|
if (!$id) {
|
throw new Exception(lang('param_error'));
|
}
|
$info = \app\seller\model\Notice::field("*")->where('id', $id)->withAttr('publish_time', function ($value) {return date('Y-m-d H:i:s', $value);})->find();
|
if ($info) {
|
$result['info']=$info;
|
return $this->successResponse($result);
|
} else {
|
throw new Exception(lang('no_data_found'));
|
}
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
/*
|
* 更新公告状态
|
*/
|
function updateExt() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$postField = 'id,status';
|
$data = Request::only(explode(',', $postField), 'post');
|
if (!$data['id']) {
|
throw new Exception(lang('parameter_error'));
|
}
|
Db::name('notice')->update($data);
|
$result['message'] = '操作成功';
|
return $this->successResponse($result);
|
}
|
catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 保存公告信息
|
*/
|
public function saveNotice() {
|
// 开始数据库事务
|
Db::startTrans();
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
|
$data = Request::post();
|
$validate = new \app\admin\validate\Notice();
|
$check_result = $validate->check($data);
|
if (!$check_result) {
|
throw new Exception($validate->getError());
|
}
|
$id = input('post.id');
|
if (is_numeric($id)) {
|
$data['publish_time'] = strtotime($data['publish_time']);
|
$data['update_time'] = time();
|
Db::name('notice')->where('id', $id)->update($data);//更新数据
|
// 提交事务
|
Db::commit();
|
$result['id'] = $id;
|
$result['message'] = lang('successfully_saved');
|
return $this->successResponse($result);
|
} else {
|
unset($data['id']);
|
$data['publish_time'] = strtotime($data['publish_time']);
|
$data['add_time'] = time();
|
$data['update_time'] = time();
|
$newid = Db::name('notice')->insertGetId($data);
|
Db::commit();
|
$result['id'] = $newid;
|
$result['message'] = lang('successfully_saved');
|
return $this->successResponse($result);
|
}
|
} catch (\Exception $exc) {
|
// 回滚事务
|
Db::rollback();
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
/*
|
* 上传附件
|
*/
|
public function uploadAttachment() {
|
$notice_id = input('notice_id');
|
$ext_arr = array(
|
'image' => array('gif', 'jpg', 'jpeg', 'bmp', 'png'),
|
'files' => array('doc', 'docx', 'xls', 'xlsx', 'pptx', 'ppt', 'pdf'),
|
);
|
$maxSizeArr = array(
|
'image' => 1024 * 1024 * 10,
|
'files' => 1024 * 1024 * 250,
|
);
|
|
$upload = new \common\UploadFile(); // 实例化上传类
|
$upload->uploadReplace = TRUE; //同名文件自动覆盖
|
$upload->autoSub = TRUE; // 启用子目录保存文件
|
$upload->subType = 'date'; // 子目录创建方式 可以使用hash date custom
|
$upload->dateFormat = 'Y/m/d'; //定义 subType=date 后,才需要定义dateFormat
|
$upload->maxSize = 1024 * 1024 * 0; // 设置附件上传大小 10M
|
$upload->maxSizeArr = $maxSizeArr; // 设置附件上传大小 10M
|
$upload->allowExts = $ext_arr; // 设置附件上传类型
|
$upload->savePath = './static/upload/'; // 设置附件上传目录
|
$backinfo = $upload->uploadmore();
|
if (!$backinfo) {// 上传错误提示错误信息
|
return array('code' => 0, 'message' => $upload->getErrorMsg());
|
} else {
|
// 上传成功 获取上传文件信息
|
foreach ($backinfo as $info) {
|
$photo = $info['savename'];
|
if (!empty($photo)) {
|
$filed['notice_id'] = $notice_id;
|
$filed['title'] = $info['name'];
|
$filed['filename'] = $photo;
|
$filed['filesize'] = getfileSize('.' . $photo);
|
$filed['ext'] = strtoupper(getext($photo));
|
$filed['add_time'] = time();
|
Db::name('notice_attachment')->insertGetId($filed);
|
}
|
}
|
return $this->successResponse(lang('successfully_uploaded'));
|
///////////////
|
}
|
}
|
|
/*
|
* 获取附件列表
|
*/
|
public function getAttachmentList() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$notice_id = input('notice_id');
|
$where['notice_id'] = $notice_id;
|
$list = Db::name('notice_attachment')->field('id,title,filename,filesize,ext')->where($where)->withAttr('filesize', function ($value) {
|
return getsizebytype($value);
|
})->order('id asc')->select()->toArray();///
|
if ($list) {
|
$result['list'] = $list;
|
return $this->successResponse($result);
|
} else {
|
throw new Exception(lang('no_data_found'));
|
}
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
/*
|
* 删除公告
|
*/
|
public function deleteNotice() {
|
// 启动事务
|
Db::startTrans();
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$id = input('id');
|
if (!$id || !is_numeric($id)) {
|
throw new Exception(lang('parameter_error'));
|
}
|
$condition['id'] = $id;
|
$info = Db::name('notice')->field('id')->where($condition)->find();
|
if (!$info) {
|
throw new Exception(lang('delete_failed'));
|
}
|
$where['notice_id'] = $id;
|
$attachmentList = Db::name('notice_attachment')->field('id,filename')->where($where)->select()->toArray();
|
if ($attachmentList) {
|
foreach ($attachmentList as $k => $v) {
|
delpic($v['filename']);
|
}
|
Db::name('notice_attachment')->where($where)->delete();//删除公告附件
|
}
|
Db::name('notice')->where('id', $id)->delete();//删除公告
|
////执行成功,提交事务
|
Db::commit();
|
return $this->successResponse(lang('delete_successful'));
|
} catch (\Exception $exc) {
|
//// 回滚事务
|
Db::rollback();
|
return $this->errorResponse($exc->getMessage());
|
}
|
|
}
|
/*
|
* 删除公告附件
|
*/
|
public function deleteAttachment() {
|
// 启动事务
|
Db::startTrans();
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
|
$id = input('id');
|
if (!$id || !is_numeric($id)) {
|
throw new Exception(lang('parameter_error'));
|
}
|
$condition['id'] = $id;
|
$info = Db::name('notice_attachment')->field('id,filename')->where($condition)->find();
|
if (!$info) {
|
throw new Exception(lang('delete_failed'));
|
}
|
Db::name('notice_attachment')->where('id', $id)->delete();//删除公告附件
|
delpic($info['filename']);
|
////执行成功,提交事务
|
Db::commit();
|
return $this->successResponse(lang('delete_successful'));
|
} catch (\Exception $exc) {
|
//// 回滚事务
|
Db::rollback();
|
return $this->errorResponse($exc->getMessage());
|
}
|
|
}
|
|
|
|
}
|