luck-draw/app/controller/manager/Block.php

232 lines
8.1 KiB
PHP
Raw Normal View History

2022-02-22 09:27:27 +00:00
<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.hisiphp.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源您可用于学习和商用但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>开发者QQ群50304283
// +----------------------------------------------------------------------
namespace app\controller\manager;
use app\model\ArchivesCategory as ArticleCategoryModel;
use app\model\ArchivesModelField;
use app\model\Block as BlockModel;
use app\model\System;
use app\validate\Block as VBlock;
use app\repository\CmsRepository;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
2022-02-28 05:53:41 +00:00
use think\facade\Config as CConfig;
2022-02-22 09:27:27 +00:00
/**
* 碎片控制器
* @package app\controller\cms
*/
class Block extends Base
{
protected function initialize()
{
2022-02-28 05:53:41 +00:00
2022-02-22 09:27:27 +00:00
parent::initialize();
$action = $this->request->action();
$cid = $this->request->param('cid/d');
if (($action == 'add' || $action == 'edit') && !$this->request->isPost()) {
$showFieldList = ArchivesModelField::showFieldList();//所有栏目 可展示字段列表
$currentShowFields = $showFieldList[$cid] ?? [];//当前选中栏目 可展示字段列表
$this->data['system'] = System::getSystem();
$this->data['currentList'] = $currentShowFields;
}
$this->data['jsonList'] = $this->xmSelectJson([$cid]);
2022-02-28 05:53:41 +00:00
CConfig::load('extra/upload', 'system_upload');
if(empty(config('system_upload'))){
$this->data['uploadConfig'] = System::findById(1);
}else{
$this->data['uploadConfig'] = config('system_upload');
}
2022-02-22 09:27:27 +00:00
}
public function index()
{
if ($this->request->isAjax()) {
$page = input('page/d', 1);
$limit = input('size/d', 20);
$keyword = input('searchParams.keyword/s');
$categoryId = input('searchParams.category_id/d', 0);
$where = [];
if ($categoryId > 0) {
$where[] = ['category_id', '=', $categoryId];
}
if (!empty($keyword)) {
$where[] = ["name|title", 'like', '%'.$keyword.'%'];
}
$items = BlockModel::findList($where, [], $page, $limit, function ($q) {
return $q->with(["category"])->withAttr("content",function ($value,$data){
switch ($data["type"]){
case BlockModel::BLOCK://
return $value;
break;
case BlockModel::TEXT:
return $value;
break;
case BlockModel::IMG:
return "<img src='{$value}'/>";
break;
case BlockModel::GROUP:
$data = explode(",",$value);
$str = "";
foreach ($data as $vdata){
$str.="<img src='{$vdata}'/>";
}
return $str;
case BlockModel::FILE:
return $value;
break;
case BlockModel::VIDEO:
return $value;
break;
case BlockModel::ING_LIST:
return $value;
break;
}
});
},["id"=>"desc"]);
return $this->json(0, '操作成功', $items);
}
return $this->view();
}
public function add(){
if ($this->request->isPost()) {
$postData = $this->request->post();
try {
$this->validate($postData, VBlock::class);
$postData["content"] = $postData["content".$postData["type"]];
$hasWhere =[["name","=",$postData["name"]]] ;
if( $postData["category_id"] > 0 ){
$hasWhere[] = ["category_id","=",$postData["category_id"]];
}
$other = BlockModel::findOne($hasWhere);
if(!empty($other)){
throw new ValidateException("键值重复");
}
}catch (ValidateException $e){
return $this->json(4001,$e->getError());
}
try {
BlockModel::create($postData);
return $this->json();
}catch (\Exception $e){
return $this->json(0,'保存失败'. $e->getMessage());
}
}
$this->data["types"] = BlockModel::getTypes();
return $this->view();
}
public function edit(){
$id = input("id/d");
$item = BlockModel::findById($id);
if ($this->request->isPost()) {
$postData = $this->request->post();
try {
$this->validate($postData, VBlock::class);
$postData["content"] = $postData["content".$postData["type"]];
$hasWhere =[["name","=",$postData["name"]],["id","<>",$id]] ;
if( $postData["category_id"] > 0 ){
$hasWhere[] = ["category_id","=",$postData["category_id"]];
}else{
$hasWhere[] = ["category_id","=",0];
}
$other = BlockModel::findOne($hasWhere);
if(!empty($other)){
throw new ValidateException("键值重复");
}
}catch (ValidateException $e){
return $this->json(4001,$e->getError());
}
try {
$item->save($postData);
return $this->json();
}catch (\Exception $e){
return $this->json(0,'保存失败'. $e->getMessage());
}
}
if(empty($item)){
return $this->error("碎片不存在");
}
$item =$item->toArray();
if($item['type'] == BlockModel::ING_LIST){
$contentKey = array_keys($item['content']);
$this->data['maxKey'] = max($contentKey)+1;
}else{
$this->data['maxKey'] = 0;
}
$this->data["item"] = $item;
2022-02-28 05:53:41 +00:00
$this->data["config"] = $item;
2022-02-22 09:27:27 +00:00
$this->data["types"] = BlockModel::getTypes();
return $this->view();
}
/**
* 删除
*
* @return Json
*/
public function del()
{
if ($this->request->isPost()) {
$id = input('id/d', 0);
BlockModel::deleteById($id);
return $this->json();
}
return $this->json(4001, '非法请求!');
}
/**
* 内容分类 构造xmSelect json数据[xmSelect用]
*
* @param array $selected
* @param array $disabled
* @return false|string
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
private function xmSelectJson(array $selected = [], array $disabled = [])
{
$category = ArticleCategoryModel::order('sort', 'desc')
->field('id,pid,title')
->select()
->toArray();
foreach ($category as $k => $m) {
$category[$k]['selected'] = in_array($m['id'], $selected);
$category[$k]['disabled'] = in_array($m['id'], $disabled);
}
$category = CmsRepository::getInstance()->buildMenuChild(0, $category);
$category = CmsRepository::getInstance()->handleSelectedList($category);
return json_encode($category, JSON_UNESCAPED_UNICODE);
}
}