| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997 |
- <?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\services\system;
- use app\dao\system\SystemCrudDao;
- use app\services\BaseServices;
- use crmeb\exceptions\AdminException;
- use crmeb\services\crud\Controller;
- use crmeb\services\crud\Dao;
- use crmeb\services\crud\Make;
- use crmeb\services\crud\Model;
- use crmeb\services\crud\Route;
- use crmeb\services\crud\Service;
- use crmeb\services\crud\Validate;
- use crmeb\services\crud\ViewApi;
- use crmeb\services\crud\ViewPages;
- use crmeb\services\crud\ViewRouter;
- use crmeb\services\FileService;
- use Phinx\Db\Adapter\AdapterFactory;
- use think\facade\Db;
- use think\helper\Str;
- use think\migration\db\Table;
- /**
- * Class SystemCrudServices
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/6
- * @package app\services\system
- */
- class SystemCrudServices extends BaseServices
- {
- //不能生成的系统自带表
- const NOT_CRUD_TABANAME = [
- 'system_config', 'system_attachment', 'system_attachment_category', 'system_config_tab',
- 'system_admin', 'eb_system_city', 'system_log', 'system_menus', 'system_notice',
- 'system_notice_admin', 'system_notification', 'system_role', 'system_route',
- 'system_route_cate', 'system_storage', 'system_timer', 'system_user_level',
- 'system_crud', 'wechat_key', 'user_label_relation', 'user_brokerage_frozen',
- 'user_brokerage', 'store_product_cate', 'store_bargain_user_help', 'shipping_templates_region',
- 'shipping_templates_no_delivery', 'shipping_templates_free', 'other_order_status', 'lang_code',
- 'lang_country', 'app_version', 'user', 'wechat_user', 'template_message', 'store_order', 'other_order',
- 'store_order_cart_info', 'store_order_economize', 'store_order_invoice', 'store_order_refund',
- 'store_order_status', 'store_pink', 'agent_level', 'agent_level_task', 'agent_level_task_record',
- 'agreement', 'app_version', 'article', 'article_category', 'article_content', 'auxiliary', 'cache',
- 'capital_flow', 'category', 'diy', 'express', 'lang_type', 'live_anchor', 'live_goods', 'live_room',
- 'live_room_goods', 'luck_lottery', 'luck_lottery_record', 'luck_prize', 'member_card', 'member_card_batch',
- 'member_right', 'member_ship', 'message_system', 'other_order', 'other_order_status', 'out_account', 'out_interface',
- 'page_categroy', 'page_link', 'qrcode', 'shipping_templates', 'shipping_templates_free', 'shipping_templates_no_delivery',
- 'shipping_templates_region', 'sms_record', 'store_advance', 'store_bargain', 'store_bargain_user', 'store_bargain_user_help',
- 'store_cart', 'store_category', 'store_combination', 'store_coupon_issue', 'store_coupon_issue_user', 'store_coupon_product',
- 'store_coupon_user', 'store_integral', 'store_integral_order', 'store_integral_order_status', 'store_order', 'store_order_cart_info',
- 'store_order_economize', 'store_order_invoice', 'store_order_refund', 'store_order_status', 'store_pink', 'store_product',
- 'store_product_attr', 'store_product_attr_result', 'store_product_attr_value', 'store_product_cate', 'store_product_coupon',
- 'store_product_description', 'store_product_log', 'store_product_relation', 'store_service', 'store_service_feedback',
- 'store_product_reply', 'store_product_rule', 'store_product_virtual', 'store_seckill', 'store_seckill_time',
- 'store_service_log', 'store_service_record', 'store_service_speechcraft', 'store_visit', 'system_attachment',
- 'system_attachment_category', 'system_city', 'system_config', 'system_config_tab', 'system_file', 'system_file_info',
- 'system_group', 'system_group_data', 'system_log', 'system_notice', 'system_notice_admin', 'system_notification',
- 'system_role', 'system_route', 'system_route_cate', 'system_storage', 'system_store', 'system_store_staff',
- 'system_timer', 'system_user_level', 'template_message', 'upgrade_log', 'user', 'user_address',
- 'user_bill', 'user_brokerage', 'user_brokerage_frozen', 'user_cancel', 'user_enter', 'user_extract',
- 'user_friends', 'user_group', 'user_invoice', 'user_label', 'user_label_relation', 'user_level', 'user_money',
- 'user_notice', 'user_notice_see', 'user_recharge', 'user_search', 'user_sign', 'user_spread',
- 'user_visit', 'wechat_key', 'wechat_media', 'wechat_message', 'wechat_news_category', 'wechat_qrcode',
- 'wechat_qrcode_cate', 'wechat_qrcode_record', 'wechat_reply', 'wechat_user'
- ];
- //表字符集
- const TABLR_COLLATION = 'utf8mb4_general_ci';
- /**
- * SystemCrudServices constructor.
- * @param SystemCrudDao $dao
- */
- public function __construct(SystemCrudDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * @return array
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function getList()
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->selectList([], 'add_time,id,name,table_name,table_comment,table_collation', $page, $limit, 'id desc');
- $count = $this->dao->count();
- return compact('list', 'count');
- }
- /**
- * 数据库字段类型
- * @return \string[][]
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function getTabelRule()
- {
- $rule = [
- 'varchar' => 'string',
- 'int' => 'integer',
- 'biginteger' => 'bigint',
- 'tinyint' => 'boolean',
- ];
- return [
- 'types' => [
- 'varchar',
- 'char',
- 'text',
- 'longtext',
- 'tinytext',
- // 'enum',
- 'blob',
- 'binary',
- 'varbinary',
- 'datetime',
- 'timestamp',
- 'time',
- 'date',
- 'year',
- 'boolean',
- 'tinyint',
- 'int',
- 'decimal',
- 'float',
- 'json',
- // 'addTimestamps',
- // 'addSoftDelete',
- ],
- 'form' => [
- [
- 'value' => '',
- 'label' => '请选择',
- ],
- [
- 'value' => 'input',
- 'label' => '输入框',
- ],
- [
- 'value' => 'number',
- 'label' => '数字输入框',
- ],
- [
- 'value' => 'textarea',
- 'label' => '多行文本框',
- ],
- [
- 'value' => 'dateTime',
- 'label' => '单选日期时间',
- ],
- // [
- // 'value' => 'dateTimeRange',
- // 'label' => '日期时间区间选择',
- // ],
- // [
- // 'value' => 'checkbox',
- // 'label' => '多选框',
- // ],
- [
- 'value' => 'radio',
- 'label' => '单选框',
- ],
- [
- 'value' => 'select',
- 'label' => '下拉框',
- ],
- [
- 'value' => 'frameImageOne',
- 'label' => '单图选择'
- ],
- [
- 'value' => 'frameImages',
- 'label' => '多图选择'
- ],
- ],
- 'rule' => $rule
- ];
- }
- /**
- * 改变数据库类型
- * @param string $type
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/13
- */
- public function changeTabelRule(string $type)
- {
- if (!in_array($type, $this->getTabelRule()['types'])) {
- throw new AdminException(500044);
- }
- return $this->getTabelRule()['rule'][$type] ?? $type;
- }
- /**
- * @param string $tableName
- * @return mixed
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/14
- */
- public function getTableInfo(string $tableName)
- {
- $sql = 'SELECT * FROM `information_schema`.`TABLES` WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?';
- $tableInfo = Db::query($sql, [config('database.connections.mysql.database'), $this->getTableName($tableName)]);
- return $tableInfo[0] ?? [];
- }
- /**
- * 获取表字段
- * @param string $tableName
- * @return mixed
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/7
- */
- public function getColumnNamesList(string $tableName)
- {
- $sql = 'SELECT * FROM `information_schema`.`columns` WHERE TABLE_SCHEMA = ? AND table_name = ? ORDER BY ORDINAL_POSITION';
- $column = Db::query($sql, [config('database.connections.mysql.database'), $this->getTableName($tableName)]);
- $columns = [];
- foreach ($column as $item) {
- $column = [
- 'name' => $item['COLUMN_NAME'],
- 'type' => $item['DATA_TYPE'],
- 'dataType' => stripos($item['COLUMN_TYPE'], '(') !== false ? substr_replace($item['COLUMN_TYPE'], '', stripos($item['COLUMN_TYPE'], ')') + 1) : $item['COLUMN_TYPE'],
- 'default' => $item['COLUMN_DEFAULT'],
- 'null' => $item['IS_NULLABLE'] == 'YES',
- 'primaryKey' => $item['COLUMN_KEY'] == 'PRI',
- 'unsigned' => (bool)stripos($item['COLUMN_TYPE'], 'unsigned'),
- 'autoIncrement' => stripos($item['EXTRA'], 'auto_increment') !== false,
- 'comment' => $item['COLUMN_COMMENT'],
- 'limit' => $item['CHARACTER_MAXIMUM_LENGTH'] ?: $item['NUMERIC_PRECISION'],
- ];
- $columns[$item['COLUMN_NAME']] = $column;
- }
- return $columns;
- }
- /**
- * @param array $data
- * @return array
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/12
- */
- public function valueReplace(array $data)
- {
- $replace = ['phar://'];
- $newData = [];
- foreach ($data as $key => $item) {
- if (is_array($item)) {
- $item = $this->valueReplace($item);
- } else {
- $item = str_replace($replace, '', $item);
- }
- $newData[str_replace($replace, '', $key)] = $item;
- }
- return $newData;
- }
- /**
- * 更新表字段
- * @param string $tableName
- * @param string $field
- * @param string $changeFiled
- * @param string $type
- * @param string $limit
- * @param string $default
- * @param string $comment
- * @param array $options
- * @return mixed
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/24
- */
- protected function updateAlter(string $tableName, string $field, string $changeFiled, string $type, $limit = '', string $default = '', string $comment = '', array $options = [])
- {
- $tableName = $this->getTableName($tableName);
- $comment = addslashes($comment);
- $field = addslashes($field);
- $changeFiled = addslashes($changeFiled);
- $type = addslashes($type);
- $default = addslashes($default);
- if (in_array(strtolower($type), ['text', 'longtext', 'tinytext'])) {
- $sql = "ALTER TABLE `$tableName` CHANGE `$field` `$changeFiled` $type CHARACTER SET utf8mb4 COLLATE " . self::TABLR_COLLATION . " NULL COMMENT '$comment';";
- } else {
- $sql = "ALTER TABLE `$tableName` CHANGE `$field` `$changeFiled` $type($limit) NOT NULL DEFAULT '$default' COMMENT '$comment';";
- }
- return Db::execute($sql);
- }
- /**
- * 添加字段
- * @param string $tableName
- * @param string $field
- * @param string $prevFiled
- * @param string $type
- * @param string $limit
- * @param string $default
- * @param string $comment
- * @param array $options
- * @return mixed
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/24
- */
- public function addAlter(string $tableName, string $field, string $prevFiled, string $type, $limit = '', string $default = '', string $comment = '', array $options = [])
- {
- $tableName = $this->getTableName($tableName);
- $comment = addslashes($comment);
- $field = addslashes($field);
- $prevFiled = addslashes($prevFiled);
- $type = addslashes($type);
- $default = addslashes($default);
- if ($prevFiled) {
- $after = "AFTER `$prevFiled`";
- } else {
- $after = "";
- }
- if (in_array(strtolower($type), ['text', 'longtext', 'tinytext'])) {
- $sql = "ALTER TABLE `$tableName` ADD `$field` $type NULL COMMENT '$comment' $after;";
- } else {
- $defaultSql = "NOT NULL DEFAULT '$default'";
- //处理时间字段默认值
- if (in_array(strtolower($type), ['datetime', 'timestamp', 'time', 'date', 'year'])) {
- switch ($field) {
- case 'delete_time':
- $defaultSql = 'NULL DEFAULT NULL';
- break;
- case 'create_time':
- case 'update_time':
- $defaultSql = 'NOT NULL DEFAULT CURRENT_TIMESTAMP';
- break;
- }
- }
- $limitSql = $limit ? '(' . $limit . ')' : '';
- $sql = "ALTER TABLE `$tableName` ADD `$field` $type$limitSql $defaultSql COMMENT '$comment' $after;";
- }
- return Db::execute($sql);
- }
- /**
- * 删除表字段
- * @param string $tableName
- * @param string $field
- * @return mixed
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/24
- */
- protected function deleteAlter(string $tableName, string $field)
- {
- $tableName = $this->getTableName($tableName);
- $field = addslashes($field);
- $sql = "ALTER TABLE `$tableName` DROP `$field`";
- return Db::execute($sql);
- }
- /**
- * 修改表备注
- * @param string $tableName
- * @param string $common
- * @return mixed
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/24
- */
- protected function updateFromCommon(string $tableName, string $common)
- {
- $tableName = $this->getTableName($tableName);
- $common = addslashes($common);
- $sql = "ALTER TABLE `$tableName` COMMENT = '$common';";
- return Db::execute($sql);
- }
- /**
- * 对比字段变动了更改
- * @param string $tableName
- * @param array $deleteField
- * @param array $tableField
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/24
- */
- protected function diffAlter(string $tableName, array $deleteField, array $tableField)
- {
- $updateAlter = [];
- $addAlter = [];
- $columns = $this->getColumnNamesList($tableName);
- $fieldAll = array_column($columns, 'name');
- $prevFiled = $columns[count($columns) - 1]['name'] ?? '';
- //对比数据库字段
- foreach ($tableField as $item) {
- if ($item['primaryKey']) {
- continue;
- }
- //前台新增的字段进行添加
- if (!(isset($item['default_field']) &&
- isset($item['default_field_type']) &&
- isset($item['default_limit']) &&
- isset($item['default_comment']) &&
- isset($item['default_default']))
- ) {
- if (!in_array($item['field'], $fieldAll)) {
- $addAlter[] = [
- 'prev_filed' => $prevFiled,
- 'field' => $item['field'],
- 'limit' => $item['limit'],
- 'type' => $item['field_type'],
- 'comment' => $item['comment'],
- 'default' => $item['default'],
- ];
- }
- continue;
- } else {
- //从数据库中新增的字段,并没有记录在表中做兼容处理;
- //默认字段没有在数据库中,需要添加字段;
- if (!in_array($item['default_field'], $fieldAll)) {
- $addAlter[] = [
- 'prev_filed' => $prevFiled,
- 'field' => $item['field'],
- 'limit' => $item['limit'],
- 'type' => $item['field_type'],
- 'comment' => $item['comment'],
- 'default' => $item['default'],
- ];
- continue;
- }
- }
- if ($item['default_field'] != $item['field'] && in_array($item['field_type'], ['addTimestamps', 'addSoftDelete'])) {
- throw new AdminException($item['field'] . '字段不允许被更改');
- }
- //数据库表存在的,字段,并且被修改
- if ($item['default_field'] != $item['field'] ||
- $item['default_field_type'] != $item['field_type'] ||
- $item['default_limit'] != $item['limit'] ||
- $item['default_comment'] != $item['comment'] ||
- $item['default_default'] != $item['default']) {
- $updateAlter[] = [
- 'default_field' => $item['default_field'],
- 'field' => $item['field'],
- 'limit' => $item['limit'],
- 'type' => $item['field_type'],
- 'comment' => $item['comment'],
- 'default' => $item['default'],
- ];
- }
- }
- //更新数据库字段
- foreach ($updateAlter as $item) {
- $this->updateAlter($tableName, $item['default_field'], $item['field'], $item['type'], $item['limit'], $item['default'], $item['comment']);
- }
- //添加字段
- foreach ($addAlter as $item) {
- $this->addAlter($tableName, $item['field'], $item['prev_filed'], $item['type'], $item['limit'], $item['default'], $item['comment']);
- }
- //删除多余字段
- foreach ($deleteField as $item) {
- $this->deleteAlter($tableName, $item);
- }
- }
- /**
- * 创建
- * @param array $data
- * @return mixed
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/11
- */
- public function createCrud(int $id, array $data)
- {
- $tableName = $data['tableName'];
- $tableField = $this->valueReplace($data['tableField']);
- $filePath = $this->valueReplace($data['filePath']);
- $modelName = !empty($data['modelName']) ? $data['modelName'] : $tableName;
- $tableComment = !empty($data['tableComment']) ? $data['tableComment'] : $modelName;
- //检测是否为系统表
- if (in_array($tableName, self::NOT_CRUD_TABANAME)) {
- throw new AdminException(500041);
- }
- $data['softDelete'] = false;
- $tableInfo = null;
- //先检查表存在则
- if ($id) {
- $this->updateFromCommon($tableName, $tableComment);
- //读取数据库表
- $tableInfo = $this->getTableInfo($tableName);
- if ($tableInfo) {
- //对比字段进行更新/删除字段
- $this->diffAlter($tableName, $data['deleteField'], $tableField);
- }
- } else {
- if ($this->dao->count(['table_name' => $tableName])) {
- throw new AdminException('表已经被生成过,请在列表中进行修改');
- }
- }
- //创建数据库
- $tableCreateInfo = null;
- if ($tableField && (!$data['isTable'] || !$tableInfo)) {
- $tableCreateInfo = $this->makeDatebase($tableName, $tableComment, $tableField);
- if ($tableCreateInfo['softDelete']) {
- $data['softDelete'] = true;
- }
- }
- //获取主键
- foreach ($tableField as $value) {
- if ($value['primaryKey']) {
- $data['key'] = $value['field'];
- break;
- }
- }
- $routeName = 'crud/' . Str::snake($tableName);
- $uniqueAuth = Str::snake($tableName) . '-crud-list-index';
- //增加保存的绝对路径
- foreach ($filePath as $k => $i) {
- if (in_array($k, ['pages', 'router', 'api'])) {
- $filePath[$k] = Make::adminTemplatePath() . $i;
- } else {
- $filePath[$k] = app()->getRootPath() . $i;
- }
- }
- //创建菜单
- if (!$data['menuName']) {
- $data['menuName'] = $tableName;
- }
- $dataMenu = [
- 'pid' => $data['pid'],
- 'menu_name' => $data['menuName'],
- 'menu_path' => '/' . $routeName,
- 'auth_type' => 1,
- 'is_show' => 1,
- 'is_show_path' => 1,
- 'is_del' => 0,
- 'unique_auth' => $uniqueAuth,
- 'is_header' => $data['pid'] ? 0 : 1,
- ];
- $crudInfo = null;
- if ($id) {
- $crudInfo = $this->dao->get($id);
- }
- $res = $this->transaction(function () use ($tableComment, $tableCreateInfo, $crudInfo, $modelName, $filePath, $tableName, $routeName, $data, $dataMenu) {
- $routeService = app()->make(SystemRouteServices::class);
- $meunService = app()->make(SystemMenusServices::class);
- //修改菜单名称
- if ($crudInfo) {
- //菜单存在的时候进行修改
- if ($crudInfo->menu_id && $meunService->value(['id' => [$crudInfo->menu_id]], 'id')) {
- $meunService->update($crudInfo->menu_id, $dataMenu);
- $menuInfo = (object)['id' => $crudInfo->menu_id];
- } else {
- $menuInfo = $meunService->save($dataMenu);
- }
- //删除掉添加的路由权限
- if ($crudInfo->route_ids) {
- $routeService->deleteRoutes($crudInfo->route_ids);
- }
- //删除掉权限路由
- if ($crudInfo->menu_ids) {
- app()->make(SystemMenusServices::class)->deleteMenus($crudInfo->menu_ids);
- }
- } else {
- $menuInfo = $meunService->save($dataMenu);
- }
- //写入路由权限
- $cateId = app()->make(SystemRouteServices::class)->topCateId('adminapi', 'CRUD');
- $ruleData = [
- [
- 'path' => $routeName,
- 'method' => 'GET',
- 'name' => $modelName . '列表接口',
- 'app_name' => 'adminapi',
- 'cate_id' => $cateId,
- 'unique_auth' => '',
- 'add_time' => date('Y-m-d H:i:s')
- ],
- [
- 'path' => $routeName . '/create',
- 'method' => 'GET',
- 'name' => $modelName . '获取创建表单接口',
- 'app_name' => 'adminapi',
- 'cate_id' => $cateId,
- 'unique_auth' => Str::snake($tableName) . '-add',
- 'add_time' => date('Y-m-d H:i:s')
- ],
- [
- 'path' => $routeName,
- 'method' => 'POST',
- 'name' => $modelName . '保存接口',
- 'app_name' => 'adminapi',
- 'cate_id' => $cateId,
- 'unique_auth' => '',
- 'add_time' => date('Y-m-d H:i:s')
- ],
- [
- 'path' => $routeName . '/<id>/edit',
- 'method' => 'GET',
- 'name' => $modelName . '获取修改表单接口',
- 'app_name' => 'adminapi',
- 'cate_id' => $cateId,
- 'unique_auth' => '',
- 'add_time' => date('Y-m-d H:i:s')
- ],
- [
- 'path' => $routeName . '/<id>',
- 'method' => 'PUT',
- 'name' => $modelName . '修改接口',
- 'app_name' => 'adminapi',
- 'cate_id' => $cateId,
- 'unique_auth' => '',
- 'add_time' => date('Y-m-d H:i:s')
- ],
- [
- 'path' => $routeName . '/<id>',
- 'method' => 'DELETE',
- 'name' => $modelName . '删除接口',
- 'app_name' => 'adminapi',
- 'cate_id' => $cateId,
- 'unique_auth' => '',
- 'add_time' => date('Y-m-d H:i:s')
- ],
- ];
- $routeList = $routeService->saveAll($ruleData);
- $routeIds = array_column($routeList->toArray(), 'id');
- //记录权限加入菜单表
- $menuData = [];
- foreach ($ruleData as $item) {
- $menuData[] = [
- 'pid' => $menuInfo->id ?: 0,
- 'methods' => $item['method'],
- 'api_url' => $item['path'],
- 'unique_auth' => $item['unique_auth'],
- 'menu_name' => $item['name'],
- 'is_del' => 0,
- 'auth_type' => 2,
- ];
- }
- $menus = app()->make(SystemMenusServices::class)->saveAll($menuData);
- $menuIds = array_column($menus->toArray(), 'id');
- //生成文件
- $make = $this->makeFile($tableName, $routeName, true, $data, $filePath);
- $makePath = [];
- foreach ($make as $key => $item) {
- $makePath[$key] = $item['path'];
- }
- if ($tableCreateInfo && isset($tableCreateInfo['table']) && $tableCreateInfo['table'] instanceof Table) {
- //创建数据库
- $tableCreateInfo['table']->create();
- }
- $crudDate = [
- 'pid' => $data['pid'],
- 'name' => $data['menuName'],
- 'model_name' => $data['modelName'],
- 'table_name' => $tableName,
- 'table_comment' => $tableComment,
- 'table_collation' => self::TABLR_COLLATION,
- 'field' => json_encode($data),//提交的数据
- 'menu_ids' => json_encode($menuIds),//生成的菜单id
- 'menu_id' => $menuInfo->id,//生成的菜单id
- 'make_path' => json_encode($makePath),
- 'route_ids' => json_encode($routeIds),
- ];
- if ($crudInfo) {
- $res = $this->dao->update($crudInfo->id, $crudDate);
- } else {
- $crudDate['add_time'] = time();
- //记录crud生成
- $res = $this->dao->save($crudDate);
- }
- return $res;
- });
- return $res->toArray();
- }
- /**
- * 获取数据库配置
- * @return array
- */
- protected function getDbConfig(): array
- {
- $default = app()->config->get('database.default');
- $config = app()->config->get("database.connections.{$default}");
- if (0 == $config['deploy']) {
- $dbConfig = [
- 'adapter' => $config['type'],
- 'host' => $config['hostname'],
- 'name' => $config['database'],
- 'user' => $config['username'],
- 'pass' => $config['password'],
- 'port' => $config['hostport'],
- 'charset' => $config['charset'],
- 'table_prefix' => $config['prefix'],
- ];
- } else {
- $dbConfig = [
- 'adapter' => explode(',', $config['type'])[0],
- 'host' => explode(',', $config['hostname'])[0],
- 'name' => explode(',', $config['database'])[0],
- 'user' => explode(',', $config['username'])[0],
- 'pass' => explode(',', $config['password'])[0],
- 'port' => explode(',', $config['hostport'])[0],
- 'charset' => explode(',', $config['charset'])[0],
- 'table_prefix' => explode(',', $config['prefix'])[0],
- ];
- }
- $table = app()->config->get('database.migration_table', 'migrations');
- $dbConfig['default_migration_table'] = $dbConfig['table_prefix'] . $table;
- return $dbConfig;
- }
- public function getAdapter()
- {
- $options = $this->getDbConfig();
- $adapter = AdapterFactory::instance()->getAdapter($options['adapter'], $options);
- if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
- $adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
- }
- return $adapter;
- }
- /**
- * 创建数据库
- * @param string $tableName
- * @param string $tableComment
- * @param array $tableField
- * @return array
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/7
- */
- public function makeDatebase(string $tableName, string $tableComment, array $tableField = [], string $collation = self::TABLR_COLLATION)
- {
- $timestampsField = [];
- $softDelete = false;
- $timestamps = false;
- $indexField = [];
- //创建表
- $table = new Table($tableName, ['comment' => $tableComment, 'collation' => $collation], $this->getAdapter());
- //创建字段
- foreach ($tableField as $item) {
- if (isset($item['primaryKey']) && $item['primaryKey']) {
- continue;
- }
- $option = [];
- if (isset($item['limit'])) {
- $option['limit'] = (int)$item['limit'];
- }
- if (isset($item['default'])) {
- $option['default'] = $item['default'];
- }
- //创建伪删除
- if ($item['field_type'] === 'addSoftDelete') {
- $table->addSoftDelete();
- $softDelete = true;
- } else if ($item['field_type'] == 'timestamp' &&
- ($item['field'] === 'create_time' || $item['field'] === 'update_time')) {
- $timestampsField[] = $item;
- } else {
- $option['comment'] = $item['comment'];
- $fieldType = $this->changeTabelRule($item['field_type']);
- if (in_array($fieldType, ['text', 'longtext', 'tinytext'])) {
- unset($option['limit']);
- }
- //判断字段类型
- if ($fieldType == 'boolean' && isset($option['default']) && $option['default'] === '') {
- unset($option['default']);
- }
- $table->addColumn($item['field'], $this->changeTabelRule($item['field_type']), $option);
- }
- }
- //创建索引
- if (!empty($data['tableIndex'])) {
- $indexField = $data['tableIndex'];
- foreach ($data['tableIndex'] as $item) {
- $table->addIndex($item);
- }
- }
- //如果是成对出现的create_time和update_time就直接增加修改和添加时间
- if (count($timestampsField) == 2) {
- //创建修改和增加时间
- $table->addTimestamps();
- $timestamps = true;
- } else {
- //如果是一个数组,增加一列
- foreach ($timestampsField as $item) {
- $option['comment'] = $item['comment'];
- $table->addColumn($item['field'], $this->changeTabelRule($item['field_type']), $option);
- }
- }
- return compact('indexField', 'softDelete', 'timestamps', 'table');
- }
- /**
- * 创建文件返回文件路径和内容
- * @param string $tableName
- * @param string $routeName
- * @param bool $isMake
- * @param array $options
- * @param array $filePath
- * @param string $basePath
- * @return array[]
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/7
- */
- public function makeFile(string $tableName, string $routeName, bool $isMake = false, array $options = [], array $filePath = [], string $basePath = '')
- {
- $options['fromField'] = is_array($options['fromField']) ? $options['fromField'] : [];
- $options['columnField'] = is_array($options['columnField']) ? $options['columnField'] : [];
- //生成模型
- $model = app()->make(Model::class);
- $model->setFilePathName($filePath['model'] ?? '')->setbasePath($basePath)->handle($tableName, $options);
- //生成dao
- $dao = app()->make(Dao::class);
- $dao->setFilePathName($filePath['dao'] ?? '')->setbasePath($basePath)->handle($tableName, [
- 'usePath' => $model->getUsePath(),
- 'modelName' => $options['modelName'] ?? '',
- ]);
- //生成service
- $service = app()->make(Service::class);
- $service->setFilePathName($filePath['service'] ?? '')->setbasePath($basePath)->handle($tableName, [
- 'field' => $options['fromField'],
- 'key' => $options['key'],
- 'usePath' => $dao->getUsePath(),
- 'modelName' => $options['modelName'] ?? '',
- ]);
- //生成验证器
- $validate = app()->make(Validate::class);
- $validate->setFilePathName($filePath['validate'] ?? '')->setbasePath($basePath)->handle($tableName, [
- 'field' => $options['fromField'],
- 'modelName' => $options['modelName'] ?? '',
- ]);
- //生成控制器
- $controller = app()->make(Controller::class);
- $controller->setFilePathName($filePath['controller'] ?? '')->setbasePath($basePath)->handle($tableName, [
- 'usePath' => $service->getUsePath(),
- 'modelName' => $options['modelName'] ?? '',
- 'validateName' => '\\' . str_replace('/', '\\', $validate->getUsePath()) . 'Validate::class',
- 'field' => array_column($options['fromField'], 'field'),
- ]);
- //生成路由
- $route = app()->make(Route::class);
- $route->setFilePathName($filePath['route'] ?? '')->setbasePath($basePath)->handle($tableName, [
- 'menus' => $options['modelName'] ?? $options['menuName'],
- 'route' => $routeName
- ]);
- //生成前台路由
- $viewRouter = app()->make(ViewRouter::class);
- $viewRouter->setFilePathName($filePath['router'] ?? '')->setbasePath($basePath)->handle($tableName, [
- 'route' => $routeName,
- 'menuName' => $options['menuName'],
- ]);
- //生成前台接口
- $viewApi = app()->make(ViewApi::class);
- $viewApi->setFilePathName($filePath['api'] ?? '')->setbasePath($basePath)->handle($tableName, [
- 'route' => $routeName,
- ]);
- //生成前台页面
- $viewPages = app()->make(ViewPages::class);
- $viewPages->setFilePathName($filePath['pages'] ?? '')->setbasePath($basePath)->handle($tableName, [
- 'field' => $options['columnField'],
- 'route' => $routeName,
- 'key' => $options['key'],
- 'pathApiJs' => '@/' . str_replace('\\', '/', str_replace([Make::adminTemplatePath(), '.js'], '', $viewApi->getPath())),
- ]);
- //创建文件
- if ($isMake) {
- FileService::batchMakeFiles([$model, $validate, $dao, $service, $controller, $route, $viewApi, $viewPages, $viewRouter]);
- }
- return [
- 'controller' => [
- 'path' => $this->replace($controller->getPath()),
- 'content' => $controller->getContent()
- ],
- 'model' => [
- 'path' => $this->replace($model->getPath()),
- 'content' => $model->getContent()
- ],
- 'dao' => [
- 'path' => $this->replace($dao->getPath()),
- 'content' => $dao->getContent()
- ],
- 'route' => [
- 'path' => $this->replace($route->getPath()),
- 'content' => $route->getContent()
- ],
- 'service' => [
- 'path' => $this->replace($service->getPath()),
- 'content' => $service->getContent()
- ],
- 'validate' => [
- 'path' => $this->replace($validate->getPath()),
- 'content' => $validate->getContent()
- ],
- 'router' => [
- 'path' => $this->replace($viewRouter->getPath()),
- 'content' => $viewRouter->getContent()
- ],
- 'api' => [
- 'path' => $this->replace($viewApi->getPath()),
- 'content' => $viewApi->getContent()
- ],
- 'pages' => [
- 'path' => $this->replace($viewPages->getPath()),
- 'content' => $viewPages->getContent()
- ],
- ];
- }
- protected function replace(string $path)
- {
- return str_replace([app()->getRootPath(), Make::adminTemplatePath()], '', $path);
- }
- /**
- * @param string $tableName
- * @param bool $fullName
- * @return string
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2023/4/7
- */
- public function getTableName(string $tableName, bool $fullName = true)
- {
- $tablePrefix = config('database.connections.mysql.prefix');
- $pattern = '/^' . $tablePrefix . '/i';
- return ($fullName ? $tablePrefix : '') . (preg_replace($pattern, '', $tableName));
- }
- }
|