| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890 |
- <?php
- /**
- * +----------------------------------------------------------------------
- * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- * +----------------------------------------------------------------------
- * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- * +----------------------------------------------------------------------
- * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- * +----------------------------------------------------------------------
- * | Author: CRMEB Team <admin@crmeb.com>
- * +----------------------------------------------------------------------
- */
- namespace app\adminapi\controller\v1\setting;
- use app\adminapi\controller\AuthController;
- use app\Request;
- use app\services\system\log\SystemFileServices;
- use app\services\system\SystemCrudDataService;
- use app\services\system\SystemCrudServices;
- use app\services\system\SystemMenusServices;
- use crmeb\services\CacheService;
- use crmeb\services\crud\enum\FormTypeEnum;
- use crmeb\services\crud\Make;
- use crmeb\services\crud\Service;
- use crmeb\services\FileService;
- use think\facade\App;
- use think\helper\Str;
- use think\Response;
- /**
- * Class SystemCrud
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/6
- * @package app\adminapi\controller\v1\setting
- */
- class SystemCrud extends AuthController
- {
- /**
- * SystemCrud constructor.
- * @param App $app
- * @param SystemCrudServices $services
- */
- public function __construct(App $app, SystemCrudServices $services)
- {
- parent::__construct($app);
- $this->services = $services;
- }
- /**
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function index()
- {
- return app('json')->success($this->services->getList());
- }
- /**
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function save(SystemCrudDataService $service, $id = 0)
- {
- $data = $this->request->postMore([
- ['pid', 0],//上级菜单id
- ['menuName', ''],//菜单名
- ['tableName', ''],//表名
- ['modelName', ''],//模块名称
- ['tableComment', ''],//表备注
- ['tableField', []],//表字段
- ['tableIndex', []],//索引
- ['filePath', []],//生成文件位置
- ['isTable', 0],//是否生成表
- ['deleteField', []],//删除的表字段
- ]);
- $fromField = $searchField = $hasOneField = $columnField = $tableIndex = [];
- $dictionaryids = array_column($data['tableField'], 'dictionary_id');
- if ($dictionaryids) {
- $dictionaryList = $service->getColumn([['id', 'in', $dictionaryids]], 'value', 'id');
- foreach ($dictionaryList as &$value) {
- $value = is_string($value) ? json_decode($value, true) : $value;
- }
- } else {
- $dictionaryList = [];
- }
- foreach ($data['tableField'] as $item) {
- //判断字段长度
- if (in_array($item['field_type'], [FormTypeEnum::DATE_TIME, 'timestamp', 'time', 'date', 'year']) && $item['limit'] > 6) {
- return app('json')->fail('字段' . $item['field'] . '长度不能大于6');
- }
- if ($item['field_type'] == 'enum' && !is_array($item['limit'])) {
- return app('json')->fail('数据类型为枚举时,长度为数组类型');
- }
- //收集列表展示数据
- if ($item['is_table'] && !in_array($item['field_type'], ['primaryKey', 'addSoftDelete'])) {
- if (isset($item['primaryKey']) && !$item['primaryKey']) {
- $columnField[] = [
- 'field' => $item['field'],
- 'name' => $item['table_name'] ?: $item['comment'],
- 'type' => $item['from_type'],
- ];
- }
- }
- $name = $item['table_name'] ?: $item['comment'];
- $option = $item['options'] ?? (isset($item['dictionary_id']) ? ($dictionaryList[$item['dictionary_id']] ?? []) : []);
- //收集表单展示数据
- if ($item['from_type']) {
- if (!$name) {
- return app('json')->fail(500048, [], ['field' => $item['field']]);
- }
- if (!$option && in_array($item['from_type'], [FormTypeEnum::RADIO, FormTypeEnum::SELECT])) {
- return app('json')->fail('表单类型为radio或select时,options字段不能为空');
- }
- $fromField[] = [
- 'field' => $item['field'],
- 'type' => $item['from_type'],
- 'name' => $name,
- 'required' => $item['required'],
- 'option' => $option
- ];
- }
- //搜索
- if (!empty($item['search'])) {
- $searchField[] = [
- 'field' => $item['field'],
- 'type' => $item['from_type'],
- 'name' => $name,
- 'search' => $item['search'],
- 'options' => $option
- ];
- }
- //关联
- if (!empty($item['hasOne'])) {
- $hasOneField[] = [
- 'field' => $item['field'],
- 'hasOne' => $item['hasOne'] ?? '',
- 'name' => $name,
- ];
- }
- //索引
- if (!empty($item['index'])) {
- $tableIndex[] = $item['field'];
- }
- }
- if (!$fromField) {
- return app('json')->fail(500046);
- }
- if (!$columnField) {
- return app('json')->fail(500047);
- }
- $data['fromField'] = $fromField;
- $data['tableIndex'] = $tableIndex;
- $data['columnField'] = $columnField;
- $data['searchField'] = $searchField;
- $data['hasOneField'] = $hasOneField;
- if (!$data['tableName']) {
- return app('json')->fail(500042);
- }
- $this->services->createCrud($id, $data);
- return app('json')->success(500043);
- }
- /**
- * 获取创建文件的目录存放位置
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function getFilePath()
- {
- [$tableName] = $this->request->postMore([
- ['tableName', ''],
- ], true);
- if (!$tableName) {
- return app('json')->fail(500042);
- }
- if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
- return app('json')->fail(500041);
- }
- $routeName = 'crud/' . Str::snake($tableName);
- $key = 'id';
- $tableField = [];
- $field = $this->services->getColumnNamesList($tableName);
- foreach ($field as $item) {
- if ($item['primaryKey']) {
- $key = $item['name'];
- }
- $tableField[] = [
- 'field' => $item['name'],
- 'field_type' => $item['type'],
- 'primaryKey' => (bool)$item['primaryKey'],
- 'default' => $item['default'],
- 'limit' => $item['limit'],
- 'comment' => $item['comment'],
- 'required' => false,
- 'is_table' => false,
- 'table_name' => '',
- 'from_type' => '',
- ];
- }
- $make = $this->services->makeFile($tableName, $routeName, false, [
- 'menuName' => '',
- 'key' => $key,
- 'fromField' => [],
- 'columnField' => [],
- ]);
- $makePath = [];
- foreach ($make as $k => $item) {
- $makePath[$k] = $item['path'];
- }
- return app('json')->success(compact('makePath', 'tableField'));
- }
- /**
- * @param $id
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/12
- */
- public function read($id)
- {
- if (!$id) {
- return app('json')->fail(500035);
- }
- $info = $this->services->get($id);
- if (!$info) {
- return app('json')->fail(100026);
- }
- $routeName = 'crud/' . Str::snake($info->table_name);
- $column = $this->services->getColumnNamesList($info->table_name);
- $key = 'id';
- foreach ($column as $value) {
- if ($value['primaryKey']) {
- $key = $value['name'];
- break;
- }
- }
- $softDelete = false;
- foreach ((array)$info->field['tableField'] as $item) {
- if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
- $softDelete = true;
- break;
- }
- }
- $make = $this->services->makeFile($info->table_name, $routeName, false, [
- 'menuName' => $info->name,
- 'modelName' => $info->model_name,
- 'tableField' => $info->field['tableField'] ?? [],
- 'key' => $key,
- 'softDelete' => $softDelete,
- 'fromField' => $info->field['fromField'] ?? [],
- 'columnField' => $info->field['columnField'] ?? [],
- 'searchField' => $info->field['searchField'] ?? [],
- 'hasOneField' => $info->field['hasOneField'] ?? [],
- ]);
- $data = [];
- foreach ($make as $key => $item) {
- if (in_array($key, ['pages', 'router', 'api'])) {
- $path = Make::adminTemplatePath() . $item['path'];
- } else {
- $path = app()->getRootPath() . $item['path'];
- }
- $item['name'] = $item['path'];
- try {
- $item['content'] = file_get_contents($path, LOCK_EX);
- $data[$key] = $item;
- } catch (\Throwable $e) {
- }
- }
- //调整排序
- $makeData = [];
- $names = [
- 'controller' => '控制器',
- 'validate' => '验证器',
- 'service' => '逻辑层',
- 'dao' => '数据库操作',
- 'model' => '模型层',
- 'route' => '后端路由',
- 'router' => '前端路由',
- 'api' => '前端接口',
- 'pages' => '前端页面'
- ];
- foreach ($names as $name => $value) {
- if (isset($data[$name])) {
- $data[$name]['file_name'] = $value;
- $makeData[] = $data[$name];
- }
- }
- $data = $makeData;
- $info = $info->toArray();
- //记录没有修改之前的数据
- foreach ((array)$info['field']['tableField'] as $key => $item) {
- $item['default_field'] = $item['field'];
- $item['default_limit'] = $item['limit'];
- $item['default_field_type'] = $item['field_type'];
- $item['default_comment'] = $item['comment'];
- $item['default_default'] = $item['default'];
- $item['default_default_type'] = $item['default_type'] ?? '1';
- $item['default_type'] = $item['default_type'] ?? '1';
- $item['is_table'] = !!$item['is_table'];
- $item['required'] = !!$item['required'];
- $item['index'] = isset($item['index']) && !!$item['index'];
- $item['primaryKey'] = isset($item['primaryKey']) ? (int)$item['primaryKey'] : 0;
- if (!isset($item['dictionary_id'])) {
- $item['dictionary_id'] = 0;
- }
- $info['field']['tableField'][$key] = $item;
- }
- //对比数据库,是否有新增字段
- $newColumn = [];
- $fieldAll = array_column($info['field']['tableField'], 'field');
- foreach ($column as $value) {
- if (!in_array($value['name'], $fieldAll)) {
- $newColumn[] = [
- 'field' => $value['name'],
- 'field_type' => $value['type'],
- 'primaryKey' => $value['primaryKey'] ? 1 : 0,
- 'default' => $value['default'],
- 'limit' => $value['limit'],
- 'comment' => $value['comment'],
- 'required' => '',
- 'is_table' => '',
- 'table_name' => '',
- 'from_type' => '',
- 'default_field' => $value['name'],
- 'default_limit' => $value['limit'],
- 'default_field_type' => $value['type'],
- 'default_comment' => $value['comment'],
- 'default_default' => $value['default'],
- ];
- }
- }
- if ($newColumn) {
- $info['field']['tableField'] = array_merge($newColumn, $info['field']['tableField']);
- }
- $keyInfo = $deleteInfo = $createInfo = $updateInfo = [];
- $tableField = [];
- foreach ($info['field']['tableField'] as $item) {
- if ($item['primaryKey']) {
- $keyInfo = $item;
- continue;
- }
- if ($item['field_type'] == 'timestamp' && $item['field'] === 'delete_time') {
- $deleteInfo = $item;
- continue;
- }
- if ($item['field_type'] == 'timestamp' && $item['field'] === 'create_time') {
- $createInfo = $item;
- continue;
- }
- if ($item['field_type'] == 'timestamp' && $item['field'] === 'update_time') {
- $updateInfo = $item;
- continue;
- }
- $tableField[] = $item;
- }
- if ($keyInfo) {
- array_unshift($tableField, $keyInfo);
- }
- if ($createInfo) {
- array_push($tableField, $createInfo);
- }
- if ($updateInfo) {
- array_push($tableField, $updateInfo);
- }
- if ($deleteInfo) {
- array_push($tableField, $deleteInfo);
- }
- $info['field']['tableField'] = $tableField;
- $info['field']['pid'] = (int)$info['field']['pid'];
- return app('json')->success(['file' => $data, 'crudInfo' => $info]);
- }
- /**
- * @param Request $request
- * @param SystemFileServices $service
- * @param $id
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/24
- */
- public function savefile(Request $request, SystemFileServices $service, $id)
- {
- $comment = $request->param('comment');
- $filepath = $request->param('filepath');
- $pwd = $request->param('pwd');
- if ($pwd == '') {
- return app('json')->fail('请输入文件管理密码');
- }
- if (config('filesystem.password') != $pwd) {
- return app('json')->fail('文件管理密码错误');
- }
- if (empty($filepath) || !$id) {
- return app('json')->fail(410087);
- }
- $crudInfo = $this->services->get($id, ['make_path']);
- if (!$crudInfo) {
- return app('json')->fail('修改的CRUD文件不存在');
- }
- $makeFilepath = '';
- foreach ($crudInfo->make_path as $key => $item) {
- $path = $item;
- if (in_array($key, ['pages', 'router', 'api'])) {
- $item = Make::adminTemplatePath() . $item;
- } else {
- $item = app()->getRootPath() . $item;
- }
- if ($filepath == $path) {
- $makeFilepath = $item;
- break;
- }
- }
- if (!$makeFilepath || !in_array($filepath, $crudInfo->make_path)) {
- return app('json')->fail('您没有权限修改此文件');
- }
- $res = $service->savefile($makeFilepath, $comment);
- if ($res) {
- return app('json')->success(100000);
- } else {
- return app('json')->fail(100006);
- }
- }
- /**
- * 获取tree菜单
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function getMenus()
- {
- return app('json')->success(app()->make(SystemMenusServices::class)
- ->getList(['auth_type' => 1, 'is_show' => 1], ['auth_type', 'pid', 'id', 'menu_name as label', 'id as value']));
- }
- /**
- * 获取可以进行关联的表名
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/8/2
- */
- public function getAssociationTable()
- {
- return app('json')->success($this->services->getTableAll());
- }
- /**
- * 获取表的详细信息
- * @param string $tableName
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/8/2
- */
- public function getAssociationTableInfo(string $tableName)
- {
- if (!$tableName) {
- return app('json')->fail('缺少表名');
- }
- // if (in_array($tableName, SystemCrudServices::NOT_CRUD_TABANAME)) {
- // return app('json')->fail('不允许查看当前表明细');
- // }
- $tableInfo = $this->services->getColumnNamesList($tableName);
- $data = [];
- foreach ($tableInfo as $key => $item) {
- $data[] = [
- 'label' => $item['comment'] ?: $key,
- 'value' => $key,
- 'leaf' => true
- ];
- }
- return app('json')->success($data);
- }
- /**
- * 获取创建表数据类型
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function columnType()
- {
- return app('json')->success($this->services->getTabelRule());
- }
- /**
- * @param SystemMenusServices $services
- * @param $id
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function delete(SystemMenusServices $services, $id)
- {
- if (!$id) {
- return app('json')->fail(500035);
- }
- $info = $this->services->get($id);
- if (!$info) {
- return app('json')->fail(100026);
- }
- $services->transaction(function () use ($services, $info) {
- if ($info->menu_ids) {
- $services->deleteMenus($info->menu_ids);
- }
- $info->delete();
- });
- if ($info->make_path) {
- $errorFile = [];
- foreach ($info->make_path as $key => $item) {
- if (in_array($key, ['pages', 'router', 'api'])) {
- $item = Make::adminTemplatePath() . $item;
- } else {
- $item = app()->getRootPath() . $item;
- }
- try {
- unlink($item);
- } catch (\Throwable $e) {
- $errorFile[] = $item;
- }
- }
- if ($errorFile) {
- return app('json')->success(500040, [], [
- 'message' => '文件:' . implode("\n", $errorFile) . ';无法被删除!'
- ]);
- }
- }
- return app('json')->success(100002);
- }
- /**
- * 下载文件
- * @param $id
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/15
- */
- public function download($id)
- {
- if (!$id) {
- return app('json')->fail(500035);
- }
- $info = $this->services->get($id);
- if (!$info) {
- return app('json')->fail(100026);
- }
- $zipPath = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name);
- $zipName = app()->getRootPath() . 'backup' . DS . Str::camel($info->table_name) . '.zip';
- if (is_file($zipName)) {
- unlink($zipName);
- }
- $makePath = $info->make_path ?? [];
- foreach ($makePath as $key => $item) {
- if (in_array($key, ['pages', 'router', 'api'])) {
- $item = $zipPath . str_replace(dirname(app()->getRootPath()), '', Make::adminTemplatePath()) . $item;
- } else {
- $item = $zipPath . DS . 'crmeb' . DS . $item;
- }
- $makePath[$key] = $item;
- }
- $routeName = 'crud/' . Str::snake($info->table_name);
- $column = $this->services->getColumnNamesList($info->table_name);
- $key = 'id';
- foreach ($column as $value) {
- if ($value['primaryKey']) {
- $key = $value['name'];
- break;
- }
- }
- $softDelete = false;
- foreach ((array)$info->field['tableField'] as $item) {
- if (isset($item['field_type']) && $item['field_type'] === 'addSoftDelete') {
- $softDelete = true;
- break;
- }
- }
- $this->services->makeFile($info->table_name, $routeName, true, [
- 'menuName' => $info->name,
- 'tableFields' => $info->field['tableField'] ?? [],
- 'key' => $key,
- 'softDelete' => $softDelete,
- 'fromField' => $info->field['fromField'] ?? [],
- 'columnField' => $info->field['columnField'] ?? [],
- 'searchField' => $info->field['searchField'] ?? [],
- 'hasOneField' => $info->field['hasOneField'] ?? [],
- ], $makePath, $zipPath);
- if (!extension_loaded('zip')) {
- return app('json')->fail(500039);
- }
- $fileService = new FileService();
- $fileService->addZip($zipPath, $zipName, app()->getRootPath() . 'backup');
- $key = md5($zipName);
- CacheService::set($key, [
- 'path' => $zipName,
- 'fileName' => Str::camel($info->table_name) . '.zip',
- ], 300);
- return app('json')->success(['download_url' => sys_config('site_url') . '/adminapi/download/' . $key]);
- }
- /**
- * 获取权限路由
- * @param $tableName
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/20
- */
- public function getRouteList($tableName)
- {
- $info = $this->services->get(['table_name' => $tableName]);
- if (!$info) {
- return app('json')->fail('crud详情查询失败');
- }
- $routeList = app()->make(SystemMenusServices::class)->getColumn([
- ['id', 'in', $info->menu_ids],
- ['auth_type', '=', 2]
- ], 'methods,api_url');
- $newRoute = [];
- foreach ($routeList as $item) {
- if ($item['methods'] == 'GET') {
- if (strstr($item['api_url'], 'create')) {
- $newRoute['create'] = $item['api_url'];
- } else if (strstr($item['api_url'], 'edit')) {
- $newRoute['edit'] = $item['api_url'];
- } else if (strstr($item['api_url'], 'status')) {
- $newRoute['status'] = $item['api_url'];
- } else {
- if (strstr($item['api_url'], '<id>')) {
- $newRoute['read'] = $item['api_url'];
- } else {
- $newRoute['index'] = $item['api_url'];
- }
- }
- } else if ($item['methods'] == 'DELETE') {
- $newRoute['delete'] = $item['api_url'];
- } else if ($item['methods'] == 'PUT' && strstr($item['api_url'], 'status')) {
- $newRoute['status'] = $item['api_url'];
- }
- }
- $column = $this->services->getColumnNamesList($info->table_name);
- $key = 'id';
- foreach ($column as $value) {
- if ($value['primaryKey']) {
- $key = $value['name'];
- break;
- }
- }
- $columns = [
- [
- 'title' => 'ID',
- 'key' => $key,
- 'from_type' => '',
- ]
- ];
- $readFields = [
- 'name' => $info->field['modelName'] ?: $info->field['menuName'],
- 'all' => [],
- ];
- foreach ((array)$info->field['tableField'] as $item) {
- if (isset($item['primaryKey']) && $item['primaryKey']) {
- continue;
- }
- $prefix = app()->make(Service::class)->getAttrPrefix();
- $readFields['all'][] = [
- 'field' => in_array($item['from_type'], [FormTypeEnum::FRAME_IMAGES,
- FormTypeEnum::DATE_TIME_RANGE,
- FormTypeEnum::RADIO,
- FormTypeEnum::SELECT,
- FormTypeEnum::CHECKBOX]) ? $item['field'] . $prefix : $item['field'],
- 'comment' => $item['comment'],
- 'from_type' => $item['from_type'],
- ];
- if (isset($item['is_table']) && $item['is_table']) {
- $label = '';
- if (in_array($item['from_type'], [FormTypeEnum::SWITCH, FormTypeEnum::DATE_TIME_RANGE, FormTypeEnum::FRAME_IMAGE_ONE, FormTypeEnum::FRAME_IMAGES])) {
- $keyName = 'slot';
- if ($item['from_type'] == FormTypeEnum::FRAME_IMAGES) {
- $label = $prefix;
- } else if ($item['from_type'] == FormTypeEnum::DATE_TIME_RANGE) {
- $label = $prefix;
- }
- } elseif (in_array($item['from_type'], [FormTypeEnum::RADIO, FormTypeEnum::SELECT, FormTypeEnum::CHECKBOX])) {
- $label = $prefix;
- $keyName = 'key';
- } else {
- $keyName = 'key';
- }
- $columns[] = [
- 'title' => $item['table_name'] ?: $item['comment'],
- $keyName => $item['field'] . $label,
- 'from_type' => $item['from_type'],
- ];
- }
- }
- $searchField = $info->field['searchField'] ?? [];
- $search = [];
- foreach ((array)$searchField as $item) {
- if (!$item['type']) {
- $item['type'] = FormTypeEnum::INPUT;
- }
- if ($item['search'] == 'BETWEEN') {
- $item['type'] = 'date-picker';
- } else {
- if (in_array($item['type'], [FormTypeEnum::CHECKBOX, FormTypeEnum::RADIO, FormTypeEnum::SELECT])) {
- $item['type'] = FormTypeEnum::SELECT;
- } else {
- $item['type'] = FormTypeEnum::INPUT;
- }
- }
- $search[] = [
- 'field' => $item['field'],
- 'type' => $item['type'],
- 'name' => $item['name'],
- 'option' => $item['options'] ?? [],
- ];
- }
- $route = $newRoute;
- return app('json')->success(compact('key', 'route', 'columns', 'readFields', 'search'));
- }
- /**
- * 修改或者保存字典数据
- * @param SystemCrudDataService $service
- * @param int $id
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/8/1
- */
- public function saveDataDictionary(SystemCrudDataService $service, $id = 0)
- {
- $data = $this->request->postMore([
- ['name', ''],
- ['value', []],
- ]);
- if (!$data['name']) {
- return app('json')->fail('数据字段名不能为空');
- }
- if (!$data['value']) {
- return app('json')->fail('数据字段内容不能为空');
- }
- $data['value'] = json_encode($data['value']);
- if ($id) {
- $service->update($id, $data);
- } else {
- $service->save($data);
- }
- return app('json')->success($id ? '修改成功' : '添加成功');
- }
- /**
- * 查看数据字典
- * @param SystemCrudDataService $service
- * @param $id
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/8/7
- */
- public function getDataDictionaryOne(SystemCrudDataService $service, $id)
- {
- if (!$id) {
- return app('json')->fail('缺少参数');
- }
- $info = $service->get($id);
- if (!$info) {
- return app('json')->fail('没有查询到数据');
- }
- return app('json')->success($info->toArray());
- }
- /**
- * 获取数据字典列表
- * @param SystemCrudDataService $service
- * @return Response
- * @throws \ReflectionException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/8/1
- */
- public function getDataDictionary(SystemCrudDataService $service)
- {
- $name = $this->request->get('name', '');
- $data = $service->getlistAll($name);
- return app('json')->success($data);
- }
- /**
- * 删除数据字典
- * @param SystemCrudDataService $service
- * @param $id
- * @return Response
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/8/4
- */
- public function deleteDataDictionary(SystemCrudDataService $service, $id)
- {
- if (!$id) {
- return app('json')->fail('缺少参数');
- }
- if ($service->delete($id)) {
- return app('json')->success('删除成功');
- } else {
- return app('json')->fail('删除失败');
- }
- }
- }
|