283 lines
11 KiB
PHP
283 lines
11 KiB
PHP
<?php
|
|
namespace app\models\common;
|
|
use Yii;
|
|
use yii\base\Model;
|
|
use yii\db\ActiveRecord;
|
|
use yii\web\UploadedFile;
|
|
use crazyfd\qiniu\Qiniu;
|
|
use OSS\OssClient;
|
|
use xplqcloud\cos\Cos;
|
|
class File extends ActiveRecord{
|
|
//七牛云上传
|
|
public static function qiniuUpload($fileName,$module,$uniacid,$config,$type=1,$fileUrl=null){
|
|
$ak = $config['qn_accesskey'];
|
|
$sk = $config['qn_secretkey'];
|
|
$domain = $config['qn_url'];
|
|
$bucket = $config['qn_bucket'];
|
|
$zone = $config['qn_endpoint'];
|
|
$qiniu = new Qiniu($ak, $sk,$domain, $bucket,$zone);
|
|
if($type==2){
|
|
$qiniu->uploadFile($fileUrl,$fileName);
|
|
$url = $qiniu->getLink($fileName);
|
|
unlink($fileUrl);
|
|
return $url;
|
|
}
|
|
$key = $module."/".$uniacid."/".date('Y') . "/" . date('m') . "/" . date('d')."/".date("YmdHis") .rand(1111,9999);
|
|
$key .= strtolower(strrchr($_FILES[$fileName]['name'], '.'));
|
|
if(count($_FILES)>1){
|
|
$url=[];
|
|
foreach ($_FILES as $v){
|
|
$key='';
|
|
$key = $module."/".$uniacid."/".date('Y') . "/" . date('m') . "/" . date('d')."/".date("YmdHis") .rand(1111,9999);
|
|
$key .= strtolower(strrchr($_FILES[$fileName]['name'], '.'));
|
|
$url[]=$domain.'/'.$qiniu->uploadFile($v['tmp_name'],$key)['key'];
|
|
}
|
|
}else{
|
|
$qiniu->uploadFile($_FILES[$fileName]['tmp_name'],$key);
|
|
$url = $qiniu->getLink($key);
|
|
}
|
|
return $url;
|
|
}
|
|
|
|
//阿里云上传
|
|
public static function aliUpload($fileName,$module,$uniacid,$config,$type=1,$fileUrl=null){
|
|
$accessKeyId = $config['aliyuncs_accesskey'];
|
|
$accessKeySecret =$config['aliyuncs_secret'];
|
|
$endpoint = $config['aliyuncs_endpoint'];
|
|
$url=$config['aliyuncs_url'];
|
|
function base64url_encode($data)
|
|
{
|
|
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
|
}
|
|
try {
|
|
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
|
|
$bucket=$config['aliyuncs_bucket'];
|
|
if($type==2){
|
|
$ossClient->uploadFile($bucket, $fileName, $fileUrl);
|
|
unlink($fileUrl);
|
|
return $url."/".$fileName;
|
|
}
|
|
if(count($_FILES)>1){
|
|
$returnData=[];
|
|
foreach ($_FILES as $v){
|
|
$oss_path = $module."/".$uniacid."/".date('Y') . "/" . date('m') . "/" . date('d')."/".date("YmdHis") .rand(1111,9999);
|
|
$oss_path .= strtolower(strrchr($v['name'], '.'));
|
|
$ossClient->uploadFile($bucket, $oss_path, $v['tmp_name']);
|
|
$returnData[]=$url."/".$oss_path;
|
|
return $returnData;
|
|
}
|
|
}else{
|
|
$oss_path = $module."/".$uniacid."/".date('Y') . "/" . date('m') . "/" . date('d')."/".date("YmdHis") .rand(1111,9999);
|
|
$oss_path .= strtolower(strrchr($_FILES[$fileName]['name'], '.'));
|
|
$ossClient->uploadFile($bucket, $oss_path, $_FILES[$fileName]['tmp_name']);
|
|
return $url."/".$oss_path;
|
|
}
|
|
} catch (OssException $e) {
|
|
return $e->getMessage();
|
|
}
|
|
}
|
|
|
|
//腾讯云上传
|
|
public static function txyUpload($fileName,$module,$uniacid,$config,$type=1,$fileUrl=null){
|
|
$data=[
|
|
'app_id' =>$config['xplqcloud_appid'],
|
|
'secret_id' =>$config['xplqcloud_secretid'],
|
|
'secret_key' =>$config['xplqcloud_secretkey'],
|
|
'region' =>$config['xplqcloud_endpoint'],
|
|
'bucket'=>$config['xplqcloud_bucket'],
|
|
'timeout' =>60
|
|
];
|
|
$url=$config['xplqcloud_url'];
|
|
$cos=new Cos($data);
|
|
//创建文件夹
|
|
$path = $module."/".$uniacid."/".date('Y') . "/" . date('m') . "/" . date('d');
|
|
$cos->createFolder($data['bucket'], $path);
|
|
if($type==2){
|
|
$result = $cos->upload($fileUrl,$fileName);
|
|
if($result['code']==0){
|
|
unlink($fileUrl);
|
|
return $url."/".$fileName;
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
if(count($_FILES)>1){
|
|
$returnData=[];
|
|
foreach ($_FILES as $v){
|
|
$srcPath=$v['tmp_name'];//文件夹/文件名
|
|
$dstPath = $path.'/'. date("YmdHis") .rand(1111,9999).'.'.substr(strrchr($v['name'], '.'), 1);
|
|
$result = $cos->upload($srcPath,$dstPath);
|
|
if($result['code']==0){
|
|
$returnData[]=$url."/".$dstPath;
|
|
}
|
|
}
|
|
return $returnData;
|
|
}else{
|
|
$srcPath=$_FILES[$fileName]['tmp_name'];//文件夹/文件名
|
|
$dstPath = $path.'/'. date("YmdHis") .rand(1111,9999).'.'.substr(strrchr($_FILES[$fileName]['name'], '.'), 1);
|
|
$result = $cos->upload($srcPath,$dstPath);
|
|
if($result['code']==0){
|
|
return $url."/".$dstPath;
|
|
}else{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
//$pathname保存的路径 $filename提交的图片文件参数名
|
|
public static function channelUploadImage($filename,$pathname=null,$module,$uniacid){
|
|
//dd($_FILES);die;
|
|
$pathname=$pathname?:$module."/".$uniacid."/".date('Y') . "/" . date('m') . "/" . date('d');
|
|
$returnPath = '';
|
|
$path = 'web/static/'.$pathname;
|
|
if (!file_exists($path)) {
|
|
mkdir($path, 0777,true);
|
|
chmod($path, 0777);
|
|
}
|
|
if(count($_FILES)>1){
|
|
$returnData=[];
|
|
// dd($_FILES);die;
|
|
foreach ($_FILES as $k=>$v){
|
|
$name='';
|
|
$name=date("YmdHis") .rand(1111,9999). '.'.substr(strrchr($v['name'], '.'), 1);
|
|
$patch = $path . '/' . $name;
|
|
$tmp = UploadedFile::getInstanceByName($k);
|
|
if ($tmp) {
|
|
$tmp->saveAs($patch);
|
|
$returnPath='';
|
|
$returnPath .= $path . '/' . $name;
|
|
}
|
|
$sortUrl='';
|
|
if(Yii::$app->params['isDev']==true){
|
|
$sortUrl= Yii::$app->request->hostInfo.'/addons/'.$module;
|
|
// $sortUrl= '/addons/'.$module;
|
|
}else{
|
|
$sortUrl= Yii::$app->request->hostInfo;
|
|
// $sortUrl= '';
|
|
}
|
|
$returnData[]=$sortUrl.'/'.$returnPath;
|
|
|
|
}
|
|
return $returnData;
|
|
}else{
|
|
$name=date("YmdHis") .rand(1111,9999). '.'.substr(strrchr($_FILES[$filename]['name'], '.'), 1);
|
|
$patch = $path . '/' . $name;
|
|
$tmp = UploadedFile::getInstanceByName($filename);
|
|
if ($tmp) {
|
|
$tmp->saveAs($patch);
|
|
$returnPath .= $path . '/' . $name;
|
|
}
|
|
if(Yii::$app->params['isDev']==true){
|
|
return Yii::$app->request->hostInfo.'/addons/'.$module.'/'.$returnPath;
|
|
// return '/addons/'.$module.'/'.$returnPath;//图片保存不保存域名
|
|
}else{
|
|
return Yii::$app->request->hostInfo.'/'.$returnPath;
|
|
// return '/'.$returnPath;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//验证远程附件参数
|
|
public static function checkAttachmentSet($type,$data){
|
|
if($type==1){
|
|
$ak = $data['qn_accesskey'];
|
|
$sk = $data['qn_secretkey'];
|
|
$domain = $data['qn_url'];
|
|
$bucket =$data['qn_bucket'];
|
|
$zone = $data['qn_endpoint'];
|
|
try {
|
|
$qiniu = new Qiniu($ak, $sk,$domain, $bucket,$zone);
|
|
$url="https://img.b-ke.cn/yb_o2o/2020/11/26/6843761513730601.jpg";
|
|
$qiniu->uploadByUrl($url);
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
|
|
}
|
|
}
|
|
if($type==2){
|
|
$accessKeyId = $data['aliyuncs_accesskey'];
|
|
$aliyuncs_secret = $data['aliyuncs_secret'];
|
|
$aliyuncs_bucket = $data['aliyuncs_bucket'];
|
|
$aliyuncs_url = $data['aliyuncs_url'];
|
|
$aliyuncs_endpoint = $data['aliyuncs_endpoint'];
|
|
try {
|
|
$ossClient = new OssClient($accessKeyId, $aliyuncs_secret, $aliyuncs_endpoint);
|
|
$ossClient->listBuckets();
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
|
|
}
|
|
}
|
|
if($type==3){
|
|
$config=[
|
|
'app_id' =>$data['xplqcloud_appid'],
|
|
'secret_id' =>$data['xplqcloud_secretid'],
|
|
'secret_key' =>$data['xplqcloud_secretkey'],
|
|
'region' =>$data['xplqcloud_endpoint'],
|
|
'bucket'=>$data['xplqcloud_bucket'],
|
|
'timeout' =>60
|
|
];
|
|
|
|
try {
|
|
$cos=new Cos($config);
|
|
//创建文件夹
|
|
$path = 'test';
|
|
$result=$cos->createFolder($config['bucket'], $path);
|
|
// var_dump($result);die;
|
|
if($result['code']!=0){
|
|
//var_dump($result['message']);die;
|
|
if($result['message']=='ERROR_CMD_BUCKET_NOTEXIST'){
|
|
echo json_encode(['code'=>2,'msg'=>'BUCKET参数有误']);die;
|
|
}else{
|
|
return false;
|
|
}
|
|
|
|
}
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
//图片上传
|
|
public function uploadImage($uniacid,$storeId) {
|
|
$config=Config::getSystemSet('storage',$uniacid);
|
|
//var_dump($this->wqData['uniacid']);die;
|
|
if(!$config){
|
|
$config=Config::getSystemSet('storage',0);
|
|
}
|
|
$model=Yii::$app->params['channel_model_name'];
|
|
if($config['type']==1){
|
|
$fname=File::qiniuUpload('file',$model,$uniacid,$config);
|
|
}elseif($config['type']==2){
|
|
$fname=File::aliUpload('file',$model,$uniacid,$config);
|
|
}elseif($config['type']==3){
|
|
$fname=File::txyUpload('file',$model,$uniacid,$config);
|
|
}else{
|
|
$fname=File::channelUploadImage('file','',$model,$uniacid);
|
|
}
|
|
|
|
$result=axios_request();
|
|
$data['categoryId'] = $result['category'] ?: 0;
|
|
$data['url'] = $fname;
|
|
$data['name'] = $_FILES['file']['name'];
|
|
$data['storeId'] =$storeId ?: 0;
|
|
$data['createdAt'] = time();
|
|
$data['deleteAt'] = 0;
|
|
$data['uniacid'] =$uniacid;
|
|
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_core_file}}', $data)->execute();
|
|
$fileArr= (new \yii\db\Query())
|
|
->select('url')
|
|
->from('{{%ybwm_core_file}}')
|
|
->where(['id' => $res])
|
|
->one();
|
|
return $fileArr['url'];
|
|
}
|
|
|
|
|
|
}
|