GenerateTableValidate.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\validate\tools;
  15. use app\common\model\tools\GenerateTable;
  16. use app\common\validate\BaseValidate;
  17. use think\facade\Db;
  18. /**
  19. * 代码生成选择表验证
  20. * Class SelectTableValidate
  21. * @package app\adminapi\validate\tools
  22. */
  23. class GenerateTableValidate extends BaseValidate
  24. {
  25. protected $rule = [
  26. 'id' => 'require|checkTableData',
  27. 'table' => 'require|array|checkTable',
  28. 'file' => 'require'
  29. ];
  30. protected $message = [
  31. 'id.require' => '参数缺失',
  32. 'table.require' => '参数缺失',
  33. 'table.array' => '参数类型错误',
  34. 'file.require' => '下载失败',
  35. ];
  36. /**
  37. * @notes 选择数据表场景
  38. * @return GenerateTableValidate
  39. * @author 段誉
  40. * @date 2022/6/15 18:58
  41. */
  42. public function sceneSelect()
  43. {
  44. return $this->only(['table']);
  45. }
  46. /**
  47. * @notes 需要校验id的场景
  48. * @return GenerateTableValidate
  49. * @author 段誉
  50. * @date 2022/6/15 18:58
  51. */
  52. public function sceneId()
  53. {
  54. return $this->only(['id']);
  55. }
  56. /**
  57. * @notes 下载场景
  58. * @return GenerateTableValidate
  59. * @author 段誉
  60. * @date 2022/6/24 10:02
  61. */
  62. public function sceneDownload()
  63. {
  64. return $this->only(['file']);
  65. }
  66. /**
  67. * @notes 校验选择的数据表信息
  68. * @param $value
  69. * @param $rule
  70. * @param $data
  71. * @return bool|string
  72. * @author 段誉
  73. * @date 2022/6/15 18:58
  74. */
  75. protected function checkTable($value, $rule, $data)
  76. {
  77. foreach ($value as $item) {
  78. if (!isset($item['name']) || !isset($item['comment'])) {
  79. return '参数缺失';
  80. }
  81. $exist = Db::query("SHOW TABLES LIKE'" . $item['name'] . "'");
  82. if (empty($exist)) {
  83. return '当前数据库不存在' . $item['name'] . '表';
  84. }
  85. }
  86. return true;
  87. }
  88. /**
  89. * @notes 校验当前数据表是否存在
  90. * @param $value
  91. * @param $rule
  92. * @param $data
  93. * @return bool|string
  94. * @author 段誉
  95. * @date 2022/6/15 18:58
  96. */
  97. protected function checkTableData($value, $rule, $data)
  98. {
  99. if (!is_array($value)) {
  100. $value = [$value];
  101. }
  102. foreach ($value as $item) {
  103. $table = GenerateTable::findOrEmpty($item);
  104. if ($table->isEmpty()) {
  105. return '信息不存在';
  106. }
  107. }
  108. return true;
  109. }
  110. }