Common.php 4.2 KB
<?php

namespace App\Helper;

use App\Models\Ai\AiCommand as AiCommandModel;
use App\Models\User\UserLog as UserLogModel;
use App\Models\User\UserLogin as UserLoginModel;
use Illuminate\Support\Facades\Cache;

/**
 * @name:
 */
class Common
{
    /**
     * @name :生成用户操作日志
     * @return void
     * @author :liyuhang
     * @method
     */
    public static function set_user_log($param = []){
        $data = [
            'operator_id'=>$param['operator_id'],
            'model'=>$param['model'],
            'remark'=>$param['remark']
        ];
        $model = new UserLogModel();
        return $model->add($data);
    }

    /**
     * @name :写入登录日志
     * @return void
     * @author :liyuhang
     * @method
     */
    public static function set_user_login($param = []){
        $data = [
            'user_id'=>$param['user_id'],
            'ip'=>$param['ip']
        ];
        $model = new UserLoginModel();
        return $model->add($data);
    }

    /**
     * @name :ai自动生成
     * @return mixed
     * @author :liyuhang
     * @method
     */
    public static function send_openai_msg($url,$param){
        $url = HTTP_OPENAI_URL.$url;
        $aiCommandModel = New AiCommandModel();
        //指定库获取指令
        $info = $aiCommandModel->read(['key'=>$param['key']]);
        if($info === false){
            response('指令不存在',400);
        }
        //替换关键字
        $content = str_replace('$keyword$', $param['keywords'], $info['ai']);
        $data = [
            'messages'=>[
                ['role'=>'system','content'=>$info['scene']],
                ['role'=>'assistant','content'=>$content],
            ]
        ];
        return http_post($url,json_encode($data));
    }

    /**
     * @name  :获取缓存
     * @return void
     * @author :liyuhang
     * @method
     */
    public static function get_user_cache($table,$id,$type = 'B'){
        $data = [];
        $cache = config('cache.user_is_cache');
        if(isset($cache) && ($cache['is_cache'] == true)){
            $key = 'cache_'.$table.'_'.$id.'_type';
            $data = Cache::store('file')->get($key);
        }
        return $data;
    }

    /**
     * @name :写入缓存
     * @return bool
     * @author :liyuhang
     * @method
     */
    public static function set_user_cache($data = [],$table,$id,$type = 'B'){
        $cache = config('cache.user_is_cache');
        if(isset($cache) && ($cache['is_cache'] == true)){
            $key = 'cache_'.$table.'_'.$id.'_type';
            Cache::store('file')->set($key,$data,3600);
        }
        return true;
    }

    /**
     * @name :清除缓存
     * @return bool
     * @author :liyuhang
     * @method
     */
    public static function del_user_cache($table,$id,$type = 'B'){
        $cache = config('cache.user_is_cache');
        if(isset($cache) && ($cache['is_cache'] == true)){
            if(is_array($id)){
                foreach ($id as $v){
                    $key = 'cache_'.$table.'_'.$v.'_type';
                    Cache::store('file')->pull($key);
                }
            }else{
                $key = 'cache_'.$table.'_'.$id.'_type';
            }
            Cache::store('file')->pull($key);
        }
        return true;
    }

    /**
     * @name   :(多维数组去重)array_deduplication
     * @author :lyh
     * @method :post
     * @time   :2023/5/9 10:47
     */
    public static function uniqueMultiArray($arr) {
        $arr = array_map('serialize', $arr);
        $arr = array_unique($arr);
        $arr = array_map('unserialize', $arr);
        return $arr;
    }

    /**
     * @param $targetDateTime
     * @name   :(获取时间差,精确时分秒,返回天数)getDaysToTargetDate
     * @author :lyh
     * @method :post
     * @time   :2023/5/24 9:38
     */
    public static function getDaysToTargetDate($targetDateTime)
    {
        // 当前日期时间
        $currentDateTime = new DateTime();
        // 目标日期时间
        $targetDateTimeObj = new DateTime($targetDateTime);
        // 计算日期时间差距
        $interval = $currentDateTime->diff($targetDateTimeObj);
        // 获取总天数差距
        $days = $interval->days;
        return $days;
    }
}