canyin-project/ybcy/controllers/common/FileController.php

280 lines
11 KiB
PHP
Raw Permalink Normal View History

2024-11-01 16:07:54 +08:00
<?php
/**
* 云贝餐饮连锁V2版权说明
******************************************************************
《云贝餐饮连锁V2》是武汉云贝网络科技有限公司历经2余年时间独立创作开发而成且在不断的进行升级开发中《云贝餐饮连锁V2 》的源代码、布局、界面设计、电子文档等均已提交国家版权局登记备案,并已获得著作权审批 和销售许可 (软著登字第 6839058号 登记号 2021SR0114741
官方网址为www.b-ke.cn
官方联系方式153-0719-3890(王经理)
******************************************************************
注意:本软件非开源系统,版权归武汉云贝网络科技有限公司依法所有,并受到法律的严格保护;未经过我司授权,不得擅自二开、破解、倒卖等任何的侵权、盗版行为均将追究其法律责任。
*/
namespace app\controllers\common;
use Illuminate\Support\Facades\DB;
use Yii;
use app\models\common\File;
use app\models\common\Config;
use crazyfd\qiniu\Qiniu;
use OSS\OssClient;
use xplqcloud\cos\Cos;
class FileController extends CommonController{
public $enableCsrfValidation = false;
//图片上传
public function actionUpload() {
$config=Config::getSystemSet('storage',$this->wqData['uniacid']);
//var_dump($this->wqData['uniacid']);die;
if(!$config){
$config=Config::getSystemSet('storage',0);
}
// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png","jfif");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp); // 获取文件后缀名
if (!in_array($extension, $allowedExts)){
return $this->result(2, '文件类型格式不允许上传!');
}
if($config['type']==1){
$fname=File::qiniuUpload('file',$this->wqData['module'],$this->wqData['uniacid'],$config);
}elseif($config['type']==2){
$fname=File::aliUpload('file',$this->wqData['module'],$this->wqData['uniacid'],$config);
}elseif($config['type']==3){
$fname=File::txyUpload('file',$this->wqData['module'],$this->wqData['uniacid'],$config);
}else{
$fname=File::channelUploadImage('file','',$this->wqData['module'],$this->wqData['uniacid']);
}
$result=axios_request();
$data['categoryId'] = $result['category'] ?: 0;
$data['url'] = $fname;
$data['name'] = $_FILES['file']['name'];
$data['storeId'] = $result['storeId'] ?: 0;
$data['createdAt'] = time();
$data['deleteAt'] = 0;
$data['uniacid'] = $this->wqData['uniacid'];
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_core_file}}', $data)->execute();
if ($res) {
return $this->result(1, '成功!',$res);
} else {
return $this->result(2, '失败!');
}
}
//获取拖拽图片分类
public function actionGetCategory() {
$storeId=Yii::$app->request->get('storeId');
$storeId = $storeId ?: 0;
$res=(new \yii\db\Query())
->select(['id', 'name','icon'])
->from('{{%ybwm_core_category}}')
->where('uniacid=:uniacid AND storeId=:storeId AND item=4 AND deleteAt=0',[':storeId'=>$storeId,':uniacid'=>$this->wqData['uniacid']])->all();
return $this->result(1, '操作成功!', $res);
}
//移动图片
public function actionMoveFile() {
$result=axios_request();
if(is_array($result['id'])){
$idArr=$result['id'];
}else{
$idArr=[$result['id']];
}
$res = Yii::$app->db->createCommand()->update('{{%ybwm_core_file}}', ['categoryId'=>$result['typeId']], ['in','id',$idArr])->execute();
if($res){
return $this->result(1, '成功!');
}else{
return $this->result(2, '失败!');
}
}
//获取图片
public function actionGetPicture() {
$typeId=Yii::$app->request->get('category');
$keyWords=Yii::$app->request->get('keywords');
$page=Yii::$app->request->get('page')?:1;
$size=Yii::$app->request->get('size')?:30;
$storeId=Yii::$app->request->get('storeId')?:0;
$table=(new \yii\db\Query())
->select(['id', 'url','name','fileType'])
->from('{{%ybwm_core_file}}')
->where('uniacid=:uniacid AND deleteAt=0',[':uniacid'=>$this->wqData['uniacid']]);
if($storeId){
$table->andwhere('storeId=:storeId',[':storeId'=>$storeId]);
}
if($typeId){
$table->andwhere('categoryId=:categoryId',['categoryId'=>$typeId]);
}
if($keyWords){
$table->andwhere(['like', 'name', $keyWords]);
}
//var_dump(ddSql($table->offset(($page - 1) * $size)->limit($size)->orderby('id desc')));die;
$count=$table->count();
$res=$table->offset(($page - 1) * $size)->limit($size)->orderby('id desc')->all();
echo json_encode(['code'=>1,'msg'=>'成功','data'=>$res,'count'=>intval($count),'type'=>1]);die;
}
//删除图片分类
public function actionDelPictureCategory() {
$result=axios_request();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_core_category}}', ['deleteAt'=>time()], 'id=:id', ['id' =>$result['id']])->execute();
if ($res) {
return $this->result(1, '成功!');
} else {
return $this->result(2, '失败!');
}
}
//删除图片
public function actionDelPicture() {
$result=axios_request();
if(is_array($result['id'])){
$idArr=$result['id'];
}else{
$idArr=[$result['id']];
}
$res = Yii::$app->db->createCommand()->update('{{%ybwm_core_file}}', ['deleteAt'=>time()], ['in','id',$idArr])->execute();
if ($res) {
return $this->result(1, '成功!');
} else {
return $this->result(2, '失败!');
}
}
//添加分组
public function actionSaveCategory() {
$post=axios_request();
$post['storeId']=$post['storeId']?:0;
if(!$post['name']){
return $this->result(2,'分类名称不能为空!');
}
$info=(new \yii\db\Query())
->from('{{%ybwm_core_category}}')
->where('uniacid=:uniacid AND storeId=:storeId AND item=4 AND deleteAt=0 AND name=:name',[':name'=>$post['name'],':storeId'=>$post['storeId'],':uniacid'=>$this->wqData['uniacid']])->one();
if($info){
return $this->result(2,'该名称已存在!');
}
$data['name']=$post['name'];
$data['storeId']=$post['storeId'];
$data['item']=4;
$data['uniacid']=$this->wqData['uniacid'];
$data['changeAt']=time();
if ($post['id']) {
$data['changeAt']=time();
$res = Yii::$app->db->createCommand()->update('{{%ybwm_core_category}}', $data, 'id=:id', ['id' =>$post['id']])->execute();
} else {
$data['createdAt']=time();
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_core_category}}', $data)->execute();
}
if($res){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
//获取图片回收站列表
public function actionRecyclePicture(){
$typeId=Yii::$app->request->get('category');
$keyWords=Yii::$app->request->get('keywords');
$page=Yii::$app->request->get('page')?:1;
$size=Yii::$app->request->get('size')?:30;
$storeId=Yii::$app->request->get('storeId')?:0;
$table=(new \yii\db\Query())
->select(['id', 'url','name','deleteAt'])
->from('{{%ybwm_core_file}}')
->where('uniacid=:uniacid AND deleteAt>0',[':uniacid'=>$this->wqData['uniacid']]);
if($storeId){
$table->andwhere('storeId=:storeId',[':storeId'=>$storeId]);
}
if($typeId){
$table->andwhere('categoryId=:categoryId',['categoryId'=>$typeId]);
}
if($keyWords){
$table->andwhere(['like', 'name', $keyWords]);
}
$count=$table->count();
$res=$table->offset(($page - 1) * $size)->limit($size)->orderby('id desc')->all();
echo json_encode(['code'=>1,'msg'=>'成功','data'=>$res,'count'=>intval($count),'type'=>2]);die;
}
//删除回收站的图片
public function actionDelRecyclePicture(){
$result=axios_request();
$id=$result['id'];
$row=(new \yii\db\Query())->select(['id', 'url','name','storeId','uniacid'])->from('{{%ybwm_core_file}}')->where(['id'=>$id])->one();
$domain=Yii::$app->request->hostInfo;
if(strpos($row['url'],$domain) !==false){
unlink('.'.strstr($row['url'],"/web"));
}else{
$storageConfig=(new \yii\db\Query())->from('{{%ybwm_core_system}}')->where(['uniacid'=>$row['uniacid'],'ident'=>'storage'])->one();
if(!$storageConfig){
$storageConfig=(new \yii\db\Query())->from('{{%ybwm_core_system}}')->where(['uniacid'=>0,'ident'=>'storage'])->one();
}
$config=json_decode($storageConfig['data'],true);
//dd($config);die;
$qnUrl=$config['qn_url'];
$aliUrl=$config['aliyuncs_url'];
$txUrl=$config['xplqcloud_url'];
if(strpos($row['url'],$qnUrl) !==false){
$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);
$array=explode($qnUrl.'/', $row['url']);
$url=$array[1];
try{
$qiniu->delete($url, $bucket);
} catch(\Exception $e) {
}
}
if(strpos($row['url'],$aliUrl) !==false){
$accessKeyId = $config['aliyuncs_accesskey'];
$accessKeySecret =$config['aliyuncs_secret'];
$endpoint = $config['aliyuncs_endpoint'];
$bucket=$config['aliyuncs_bucket'];
try{
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$array=explode($aliUrl.'/', $row['url']);
$url=$array[1];
$ossClient->deleteObject($bucket,$url);
} catch(OssException $e) {
}
}
if(strpos($row['url'],$txUrl) !==false){
$array=explode($txUrl.'/', $row['url']);
$url=$array[1];
$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
];
try{
$cos=new Cos($data);
$cos->delFile($url);
} catch(OssException $e) {
}
}
}
Yii::$app->db->createCommand()->delete('{{%ybwm_core_file}}','id=:id', ['id' =>$id])->execute();
return $this->result(1, '成功!');
}
}