diff --git a/app/admin/controller/MallNews.php b/app/admin/controller/MallNews.php index 5ae9af4b..f77f4fad 100644 --- a/app/admin/controller/MallNews.php +++ b/app/admin/controller/MallNews.php @@ -15,7 +15,7 @@ class MallNews extends AdminBase { if ($this->request->isAjax()) { $get = $this->request->get(); - $lists = LiveGoodsLogic::lists($get); + $lists = MallNewsLogic::lists($get); return JsonServer::success('', $lists); } return view('', [ @@ -25,12 +25,38 @@ class MallNews extends AdminBase public function add() { - return view(''); + if ($this->request->isAjax()) { + $post = $this->request->post(); + $result = MallNewsLogic::create($post); + if ($result['code'] == 1) { + return JsonServer::success('添加成功'); + } + return JsonServer::error($result['msg']); + } + + return view('', [ + 'max_upload_size' => MallNewsLogic::$MAX_UPLOAD_SIZE . 'Mb', + 'max_upload_file' => MallNewsLogic::$MAX_UPLOAD_FILE, + ]); } - public function edit() + public function audit() { - + if ($this->request->isAjax()) { + $post = $this->request->post(); + $result = MallNewsLogic::audit($post); + if ($result['code'] == 1) { + return JsonServer::success('添加成功'); + } + return JsonServer::error($result['msg']); + } + + $id = $this->request->get('id'); + return view('', [ + 'detail' => MallNewsLogic::detail($id), + 'max_upload_size' => MallNewsLogic::$MAX_UPLOAD_SIZE . 'Mb', + 'max_upload_file' => MallNewsLogic::$MAX_UPLOAD_FILE, + ]); } /** @@ -44,4 +70,13 @@ class MallNews extends AdminBase } return JsonServer::error('', $result['msg']); } + + public function delVideo() + { + if ($this->request->isAjax()) { + $file = $this->request->post('videoFile'); + MallNewsLogic::delVideo($file); + } + + } } \ No newline at end of file diff --git a/app/admin/logic/MallNewsLogic.php b/app/admin/logic/MallNewsLogic.php index b7c111da..a8353032 100644 --- a/app/admin/logic/MallNewsLogic.php +++ b/app/admin/logic/MallNewsLogic.php @@ -3,38 +3,143 @@ namespace app\admin\logic; use app\common\basics\Logic; +use app\common\model\MallNews; +use app\common\model\MallNewsFile; use app\common\server\ConfigServer; use app\common\server\storage\Driver as StorageDriver; use think\facade\Config; class MallNewsLogic extends Logic { - private static $MAX_UPLOAD_SIZE = 50 * 1024 * 1024; - private static $FILE_PATH = 'mall_news/video'; + public static $MAX_UPLOAD_SIZE = 50; + public static $MAX_UPLOAD_FILE = 6; + private static $FILE_VIDEO_PATH = 'mall_news/video'; - public static function uploadVideo() + private static function config() { - $file = request()->file('file'); - if ($file->getSize() > self::$MAX_UPLOAD_SIZE) { - return ['code' => -1, 'msg' => '视频最大上传为50M']; - } - - $config = [ + return [ 'default' => ConfigServer::get('storage', 'default', 'local'), 'engine' => ConfigServer::get('storage_engine') ?? ['local'=>[]] ]; + } - $StorageDriver = new StorageDriver($config); + public static function lists($get) + { + $where = []; + + if (!empty($get['title']) and $get['title']) { + $where[] = ['title', 'like', '%' . $get['title'] . '%']; + } + + $model = new MallNews(); + $lists = $model->field(true) + ->where($where) + ->order('sort desc, create_time desc') + ->paginate([ + 'page' => $get['page'], + 'list_rows' => $get['limit'], + 'var_page' => 'page' + ]) + ->toArray(); + + return ['count' => $lists['total'], 'lists' => $lists['data']]; + } + + // 获取详情 + public static function detail($id) + { + return MallNews::where([ + 'id' => $id, + 'delete_time' => 0 + ])->alias('mn')->join('mall_news_file mnf', 'mn.id = mnf.mall_news_id')->find()->toArray(); + } + + // 添加表单信息 + public static function create($form) + { + if (empty($form)) { + return ['code' => -1, 'msg' => '表单内容为空']; + } + + $file = []; + unset($form['file']); + + if (!is_numeric($form['sort'])) { + return ['code' => -1, 'msg' => '请输入整数']; + } + + if ($form['type'] == 0 && isset($form['mall_news_images'])) { + // 文章类型 + $file = $form['mall_news_images']; + + if (count($form['mall_news_images']) > 6) { + return ['code' => -1, 'msg' => '上传图片数量超过限制']; + } + + unset($form['mall_news_images']); + unset($form['video_file']); + } else if ($form['type'] == 1 && !empty($form['video_file'])) { + // 视频类型 + $file = $form['video_file']; + + if (!$form['video_file']) { + return ['code' => -1, 'msg' => '请上传视频']; + } + + unset($form['mall_news_images']); + unset($form['video_file']); + } + + $form['create_time'] = time(); + $id = MallNews::insertGetId($form); + if (is_array($file)) { + foreach ($file as &$v) { + MallNewsFile::create([ + 'mall_news_id' => $id, + 'src' => basename($v) + ]); + } + } else { + MallNewsFile::create([ + 'mall_news_id' => $id, + 'src' => basename($file) + ]); + } + + return ['code' => 1, 'msg' => '添加成功']; + } + + // 上传视频 + public static function uploadVideo() + { + $file = request()->file('file'); + if ($file->getSize() > self::$MAX_UPLOAD_SIZE * 1024 * 1024) { + return ['code' => -1, 'msg' => '视频最大上传为' . self::$MAX_UPLOAD_SIZE . 'Mb']; + } + + $StorageDriver = new StorageDriver(self::config()); $StorageDriver->setUploadFile('file'); - if (!$StorageDriver->upload(self::$FILE_PATH)) { + if (!$StorageDriver->upload(self::$FILE_VIDEO_PATH)) { return ['code' => -1, 'msg' => '上传错误']; } $fileName = $StorageDriver->getFileName(); if ($fileName) { - return ['code' => 1, 'msg' => '', 'data' => array('file' => Config::get('aliyunoss.domain') . self::$FILE_PATH . '/'. $fileName)]; + return ['code' => 1, 'msg' => '', 'data' => array('file' => Config::get('aliyunoss.domain') . self::$FILE_VIDEO_PATH . '/'. $fileName)]; } return ['code' => -1, 'msg' => '上传失败']; } + + // 删除视频 + public static function delVideo($file) + { + if (!$file) { + return false; + } + + $file = self::$FILE_VIDEO_PATH . '/' . basename($file); + $StorageDriver = new StorageDriver(self::config()); + $StorageDriver->delete($file); + } } \ No newline at end of file diff --git a/app/admin/view/mall_news/add.html b/app/admin/view/mall_news/add.html index 8491ed7f..ed0015d6 100644 --- a/app/admin/view/mall_news/add.html +++ b/app/admin/view/mall_news/add.html @@ -5,10 +5,27 @@ .layui-form-item .layui-input-inline { width: 240px; } .layui-form-item .map-container{ width: 600px; height: 400px; margin-left: 100px; margin-top: 20px; } #video-block {position: relative; width: 700px;height: 300px} + .del-video-btn { + visibility: hidden; + display: flex; + align-items: center; + justify-content: center; + position: absolute; + z-index: 100; + top: -11px; + right: -9px; + width: 20px; + height: 20px; + cursor: pointer; + font-size: 14px; + color: #FFFFFF; + border-radius: 50%; + background: hsla(0, 0%, 60%, .6) + }
-
+
@@ -18,18 +35,18 @@
- +
- - + +
- + @@ -37,26 +54,31 @@