BaseModel.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User xiekunyu@kaishanlaw.com
  5. * Date 2021/7/9 16:15
  6. */
  7. namespace app;
  8. use think\facade\Db;
  9. use think\Model;
  10. class BaseModel extends Model
  11. {
  12. protected $defaultSoftDelete = 0;
  13. protected $error = '';
  14. protected static $db_prefix = 'yu_';
  15. protected static $userInfo = null;
  16. protected static $uid = null;
  17. protected static function init()
  18. {
  19. self::$db_prefix = config('database.connections.mysql.prefix') ?: "yu_";
  20. self::initModel();
  21. }
  22. // 加载模型自动处理
  23. public static function initModel()
  24. {
  25. self::$userInfo=request()->userInfo ?? null;
  26. self::$uid=request()->userInfo['user_id'] ?? null;
  27. }
  28. /**
  29. * 获取树状信息
  30. * @param array $config
  31. */
  32. public static function getCheckNode($arr, $pid, $field = "parent_id", $table = '')
  33. {
  34. if (!$table) {
  35. $res = self::find($pid);
  36. } else {
  37. $res = Db::name($table)->find($pid);
  38. }
  39. if ($res) {
  40. if ($res[$field] > 0) {
  41. array_unshift($arr, $res[$field]);
  42. return self::getCheckNode($arr, $res[$field], $field, $table);
  43. }
  44. }
  45. return $arr;
  46. }
  47. // 获取错误信息
  48. public function getError()
  49. {
  50. return $this->error;
  51. }
  52. /**
  53. * 获取模型的json字段数组
  54. * @return array
  55. */
  56. public function getJsonFieldName(): array
  57. {
  58. return $this->json;
  59. }
  60. // 匹配列表信息
  61. public static function filterIdr($data, $many, $field)
  62. {
  63. if ($many) {
  64. $idr = \utils\Arr::arrayToString($data, $field, false);
  65. } else {
  66. $idr = [];
  67. if (is_array($field)) {
  68. foreach ($field as $v) {
  69. $idr[] = $data[$v];
  70. }
  71. } else {
  72. $idr = [$data[$field]];
  73. }
  74. }
  75. $key = array_search(0, $idr);
  76. if ($key) {
  77. array_splice($idr, $key, 1);
  78. }
  79. $idr = array_unique($idr);
  80. return $idr ? : [];
  81. }
  82. // 获取某一项数据的统计
  83. public static function getTotal($map,$where=[],$field,$group){
  84. return self::field($field)
  85. ->where($map)
  86. ->where($where)
  87. ->group($group)
  88. ->select()->toArray();
  89. }
  90. }