BaseModel.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. return;
  46. }
  47. self::$app_id = $session['app']['app_id'];
  48. }
  49. /**
  50. * 设置app_id (api模块)
  51. */
  52. protected static function setApiAppId()
  53. {
  54. self::$app_id = request()->param('app_id');
  55. }
  56. /**
  57. * 设置app_id (supplier模块)
  58. */
  59. protected static function setSupplierAppId()
  60. {
  61. $session = session('jjjshop_supplier');
  62. self::$app_id = $session['app']['app_id'];
  63. }
  64. /**
  65. * 定义全局的查询范围
  66. */
  67. public function scopeApp_id($query)
  68. {
  69. if (self::$app_id > 0) {
  70. $query->where($query->getTable() . '.app_id', self::$app_id);
  71. }
  72. }
  73. /**
  74. * 设置默认的检索数据
  75. */
  76. protected function setQueryDefaultValue(&$query, $default = [])
  77. {
  78. $data = array_merge($default, $query);
  79. foreach ($query as $key => $val) {
  80. if (empty($val) && isset($default[$key])) {
  81. $data[$key] = $default[$key];
  82. }
  83. }
  84. return $data;
  85. }
  86. /**
  87. * 设置基础查询条件(用于简化基础alias和field)
  88. */
  89. public function setBaseQuery($alias = '', $join = [])
  90. {
  91. // 设置别名
  92. $aliasValue = $alias ?: $this->alias;
  93. $model = $this->alias($aliasValue)->field("{$aliasValue}.*");
  94. // join条件
  95. if (!empty($join)) : foreach ($join as $item):
  96. $model->join($item[0], "{$item[0]}.{$item[1]}={$aliasValue}."
  97. . (isset($item[2]) ? $item[2] : $item[1]));
  98. endforeach; endif;
  99. return $model;
  100. }
  101. /**
  102. * 批量更新数据(支持带where条件)
  103. */
  104. public function updateAll($data)
  105. {
  106. return $this->transaction(function () use ($data) {
  107. $result = [];
  108. foreach ($data as $key => $item) {
  109. $result[$key] = self::update($item['data'], $item['where']);
  110. }
  111. return $this->toCollection($result);
  112. });
  113. }
  114. public static function onBeforeUpdate(Model $model){
  115. if($model->createTime && $model[$model->createTime]){
  116. unset($model[$model->createTime]);
  117. }
  118. if ($model->updateTime && $model[$model->updateTime]) {
  119. $model[$model->updateTime] = $model->autoWriteTimestamp($model->updateTime);
  120. }
  121. }
  122. /**
  123. * 获取当前调用的模块名称
  124. */
  125. protected static function getCalledModule()
  126. {
  127. if (preg_match('/app\\\(\w+)/', get_called_class(), $class)) {
  128. return $class[1];
  129. }
  130. return false;
  131. }
  132. /**
  133. * 返回模型的错误信息
  134. */
  135. public function getError()
  136. {
  137. return $this->error;
  138. }
  139. }