BaseModel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model;
  10. use think\Model;
  11. use think\Request;
  12. use think\Session;
  13. /**
  14. * 模型基类
  15. * Class BaseModel
  16. * @package app\common\model
  17. */
  18. class BaseModel extends Model
  19. {
  20. public static $wxapp_id;
  21. public static $base_url;
  22. protected $alias = '';
  23. /**
  24. * 模型基类初始化
  25. */
  26. public static function init()
  27. {
  28. parent::init();
  29. // 获取当前域名
  30. self::$base_url = base_url();
  31. // 后期静态绑定wxapp_id
  32. self::bindWxappId();
  33. }
  34. /**
  35. * 获取当前调用的模块名称
  36. * 例如:admin, api, store, task
  37. * @return string|bool
  38. */
  39. protected static function getCalledModule()
  40. {
  41. if (preg_match('/app\\\(\w+)/', get_called_class(), $class)) {
  42. return $class[1];
  43. }
  44. return false;
  45. }
  46. /**
  47. * 后期静态绑定类名称
  48. * 用于定义全局查询范围的wxapp_id条件
  49. * 子类调用方式:
  50. * 非静态方法: self::$wxapp_id
  51. * 静态方法中: $self = new static(); $self::$wxapp_id
  52. */
  53. private static function bindWxappId()
  54. {
  55. if ($module = self::getCalledModule()) {
  56. $callfunc = 'set' . ucfirst($module) . 'WxappId';
  57. method_exists(new self, $callfunc) && self::$callfunc();
  58. }
  59. }
  60. /**
  61. * 设置wxapp_id (store模块)
  62. */
  63. protected static function setStoreWxappId()
  64. {
  65. $session = Session::get('dyu_store');
  66. self::$wxapp_id = $session['wxapp']['wxapp_id'];
  67. }
  68. /**
  69. * 设置wxapp_id (api模块)
  70. */
  71. protected static function setApiWxappId()
  72. {
  73. $request = Request::instance();
  74. self::$wxapp_id = $request->param('wxapp_id');
  75. }
  76. /**
  77. * 定义全局的查询范围
  78. * @param \think\db\Query $query
  79. */
  80. protected function base($query)
  81. {
  82. if (self::$wxapp_id > 0) {
  83. $query->where($query->getTable() . '.wxapp_id', self::$wxapp_id);
  84. }
  85. }
  86. /**
  87. * 设置默认的检索数据
  88. * @param $query
  89. * @param array $default
  90. * @return array
  91. */
  92. protected function setQueryDefaultValue(&$query, $default = [])
  93. {
  94. $data = array_merge($default, $query);
  95. foreach ($query as $key => $val) {
  96. if (empty($val) && isset($default[$key])) {
  97. $data[$key] = $default[$key];
  98. }
  99. }
  100. return $data;
  101. }
  102. /**
  103. * 设置基础查询条件(用于简化基础alias和field)
  104. * @test 2019-4-25
  105. * @param string $alias
  106. * @param array $join
  107. * @return BaseModel
  108. */
  109. public function setBaseQuery($alias = '', $join = [])
  110. {
  111. // 设置别名
  112. $aliasValue = $alias ?: $this->alias;
  113. $model = $this->alias($aliasValue)->field("{$aliasValue}.*");
  114. // join条件
  115. if (!empty($join)) : foreach ($join as $item):
  116. $model->join($item[0], "{$item[0]}.{$item[1]}={$aliasValue}."
  117. . (isset($item[2]) ? $item[2] : $item[1]));
  118. endforeach; endif;
  119. return $model;
  120. }
  121. /**
  122. * 批量更新数据(支持带where条件)
  123. * @param array $data [0 => ['data'=>[], 'where'=>[]]]
  124. * @return \think\Collection
  125. */
  126. public function updateAll($data)
  127. {
  128. return $this->transaction(function () use ($data) {
  129. $result = [];
  130. foreach ($data as $key => $item) {
  131. $result[$key] = self::update($item['data'], $item['where']);
  132. }
  133. return $this->toCollection($result);
  134. });
  135. }
  136. }