quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
 
namespace app\common\model;
 
use think\Model;
 
/**
 * 模型基类
 */
class BaseModel extends Model
{
    public static $app_id;
    public static $agent_id;
    
    public static $base_url;
 
    protected $alias = '';
 
    protected $error = '';
 
    // 定义全局的查询范围
    protected $globalScope = ['app_id'];
 
    /**
     * 模型基类初始化
     */
    public static function init()
    {
        parent::init();
        // 获取当前域名
        self::$base_url = base_url();
        // 后期静态绑定app_id
        self::bindAppId();
    }
 
 
    /**
     * 后期静态绑定类名称
     */
    private static function bindAppId()
    {
        if ($app = app('http')->getName()) {
            if($app != 'admin' && $app != 'job' && $app != 'openapi'){
                $callfunc = 'set' . ucfirst($app) . 'AppId';
                self::$callfunc();
            }
        }
    }
 
    /**
     * 设置app_id (shop模块)
     */
    protected static function setShopAppId()
    {
        $session = session('jjjshop_store');
        if($session == null){
            return;
        }
        self::$app_id = $session['app']['app_id'];
    }
    /**
     * 设置app_id (agent模块)
     */
    protected static function setAgentAppId()
    {
        $session = session('jjjshop_dlagent');
        if($session == null){
            return;
        }
        self::$agent_id = $session['agent']['agent_id'];
    }
    /**
     * 设置app_id (operations模块)
     */
    protected static function setOperationsAppId()
    {
        $session = session('jjjshop_operations');
        if($session == null){
            return;
        }
        self::$app_id = $session['app']['app_id'];
    }
    /**
     * 设置app_id (api模块)
     */
    protected static function setApiAppId()
    {
        self::$app_id = request()->param('app_id');
    }
 
    /**
     * 设置app_id (supplier模块)
     */
    protected static function setSupplierAppId()
    {
        $session = session('jjjshop_supplier');
        if($session != null){
            self::$app_id = $session['app']['app_id'];
        }
    }
 
    /**
     * 设置app_id (branch模块) by lyzflash
     */
    protected static function setBranchAppId()
    {
        $session = session('jjjshop_branch');
        if($session != null){
            self::$app_id = $session['app']['app_id'];
        }
    }
 
    /**
     * 定义全局的查询范围
     */
    public function scopeApp_id($query)
    {
        if (self::$app_id > 0) {
            $query->where($query->getTable() . '.app_id', self::$app_id);
        }
    }
 
    /**
     * 设置默认的检索数据
     */
    protected function setQueryDefaultValue(&$query, $default = [])
    {
        $data = array_merge($default, $query);
        foreach ($query as $key => $val) {
            if (empty($val) && isset($default[$key])) {
                $data[$key] = $default[$key];
            }
        }
        return $data;
    }
 
    /**
     * 设置基础查询条件(用于简化基础alias和field)
     */
    public function setBaseQuery($alias = '', $join = [])
    {
        // 设置别名
        $aliasValue = $alias ?: $this->alias;
        $model = $this->alias($aliasValue)->field("{$aliasValue}.*");
        // join条件
        if (!empty($join)) : foreach ($join as $item):
            $model->join($item[0], "{$item[0]}.{$item[1]}={$aliasValue}."
                . (isset($item[2]) ? $item[2] : $item[1]));
        endforeach; endif;
        return $model;
    }
 
    /**
     * 批量更新数据(支持带where条件)
     */
    public function updateAll($data)
    {
        return $this->transaction(function () use ($data) {
            $result = [];
            foreach ($data as $key => $item) {
                $result[$key] = self::update($item['data'], $item['where']);
            }
            return $this->toCollection($result);
        });
    }
 
    public static function onBeforeUpdate(Model $model){
        if($model->createTime && $model[$model->createTime]){
            unset($model[$model->createTime]);
        }
        if ($model->updateTime && $model[$model->updateTime]) {
            $model[$model->updateTime] = $model->autoWriteTimestamp($model->updateTime);
        }
    }
 
    /**
     * 获取当前调用的模块名称
     */
    protected static function getCalledModule()
    {
        if (preg_match('/app\\\(\w+)/', get_called_class(), $class)) {
            return $class[1];
        }
        return false;
    }
    /**
     * 返回模型的错误信息
     */
    public function getError()
    {
        return $this->error;
    }
}