Browse Source

更多的方法

lizhen_gitee 2 years ago
parent
commit
9549e12ac3
2 changed files with 115 additions and 0 deletions
  1. 95 0
      application/common.php
  2. 20 0
      thinkphp/library/think/db/Query.php

+ 95 - 0
application/common.php

@@ -788,3 +788,98 @@ function createUniqueNo($prifix = 'P',$id = 0)
 
     return $rt;
 }
+
+//下载远程图片 到指定目录
+function downloadfile($file_url, $path = '', $save_file_name = '')
+{
+    $basepath = '/uploaded/';
+    if ($path) {
+        $basepath = $basepath . $path . '/';
+    }
+    $basepath = $basepath . date('Ymd');
+    $dir_path = ROOT_PATH . '/public' . $basepath;
+    if (!is_dir($dir_path)) {
+        mkdir($dir_path, 0777, true);
+    }
+
+    $ch = curl_init();
+
+    curl_setopt($ch, CURLOPT_URL, $file_url);
+
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+
+    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
+
+    $file = curl_exec($ch);
+
+    curl_close($ch);
+
+    //传入保存文件的名称
+    $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
+
+    $resource = fopen($dir_path. '/'. $filename, 'a');
+
+    fwrite($resource, $file);
+
+    fclose($resource);
+
+    return $dir_path . '/' . $filename;
+}
+
+//获取视频时长
+function get_video_seconds($netpath = ''){
+
+    $local_url = downloadfile($netpath,'video','');
+    $playtime = 0;
+
+    Vendor('getid3.getid3.getid3');
+    $getID3 = new \getID3();
+    $fileinfo = $getID3->analyze($local_url);
+
+    if(isset($fileinfo['playtime_seconds'])){
+        $playtime = (int)$fileinfo['playtime_seconds'];
+    }
+
+    return $playtime;
+}
+
+//post接收参数中心,只接非必须
+function request_post_hub($field_array = [],$required = [],$noempty = []){
+    if(empty($field_array)){
+        return [];
+    }
+
+    $data = [];
+
+    foreach($field_array as $key => $field){
+
+        //接收
+        if(!request()->has($field,'post')){
+            continue;
+        }
+        $newone = request()->post($field);
+
+        //追加
+        $data[$field] = $newone;
+    }
+
+    //必传
+    if(!empty($required)){
+        foreach($required as $k => $mustone){
+            if(!isset($data[$mustone])){
+                return $mustone.' required';
+            }
+        }
+    }
+
+    //必传,且不能空
+    if(!empty($noempty)){
+        foreach($noempty as $havekey => $haveone){
+            if(!isset($data[$havekey]) || empty($data[$havekey])){
+                return $haveone.' 必填';
+            }
+        }
+    }
+
+    return $data;
+}

+ 20 - 0
thinkphp/library/think/db/Query.php

@@ -1434,6 +1434,26 @@ class Query
         $this->options['page'] = [intval($page), intval($listRows)];
         return $this;
     }
+    //模仿上一个page,但是从paginate拿到page与list_rows
+    public function autopage(){
+        $config = Config::get('paginate');
+
+        /** @var Paginator $class */
+        $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']);
+        $page  = isset($config['page']) ? (int) $config['page'] : call_user_func([
+            $class,
+            'getCurrentPage',
+        ], $config['var_page']);
+
+        $page = $page < 1 ? 1 : $page;
+
+        //list_rows
+        $listRows = isset($_REQUEST['listrow']) ? (int)$_REQUEST['listrow'] : $config['list_rows'];
+
+
+        $this->options['page'] = [intval($page), intval($listRows)];
+        return $this;
+    }
 
     /**
      * 分页查询