1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- class Oplog extends Model
- {
- protected $table = 'oplog';
- public $timestamps = false;
- public static function add($table, $data_id, $type, $before = [], $after = [], $op_reason = null, $columnsConfig = 'PROJECT')
- {
- $log = new Oplog();
- $log->table_name = $table;
- $log->data_id = $data_id;
- $log->type = $type;
- $log->op_reason = $op_reason;
- $log->before_data = json_encode($before, JSON_UNESCAPED_UNICODE);
- $log->after_data = json_encode($after, JSON_UNESCAPED_UNICODE);
- $log->op_user_id = \Auth::id();
-
- $user = Users::select('name', 'role_id', 'real_name')->where('id', $log->op_user_id)->first();
- $role_name = Roles::where('id', $user->role_id)->value('name');
- $log->op_user_name = $user->real_name;
- $log->op_user_role = $role_name;
- #名词
- $columns = config('columns_note.'.$columnsConfig);
- if($type == 2){
- $log->content = '';
- if( !empty($after) && !empty($before) ){
- foreach( $after as $k=>$v ){
- if( $v != $before[$k] ){
- $k_desc = isset($columns[$k]) ? $columns[$k] : $k;
- $log->content .= $k_desc.'由'.$before[$k].'改为'. $v ."; ";
- }
- }
- }
- } elseif($type==1){
- $log->content = '创建';
- } elseif($type==3){
- $log->content = '删除';
- } elseif($type==4){
- $log->content = '锁定';
- } elseif($type==2){
- $log->content = '编辑';
- }
- $log->save();
- return true;
- }
- }
|