Util.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\common;
  3. use support\Db;
  4. use Illuminate\Database\Connection;
  5. use Illuminate\Database\Schema\Builder;
  6. class Util
  7. {
  8. /**
  9. * 获取webman-admin数据库连接
  10. * @return Connection
  11. */
  12. public static function db(): Connection
  13. {
  14. return Db::connection('mysql');
  15. }
  16. /**
  17. * 获取SchemaBuilder
  18. * @return Builder
  19. */
  20. public static function schema(): Builder
  21. {
  22. return Db::schema('mysql');
  23. }
  24. /**
  25. * 数据库字符串转义
  26. * @param $var
  27. * @return false|string
  28. */
  29. public static function pdoQuote($var)
  30. {
  31. return Util::db()->getPdo()->quote($var, \PDO::PARAM_STR);
  32. }
  33. /**
  34. * 变量或数组中的元素只能是字母数字下划线组合
  35. * @param $var
  36. * @return mixed
  37. * @throws \Exception
  38. */
  39. public static function filterAlphaNum($var)
  40. {
  41. $vars = (array)$var;
  42. array_walk_recursive($vars, function ($item) {
  43. if (is_string($item) && !preg_match('/^[a-zA-Z_0-9]+$/', $item)) {
  44. throw new \Exception('参数不合法');
  45. }
  46. });
  47. return $var;
  48. }
  49. public static function fieldDefaultLength()
  50. {
  51. return [
  52. 'string' => 255, // 对应varchar
  53. 'integer' => 11,
  54. 'decimal' => '10,2',
  55. 'dateTime' => 0
  56. ];
  57. }
  58. /**
  59. * 表单类型到插件的映射
  60. * @return \string[][]
  61. */
  62. public static function methodControlMap(): array
  63. {
  64. return [
  65. //method=>[控件]
  66. 'integer' => ['InputNumber'],
  67. 'string' => ['Input'],
  68. 'text' => ['TextArea'],
  69. 'date' => ['DatePicker'],
  70. 'enum' => ['Select'],
  71. 'float' => ['Input'],
  72. 'tinyInteger' => ['InputNumber'],
  73. 'smallInteger' => ['InputNumber'],
  74. 'mediumInteger' => ['InputNumber'],
  75. 'bigInteger' => ['InputNumber'],
  76. 'unsignedInteger' => ['InputNumber'],
  77. 'unsignedTinyInteger' => ['InputNumber'],
  78. 'unsignedSmallInteger' => ['InputNumber'],
  79. 'unsignedMediumInteger' => ['InputNumber'],
  80. 'unsignedBigInteger' => ['InputNumber'],
  81. 'decimal' => ['Input'],
  82. 'double' => ['Input'],
  83. 'mediumText' => ['TextArea'],
  84. 'longText' => ['TextArea'],
  85. 'dateTime' => ['DateTimePicker'],
  86. 'time' => ['DateTimePicker'],
  87. 'timestamp' => ['DateTimePicker'],
  88. 'char' => ['Input'],
  89. 'binary' => ['Input'],
  90. 'json' => ['input']
  91. ];
  92. }
  93. }