<?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 Message extends Common {
|
|
private $viewPath = 'Message';
|
/**
|
* 公告管理
|
* @return string
|
*/
|
public function index() {
|
View::assign('viewPath', $this->viewPath);
|
return View::fetch();
|
}
|
/*
|
* 获取消息列表
|
*/
|
public function getMessageList() {
|
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 = 'M.id asc';
|
$count = Db::name('message')
|
->alias('M')
|
->join('msg_category MC','M.msg_category_id=MC.id','left')
|
->where($condition)->count();
|
$list = Db::name('message')
|
->alias('M')
|
->join('msg_category MC','M.msg_category_id=MC.id','left')
|
->field("M.*,MC.title as catetitle")->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', 'message-index');
|
$id = input('id');
|
$other['id'] = $id;
|
View::assign('other', $other);
|
View::assign('viewPath', $this->viewPath);
|
return View::fetch();
|
}
|
|
public function getMessageDetail() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$id = input('message_id');
|
if (!$id) {
|
throw new Exception(lang('param_error'));
|
}
|
$info = \app\admin\model\Message::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('message')->update($data);
|
$result['message'] = '操作成功';
|
return $this->successResponse($result);
|
}
|
catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
/**
|
* 保存消息信息
|
*/
|
public function saveMessage() {
|
// 开始数据库事务
|
Db::startTrans();
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
|
$data = Request::post();
|
$validate = new \app\admin\validate\Message();
|
$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('message')->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('message')->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() {
|
$message_id = input('message_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['message_id'] = $message_id;
|
$filed['title'] = $info['name'];
|
$filed['filename'] = $photo;
|
$filed['filesize'] = getfileSize('.' . $photo);
|
$filed['ext'] = strtoupper(getext($photo));
|
$filed['add_time'] = time();
|
Db::name('message_attachment')->insertGetId($filed);
|
}
|
}
|
return $this->successResponse(lang('successfully_uploaded'));
|
///////////////
|
}
|
}
|
|
/*
|
* 获取附件列表
|
*/
|
public function getAttachmentList() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$message_id = input('message_id');
|
$where['message_id'] = $message_id;
|
$list = Db::name('message_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 deleteMessage() {
|
// 启动事务
|
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('message')->field('id')->where($condition)->find();
|
if (!$info) {
|
throw new Exception(lang('delete_failed'));
|
}
|
$where['message_id'] = $id;
|
$attachmentList = Db::name('message_attachment')->field('id,filename')->where($where)->select()->toArray();
|
if ($attachmentList) {
|
foreach ($attachmentList as $k => $v) {
|
delpic($v['filename']);
|
}
|
Db::name('message_attachment')->where($where)->delete();//删除消息附件
|
}
|
Db::name('message')->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('message_attachment')->field('id,filename')->where($condition)->find();
|
if (!$info) {
|
throw new Exception(lang('delete_failed'));
|
}
|
Db::name('message_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());
|
}
|
|
}
|
/*
|
* 获取消息类别列表
|
*/
|
public function getMsgCategoryList() {
|
try {
|
if (!Request::isPost()) {
|
throw new Exception(lang('request_method_incorrect'));
|
}
|
$message_id = input('message_id');
|
$where['ifshow'] = 1;
|
$list = Db::name('msg_category')->field('id,title,father_id')->where($where)->order('order_id asc,id asc')->select()->toArray();///
|
if ($list) {
|
|
$result['list'] = datalist($list, 'father_id','',true);
|
return $this->successResponse($result);
|
} else {
|
throw new Exception(lang('no_data_found'));
|
}
|
} catch (\Exception $exc) {
|
return $this->errorResponse($exc->getMessage());
|
}
|
}
|
|
|
}
|