DepInterfaceSeeder.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Database\Seeders\Developer;
  3. use Database\Seeders\BaseSeeder;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\File;
  6. use Txj\Elastic\Facades\Eav;
  7. use Txj\Elastic\Facades\ES;
  8. class DepInterfaceSeeder extends BaseSeeder
  9. {
  10. public function getColumnData()
  11. {
  12. return [
  13. 'website_id' => ['type' => 'uBigInt', 'title' => '站点id', 'is_null' => 0],
  14. 'title' => ['type' => 'string_60', 'title' => '接口标题'],
  15. 'en_alias' => ['type' => 'string_60', 'title' => '接口的英文别名'],
  16. 'category_type' => ['type' => 'uTinyint', 'title' => '接口分类', 'default' => 0, 'remark' => "1 系统接口 2 自定义复杂接口"],
  17. 'request_method' => ['type' => 'varchar', 'title' => '请求方式', 'default' => 'POST', 'remark' => 'POST GET PUT'],
  18. 'assemble_id' => ['type' => 'uBigInt', 'title' => '集合ID', 'default' => 0],
  19. 'connect_db' => ['type' => 'varchar', 'title' => '数据库链接mysql elasticsearch redis mogodb', 'default' => 'mysql'],
  20. 'operate_type' => ['type' => 'uTinyint', 'title' => '操作类型', 'default' => 0, 'remark' => '1 查询 2 添加 3修改 4删除 5上传'],
  21. 'search_type' => ['type' => 'uTinyint', 'title' => '查询参数', 'default' => 0, 'remark' => '1 单条信息查询 2 分页列表查询 3 列表查询 4 树形列表查询 (对象) 5 条件总数 6 字段总和 7字段平均值 8 字段最大值 9 字段最小值',],
  22. 'search_column_id' => ['type' => 'uBigInt', 'title' => '查询需要的字段id', 'default' => 0, 'remark' => "为了防止名称修改,特存储id", 'is_null' => true],
  23. 'add_type' => ['type' => 'uTinyint', 'title' => '添加类型', 'default' => 0, 'remark' => '1添加单条数据 2批量添加 3添加树型数据'],
  24. 'update_type' => ['type' => 'uTinyint', 'title' => '编辑类型', 'default' => 0, 'remark' => '1更新单条数据 2批量更新 3更新单个树型数据 4累计加 5累计减'],
  25. 'delete_type' => ['type' => 'uTinyint', 'title' => '删除类型', 'default' => 0, 'remark' => '1伪删除单条数据 2真实删除单条数据 3批量伪删除'],
  26. 'upload_config' => ['type' => 'object', 'title' => '上传json配置', 'default' => 0, 'remark' => '上传json配置'],
  27. 'request_settings' => ['type' => 'object', 'title' => '请求参数设置'],
  28. 'condition_settings' => ['type' => 'object', 'title' => '请求条件设置'],
  29. 'sort_settings' => ['type' => 'object', 'title' => '查询排序设置'],
  30. 'result_settings' => ['type' => 'object', 'title' => '请求结果设置'],
  31. 'conditions' => ['type' => 'object', 'title' => '组合接口,条件参数'],
  32. 'is_use' => ['type' => 'uTinyint', 'title' => '是否开启使用', 'default' => 0, 'remark' => '1 开启'],
  33. 'is_login' => ['type' => 'uTinyint', 'title' => '是否需要登录', 'default' => 0, 'remark' => '1 开启'],
  34. ];
  35. }
  36. public function init()
  37. {
  38. // 根据目录,获取接口模板
  39. $files = File::allFiles(__DIR__ . '/interfaceTemplate');
  40. foreach ($files as $file) {
  41. $content = file_get_contents($file);
  42. if ($content) {
  43. $content = trim($content);
  44. $content = preg_replace("/^return/is", '', $content, 1);
  45. $content = trim($content);
  46. $arr = json_decode($content, true);
  47. if (empty($arr)) {
  48. dd("文件json解析错误" . $file);
  49. }
  50. unset($arr['category_id']);
  51. unset($arr['desc']);
  52. $arr['website_id'] = $arr['website_id'] ?? 1;
  53. // 获取表id
  54. $arr['assemble_id'] = $this->getCustomAssembleId($arr['assemble_id']);
  55. // 将字段转为id
  56. $arr['condition_settings'] = $this->getConditionColumnId($arr['assemble_id'], $arr['condition_settings'] ?? []);
  57. if ($arr) {
  58. Eav::table($this->schema)->insert($arr);
  59. } else {
  60. dd("接口模板数据格式错误,请检查后,再试! 文件: {$file->getRelativePathname()}");
  61. }
  62. }
  63. }
  64. }
  65. // 获取集合id
  66. private function getCustomAssembleId(string $table): int
  67. {
  68. $info = DB::table('sys_assemble')->where('schema', $table)->first();
  69. if ($info) {
  70. return $info->id;
  71. } else {
  72. return 0;
  73. }
  74. }
  75. // 根据字段名称设置字段的id
  76. private function getConditionColumnId(int $assemble_id, array $conditionSettings): array
  77. {
  78. if ($conditionSettings) {
  79. foreach ($conditionSettings as $key => $row) {
  80. if (isset($row['column_id'])) {
  81. $info = DB::table('sys_assemble_column')->where('assemble_id', $assemble_id)
  82. ->where('code', $row['column_id'])->first();
  83. if ($info) {
  84. $conditionSettings[$key]['column_id'] = $info->id;
  85. }
  86. }
  87. }
  88. }
  89. return $conditionSettings;
  90. }
  91. public function run()
  92. {
  93. $this->schema = 'dep_interface';
  94. $this->title = '接口表';
  95. $this->start();
  96. }
  97. }