zm
2021-04-09 93652ca875fc904a25a7f214adc548745573950a
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
<?php
/**
 * link: http://www.zjhejiang.com/
 * copyright: Copyright (c) 2018 浙江禾匠信息科技有限公司
 * author: wxf
 */
 
namespace app\models;
 
use app\jobs\UserActionJob;
use app\models\BaseQuery\BaseActiveQuery;
 
class ModelActiveRecord extends \yii\db\ActiveRecord
{
    public $fillable = [];
    public $guarded = [];
    public $isLog = true; // 单独开关
    public static $log = true; // 全局开关
 
    /**
     * 有sql添加、更新操作时自动追加添加时间、更新时间
     * @return bool
     */
    public function beforeValidate()
    {
        $time = date('Y-m-d H:i:s', time());
        $insert = $this->isNewRecord;
 
        $isCreatedAt = false;
        $isUpdatedAt = false;
        $isDeletedAt = false;
        $isDelete = false;
        if (isset($this->attributes) && is_array($this->attributes())) {
            foreach ($this->attributes() as $item) {
                $item === 'created_at' ? $isCreatedAt = true : '';
                $item === 'updated_at' ? $isUpdatedAt = true : '';
                $item === 'deleted_at' ? $isDeletedAt = true : '';
                $item === 'is_delete' ? $isDelete = true : '';
            }
        }
 
        if ($insert === true && $isCreatedAt === true) {
            $this->created_at = $time;
        }
 
        if ($isUpdatedAt === true) {
            $this->updated_at = $time;
        }
 
        if ($isDelete === true && $isDeletedAt === true) {
            if ((int)$this->is_delete === 1) {
                $this->deleted_at = $time;
            } else {
                $this->deleted_at = '0000-00-00 00:00:00';
            }
        }
 
        return parent::beforeValidate();
    }
 
    /**
     * @return BaseActiveQuery
     */
    public static function find()
    {
        return \Yii::createObject(BaseActiveQuery::className(), [get_called_class()]);
    }
 
    public function afterSave($insert, $changedAttributes)
    {
        if (!($this->isLog === true && self::$log === true)) {
            parent::afterSave($insert, $changedAttributes);
            return true;
        }
        try {
            $isSave = true;
            try {
                if (!\Yii::$app->user->isGuest) {
                    $userId = \Yii::$app->user->id;
                    /** @var User $user */
                    $user = \Yii::$app->user->identity;
                    $userIdentity = $user->identity;
                    if ($user->mch_id == 0) {
                        // 会员影响队列时间
                        // 小程序端操作不记录日志
                        if ($userIdentity->is_super_admin == 0 && $userIdentity->is_admin == 0
                            && $userIdentity->is_operator == 0) {
                            $isSave = false;
                        }
                        // 管理员 新增操作不记录日志
                        if (($userIdentity->is_super_admin == 1 || $userIdentity->is_admin == 1) && $insert === true) {
                            $isSave = false;
                        }
                    }
                } else {
                    $userId = 0;
                }
            } catch (\Exception $e) {
                $userId = 0;
            }
            try {
                $mallId = \Yii::$app->mall->id;
            } catch (\Exception $e) {
                $mallId = 0;
            }
 
            // 更新时 保存日志
            if ($this->isLog === true && $isSave) {
                // 去除以下字段 不记录日志
                $arr = ['created_at', 'updated_at', 'deleted_at'];
                $afterUpdate = $this->attributes;
                $newBeforeUpdate = [];
                $newAfterUpdate = [];
                $remark = '数据更新';
                if (isset($afterUpdate['is_delete']) && $afterUpdate['is_delete'] == 1) {
                    $remark = '数据删除';
                }
 
                foreach ($changedAttributes as $key => $item) {
                    if (in_array($key, $arr)) {
                        unset($changedAttributes[$key]);
                        continue;
                    }
                    if ($item != $afterUpdate[$key]) {
                        try {
                            $newBeforeUpdate[$key] = \Yii::$app->serializer->decode($item);
                        } catch (\Exception $e) {
                            $newBeforeUpdate[$key] = $item;
                        }
 
                        try {
                            $newAfterUpdate[$key] = \Yii::$app->serializer->decode($afterUpdate[$key]);
                        } catch (\Exception $e) {
                            $newAfterUpdate[$key] = $afterUpdate[$key];
                        }
                    }
                }
 
                if ($newBeforeUpdate) {
                    // 黑名单之外的数据
                    if ($this->guarded) {
                        foreach ($this->guarded as $item) {
                            unset($newBeforeUpdate[$item]);
                            unset($newAfterUpdate[$item]);
                        }
                    }
 
                    // 白名单之内的数据
                    if (!$this->guarded && $this->fillable) {
                        foreach ($newBeforeUpdate as $key => $item) {
                            if (!in_array($key, $this->fillable)) {
                                unset($newBeforeUpdate[$key]);
                                unset($newAfterUpdate[$key]);
                            }
                        }
                    }
 
                    $modelName = self::className();
                    $dataArr = [
                        'newBeforeUpdate' => $newBeforeUpdate,
                        'newAfterUpdate' => $newAfterUpdate,
                        'modelName' => $modelName,
                        'modelId' => $this->attributes['id'],
                        'remark' => $remark,
                        'user_id' => $userId,
                        'mall_id' => $mallId
                    ];
                    $class = new UserActionJob($dataArr);
                    $queueId = \Yii::$app->queue3->delay(10)->push($class);
                }
            }
            parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        } catch (\Exception $e) {
            throw $e;
        }
    }
}