dlh_test_server/app/admin/logic/BusinessSchoolLogic.php

287 lines
8.4 KiB
PHP
Raw Normal View History

<?php
namespace app\admin\logic;
use app\common\basics\Logic;
use app\common\model\BusinessSchool;
use app\common\model\BusinessSchoolCategory;
use app\common\model\BusinessSchoolFile;
use app\common\server\UrlServer;
use Exception;
use think\facade\Config;
class BusinessSchoolLogic extends Logic
{
// 内容列表显示
public static function content($get)
{
try {
$where = [
['del', '=', 0]
];
if (!empty($get['title']) and $get['title'])
$where[] = ['title', 'like', '%'.$get['title'].'%'];
if (!empty($get['cid']) and is_numeric($get['cid']))
$where[] = ['cid', '=', $get['cid']];
$lists = BusinessSchool::field(true)
->where($where)
->with(['category'])
->order('sort', 'asc')
->paginate([
'page' => $get['page'],
'list_rows' => $get['limit'],
'var_page' => 'page'
])
->toArray();
foreach ($lists['data'] as &$item) {
$item['category'] = $item['category']['name'] ?? '未知';
$item['is_show'] = $item['is_show'] ? '显示' : '隐藏';
}
return ['count'=>$lists['total'], 'lists'=>$lists['data']];
} catch (Exception $e) {
return ['error'=>$e->getMessage()];
}
}
// 添加内容
public static function add_content($post)
{
try {
$len = strlen(Config::get('aliyunoss.domain'));
$id = BusinessSchool::insertGetId([
'cid' => $post['cid'],
'title' => $post['title'],
'video' => $post['video'] ? substr($post['video'], $len, strlen($post['video'])) : '',
'content' => $post['content'] ?? '',
'intro' => $post['intro'] ?? '',
'sort' => $post['sort'] ?? 0,
'is_show' => $post['is_show']
]);
if (!empty($post['image'])) {
$image = [];
foreach ($post['image'] as $k => &$v) {
$image[$k]['bsid'] = $id;
$image[$k]['src'] = UrlServer::setFileUrl($v);
$image[$k]['type'] = 1;
}
BusinessSchoolFile::insertAll($image);
}
if (!empty($post['file'])) {
$file = [];
foreach ($post['file'] as $k => &$v) {
$file[$k]['bsid'] = $id;
$file[$k]['src'] = substr($v, $len, strlen($v));
$file[$k]['type'] = 2;
}
BusinessSchoolFile::insertAll($file);
}
return true;
} catch (\Exception $e) {
static::$error = $e->getMessage();
return false;
}
}
// 修改内容
public static function edit_content($post)
{
try {
$len = strlen(Config::get('aliyunoss.domain'));
BusinessSchool::update([
'cid' => $post['cid'],
'title' => $post['title'],
'video' => $post['video'] ? substr($post['video'], $len, strlen($post['video'])) : '',
'content' => $post['content'] ?? '',
'intro' => $post['intro'] ?? '',
'sort' => $post['sort'] ?? 0,
'is_show' => $post['is_show']
], ['id' => $post['id']]);
BusinessSchoolFile::where(['bsid' => $post['id']])->delete();
if (!empty($post['image'])) {
$image = [];
foreach ($post['image'] as $k => &$v) {
$image[$k]['bsid'] = $post['id'];
$image[$k]['src'] = UrlServer::setFileUrl($v);
$image[$k]['type'] = 1;
}
BusinessSchoolFile::insertAll($image);
}
if (!empty($post['file'])) {
$file = [];
foreach ($post['file'] as $k => &$v) {
$file[$k]['bsid'] = $post['id'];
$file[$k]['src'] = substr($v, $len, strlen($v));
$file[$k]['type'] = 2;
}
BusinessSchoolFile::insertAll($file);
}
return true;
} catch (\Exception $e) {
static::$error = $e->getMessage();
return false;
}
}
// 删除内容
public static function del_content($id)
{
try {
BusinessSchool::update([
'del' => 1,
'delete_time' => time(),
], ['id'=>$id]);
return true;
} catch (\Exception $e) {
static::$error = $e->getMessage();
return false;
}
}
// 内容详情
public static function detail_content($id)
{
$res = BusinessSchool::field(true)->findOrEmpty($id)->toArray();
2024-09-23 17:26:20 +08:00
$file = BusinessSchoolFile::where('bsid', $id)->select()->toArray();
foreach ($file as &$v) {
if ($v['type'] == 1) {
$res['image'][] = $v['src'];
} else if($v['type'] == 2) {
$res['file'][] = $v['src'];
}
}
return $res;
}
// 获取分类
public static function getCategory()
{
try {
return BusinessSchoolCategory::field(true)
->where(['del' => 0, 'is_show' => 1])
->order('id', 'desc')
->select()
->toArray();
} catch (\Exception $e) {
return [];
}
}
// 分类列表
public static function category($get)
{
try {
$where = [
['del', '=', 0]
];
$model = new BusinessSchoolCategory();
$lists = $model->field(true)
->where($where)
->order('id desc')
->paginate([
'page' => $get['page'],
'list_rows' => $get['limit'],
'var_page' => 'page'
])
->toArray();
foreach ($lists['data'] as &$item) {
$item['is_show'] = $item['is_show'] ? '启用' : '停用';
}
return ['count'=>$lists['total'], 'lists'=>$lists['data']];
} catch (Exception $e) {
return ['error'=>$e->getMessage()];
}
}
// 分类详情
public static function detail_category($id)
{
$model = new BusinessSchoolCategory();
return $model->field(true)->findOrEmpty($id)->toArray();
}
// 添加分类
public static function add_category($post)
{
try {
BusinessSchoolCategory::create([
'name' => $post['name'],
'sort' => $post['sort'],
'is_show' => $post['is_show']
]);
return true;
} catch (\Exception $e) {
static::$error = $e->getMessage();
return false;
}
}
// 修改分类
public static function edit_category($post)
{
try {
BusinessSchoolCategory::update([
'name' => $post['name'],
'sort' => $post['sort'],
'is_show' => $post['is_show']
], ['id'=>$post['id']]);
return true;
} catch (\Exception $e) {
static::$error = $e->getMessage();
return false;
}
}
// 删除分类
public static function del_category($id)
{
try {
BusinessSchoolCategory::update([
'del' => 1,
'delete_time' => time()
], ['id'=>$id]);
return true;
} catch (\Exception $e) {
static::$error = $e->getMessage();
return false;
}
}
// 隐藏分类
public static function hide_category($id)
{
try {
$model = new BusinessSchoolCategory();
$category = $model->findOrEmpty($id)->toArray();
BusinessSchoolCategory::update([
'is_show' => !$category['is_show'],
'update_time' => time()
], ['id'=>$id]);
return true;
} catch (\Exception $e) {
static::$error = $e->getMessage();
return false;
}
}
}