|
|
<?php
|
|
|
/**
|
|
|
+-----------------------------------------------------------------------------------------------------------------------
|
|
|
* 管理端控制层: 管理端 控制类 基类
|
|
|
* 控制层:后台控制层基类,提供了对后台对象的增、删、改、查、文件上传、审核等操作
|
|
|
* 后台所有控制层子类继承该类即可,只需在子类中进行参数的配置
|
|
|
+-----------------------------------------------------------------------------------------------------------------------
|
|
|
*
|
|
|
* PHP version 7
|
|
|
*
|
|
|
* @category App\Admin\Controllers\Admin
|
|
|
* @package App\Admin\Controllers\Admin
|
|
|
* @author Richer <yangzi1028@163.com>
|
|
|
* @date 2021年6月23日14:47:37
|
|
|
* @copyright 2020-2022 Richer (http://www.Richer.com/)
|
|
|
* @license http://www.Richer.com/ License
|
|
|
* @link http://www.Richer.com/
|
|
|
*/
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
|
|
use App\Admin\Extensions\Actions\Audit;
|
|
|
use App\Admin\Extensions\Actions\Disable;
|
|
|
use App\Admin\Extensions\Actions\Enable;
|
|
|
use App\Admin\Extensions\Actions\Offline;
|
|
|
use App\Admin\Extensions\Actions\Online;
|
|
|
use App\Admin\Extensions\CustomActions;
|
|
|
use App\Admin\Grids\Filter\AreaFilter;
|
|
|
use App\Admin\Rewrite\Facades\Admin;
|
|
|
use App\Admin\Traits\GridFilterTrait;
|
|
|
use App\Models\System\AuditRecord;
|
|
|
use App\Models\System\OperationLog;
|
|
|
use App\Models\Traits\AdministrativeDivisionTrait;
|
|
|
use App\Models\User\User;
|
|
|
use App\Admin\Rewrite\Grid;
|
|
|
use App\Admin\Rewrite\Form;
|
|
|
//use Encore\Admin\Grid;
|
|
|
//use Encore\Admin\Form;
|
|
|
use App\Admin\Rewrite\Show;
|
|
|
use Encore\Admin\Layout\Content;
|
|
|
use Encore\Admin\Controllers\AdminController;
|
|
|
use App\Admin\Traits\FormFieldTrait;
|
|
|
use App\Admin\Traits\GridFieldTrait;
|
|
|
use App\Admin\Traits\ShowFieldTrait;
|
|
|
use Exception;
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
use Illuminate\Support\Arr;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
/**
|
|
|
* Class BaseController
|
|
|
*
|
|
|
* @category App\Admin\Controllers
|
|
|
* @package App\Admin\Controllers
|
|
|
* @author Richer <yangzi1028@163.com>
|
|
|
* @date 2020年11月13日 01:26:09
|
|
|
* @copyright 2020-2022 Richer (http://www.Richer.com/)
|
|
|
* @license http://www.Richer.com/ License
|
|
|
* @link http://www.Richer.com/
|
|
|
*/
|
|
|
class BaseController extends AdminController
|
|
|
{
|
|
|
use GridFieldTrait, FormFieldTrait, ShowFieldTrait, GridFilterTrait, AreaFilter, AdministrativeDivisionTrait;
|
|
|
|
|
|
/**
|
|
|
* 模型
|
|
|
*
|
|
|
* @var Model
|
|
|
*/
|
|
|
protected $model;
|
|
|
|
|
|
/**
|
|
|
* 用户
|
|
|
*
|
|
|
* @var User
|
|
|
*/
|
|
|
protected $user = null;
|
|
|
|
|
|
/**
|
|
|
* 是否可以新增
|
|
|
*
|
|
|
* @var boolean
|
|
|
*/
|
|
|
protected $can_create = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可以删除
|
|
|
*
|
|
|
* @var boolean
|
|
|
*/
|
|
|
protected $can_delete = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可以编辑
|
|
|
*
|
|
|
* @var boolean
|
|
|
*/
|
|
|
protected $can_edit = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可以查看详情
|
|
|
*
|
|
|
* @var boolean
|
|
|
*/
|
|
|
protected $can_view = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可以审核
|
|
|
*
|
|
|
* @var boolean
|
|
|
*/
|
|
|
protected $can_audit = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可以导出
|
|
|
*
|
|
|
* @var boolean
|
|
|
*/
|
|
|
protected $can_export = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可以上架
|
|
|
*
|
|
|
* @var boolean
|
|
|
*/
|
|
|
protected $can_unshelve = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可以推荐
|
|
|
*
|
|
|
* @var boolean
|
|
|
*/
|
|
|
protected $can_recommend = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可禁用
|
|
|
*
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $can_disable = false;
|
|
|
|
|
|
/**
|
|
|
* 是否展示软删除的回收站
|
|
|
*
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $show_trashed = false;
|
|
|
|
|
|
/**
|
|
|
* 是否可以导入
|
|
|
*
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $can_import = false;
|
|
|
|
|
|
/**
|
|
|
* 列表是否开启row selector
|
|
|
*
|
|
|
* @var bool
|
|
|
*/
|
|
|
protected $grid_row_selector = false;
|
|
|
|
|
|
/**
|
|
|
* 是否开启列表操作下来菜单形式
|
|
|
*/
|
|
|
protected $dropdownActions = false;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 资源显示的中名称
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $title;
|
|
|
|
|
|
/**
|
|
|
* 资源描述
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
protected $description = [];
|
|
|
|
|
|
/**
|
|
|
* 资源是否有tab标签
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $tab = [];
|
|
|
|
|
|
/**
|
|
|
* 资源是否有子类,并且子类tab的名称
|
|
|
*
|
|
|
* @var string
|
|
|
*/
|
|
|
protected $hasMany = [];
|
|
|
|
|
|
/**
|
|
|
* 当前对象的Grid对象实例
|
|
|
*
|
|
|
* @var \Encore\Admin\Grid
|
|
|
*/
|
|
|
protected $grid = null;
|
|
|
|
|
|
/**
|
|
|
* 当前对象的Form对象实例
|
|
|
*
|
|
|
* @var Form
|
|
|
*/
|
|
|
protected $form = null;
|
|
|
|
|
|
/**
|
|
|
* 当前对象的Form对象实例
|
|
|
*
|
|
|
* @var \Encore\Admin\Show
|
|
|
*/
|
|
|
protected $show = null;
|
|
|
|
|
|
/**
|
|
|
* 执行构造方法
|
|
|
* BaseController constructor.
|
|
|
*
|
|
|
* @param null $model 模型
|
|
|
*/
|
|
|
public function __construct($model = null)
|
|
|
{
|
|
|
$this->model = $model;
|
|
|
$this->user = Admin::user();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 审核列表 interface.
|
|
|
*
|
|
|
* @param Content $content 内容
|
|
|
*
|
|
|
* @return Content
|
|
|
*/
|
|
|
public function audit(Content $content)
|
|
|
{
|
|
|
return $content
|
|
|
->header($this->title.'审核列表')
|
|
|
->description($this->description)
|
|
|
->body($this->grid());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Edit interface.
|
|
|
*
|
|
|
* @param mixed $id id
|
|
|
* @param Content $content content
|
|
|
*
|
|
|
* @return Content
|
|
|
*/
|
|
|
public function edit($id, Content $content)
|
|
|
{
|
|
|
return $content
|
|
|
->title($this->title())
|
|
|
->description($this->description['edit'] ?? __('edit'))
|
|
|
->body($this->form($id)->edit($id));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基类方法:渲染生成grid对象
|
|
|
*
|
|
|
* @return Grid
|
|
|
*/
|
|
|
protected function grid()
|
|
|
{
|
|
|
$this->grid = new Grid($this->model);
|
|
|
|
|
|
// 设置列表标题
|
|
|
$this->setGridTitle();
|
|
|
|
|
|
// 增加 tab
|
|
|
$this->setGridTab();
|
|
|
|
|
|
// 设置默认排序规则
|
|
|
$this->setGridOrder();
|
|
|
|
|
|
// 设置默认查询条件
|
|
|
$this->setGridQuery();
|
|
|
|
|
|
// 渲染表格头筛选条件
|
|
|
$this->renderGridFilter();
|
|
|
|
|
|
// 渲染表格导出
|
|
|
$this->renderGridExporter();
|
|
|
|
|
|
// 渲染字段
|
|
|
$this->renderGridFields();
|
|
|
|
|
|
// 渲染数据操作 action
|
|
|
$this->renderGridActions();
|
|
|
|
|
|
// 渲染顶部自定义工具
|
|
|
$this->renderGridTools();
|
|
|
|
|
|
return $this->grid;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置默认排序规则
|
|
|
*
|
|
|
* @return void $grid grid
|
|
|
*/
|
|
|
public function setGridTitle()
|
|
|
{
|
|
|
// $this->grid->setTitle('admin.'.$this->title);
|
|
|
$this->grid->setTitle($this->title);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置 tab
|
|
|
*/
|
|
|
public function setGridTab()
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置默认排序规则
|
|
|
*
|
|
|
*/
|
|
|
public function setGridOrder()
|
|
|
{
|
|
|
if (!request('_sort')) {
|
|
|
$this->grid->model()->latest();
|
|
|
} else {
|
|
|
$this->setGridOrderSpecial();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置特殊的查询规则
|
|
|
*/
|
|
|
public function setGridOrderSpecial()
|
|
|
{
|
|
|
$_sort = request('_sort');
|
|
|
$column = Arr::get($_sort, 'column');
|
|
|
switch ($column) {
|
|
|
case 'name':// 中文排序字段
|
|
|
case 'title':// 中文排序字段
|
|
|
$_sort['convert'] = 1;
|
|
|
break;
|
|
|
case 'user.username':
|
|
|
$_sort['column'] = 'mobile';
|
|
|
break;
|
|
|
case 'gender_show':
|
|
|
$_sort['column'] = 'gender';
|
|
|
break;
|
|
|
}
|
|
|
request()->offsetSet('_sort', $_sort);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置默认查询条件
|
|
|
* 增加数据权限的判断。步骤:
|
|
|
* 1、判断用户是否是超级管理员,并判断用户的pid,如果pid为0 则为平台总用户,不进行数据权限的判断
|
|
|
* 2、如果pid大于0 则为 平台子管理员,则需要进行数据权限的判断
|
|
|
*
|
|
|
* @return void $grid grid
|
|
|
*/
|
|
|
public function setGridQuery()
|
|
|
{
|
|
|
// add by Richer 于 2021年8月31日16:59:30 回收站
|
|
|
if (request('trashed') == 1) {
|
|
|
$this->grid->model()->onlyTrashed();
|
|
|
}
|
|
|
|
|
|
$this->buildQuery($this->grid->model());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 渲染grid字段
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function renderGridFields()
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为grid增加筛选条件
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function renderGridFilter()
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为grid增加筛选条件
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function renderGridExporter()
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为grid的行增加操作按钮
|
|
|
*
|
|
|
* @param bool $disable 是否可用
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function renderGridActions($disable = false)
|
|
|
{
|
|
|
// 设置行操作的样式1.7.3版本以后的操作样式,如果表格字段多或者操作按钮过多可以将按钮进行修改
|
|
|
$this->setActionClass();
|
|
|
|
|
|
// 渲染每行的操作
|
|
|
$this->setRowActions();
|
|
|
|
|
|
// TODO 需要根据数据权限和功能权限来判断
|
|
|
// 禁用系统的新增按钮
|
|
|
if (!$this->can_create) {
|
|
|
$this->grid->disableCreateButton();
|
|
|
}
|
|
|
|
|
|
// 禁用 导出按钮
|
|
|
if (!$this->show_trashed) {
|
|
|
$this->grid->disableTrashedButton();
|
|
|
}
|
|
|
|
|
|
// 禁用 导入按钮
|
|
|
if (!$this->can_import) {
|
|
|
$this->grid->disableImportButton();
|
|
|
}
|
|
|
|
|
|
// $this->grid->disableTools();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为grid渲染工具
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function renderGridTools()
|
|
|
{
|
|
|
$this->grid->tools(
|
|
|
function ($tools) {
|
|
|
//关闭批量删除
|
|
|
$tools->batch(
|
|
|
function ($batch) {
|
|
|
$batch->disableDelete();
|
|
|
}
|
|
|
);
|
|
|
|
|
|
// 去掉筛选的按钮
|
|
|
$tools -> disableFilterButton();
|
|
|
|
|
|
// 去掉刷新的按钮
|
|
|
$tools -> disableRefreshButton();
|
|
|
}
|
|
|
);
|
|
|
|
|
|
// 禁用导出数据按钮
|
|
|
if ($this->can_export === false) {
|
|
|
$this->grid->disableExport();
|
|
|
}
|
|
|
|
|
|
//禁用行选择checkbox
|
|
|
if ($this->grid_row_selector === false) {
|
|
|
$this->grid->disableRowSelector();
|
|
|
}
|
|
|
|
|
|
// 禁用选择列
|
|
|
// $this->grid->disableColumnSelector();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置行操作的样式
|
|
|
*
|
|
|
* @return void $grid grid
|
|
|
*/
|
|
|
public function setActionClass()
|
|
|
{
|
|
|
// 设置为1.7.3版本以后的操作样式
|
|
|
if ($this->dropdownActions === true) {
|
|
|
$this->grid->setActionClass(\App\Admin\Rewrite\Grid\Displayers\DropdownActions::class);
|
|
|
}
|
|
|
|
|
|
// 渲染批量操作按钮
|
|
|
$this->grid->tools(
|
|
|
function (Grid\Tools $tools) {
|
|
|
//
|
|
|
}
|
|
|
);
|
|
|
|
|
|
// 自定义按钮操作
|
|
|
$this->setCustomActions();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 自定义按钮
|
|
|
*/
|
|
|
public function setCustomActions()
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置每行的操作
|
|
|
* 根据每个模块设置的权限来判断当前的操作
|
|
|
*
|
|
|
* @return void $grid grid
|
|
|
*/
|
|
|
protected function setRowActions()
|
|
|
{
|
|
|
// 基本权限
|
|
|
$can_edit = $this->can_edit;
|
|
|
$can_delete = $this->can_delete;
|
|
|
$can_view = $this->can_view;
|
|
|
$can_audit = $this->can_audit;
|
|
|
$can_unshelve = $this->can_unshelve;
|
|
|
$can_recommend = $this->can_recommend;
|
|
|
$can_disable = $this->can_disable;
|
|
|
$dropdownActions = $this->dropdownActions;
|
|
|
|
|
|
$self = $this;
|
|
|
|
|
|
// 对表格的每行数据进行权限的判断和操作按钮的生成
|
|
|
$this->grid->actions(
|
|
|
function ($actions) use (
|
|
|
$self,
|
|
|
$can_view,
|
|
|
$can_edit,
|
|
|
$can_delete,
|
|
|
$can_audit,
|
|
|
$can_unshelve,
|
|
|
$can_recommend,
|
|
|
$can_disable,
|
|
|
$dropdownActions
|
|
|
) {
|
|
|
|
|
|
// 当前数据的主键
|
|
|
$id = $actions->getKey();
|
|
|
// 当前的url
|
|
|
$url = $actions->getResource();
|
|
|
|
|
|
// 禁用系统相关按钮
|
|
|
$actions->disableView();
|
|
|
// 查看按钮
|
|
|
if ($can_view === true) {
|
|
|
if ($dropdownActions === false) {
|
|
|
$actions -> disableView();
|
|
|
$actions->append(CustomActions::renderView($id, $url));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 编辑按钮
|
|
|
$actions -> disableEdit();
|
|
|
if ($can_edit === true) {
|
|
|
if ($dropdownActions === false) {
|
|
|
$actions->append(CustomActions::renderEdit($id, $url));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 删除按钮
|
|
|
$actions -> disableDelete();
|
|
|
if ($can_delete === true) {
|
|
|
if ($dropdownActions === false) {
|
|
|
$actions->append(CustomActions::renderDelete($id, $url));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 可审核
|
|
|
if ($can_audit && $actions->row->audited_status == config('constants.UNAUDITED')) {
|
|
|
if ($dropdownActions === true) {
|
|
|
$actions->add(new Audit());
|
|
|
} else {
|
|
|
$actions->append(CustomActions::renderAudit($id, $url));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 可上下架
|
|
|
if ($can_unshelve) {
|
|
|
// 下架按钮
|
|
|
if ($actions->row->unshelved_status == config('constants.SHELVED')) {
|
|
|
if ($dropdownActions === true) {
|
|
|
$actions->add(new Offline());
|
|
|
} else {
|
|
|
//
|
|
|
$actions->append(CustomActions::renderUnshelvedBtn($id, $url));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if ($actions->row->unshelved_status == config('constants.UNSHELVED')) {
|
|
|
// 上架按钮
|
|
|
if ($dropdownActions === true) {
|
|
|
$actions->add(new Online());
|
|
|
} else {
|
|
|
$actions->append(CustomActions::renderShelvedBtn($id, $url));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 可取消
|
|
|
if ($can_recommend) {
|
|
|
// 下架按钮
|
|
|
if ($actions->row->recommended_status == config('constants.UN_RECOMMENDED')) {
|
|
|
if ($dropdownActions === true) {
|
|
|
// $actions->add(new Offline());
|
|
|
} else {
|
|
|
//
|
|
|
$actions->append(CustomActions::renderRecommendBtn($id, $url));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if ($actions->row->recommended_status == config('constants.RECOMMENDED')) {
|
|
|
// 上架按钮
|
|
|
if ($dropdownActions === true) {
|
|
|
// $actions->add(new Online());
|
|
|
} else {
|
|
|
$actions->append(CustomActions::renderUnRecommendBtn($id, $url));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 可禁用
|
|
|
if ($can_disable && request('trashed') != 1) {
|
|
|
if ($actions->row->status == config('constants.ENABLE')) {
|
|
|
if ($dropdownActions === true) {
|
|
|
$actions->add(new Disable());
|
|
|
} else {
|
|
|
$actions->append(CustomActions::renderDisable($id, $url));
|
|
|
}
|
|
|
} else {
|
|
|
if ($dropdownActions === true) {
|
|
|
$actions->add(new Enable());
|
|
|
} else {
|
|
|
$actions->append(CustomActions::renderEnable($id, $url));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// add by Richer 于 2021年8月31日17:11:06 软删除恢复
|
|
|
if (request('trashed') == 1) {
|
|
|
$actions->append(CustomActions::renderRestore($id, $url));
|
|
|
}
|
|
|
|
|
|
// 自定义按钮操作
|
|
|
$self->setRowCustomActions($actions);
|
|
|
}
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 自定义每行的操作
|
|
|
* 根据每个模块设置的权限来判断当前的操作
|
|
|
*
|
|
|
* @param $actions
|
|
|
* @return void $grid grid
|
|
|
*/
|
|
|
public function setRowCustomActions($actions)
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 外部href
|
|
|
*
|
|
|
* @return string
|
|
|
*/
|
|
|
public function href()
|
|
|
{
|
|
|
// 获取列表的路径
|
|
|
$path = request()->getUri();
|
|
|
$url = \Arr::first(explode('?', $path));
|
|
|
$param = \Arr::last(explode('?', $path));
|
|
|
|
|
|
if ($param) {
|
|
|
//
|
|
|
}
|
|
|
$edit_path = $url.'/edit?'.$param;
|
|
|
// 创建审核的提交数据
|
|
|
$pjax_url = $path;
|
|
|
|
|
|
return $pjax_url;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基类方法渲染生成form对象
|
|
|
*
|
|
|
* @param Int $id 用户id
|
|
|
*
|
|
|
* @return Form
|
|
|
*/
|
|
|
protected function form($id = null)
|
|
|
{
|
|
|
// 实例form对象
|
|
|
$this->form = new Form($this->model);
|
|
|
// 渲染自定义工具
|
|
|
$this->renderFormTools();
|
|
|
// 渲染 form 字段
|
|
|
$this->renderFormFields($id);
|
|
|
// 在表单提交前调用,对表单进行处理
|
|
|
$this->submitted();
|
|
|
// 保存前回调
|
|
|
$this->saving();
|
|
|
// 保存后回调
|
|
|
$this->saved();
|
|
|
|
|
|
return $this->form;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为grid渲染工具
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function renderFormTools()
|
|
|
{
|
|
|
// if ($this->form->isCreating()) {
|
|
|
// dump(1);
|
|
|
// $this->form->tools(function(Form\Tools $tools) use ($this->form) {
|
|
|
|
|
|
// });
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 渲染表单字段
|
|
|
*
|
|
|
* @param Int $id 用户id
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function renderFormFields($id)
|
|
|
{
|
|
|
// 判断当前的form表单是否有tab
|
|
|
$tabs = $this->tab;
|
|
|
|
|
|
$column = 1;//当前form表单显示的列数
|
|
|
|
|
|
switch (count($tabs)) {
|
|
|
case 1:
|
|
|
$this->form->tab(
|
|
|
$tabs[0]['comment'],
|
|
|
function ($form) use ($id, $column) {
|
|
|
// 为form设置通用字段
|
|
|
$this -> setTab1FormCommonFields($form, $id, $column);
|
|
|
}
|
|
|
);
|
|
|
break;
|
|
|
case 2:
|
|
|
$this->form->tab(
|
|
|
$tabs[0]['comment'],
|
|
|
function ($form) use ($id, $column) {
|
|
|
// 为form设置通用字段
|
|
|
$this -> setTab1FormCommonFields($form, $id, $column);
|
|
|
}
|
|
|
)->tab(
|
|
|
$tabs[1]['comment'],
|
|
|
function ($form) use ($id) {
|
|
|
// 设置子类信息;设置第二个tab的form
|
|
|
$this -> setTab2FormCommonFields($form, $id);
|
|
|
}
|
|
|
);
|
|
|
break;
|
|
|
case 3:
|
|
|
$this->form->tab(
|
|
|
$tabs[0]['comment'],
|
|
|
function ($form) use ($id, $column) {
|
|
|
// 为form设置通用字段
|
|
|
$this->setTab1FormCommonFields($form, $id, $column);
|
|
|
}
|
|
|
)->tab(
|
|
|
$tabs[1]['comment'],
|
|
|
function ($form) use ($id) {
|
|
|
// 设置子类信息;设置第二个tab的form
|
|
|
$this->setTab2FormCommonFields($form, $id);
|
|
|
}
|
|
|
)->tab(
|
|
|
$tabs[2]['comment'],
|
|
|
function ($form) use ($id) {
|
|
|
// 设置子类信息;设置第二个tab的form
|
|
|
$this->setTab3FormCommonFields($form, $id);
|
|
|
}
|
|
|
);
|
|
|
break;
|
|
|
case 4:
|
|
|
$this->form->tab(
|
|
|
$tabs[0]['comment'],
|
|
|
function ($form) use ($id, $column) {
|
|
|
// 为form设置通用字段
|
|
|
$this->setTab1FormCommonFields($form, $id, $column);
|
|
|
}
|
|
|
)->tab(
|
|
|
$tabs[1]['comment'],
|
|
|
function ($form) use ($id) {
|
|
|
// 设置子类信息;设置第二个tab的form
|
|
|
$this->setTab2FormCommonFields($form, $id);
|
|
|
}
|
|
|
)->tab(
|
|
|
$tabs[2]['comment'],
|
|
|
function ($form) use ($id) {
|
|
|
// 设置子类信息;设置第二个tab的form
|
|
|
$this->setTab3FormCommonFields($form, $id);
|
|
|
}
|
|
|
)->tab(
|
|
|
$tabs[3]['comment'],
|
|
|
function ($form) use ($id) {
|
|
|
// 设置子类信息;设置第二个tab的form
|
|
|
$this->setTab4FormCommonFields($form, $id);
|
|
|
}
|
|
|
);
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
// 为form设置通用字段
|
|
|
$this->setTab1FormCommonFields($this->form, $id, $column);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基类方法:为form表单设置通用字段
|
|
|
*
|
|
|
* @param Form $form form
|
|
|
* @param integer $id 用户id
|
|
|
* @param integer $column form表单显示几列
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
protected function setTab1FormCommonFields($form, $id, $column = 2)
|
|
|
{
|
|
|
// 设置专用字段
|
|
|
$this -> setTab1FormSpecialFields($form, $id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基类方法:为form表单设置专用特殊字段
|
|
|
*
|
|
|
* @param Form $form form
|
|
|
* @param integer $id 用户id
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function setTab1FormSpecialFields($form, $id)
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置第二个tab的form字段
|
|
|
*
|
|
|
* @param Form $form form
|
|
|
* @param integer $id 用户id
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function setTab2FormCommonFields($form, $id)
|
|
|
{
|
|
|
// 设置每个子类的特殊字段
|
|
|
$this -> setTab2FormSpecialFields($form, $id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基类方法:为form表单第二个tab设置专用特殊字段
|
|
|
*
|
|
|
* @param Form $form form
|
|
|
* @param integer $id 用户id
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function setTab2FormSpecialFields($form, $id)
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置第二个tab的form字段
|
|
|
*
|
|
|
* @param Form $form form
|
|
|
* @param integer $id 用户id
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function setTab3FormCommonFields($form, $id)
|
|
|
{
|
|
|
// 设置每个子类的特殊字段
|
|
|
$this -> setTab3FormSpecialFields($form, $id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基类方法:为form表单第二个tab设置专用特殊字段
|
|
|
*
|
|
|
* @param Form $form form
|
|
|
* @param integer $id 用户id
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function setTab3FormSpecialFields($form, $id)
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置第4个tab的form字段
|
|
|
*
|
|
|
* @param Form $form form
|
|
|
* @param integer $id 用户id
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function setTab4FormCommonFields($form, $id)
|
|
|
{
|
|
|
// 设置每个子类的特殊字段
|
|
|
$this -> setTab4FormSpecialFields($form, $id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置第4tab的form字段
|
|
|
*
|
|
|
* @param Form $form form
|
|
|
* @param integer $id 用户id
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
public function setTab4FormSpecialFields($form, $id)
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 在表单提交前调用
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
protected function submitted()
|
|
|
{
|
|
|
$this->form->submitted(
|
|
|
function (Form $form) {
|
|
|
//...
|
|
|
}
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存前回调
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
protected function saving()
|
|
|
{
|
|
|
//保存前回调
|
|
|
$this->form->saving(
|
|
|
function (Form $form) {
|
|
|
|
|
|
// 只有后台新增的数据才设置,更新的时候不进行设置
|
|
|
// 获取操作
|
|
|
$actions = request()->route()->getAction();
|
|
|
|
|
|
$table = $this->model->getTable();
|
|
|
|
|
|
// 如果是以@store结尾则为新增操作
|
|
|
if (Str::endsWith($actions['uses'], '@store')) {
|
|
|
$user = Admin::user();
|
|
|
$table = $this->model->getTable();
|
|
|
// 设置用户ID
|
|
|
if (Schema::hasColumn($table, 'admin_id')) {
|
|
|
// 隐藏域的值:默认创建时当前登录用户创建
|
|
|
$form->model()->setAttribute('admin_id', $user->id);
|
|
|
}
|
|
|
|
|
|
// 设置店铺ID
|
|
|
if (Schema::hasColumn($table, 'shop_id')) {
|
|
|
// 隐藏域的值:默认创建时当前登录用户创建
|
|
|
// $form->model()->setAttribute('shop_id', $user->shop_id);
|
|
|
}
|
|
|
|
|
|
if (Schema::hasColumn($table, 'user_model')) {
|
|
|
// 如果没有用户,默认设置为当前操作的用户
|
|
|
$form->model()->setAttribute('user_model', 'admin');
|
|
|
}
|
|
|
|
|
|
if (Schema::hasColumn($table, 'created_by')) {
|
|
|
// 隐藏域的值:默认创建时当前登录用户创建
|
|
|
$form->model()->setAttribute('created_by', Admin::user()->id);
|
|
|
}
|
|
|
|
|
|
if (Schema::hasColumn($table, 'created_model')) {
|
|
|
// 如果没有用户,默认设置为当前操作的用户
|
|
|
$form->model()->setAttribute('created_model', 'admin');
|
|
|
}
|
|
|
} else {
|
|
|
if (Schema::hasColumn($table, 'updated_by')) {
|
|
|
// 隐藏域的值:默认创建时当前登录用户创建
|
|
|
$form->model()->setAttribute('updated_by', Admin::user()->id);
|
|
|
}
|
|
|
|
|
|
if (Schema::hasColumn($table, 'updated_model')) {
|
|
|
// 如果没有用户,默认设置为当前操作的用户
|
|
|
$form->model()->setAttribute('updated_model', 'admin');
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存后回调
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
protected function saved()
|
|
|
{
|
|
|
// 保存后回调
|
|
|
$this->form->saved(function (Form $form) {
|
|
|
//...
|
|
|
});
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 基类方法:渲染生成show
|
|
|
*
|
|
|
* @param Int $id 用户id
|
|
|
*
|
|
|
* @return Show
|
|
|
*/
|
|
|
protected function detail($id)
|
|
|
{
|
|
|
// 根据主键和模型实例化show对象
|
|
|
$this ->show = new Show($this ->model->find($id));
|
|
|
|
|
|
// 渲染自定义工具
|
|
|
$this->renderShowTools();
|
|
|
// 渲染 Show 字段
|
|
|
$this->renderShowFields();
|
|
|
|
|
|
return $this->show;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为详情show渲染工具
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
protected function renderShowTools()
|
|
|
{
|
|
|
// 设置页面左上角的标题和右上角的工具栏
|
|
|
$this->show->panel()->style('')->title(__($this->title). ' ' . __('detail'))->tools(
|
|
|
function ($tools) {
|
|
|
// TODO 需要根据权限来判断 =====
|
|
|
$tools->disableEdit();// 禁用编辑按钮
|
|
|
|
|
|
$tools->disableDelete();// 禁用删除按钮
|
|
|
}
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为详情show渲染字段
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
protected function renderShowFields()
|
|
|
{
|
|
|
//
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 置顶 推荐
|
|
|
*
|
|
|
* @param Int $id 用户id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
*/
|
|
|
public function recommend($id)
|
|
|
{
|
|
|
try {
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
$model->recommended_status = config('constants.RECOMMENDED');
|
|
|
$model->recommended_at = date('Y-m-d H:i:s');
|
|
|
$model->recommended_by = $this->getUser()->id;
|
|
|
$result = $model->save();
|
|
|
// 判断操作结果
|
|
|
if ($result) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'code' => 0,
|
|
|
'status' => 1,
|
|
|
'message' => __('set_recommend_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('set_recommend_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('no_data'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取消推荐
|
|
|
*
|
|
|
* @param Int $id 用户id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
*/
|
|
|
public function unRecommend($id)
|
|
|
{
|
|
|
try {
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
$model->recommended_status = config('constants.UN_RECOMMENDED');
|
|
|
$model->recommended_at = null;
|
|
|
$model->recommended_by = 0;
|
|
|
$result = $model->save();
|
|
|
|
|
|
// 判断操作结果
|
|
|
if ($result) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'code' => 0,
|
|
|
'status' => 1,
|
|
|
'message' => __('cancel_recommend_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'code' => 1,
|
|
|
'status' => 0,
|
|
|
'message' => __('cancel_recommend_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('no_data'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 下架
|
|
|
* update by Richer 于 2020年3月31日16:34:49 业务逻辑修改
|
|
|
* 下架的资源需要将公共库的数据删除
|
|
|
*
|
|
|
* @param Int $id 用户id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
*/
|
|
|
public function unshelve($id)
|
|
|
{
|
|
|
try {
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
$model->unshelved_status = 1;
|
|
|
$model->unshelved_opinion = request('unshelved_opinion');
|
|
|
$model->unshelved_at = date('Y-m-d H:i:s');
|
|
|
$model->unshelved_by = $user->id;
|
|
|
$result = $model->save();
|
|
|
|
|
|
// 判断操作结果
|
|
|
if ($result) {
|
|
|
// // 操作成功后记录操作日志
|
|
|
// $model->operationLogs()->create([
|
|
|
// 'user_id' => $user->id,
|
|
|
// 'user_type' => 'admin',
|
|
|
// 'type' => OperationLog::UNSHELVE,
|
|
|
// 'path' => request()->path(),
|
|
|
// 'method' => request()->method(),
|
|
|
// 'ip' => request()->ip(),
|
|
|
// 'input' => request()->all(),
|
|
|
// ]);
|
|
|
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 1,
|
|
|
'message' => __('unshelved_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('unshelved_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('no_data'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 上架
|
|
|
*
|
|
|
* @param Int $id 用户id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
*/
|
|
|
public function shelve($id)
|
|
|
{
|
|
|
try {
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
$model->unshelved_status = config('constants.SHELVED');
|
|
|
$model->unshelved_opinion= '';
|
|
|
$model->unshelved_at = null;
|
|
|
$model->unshelved_by = 0;
|
|
|
$result = $model->save();
|
|
|
// 判断操作结果
|
|
|
if ($result) {
|
|
|
// 通知用户已经上架
|
|
|
// User::find($object->user_id)->notify(new \App\Notifications\Online($object));
|
|
|
|
|
|
// 操作成功后记录操作日志
|
|
|
// $model->operationLogs()->create([
|
|
|
// 'user_id' => $user->id,
|
|
|
// 'user_type' => 'admin',
|
|
|
// 'type' => OperationLog::SHELVE,
|
|
|
// 'path' => request()->path(),
|
|
|
// 'method' => request()->method(),
|
|
|
// 'ip' => request()->ip(),
|
|
|
// 'input' => request()->all(),
|
|
|
// ]);
|
|
|
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 1,
|
|
|
'message' => __('shelved_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('shelved_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('no_data'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 禁用
|
|
|
*
|
|
|
* @param Int $id id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
*/
|
|
|
public function disable($id)
|
|
|
{
|
|
|
try {
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
$model->status = config('constants.DISABLE');
|
|
|
$result = $model->save();
|
|
|
// 判断操作结果
|
|
|
// 判断操作结果
|
|
|
if ($result) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 1,
|
|
|
'message' => __('disable_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('disable_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('no_data'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 启用
|
|
|
*
|
|
|
* @param Int $id 用户id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
*/
|
|
|
public function enable($id)
|
|
|
{
|
|
|
try {
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
$model->status = config('constants.ENABLE');
|
|
|
$result = $model->save();
|
|
|
// 判断操作结果
|
|
|
if ($result) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 1,
|
|
|
'message' => __('enable_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('enable_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => __('no_data'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 审核通过:多级审核
|
|
|
*
|
|
|
* @param integer $id 主键id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function pass($id)
|
|
|
{
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
|
|
|
// 判断审核权限
|
|
|
$can_audit = $this->checkCanAudit($model);
|
|
|
if ($can_audit === false) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>__('audit_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
|
|
|
// 用户的角色来设置审核状态
|
|
|
$user = $this->getUser();
|
|
|
$audited_status = config('constants.AUDIT_PASSED');
|
|
|
$audited_at = date('Y-m-d H:i:s');
|
|
|
$audited_by = $user->id;
|
|
|
$audited_opinion = request('audit_opinion');
|
|
|
// 村管理审核员
|
|
|
switch (true) {
|
|
|
case $user->isRole('village.auditor'):
|
|
|
$model->status = $model::FIRST_AUDIT_PASSED;;
|
|
|
break;
|
|
|
case $user->isRole('village.leader'):
|
|
|
$model->status = $model::PUBLISHED;
|
|
|
break;
|
|
|
// case $user->isRole('village.auditor'):
|
|
|
// $model->first_audited_status = $audited_status;
|
|
|
// $model->first_audited_at = $audited_at;
|
|
|
// $model->first_audited_by = $audited_by;
|
|
|
// $model->first_audited_opinion = $audited_opinion;
|
|
|
// break;
|
|
|
// case $user->isRole('village.leader'):
|
|
|
// $model->second_audited_status = $audited_status;
|
|
|
// $model->second_audited_at = $audited_at;
|
|
|
// $model->second_audited_by = $audited_by;
|
|
|
// $model->second_audited_opinion = $audited_opinion;
|
|
|
// $model->status = $model::PUBLISHED;
|
|
|
// $model->released_at = $audited_at;
|
|
|
// break;
|
|
|
}
|
|
|
$result = $model->save();
|
|
|
if ($result) {
|
|
|
// 将审核结果写入到审核记录中
|
|
|
$auditRecord = [
|
|
|
'user_id' => $audited_by,
|
|
|
'user_type' => 'admin',
|
|
|
'audited_by' => $audited_by,
|
|
|
'audited_status' => $audited_status,
|
|
|
'audited_at' => $audited_at,
|
|
|
'audited_opinion' => $audited_opinion,
|
|
|
];
|
|
|
$model->auditRecords()->create($auditRecord);
|
|
|
// 事务提交
|
|
|
DB::commit();
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 1,
|
|
|
'message' =>__('audit_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>__('audit_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
DB::rollBack();
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => $e->getMessage(),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
// try {
|
|
|
// // 获取数据,如果没有获取到抛出异常
|
|
|
// $model = $this->model->findOrFail($id);
|
|
|
// $model->audited_status = config('constants.AUDIT_PASSED');
|
|
|
// $model->audited_at = date('Y-m-d H:i:s');
|
|
|
// $model->audited_by = Admin::user()->id;
|
|
|
//
|
|
|
// $result = $model->save();
|
|
|
//
|
|
|
// if ($result) {
|
|
|
// return response()->json(
|
|
|
// [
|
|
|
// 'status' => 1,
|
|
|
// 'message' =>__('audit_succeeded'),
|
|
|
// ]
|
|
|
// );
|
|
|
// } else {
|
|
|
// return response()->json(
|
|
|
// [
|
|
|
// 'status' => 0,
|
|
|
// 'message' =>__('audit_failed'),
|
|
|
// ]
|
|
|
// );
|
|
|
// }
|
|
|
// } catch (ModelNotFoundException $e) {
|
|
|
// return response()->json(
|
|
|
// [
|
|
|
// 'status' => 0,
|
|
|
// 'message' => __('no_data'),
|
|
|
// ]
|
|
|
// );
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 审核不通过
|
|
|
*
|
|
|
* @param integer $id 主键id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function refuse($id)
|
|
|
{
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
$audited_opinion = request('audit_opinion');
|
|
|
if (!$audited_opinion) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>'请输入审核意见!',
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
|
|
|
// 判断审核权限
|
|
|
$can_audit = $this->checkCanAudit($model);
|
|
|
if ($can_audit === false) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>__('audit_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
|
|
|
// 用户的角色来设置审核状态
|
|
|
$user = $this->getUser();
|
|
|
$audited_status = config('constants.AUDIT_NOT_PASSED');
|
|
|
$audited_at = date('Y-m-d H:i:s');
|
|
|
$audited_by = $user->id;
|
|
|
$model->status = $model::AUDIT_NOT_PASSED;
|
|
|
|
|
|
//
|
|
|
//
|
|
|
// // 村管理审核员
|
|
|
// switch (true) {
|
|
|
// case $user->isRole('village.auditor'):
|
|
|
// $model->first_audited_status = $audited_status;
|
|
|
// $model->first_audited_at = $audited_at;
|
|
|
// $model->first_audited_by = $audited_by;
|
|
|
// $model->first_audited_opinion = $audited_opinion;
|
|
|
// // TODO 是否要清空以前的审批记录
|
|
|
// $model->submitted_status = 0;
|
|
|
// $model->submitted_at = null;
|
|
|
// $model->submitted_by = 0;
|
|
|
// $model->second_audited_status = config('constants.UNAUDITED');
|
|
|
// $model->second_audited_at = null;
|
|
|
// $model->second_audited_by = 0;
|
|
|
// $model->second_audited_opinion = 0;
|
|
|
// break;
|
|
|
// case $user->isRole('village.leader'):
|
|
|
// $model->second_audited_status = $audited_status;
|
|
|
// $model->second_audited_at = $audited_at;
|
|
|
// $model->second_audited_by = $audited_by;
|
|
|
// $model->second_audited_opinion = $audited_opinion;
|
|
|
// break;
|
|
|
// }
|
|
|
|
|
|
$result = $model->save();
|
|
|
if ($result) {
|
|
|
// 将审核结果写入到审核记录中
|
|
|
$auditRecord = [
|
|
|
'user_id' => $audited_by,
|
|
|
'user_type' => 'admin',
|
|
|
'audited_by' => $audited_by,
|
|
|
'audited_status' => $audited_status,
|
|
|
'audited_at' => $audited_at,
|
|
|
'audited_opinion' => $audited_opinion,
|
|
|
];
|
|
|
$model->auditRecords()->create($auditRecord);
|
|
|
// 事务提交
|
|
|
DB::commit();
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 1,
|
|
|
'message' =>__('audit_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>__('audit_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
DB::rollBack();
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => $e->getMessage(),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
|
|
|
|
|
|
try {
|
|
|
//
|
|
|
$audited_opinion = request('audit_opinion');
|
|
|
if (!$audited_opinion) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>'请输入审核意见!',
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
$model->audited_status = config('constants.AUDIT_NOT_PASSED');
|
|
|
$model->audited_opinion = $audited_opinion;
|
|
|
$model->audited_at = date('Y-m-d H:i:s');
|
|
|
$model->audited_by = Admin::user()->id;
|
|
|
|
|
|
$result = $model->save();
|
|
|
|
|
|
if ($result) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 1,
|
|
|
'message' =>__('audit_succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>__('audit_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => '当前数据不存在!',
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 提交审核
|
|
|
*
|
|
|
* @param integer $id 主键id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function submit($id)
|
|
|
{
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->findOrFail($id);
|
|
|
|
|
|
// 判断审核权限
|
|
|
$can_audit = $this->checkCanSubmitAudit($model);
|
|
|
if ($can_audit === false) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>__('提交审核失败。'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
$audited_opinion = request('audit_opinion');
|
|
|
$user = $this->getUser();
|
|
|
$audited_by = $user->id;
|
|
|
$audited_at = date('Y-m-d H:i:s');
|
|
|
$model->status = $model::SUBMITTED;
|
|
|
$model->submitted_at = $audited_at;
|
|
|
$model->submitted_by = $audited_by;
|
|
|
|
|
|
$result = $model->save();
|
|
|
if ($result) {
|
|
|
// 将审核结果写入到审核记录中
|
|
|
$auditRecord = [
|
|
|
'user_id' => $audited_by,
|
|
|
'user_type' => 'admin',
|
|
|
'audited_by' => $audited_by,
|
|
|
'audited_status' => AuditRecord::SUBMITTED,
|
|
|
'audited_at' => $audited_at,
|
|
|
'audited_opinion' => $audited_opinion,
|
|
|
|
|
|
];
|
|
|
$model->auditRecords()->create($auditRecord);
|
|
|
DB::commit();
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 1,
|
|
|
'message' =>__('提交审核成功。'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' =>__('audit_failed'),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (Exception $e) {
|
|
|
DB::rollBack();
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => $e->getMessage(),
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 恢复软删除数据
|
|
|
*
|
|
|
* @param integer $id 主键id
|
|
|
*
|
|
|
* @return JsonResponse
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function restore($id)
|
|
|
{
|
|
|
try {
|
|
|
// 获取数据,如果没有获取到抛出异常
|
|
|
$model = $this->model->onlyTrashed()->findOrFail($id);
|
|
|
$model->deleted_at = null;
|
|
|
$model->restored_at = now()->toDateTimeString();
|
|
|
$model->restored_by = Admin::user()->id;
|
|
|
$model->restored_opinion = request('restored_opinion', '');
|
|
|
$result = $model->save();
|
|
|
if ($result) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'code' => 0,
|
|
|
'status' => 1,
|
|
|
'message' => __('restore') . __('succeeded'),
|
|
|
]
|
|
|
);
|
|
|
} else {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'code' => 1001,
|
|
|
'status' => 0,
|
|
|
'message' => __('restore') . __('failed'),
|
|
|
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
return response()->json(
|
|
|
[
|
|
|
'status' => 0,
|
|
|
'message' => '当前数据不存在!',
|
|
|
]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取当前登录用户信息
|
|
|
*
|
|
|
* @return User|\Illuminate\Contracts\Auth\Authenticatable|null
|
|
|
*/
|
|
|
public function getUser()
|
|
|
{
|
|
|
return $this->user ?? $this->user = \Admin::user();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据用户角色构建查询语句
|
|
|
*
|
|
|
* @param null $query 查询语句
|
|
|
* @param null $user 用户
|
|
|
* @param null $user_role 用户角色
|
|
|
* @return mixed
|
|
|
*/
|
|
|
public function buildQuery($query, $user = null, $user_role = null)
|
|
|
{
|
|
|
if (!$user) {
|
|
|
$user = $this->getUser();
|
|
|
}
|
|
|
// 只有管理员和录入员才能进行添加和编辑
|
|
|
if (!$user->isAdministrator() && !$user->isRole('village.reporter')) {
|
|
|
$this->can_create = false;
|
|
|
// 是否可编辑
|
|
|
$this->can_edit = false;
|
|
|
// 是否可删除
|
|
|
$this->can_delete = false;
|
|
|
}
|
|
|
|
|
|
|
|
|
// // 根据角色来判断
|
|
|
// $role = Admin::user()->roles->pluck('slug')->first();
|
|
|
// switch ($role) {
|
|
|
// case 'Admin':
|
|
|
// $this->can_delete = false;
|
|
|
// break;
|
|
|
// case 'KPO/NPT':
|
|
|
// $this->can_create = false;
|
|
|
// $this->can_edit = false;
|
|
|
// $this->can_delete = false;
|
|
|
// $this->can_import = false;
|
|
|
// break;
|
|
|
// case 'Teacher':// 获取我的全部学生
|
|
|
// case 'PL':
|
|
|
// break;
|
|
|
// }
|
|
|
|
|
|
return $query;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
public function importPage()
|
|
|
{
|
|
|
$model = $this->model;
|
|
|
return view('admin::grid.import', compact('model')) ;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 是否是管理客户端
|
|
|
*
|
|
|
* @return bool
|
|
|
*/
|
|
|
public function isAdminClient()
|
|
|
{
|
|
|
return config('admin.route.prefix') === 'admin';
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 是否是机构户端
|
|
|
*
|
|
|
* @return bool
|
|
|
*/
|
|
|
public function isAgencyClient()
|
|
|
{
|
|
|
return config('admin.route.prefix') === 'agency';
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 验证是否能进行提交审核操作
|
|
|
*
|
|
|
* @param $model
|
|
|
* @param $status
|
|
|
* @return bool
|
|
|
*/
|
|
|
public function checkCanSubmitAudit($model, $status = 0)
|
|
|
{
|
|
|
$can = false;
|
|
|
$user = Admin::user();
|
|
|
if ($model->status == $model::UNSUBMITTED || $model->status == $model::AUDIT_NOT_PASSED) {
|
|
|
if ($user->isRole('village.reporter') || $user->isAdministrator()) {
|
|
|
$can = true;
|
|
|
}
|
|
|
}
|
|
|
return $can;
|
|
|
|
|
|
if ($model->status == $status) {
|
|
|
// 判断审核状态,如果一级审核未审核或者二级审核状态为 不通过
|
|
|
$audited_status = $model->first_audited_status;
|
|
|
$audited_at = $model->first_audited_at;
|
|
|
$submitted_at = $model->submitted_at;
|
|
|
// 判断用户的角色
|
|
|
if (($user->isRole('village.reporter') || $user->isAdministrator())) {
|
|
|
if ($model->submitted_status == 0 || ($audited_status == config('constants.AUDIT_NOT_PASSED') && $audited_at > $submitted_at)) {
|
|
|
$can = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return $can;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 验证是否能进行审核操作
|
|
|
* 1、村管理录入员 发布信息
|
|
|
* 2、村管理审核员审核,通过后进入村领导审核,不通过则退回到 村管理录入员
|
|
|
* 3、村领导审核通过后则直接发布,不通过则直接退回到 村管理录入员
|
|
|
* @param $model
|
|
|
* @param $status
|
|
|
* @return bool
|
|
|
*/
|
|
|
public function checkCanAudit($model, $status = 0)
|
|
|
{
|
|
|
$can_audit = false;
|
|
|
$user = $this->getUser();
|
|
|
switch ($model->status) {
|
|
|
// case $model::UNSUBMITTED: // 未提交审核
|
|
|
// case $model::AUDIT_NOT_PASSED: // 已提交审核
|
|
|
// if (($user->isRole('village.reporter') || $user->isAdministrator())) {
|
|
|
// $can_audit = true;
|
|
|
// }
|
|
|
// break;
|
|
|
case $model::SUBMITTED: // 已提交审核
|
|
|
if ($user->isRole('village.auditor')) {
|
|
|
$can_audit = true;
|
|
|
}
|
|
|
break;
|
|
|
case $model::FIRST_AUDIT_PASSED: // 一级审核通过
|
|
|
if ($user->isRole('village.leader')) {
|
|
|
$can_audit = true;
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
return $can_audit;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($model->status == $status) {
|
|
|
// 判断用户的角色
|
|
|
// 判断审核状态,如果一级审核未审核或者二级审核状态为 不通过
|
|
|
$first_audited_status = $model->first_audited_status;
|
|
|
$first_audited_at = $model->first_audited_at;
|
|
|
$second_audited_status = $model->second_audited_status;
|
|
|
$second_audited_at = $model->second_audited_at;
|
|
|
$third_audited_status = $model->third_audited_status;
|
|
|
$submitted_at = $model->submitted_at;
|
|
|
$submitted_status = $model->submitted_status;
|
|
|
|
|
|
$user = Admin::user();
|
|
|
switch (true) {
|
|
|
case $first_audited_status == config('constants.AUDIT_NOT_PASSED'):
|
|
|
if ($user->isRole('village.auditor') && $submitted_status == 1 && $submitted_at > $first_audited_at) {
|
|
|
$can_audit = true;
|
|
|
}
|
|
|
break;
|
|
|
case $first_audited_status == config('constants.UNAUDITED'):
|
|
|
case $second_audited_status == config('constants.AUDIT_NOT_PASSED'):
|
|
|
if ($user->isRole('village.auditor') && (!$second_audited_at || $second_audited_at > $first_audited_at)) {
|
|
|
$can_audit = true;
|
|
|
}
|
|
|
|
|
|
if ($user->isRole('village.leader') && (!$second_audited_at || $second_audited_at < $first_audited_at)) {
|
|
|
$can_audit = true;
|
|
|
}
|
|
|
break;
|
|
|
case $second_audited_status == config('constants.UNAUDITED'):
|
|
|
if ($user->isRole('village.leader')) {
|
|
|
$can_audit = true;
|
|
|
}
|
|
|
break;
|
|
|
case $third_audited_status == config('AUDIT_NOT_PASSED.UNAUDITED'):
|
|
|
// if ($user->isRole('village.leader')) {
|
|
|
// $can_audit = true;
|
|
|
// }
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return $can_audit;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 渲染自定义的分页 ajax
|
|
|
*
|
|
|
* @param $paginator
|
|
|
* @param null $transformer
|
|
|
* @param array $params
|
|
|
* @return array
|
|
|
*/
|
|
|
public function renderPaginator($paginator, $transformer = null, $params = [])
|
|
|
{
|
|
|
// dump($paginator);
|
|
|
// 追加查询条件
|
|
|
if ($params) {
|
|
|
array_walk($params, function ($param, $key) use (&$paginator) {
|
|
|
$paginator->appends($key, $param);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
$items = $paginator->items() ;
|
|
|
|
|
|
// 对数据进行转换
|
|
|
$data = [];
|
|
|
if ($transformer) {
|
|
|
foreach ($items as $vo) {
|
|
|
$data[] = $transformer->transform($vo);
|
|
|
}
|
|
|
} else {
|
|
|
$data = $items;
|
|
|
}
|
|
|
|
|
|
// 获取当前的页码
|
|
|
$page = request('page', 1);
|
|
|
|
|
|
// 全部数据
|
|
|
$total = $paginator->total();
|
|
|
|
|
|
// 全部页码
|
|
|
$lastPage = $paginator->lastPage();
|
|
|
|
|
|
// 当前页码
|
|
|
$currentPage = $paginator->currentPage();
|
|
|
|
|
|
// 中间的页码数组
|
|
|
$elements = [];
|
|
|
$len = $lastPage - 2;
|
|
|
$center_page = 8;
|
|
|
// 创建数组
|
|
|
if ($lastPage <= 10) {
|
|
|
for ($i = 1; $i <= $lastPage; $i++) {
|
|
|
$elements[0][$i] = $i;
|
|
|
}
|
|
|
} else {
|
|
|
if ($currentPage < $center_page) {
|
|
|
for ($i = 1; $i <= $center_page; $i++) {
|
|
|
$elements[0][$i] = $i;
|
|
|
}
|
|
|
$elements[1] = '...';
|
|
|
for ($i = $lastPage-1; $i <= $lastPage; $i++) {
|
|
|
$elements[2][$i] = $i;
|
|
|
}
|
|
|
}/*elseif($currentPage === $lastPage /2){
|
|
|
// for ($i = $lastPage /2 - 4; $i <= $lastPage /2 - 4; $i++) {
|
|
|
// $elements[0][$i] = $i;
|
|
|
// }
|
|
|
// $elements[1] = '...';
|
|
|
// for ($i = $center_page + 1; $i <= $center_page + 4; $i++) {
|
|
|
// $elements[2][$i] = $i;
|
|
|
// }
|
|
|
// $elements[2]['...'] = '...';
|
|
|
// for ($i = $lastPage-1; $i <= $lastPage; $i++) {
|
|
|
// $elements[2][$i] = $i;
|
|
|
// }
|
|
|
}*/ elseif ($currentPage <= $lastPage /2 + 1) {
|
|
|
for ($i = 1; $i <= $center_page - 4; $i++) {
|
|
|
$elements[0][$i] = $i;
|
|
|
}
|
|
|
$elements[1] = '...';
|
|
|
for ($i = $center_page + 1; $i <= $center_page + 4; $i++) {
|
|
|
$elements[2][$i] = $i;
|
|
|
}
|
|
|
$elements[2]['...'] = '...';
|
|
|
for ($i = $lastPage-1; $i <= $lastPage; $i++) {
|
|
|
$elements[2][$i] = $i;
|
|
|
}
|
|
|
} else {
|
|
|
for ($i = 1; $i <= 2; $i++) {
|
|
|
$elements[0][$i] = $i;
|
|
|
}
|
|
|
$elements[1] = '...';
|
|
|
for ($i = $lastPage-$center_page-1; $i <= $lastPage; $i++) {
|
|
|
$elements[2][$i] = $i;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//dd($elements);
|
|
|
|
|
|
|
|
|
|
|
|
$prev = ($page - 1) > 0 ? $page - 1 : 1;
|
|
|
|
|
|
$next = ($page + 1) < $total ? $page + 1 : $total;
|
|
|
|
|
|
return [
|
|
|
'items' => $data,
|
|
|
'prev' => $prev,
|
|
|
'next' => $next,
|
|
|
'total' => $total,
|
|
|
'lastPage' => $lastPage,
|
|
|
'page' => $page,
|
|
|
'pageSize' => request('per_page', 10),
|
|
|
'paginator' => $paginator,
|
|
|
'elements' => $elements,
|
|
|
'url' => request()->url(),
|
|
|
];
|
|
|
}
|
|
|
} |
...
|
...
|
|