BaseModel.php 3.6 KB

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