BaseModel.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 模型基类
  6. */
  7. class BaseModel extends Model
  8. {
  9. public static $app_id;
  10. public static $base_url;
  11. protected $alias = '';
  12. protected $error = '';
  13. // 定义全局的查询范围
  14. protected $globalScope = ['app_id'];
  15. /**
  16. * 模型基类初始化
  17. */
  18. public static function init()
  19. {
  20. parent::init();
  21. // 获取当前域名
  22. self::$base_url = base_url();
  23. // 后期静态绑定app_id
  24. self::bindAppId();
  25. }
  26. /**
  27. * 后期静态绑定类名称
  28. */
  29. private static function bindAppId()
  30. {
  31. if ($app = app('http')->getName()) {
  32. if($app != 'admin' && $app != 'job'){
  33. $callfunc = 'set' . ucfirst($app) . 'AppId';
  34. self::$callfunc();
  35. }
  36. }
  37. }
  38. /**
  39. * 设置app_id (shop模块)
  40. */
  41. protected static function setShopAppId()
  42. {
  43. $session = session('jjjshop_store');
  44. if($session != null){
  45. self::$app_id = $session['app']['app_id'];
  46. }
  47. }
  48. /**
  49. * 设置app_id (api模块)
  50. */
  51. protected static function setApiAppId()
  52. {
  53. self::$app_id = request()->param('app_id');
  54. }
  55. /**
  56. * 定义全局的查询范围
  57. */
  58. public function scopeApp_id($query)
  59. {
  60. if (self::$app_id > 0) {
  61. $query->where($query->getTable() . '.app_id', self::$app_id);
  62. }
  63. }
  64. /**
  65. * 设置默认的检索数据
  66. */
  67. protected function setQueryDefaultValue($query, $default = [])
  68. {
  69. $data = array_merge($default, $query);
  70. foreach ($query as $key => $val) {
  71. if (empty($val) && isset($default[$key])) {
  72. $data[$key] = $default[$key];
  73. }
  74. }
  75. return $data;
  76. }
  77. /**
  78. * 设置基础查询条件(用于简化基础alias和field)
  79. */
  80. public function setBaseQuery($alias = '', $join = [])
  81. {
  82. // 设置别名
  83. $aliasValue = $alias ?: $this->alias;
  84. $model = $this->alias($aliasValue)->field("{$aliasValue}.*");
  85. // join条件
  86. if (!empty($join)) : foreach ($join as $item):
  87. $model->join($item[0], "{$item[0]}.{$item[1]}={$aliasValue}."
  88. . (isset($item[2]) ? $item[2] : $item[1]));
  89. endforeach; endif;
  90. return $model;
  91. }
  92. /**
  93. * 批量更新数据(支持带where条件)
  94. */
  95. public function updateAll($data)
  96. {
  97. return $this->transaction(function () use ($data) {
  98. $result = [];
  99. foreach ($data as $key => $item) {
  100. $result[$key] = self::update($item['data'], $item['where']);
  101. }
  102. return $this->toCollection($result);
  103. });
  104. }
  105. public static function onBeforeUpdate(Model $model){
  106. if($model->createTime && $model[$model->createTime]){
  107. unset($model[$model->createTime]);
  108. }
  109. if ($model->updateTime && $model[$model->updateTime]) {
  110. $model[$model->updateTime] = $model->autoWriteTimestamp();
  111. }
  112. }
  113. /**
  114. * 获取当前调用的模块名称
  115. */
  116. protected static function getCalledModule()
  117. {
  118. if (preg_match('/app\\\(\w+)/', get_called_class(), $class)) {
  119. return $class[1];
  120. }
  121. return false;
  122. }
  123. /**
  124. * 返回模型的错误信息
  125. */
  126. public function getError()
  127. {
  128. return $this->error;
  129. }
  130. }