228 lines
6.5 KiB
PHP
228 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace app\admin\logic;
|
|
|
|
use app\common\basics\Logic;
|
|
use app\common\model\MallNews;
|
|
use app\common\model\MallNewsFile;
|
|
use app\common\server\storage\Driver as StorageDriver;
|
|
use app\common\server\UrlServer;
|
|
use think\facade\Config;
|
|
use think\facade\Db;
|
|
|
|
class MallNewsLogic extends Logic
|
|
{
|
|
public static $MAX_UPLOAD_SIZE = 50;
|
|
public static $MAX_UPLOAD_FILE = 6;
|
|
private static $FILE_VIDEO_PATH = 'mall_news/video';
|
|
|
|
private static function config()
|
|
{
|
|
return [
|
|
"default" => "aliyun",
|
|
"engine" => [
|
|
"aliyun" => [
|
|
"bucket" => Config::get('aliyunoss.bucket'),
|
|
"access_key_id" => Config::get('aliyunoss.access_key_id'),
|
|
"access_key_secret" => Config::get('aliyunoss.access_key_secret'),
|
|
"domain" => Config::get('aliyunoss.domain'),
|
|
]
|
|
]
|
|
];
|
|
}
|
|
|
|
public static function lists($get)
|
|
{
|
|
$where = [];
|
|
|
|
if (!empty($get['title']) and $get['title']) {
|
|
$where[] = ['title', 'like', '%' . $get['title'] . '%'];
|
|
}
|
|
|
|
$where[] = ['delete_time', '=', 0];
|
|
$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)
|
|
{
|
|
$result = MallNews::with('files')->where(['id' => $id, 'delete_time' => 0])->find();
|
|
if (isset($result['files'])) {
|
|
foreach ($result['files'] as &$v) {
|
|
if ($result['type'] == 0) {
|
|
$v['src'] = UrlServer::getFileUrl('uploads/images/' . $v['src']);
|
|
} else {
|
|
$v['src'] = Config::get('aliyunoss.domain') . self::$FILE_VIDEO_PATH . '/'. $v['src'];
|
|
}
|
|
}
|
|
}
|
|
|
|
$result['shop'] = Db::name('goods')->where('id', $result['shop_id'])->find();
|
|
return $result;
|
|
}
|
|
|
|
// 添加表单信息
|
|
public static function create($form)
|
|
{
|
|
$result = self::checkForm($form);
|
|
if (isset($result['code']) && $result['code'] == -1) {
|
|
return $result;
|
|
}
|
|
|
|
$form['create_time'] = time();
|
|
$file = $result['data'];
|
|
|
|
$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 edit($form)
|
|
{
|
|
$result = self::checkForm($form);
|
|
if (isset($result['code']) && $result['code'] == -1) {
|
|
return $result;
|
|
}
|
|
|
|
$form['update_time'] = time();
|
|
$file = $result['data'];
|
|
|
|
MallNews::where('id', $form['id'])->update($form);
|
|
|
|
if (is_array($file)) {
|
|
MallNewsFile::where('mall_news_id', $form['id'])->delete();
|
|
foreach ($file as &$v) {
|
|
MallNewsFile::create([
|
|
'mall_news_id' => $form['id'],
|
|
'src' => basename($v)
|
|
]);
|
|
}
|
|
} else {
|
|
MallNewsFile::where('mall_news_id', $form['id'])->delete();
|
|
MallNewsFile::create([
|
|
'mall_news_id' => $form['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_VIDEO_PATH)) {
|
|
return ['code' => -1, 'msg' => '上传错误'];
|
|
}
|
|
|
|
$fileName = $StorageDriver->getFileName();
|
|
if ($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);
|
|
}
|
|
|
|
// 删除文章
|
|
public static function del($id)
|
|
{
|
|
$result = MallNews::where([
|
|
'id' => $id
|
|
])->update([
|
|
'delete_time' => time(),
|
|
]);
|
|
|
|
if ($result) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 选择商品
|
|
public static function selectGoods()
|
|
{
|
|
|
|
}
|
|
|
|
private static function checkForm(&$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']);
|
|
}
|
|
|
|
return ['code' => 1, 'data' => $file];
|
|
}
|
|
} |