123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace Database\Seeders\Developer;
- use Database\Seeders\BaseSeeder;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\File;
- use Txj\Elastic\Facades\Eav;
- use Txj\Elastic\Facades\ES;
- /**
- * 接口模板
- */
- class DepInterTempSeeder extends BaseSeeder
- {
- public function getColumnData()
- {
- return [
- 'category_id' => ['type' => 'uBigInt', 'title' => '分类id', 'default' => 0],
- 'desc' => ['type' => 'text', 'title' => '接口描述'],
- 'title' => ['type' => 'string_60', 'title' => '接口标题'],
- 'en_alias' => ['type' => 'string_60', 'title' => '接口的英文别名'],
- 'category_type' => ['type' => 'uTinyint', 'title' => '接口分类', 'default' => 0, 'remark' => "1 普通接口 2 组合接口"],
- 'request_method' => ['type' => 'varchar', 'title' => '请求方式', 'default' => 'POST', 'remark' => 'POST GET PUT'],
- 'assemble_id' => ['type' => 'uBigInt', 'title' => '集合ID', 'default' => 0,],
- 'connect_db' => ['type' => 'varchar', 'title' => '数据库链接mysql elasticsearch redis mogodb', 'default' => 'mysql'],
- 'operate_type' => ['type' => 'uTinyint', 'title' => '操作类型', 'default' => 0, 'remark' => '1 查询 2 添加 3修改 4删除 5上传'],
- 'search_type' => ['type' => 'uTinyint', 'title' => '查询参数', 'default' => 0, 'remark' => '1 单条信息查询 2 分页列表查询 3 列表查询 4 树形列表查询 (对象) 5 条件总数 6 字段总和 7字段平均值 8 字段最大值 9 字段最小值',],
- 'search_column_id' => ['type' => 'uBigInt', 'title' => '查询需要的字段id', 'default' => 0, 'remark' => "为了防止名称修改,特存储id", 'is_null' => true],
- 'add_type' => ['type' => 'uTinyint', 'title' => '添加类型', 'default' => 0, 'remark' => '1添加单条数据 2批量添加 3添加树型数据'],
- 'update_type' => ['type' => 'uTinyint', 'title' => '编辑类型', 'default' => 0, 'remark' => '1更新单条数据 2批量更新 3更新单个树型数据 4累计加 5累计减'],
- 'delete_type' => ['type' => 'uTinyint', 'title' => '删除类型', 'default' => 0, 'remark' => '1伪删除单条数据 2真实删除单条数据 3批量伪删除'],
- 'upload_type' => ['type' => 'uTinyint', 'title' => '上传类型', 'default' => 0, 'remark' => '1图片 2音频 3视频 4其他'],
- 'request_settings' => ['type' => 'json', 'title' => '请求参数设置'],
- 'condition_settings' => ['type' => 'json', 'title' => '请求条件设置'],
- 'sort_settings' => ['type' => 'json', 'title' => '查询排序设置'],
- 'result_settings' => ['type' => 'json', 'title' => '请求结果设置'],
- 'conditions' => ['type' => 'json', 'title' => '组合接口,条件参数'],
- 'is_use' => ['type' => 'uTinyint', 'title' => '是否开启使用', 'default' => 0, 'remark' => '1 开启'],
- 'is_login' => ['type' => 'uTinyint', 'title' => '是否需要登录', 'default' => 0, 'remark' => '1 开启'],
- ];
- }
- public function init()
- {
- // 根据目录,获取接口模板
- $files = File::allFiles(__DIR__ . '/interfaceTemplate');
- foreach ($files as $file) {
- $content = file_get_contents($file);
- if ($content) {
- $content = trim($content);
- $content = preg_replace("/^return/is", '', $content, 1);
- $arr = json_decode($content, true);
- if (empty($arr)) {
- dd("文件json解析错误" . $file);
- }
- unset($arr['category_id']);
- unset($arr['desc']);
- $arr['website_id'] = 1;
- // 获取表id
- $arr['assemble_id'] = $this->getCustomAssembleId($arr['assemble_id']);
- // 将字段转为id
- $arr['condition_settings'] = $this->getConditionColumnId($arr['assemble_id'], $arr['condition_settings'] ?? []);
- if ($arr) {
- Eav::table($this->schema)->insert($arr);
- } else {
- dd("接口模板数据格式错误,请检查后,再试! 文件: {$file->getRelativePathname()}");
- }
- }
- }
- }
- public function run()
- {
- $this->schema = 'dep_inter_template';
- $this->title = '接口模板表';
- $this->start();
- }
- // 获取集合id
- private function getCustomAssembleId(string $table): int
- {
- $info = DB::table('sys_assemble')->where('schema', $table)->first();
- if ($info) {
- return $info->id;
- } else {
- return 0;
- }
- }
- // 根据字段名称设置字段的id
- private function getConditionColumnId(int $assemble_id, $conditionSettings): array
- {
- if ($assemble_id && $conditionSettings) {
- foreach ($conditionSettings as $key => $row) {
- if (isset($row['column_id'])) {
- $info = DB::table('sys_assemble_column')->where('assemble_id', $assemble_id)
- ->where('code', $row['column_id'])->first();
- if ($info) {
- $conditionSettings[$key]['column_id'] = $info->id;
- }
- }
- }
- }
- return $conditionSettings;
- }
- }
|