yingzi 2 سال پیش
والد
کامیت
3cb14e57ec
4فایلهای تغییر یافته به همراه562 افزوده شده و 0 حذف شده
  1. 46 0
      app/api/controller/User.php
  2. 3 0
      app/api/route/user.php
  3. 18 0
      config/qiniu.php
  4. 495 0
      library/utils/Qiniu.php

+ 46 - 0
app/api/controller/User.php

@@ -34,6 +34,7 @@ use app\model\api\PayTrade as PayTradeModel;
 use app\Request;
 use library\services\UtilService;
 use library\utils\QRcodeComm;
+use library\utils\Qiniu;
 use library\utils\IdentityCard;
 use library\utils\WxpayV2 as wxpayApi;
 use think\db\exception\DbException;
@@ -789,6 +790,51 @@ class User extends BaseController
 //        return app('json')->success(['img'=>$img_url['url']]);
 //    }
     
+    /**
+     * 七牛上传图片
+     * @param Request $request
+     */
+    public function qiniuUpload(Request $request)
+    {
+        $file = $request->file('file');
+        if (empty($file)) {
+            return app('json')->fail("未上传文件");
+        }
+        $rootTmp    = config('filesystem.disks.local.root') . '/' . \think\facade\Filesystem::putFile('tmp', $file);
+        $image_size = @getimagesize($rootTmp);
+        if ($image_size[0] > 1000) {
+            $imgS = Image::open($rootTmp);
+            $imgS->thumb(1000, $image_size[1]);
+            $imgS->save($rootTmp);
+        } else {
+            if ($image_size[1] > 1000) {
+                $imgS = Image::open($rootTmp);
+                $imgS->thumb($image_size[0], 1000);
+                $imgS->save($rootTmp);
+            }
+        }
+        $qiniu   = new  Qiniu;
+        $img_url = $qiniu->updateFile('img', $rootTmp, $rootTmp);
+        if (empty($img_url['url'])) {
+            return app('json')->fail($qiniu->getError());
+        }
+        @unlink($rootTmp);
+        return app('json')->success(['img' => $img_url['url']]);
+    }
+    
+    /**
+     * 七牛上传token
+     * @param Request $request
+     */
+    public function qiniuUploadToken(Request $request){
+        [$bucket] = UtilService::getMore([
+            ['bucket', ''],
+        ], $request,true);
+        $qiniu   = new  Qiniu;
+        $token = $qiniu->createUploadToken($bucket);
+        return app('json')->success(['token' => $token]);
+    }
+    
    
     
     

+ 3 - 0
app/api/route/user.php

@@ -43,6 +43,9 @@ Route::group('user', function () {
     Route::rule('upload', 'User/upload');
     //视频上传
     Route::rule('uploadVideo', 'User/uploadVideo');
+    //七牛图片上传token
+    Route::rule('qiniuUploadToken', 'User/qiniuUploadToken');
+    
 
     
 })->middleware([

+ 18 - 0
config/qiniu.php

@@ -0,0 +1,18 @@
+<?php
+// +----------------------------------------------------------------------
+// | [ WE CAN DO IT MORE SIMPLE  ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2018-2020 rights reserved.
+// +----------------------------------------------------------------------
+// | Author: TABLE ME
+// +----------------------------------------------------------------------
+// | Date: 2020-08-31 15:20
+// +----------------------------------------------------------------------
+
+return [
+    'accessKeyId'       => 'GgzwuoKNTJWzBcwQ6Y0-vSp-pQTit',
+    'accessKeySecret'   => 'lELF5Ak1l0PEZvYjJc2YrgGWME-zlkZYY0A5C',
+    'endpoint'          => 'http://media.test.com/',
+    'bucket'            => 'test',
+    'bingW'             => true //如果绑定域名请设置Ture,如果没有请设置flase
+];

+ 495 - 0
library/utils/Qiniu.php

@@ -0,0 +1,495 @@
+<?php
+declare (strict_types=1);
+namespace library\utils;
+// +----------------------------------------------------------------------
+// | [ WE CAN DO IT MORE SIMPLE  ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2018-2020 rights reserved.
+// +----------------------------------------------------------------------
+// | Author: TABLE ME
+// +----------------------------------------------------------------------
+// | Date: 2020-08-31 15:18
+// +----------------------------------------------------------------------
+
+use Qiniu\Storage\UploadManager;
+use Qiniu\Storage\ArgusManager;
+use Qiniu\Storage\BucketManager;
+use Qiniu\Config as ServerConfig;
+
+class Qiniu {
+
+    private $qiniu = null;
+
+    public $config = array();
+
+    private $error = null;
+
+
+    /**
+     * 基本配置
+     * @param type $conf
+     * <p>
+     * array(
+     * 'accessKeyId'     => '',
+     * 'accessKeySecret' => '',
+     * 'endpoint'        => '',
+     * 'bucket'          => ''
+    );
+     * </p>
+     */
+    public function __construct($conf = array()) {
+        $c = config('qiniu');
+        foreach($c as $k=>$v){
+            if(isset($conf[$k])){
+                $this->config[$k] = $conf[$k];
+            } else {
+                $this->config[$k] = $v;
+            }
+        }
+
+        try {
+            $this->qiniu = new \Qiniu\Auth($this->config['accessKeyId'],$this->config['accessKeySecret']);
+        } catch (Exception $ex) {
+            $this->error = $ex->getMessage();
+            $this->log_result($ex->getMessage(), $this->daatadir.'error');
+        }
+
+    }
+    /**
+     * 图片删除
+     * @param type $img
+     */
+    public function imageDelete($img=""){
+        $img = str_replace("http://media.lolitaapp.com/","", $img);
+        $img = str_replace("https://media.lolitaapp.com/","", $img);
+        $config = new ServerConfig();
+        $bucketManager = new BucketManager($this->qiniu, $config);
+        $err = $bucketManager->delete($this->config['bucket'], $img);
+        
+    }
+    /**
+     * 图片鉴别
+     * @param type $img
+     * @param type $scenes pulp黄,terror暴,politician敏感,ads广告
+     * @return boolean
+     */
+    public function imageCensor($img="",$scenes=["pulp","terror","politician","ads"]){
+        try{
+            $config = new ServerConfig();
+            $argusManager = new ArgusManager($this->qiniu, $config);
+            $body=[
+                "data"=>[
+                    "uri"=>$img
+                ],
+                "params"=>[
+                    "scenes"=>$scenes
+                ]
+            ];
+            list($ret, $err) = $argusManager->censorImage(json_encode($body));
+            if ($err !== null) {
+                return false;
+            } else {
+                if(empty($ret) || empty($ret["code"]) || empty($ret["result"]) || $ret["code"]!=200){
+                    return false;
+                }
+                return $ret["result"];
+            }
+        }catch(Exception $ex){
+            return false;
+        }
+        
+    }
+    /**
+     * 获取qiuniu信息
+     * @global \OSS\OssClient $_A
+     */
+    public  function getqiniu() {
+        return $this->qiniu;
+    }
+
+    /**
+     * 获取bucket信息
+     * @return type
+     */
+    public function getBucketName(){
+        return $this->config['bucket'];
+    }
+
+
+    /**
+     * 获取预地址
+     */
+    public function getYuFile($path,$exp = 'jpg'){
+        $object = $this->mk_str().'.'.$exp;
+        $os_path = $path.DIRECTORY_SEPARATOR.date('Y').DIRECTORY_SEPARATOR.date('m').DIRECTORY_SEPARATOR.date('d').DIRECTORY_SEPARATOR;
+        return $os_path.$object;
+    }
+
+
+    /**
+     * 发送短信
+     * @param $template_id
+     * @param $mobiles
+     * @param null $parameters
+     */
+    public function sendSms($mobiles,$template_id,$parameters = null){
+        $sms = new \Qiniu\Sms\Sms($this->qiniu);
+        $rAr = [];
+        try {
+            $d = $sms->sendMessage($template_id, [$mobiles], $parameters);
+            if($d[1] instanceof Qiniu\Http\Error) {
+                $dObj = $d[1];
+                $rAr['Code'] = 'error';
+                $rAr['Message'] = $dObj->message();
+            } else {
+                $rAr['Code'] = 'OK';
+                $rAr['Message'] = '';
+            }
+        }catch (\Exception $e) {
+            $rAr['Code'] = 'error';
+            $rAr['Message'] = $e;
+        }
+        return $rAr;
+    }
+
+    /**
+     * 上传文件 | 内容写入
+     * @param type $path 目录
+     * @param type $content 内容
+     * @param type $type 后缀
+     */
+    public function updateFileContent($path,$content,$type = 'jpg',$file = ''){
+        $type = strtolower($type);
+        if($type == 'jpeg') $type = 'jpg';
+        $object = '';
+        if(empty($file)) {
+            $object = $this->mk_str().'.'.$type;
+            $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
+        } else {
+            $os_path = $path.'/' . $file;
+        }
+
+
+
+        $token = $this->qiniu->uploadToken($this->config['bucket']);
+
+        $uploadMgr = new \Qiniu\Storage\UploadManager();
+
+        list($ret, $err) = $uploadMgr->put($token, $os_path.$object, $content);
+
+
+        $r['status'] = 0;
+        if ($err !== null) {
+            $r['status'] = 0;
+            $this->error = $err;
+        } else{
+            $r['status'] = 1;
+            $r['url'] = $this->config['endpoint'].$ret['key'];
+        }
+        return $r;
+    }
+    /**
+     * 生成上传token
+     */
+    public function createUploadToken($bucket=null){
+        $bucket = empty($bucket) ? $this->config['bucket'] : $bucket;
+        $token = $this->qiniu->uploadToken($bucket);
+        return $token;
+    }
+
+    /**
+     * 上传文件
+     * @param type $path 目录
+     * @param type $name 上传文件名称【后缀记录】
+     * @param type $files 上传实际文件
+     * @param type $ext 限制后缀名
+     * @return boolean
+     */
+    public function updateFile($path,$name,$files,$ext = array('jpg','png','gif','webp')) {
+        if (empty($files)) {
+            $this->error = '没有上传的文件!';
+            return false;
+        }
+
+        $file['ext']    = strtolower(pathinfo($name, PATHINFO_EXTENSION));
+
+        if($file['ext'] == 'jpeg'){
+            $file['ext'] = 'jpg';
+        }
+
+
+        if(!in_array($file['ext'], $ext)){
+            $this->error = '上传格式发生错误!';
+            return false;
+        }
+
+        if(!file_exists($files)){
+            $this->error = '上传文件不存在!';
+            return false;
+        }
+
+
+
+        $object = $this->mk_str().'.'.$file['ext'];
+
+        $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
+
+        $token = $this->qiniu->uploadToken($this->config['bucket']);
+
+        $uploadMgr = new \Qiniu\Storage\UploadManager();
+
+        list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
+
+
+        $r['status'] = 0;
+        if ($err !== null) {
+            $r['status'] = 0;
+            $this->error = $err;
+        } else{
+            $r['status'] = 1;
+            $r['key'] = $ret['key'];
+            $r['url'] = $this->config['endpoint'].$ret['key'];
+        }
+        return $r;
+    }
+
+
+
+    /**
+     * 上传文件[带压缩]
+     * @param $path 保存带头前缀
+     * @param $name 文件名
+     * @param $files 上传文件
+     * @param int $type 压缩情况
+     * @param array $ext 上传后缀
+     */
+    public function updateFileThum($path,$name,$files,$type = 0,$ext = array('jpg','png','gif','zip','mp4','webp')){
+        $thum = array(
+            0 => '320',
+            1 => '100',
+            2 => '800'
+        );
+
+        if (empty($files)) {
+            $this->error = '没有上传的文件!';
+            return false;
+        }
+
+        $file['ext']    = strtolower(pathinfo($name, PATHINFO_EXTENSION));
+
+
+        if(!in_array($file['ext'], $ext)){
+            $this->error = '上传格式发生错误!';
+            return false;
+        }
+
+        if(!file_exists($files)){
+            $this->error = '上传文件不存在!';
+            return false;
+        }
+        //文件名
+        $file_str = $this->mk_str();
+
+        $object = $file_str.'.'.$file['ext'];
+
+        $thum_obj = $file_str .'_' . $thum[$type] . '.'.$file['ext'];
+
+        $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
+
+        $token = $this->qiniu->uploadToken($this->config['bucket']);
+
+        $uploadMgr = new UploadManager();
+
+        list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
+
+        if (empty($err)) {
+            $jh_ar = array('jpg','png','gif');
+            if(in_array($file['ext'], $jh_ar)) {
+                //进行持久化操作
+                $this->Fop($os_path . $object, $os_path . $thum_obj, $thum[$type]);
+            }
+            $r['status'] = 1;
+            $r['url'] = $this->config['endpoint'] . $ret['key'];
+        } else{
+            $r['status'] = 0;
+            $this->error = $err;
+        }
+
+        return $r;
+    }
+
+    /**
+     * 上传文件【】
+     **/
+    public function TupdateFile($newFile,$name,$files){
+        if (empty($files)) {
+            $this->error = '没有上传的文件!';
+            return false;
+        }
+
+        $token = $this->qiniu->uploadToken($this->config['bucket']);
+        $uploadMgr = new \Qiniu\Storage\UploadManager();
+        list($ret, $err) = $uploadMgr->putFile($token, $newFile, $files);
+
+        $r['status'] = 0;
+        if ($err !== null) {
+            $r['status'] = 0;
+            $this->error = $err;
+        } else{
+            $r['status'] = 1;
+            $r['url'] = $this->config['endpoint'].$ret['key'];
+        }
+        return $r;
+    }
+    /**
+     * 带路径文件夹文件上传
+     * @param type $path
+     * @param type $name
+     * @param type $files
+     * @param type $ext
+     * @return boolean|string
+     */
+    public function pathFileUpload($path,$name,$files,$ext = array('svga')){
+        if (empty($files)) {
+            $this->error = '没有上传的文件!';
+            return false;
+        }
+        $file['ext']    = strtolower(pathinfo($name, PATHINFO_EXTENSION));
+        if(!in_array($file['ext'], $ext)){
+            $this->error = '上传格式发生错误!';
+            return false;
+        }
+        if(!file_exists($files)){
+            $this->error = '上传文件不存在!';
+            return false;
+        }
+        $object = $this->mk_str().'.'.$file['ext'];
+        $os_path = $path.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
+        $token = $this->qiniu->uploadToken($this->config['bucket']);
+        $uploadMgr = new UploadManager();
+        list($ret, $err) = $uploadMgr->putFile($token, $os_path.$object, $files);
+        $r = [];
+        $r['status'] = 0;
+        if ($err !== null) {
+            $r['status'] = 0;
+            $this->error = $err;
+        } else{
+            $r['status'] = 1;
+            $r['url'] = $this->config['endpoint'].$ret['key'];
+        }
+        return $r;
+    }
+
+
+    /**
+     * 持久化操作
+     * @param $key
+     * @param $savekey
+     * @param $daima
+     */
+    public function Fop($key,$savekey,$type = 320){
+
+        if($type == 320){
+            $daima = 'imageView2/1/w/320/h/320/q/100|imageslim';
+        }
+        $pipeline = 'mg_51';
+        $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.mmgg.com/api", false);
+        $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
+        list($id, $err) = $pfop->execute($key, $fops);
+    }
+
+    /**
+     * @param $obj
+     */
+    public function watermark($obj){
+        $jh_ar = array('jpg','png','gif');
+        $expAr = explode('.',$obj);
+        $img = '';
+        $exp = end($expAr);
+        if(in_array($exp, $jh_ar)) {
+            $fileAr = explode('/',$obj);
+            $fileName = end($fileAr);
+            $imgPath = str_replace($fileName,'',$obj);
+            $img = md5('watermark'.$fileName).'.'.$exp;
+            //进行持久化操作
+            $this->FopSyin($obj, $imgPath.$img);
+        }
+        return $imgPath.$img;
+    }
+
+    //
+
+    /**
+     * 持久化操作
+     * @param $key
+     * @param $savekey
+     * @param $daima
+     */
+    public function FopSyin($key,$savekey){
+        $daima = "imageView2/0/interlace/1/q/100|watermark/1/image/aHR0cHM6Ly9pbWcuZ3VhaW1laWRhLmNvbS9zeWluX2xvZ2luLnBuZw==/dissolve/64/gravity/SouthEast/dx/10/dy/10|imageslim";
+        $pipeline = 'task0013';
+        $pfop = new \Qiniu\Processing\PersistentFop($this->qiniu,$this->config['bucket'],$pipeline,"http://www.guaimeida.com/", false);
+        $fops = $daima."|saveas/" . \Qiniu\base64_urlSafeEncode($this->config['bucket'] . ":".$savekey);
+        list($id, $err) = $pfop->execute($key, $fops);
+        var_dump($err);
+    }
+
+
+    private function mk_str(){
+        $d = sha1((string)uniqid((string)mt_rand(),true));
+        $str = '';
+        for($i = 0;$i<strlen($d);$i++){
+            $v = $d[$i];
+            if(ctype_alpha($d)){
+                $rnd_true = rand(0, 1);
+
+                if($rnd_true){
+                    $v = strtolower($d[$i]);
+                } else {
+                    $v = strtoupper($d[$i]);
+                }
+            }
+            $str .= $v;
+        }
+        //防止内存加载不变
+        $time = substr((string)time(),0,-6);
+        $tarray = array('A','B','C','D','E','F','G','H','M','T');
+        $vstr = '';
+        for($i = 0;$i<strlen($time);$i++){
+            if($time[$i]  % 2 == 0){
+                $vstr .= 'X';
+            } else {
+                $vstr .= $tarray[rand(0, 9)];
+            }
+        }
+
+        $str .= '-'.$vstr;
+
+        return $str;
+    }
+
+
+    /**
+     * 获取错误
+     * @return type
+     */
+    public function getError(){
+        return $this->error;
+    }
+
+
+    /**
+     * 写入日志
+     * @param type $word
+     * @param type $name
+     */
+    private function log_result($word, $name) {
+
+        $fp = fopen($name . "_log.txt", "a");
+        flock($fp, LOCK_EX);
+        fwrite($fp, $word . "\n");
+        flock($fp, LOCK_UN);
+        fclose($fp);
+    }
+
+}