文件
mashangyoujiu/Application/Admin/Controller/ImageGalleryController.class.php

386 行
15 KiB
PHP
可执行文件

<?php
namespace Admin\Controller;
use Common\Model\ImageGalleryModel;
use Common\Model\RetailGoodsModel;
use Common\Model\RetailMgoodsModel;
use Think\Controller;
//[1.8.50]
/**
* 合伙人图片管理
* Class ImageGalleryController
* @package Admin\Controller
*/
class ImageGalleryController extends BaseController
{
public function index()
{
$from_class1 = array(
array('id' => 1, 'name' => '合伙人'),
array('id' => 2, 'name' => '代理商'),
array('id' => 3, 'name' => '商户'),
array('id' => 4, 'name' => '门店'),
array('id' => 5, 'name' => '导购员'),
array('id' => 0, 'name' => '其他'),
);
array_unshift($from_class1, array('id' => -1, 'name' => '所有'));
$this->assign('from_class1', json_encode($from_class1));
$arr = array(
'uploadImage' => '/index.php/Admin/ImageGallery/uploadFile',
'delImage' => '/index.php/Admin/ImageGallery/deleteMoreFile',
);
$authArr = checkFunction($arr);
$this->assign('authArr', $authArr);
$storage_type = D('RetailAdmin')->getFindField(array('id' => session('admin_userinfo')['id']), 'storage_type');
$this->assign('storage_type', intval($storage_type));
$this->display();
}
public function getAllImages()
{
$page = I('post.page');
$limit = I('post.limit');
$where['storage_type'] = I('post.type');
$from_type_id1 = I('post.selectTypeP');
$from_type_id2 = I('post.selectTypeC');
if ($from_type_id2 > 0) {
$where['from_type_id'] = $from_type_id2;
} else if ($from_type_id1 > 0) {
$data = M('upload_image_class')->where(array('user_type' => $from_type_id1))->getField('id', true);
$where['from_type_id'] = array('in', $data);
}
$ImageGalleryModel = new ImageGalleryModel();
$images = $ImageGalleryModel->getSelect($where, '', 'id desc', $page, $limit);
if ($images) {
foreach ($images as &$val) {
if ($val['add_time']) {
$val['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
}
if ($val['size']) {
$val['size'] = getFileSize($val['size']);
}
$val['checked'] = false;
}
$res['list'] = $images;
$res['status'] = 1;
$res['msg'] = '获取成功';
} else {
$res['list'] = array();
$res['status'] = 1;
$res['msg'] = '暂无数据';
}
if (empty($where['from_type_id'])) {
$res['count'] = $ImageGalleryModel->getCount($where);
}
$this->ajaxReturn($res);
}
public function deleteMoreFile()
{
$id = I('post.id');
$id = explode(',', $id);
$images = M('image_gallery')->where(array('id' => array('in', $id)))->select();
$re = deleteUploadFile($images, $id);
$res['data'] = $re;
if ($re['status'] == 1) {
$res['status'] = 1;
$res['msg'] = '删除成功';
} else {
$res['status'] = 0;
$res['msg'] = '删除失败';
}
$this->ajaxReturn($res);
}
/**
* 下载文件并返回存储地址
* @param $url
* @param $host
* @return mixed|string
*/
protected function getDownFilePath($url, $host)
{
$host = explode(',', $host);
$path = str_replace($host, '', $url);
if ($path == $url) {
return false;
}
$path = strtolower(substr($path, 0, 6)) === 'upload' ? ucfirst($path) : 'Upload/' . $path;
$arr = explode('/', $path);
$name = $arr[count($arr) - 1];//文件名称
unset($arr[count($arr) - 1]);
$catalog = implode('/', $arr);//存储目录
//判断文件目录是否存在
if (!is_dir($catalog)) {
$catalog_t = '.';
foreach ($arr as $value) {
$catalog_t .= '/' . $value;
!is_dir($catalog_t);
mkdir($catalog_t, 0777);
}
}
$path = './' . $path;//文件最终地址
$data = array(
'catalog' => $catalog,
'name' => $name,
'path' => $path,
);
$file = file_get_contents($url);
$res = file_put_contents($path, $file);
if ($res) {
return $data;
} else {
return false;
}
}
public function transferToOut()
{
$transfer_from = I('post.transfer_from');//迁移类型
$transfer_to = I('post.transfer_to');//迁移至???
$del_file_state = I('post.del_file_state');//迁移后是否删除源文件
$url_list = I('post.url_list');//迁移后是否删除源文件
if (!in_array($transfer_from, array('1', '2', '3', '4')) || !in_array($transfer_to, array('1', '2', '3', '4')) || !in_array($del_file_state, array('1', '2')) || $transfer_from == $transfer_to) {
$res['status'] = 0;
$res['msg'] = '请完善请求参数';
$this->ajaxReturn($res);
}
$config = M('upload_config')->where(array('id' => 1))->find();
$qiniu_access_key = $config['qiniu_access_key'];
$qiniu_secret_key = $config['qiniu_secret_key'];
$qiniu_outer_chain_url = $config['qiniu_outer_chain_url'];
$qiniu_storage_space = $config['qiniu_storage_space'];
$tencent_secret_id = $config['tencent_secret_id'];
$tencent_secret_key = $config['tencent_secret_key'];
$tencent_outer_chain_url = $config['tencent_outer_chain_url'];
$tencent_storage_space = $config['tencent_storage_space'];
$tencent_region = $config['tencent_region'];
$oss_access_key_id = $config['oss_access_key_id'];
$oss_access_key_secret = $config['oss_access_key_secret'];
$oss_outer_chain_url = $config['oss_outer_chain_url'];
$oss_storage_space = $config['oss_storage_space'];
$oss_region = $config['oss_region'];
if ($transfer_to == 2 && (empty($qiniu_access_key) || empty($qiniu_secret_key) || empty($qiniu_outer_chain_url) || empty($qiniu_storage_space))) {
$res['status'] = 0;
$res['msg'] = '您未完善七牛云对象储存配置信息,请先前往“系统设置->系统设置->图片存储设置”处配置';
$this->ajaxReturn($res);
} else if ($transfer_to == 3 && (empty($tencent_secret_id) || empty($tencent_secret_key) || empty($tencent_outer_chain_url) || empty($tencent_storage_space) || empty($tencent_region))) {
$res['status'] = 0;
$res['msg'] = '您未完善腾讯云对象储存配置信息,请先前往“系统设置->系统设置->图片存储设置”处配置';
$this->ajaxReturn($res);
} else if ($transfer_to == 4 && (empty($oss_access_key_id) || empty($oss_access_key_secret) || empty($oss_outer_chain_url) || empty($oss_storage_space) || empty($oss_region))) {
$res['status'] = 0;
$res['msg'] = '您未完善阿里云对象储存配置信息,请先前往“系统设置->系统设置->图片存储设置”处配置';
$this->ajaxReturn($res);
}
$ImageGalleryModel = new ImageGalleryModel();
$map = array(
'storage_type' => $transfer_from,
array(
array(
'source_type' => 1,
),
array(
'source_type' => 2,
'from_type_id' => array('in', '17,27'),
),
'_logic' => 'OR'
),
);
$fields = array(
'id', // bigint(19) NOT NULL AUTO_INCREMENT,
'path', // varchar(255) DEFAULT NULL COMMENT '图片预览路径',
'catalog', // varchar(255) DEFAULT NULL COMMENT '目录',
'name', // varchar(100) DEFAULT NULL COMMENT '图片名称',
'size', // varchar(20) DEFAULT '0' COMMENT '文件大小',
);
$limit = 20;
$list = $ImageGalleryModel->getSelect($map, $fields, 'id desc', 1, $limit);
$GoodsModel = new RetailGoodsModel();
$MGoodsModel = new RetailMgoodsModel();
foreach ($list as $value) {
$m_goods = array();
$s_goods = array();
if ($value['source_type'] == 2) {
$map = array(
'details' => array('like', "%{$value['path']}%"),
);
$m_goods = $MGoodsModel->getSelect($map, 'id,details');
$s_goods = $GoodsModel->getSelect($map, 'id,details');
}
$arr = explode('.', $value['name']);
$extension = $arr[count($arr) - 1];
$catalog = $value['catalog'];
$name = $value['name'];
$path = $value['path'];
//根据图片原来的存储获取迁移参数
switch ($transfer_from) {
case '1':
$path = '.' . $path;
if (!file_exists($path)) {
$ImageGalleryModel->doDelete(array('id' => $value['id']));
$res['status'] = 2;
$res['msg'] = '请继续迁移';
$this->ajaxReturn($res);
}
break;
case '2':
case '3':
case '4':
$old_url = $url_list ? $url_list : ($transfer_from == 2 ? $qiniu_outer_chain_url : ($transfer_from == 3 ? $tencent_outer_chain_url : $oss_outer_chain_url));
$down = $this->getDownFilePath($value['path'], $old_url);
if ($down) {
$path = $down['path'];
$name = $down['name'];
$catalog = $down['catalog'];
} else {
$res['status'] = 0;
$res['msg'] = '图片迁移失败';
$this->ajaxReturn($res);
}
break;
}
//开始迁移
switch ($transfer_to) {
case '1':
//本地
$upload = true;
break;
case '2':
//七牛云
$url = $qiniu_outer_chain_url;
$name = ($catalog ? ($catalog . '/') : '') . $name;
$catalog = '';
$upload = toUploadQiNiuYun($path, $extension, $config, $catalog, $name);
break;
case '3':
//腾讯云
$url = $tencent_outer_chain_url;
$upload = uploadFilesToTencent($path, $extension, $config, $catalog, $name);
break;
case '4':
//阿里云
$url = $oss_outer_chain_url;
$upload = uploadFilesToOSS($path, $extension, $config, $catalog, $name);
break;
}
$save_path = ($url ? $url : '/') . (($catalog ? ($catalog . '/') : '') . $name);
if ($upload) {
if (!empty($m_goods) || !empty($s_goods)) {
foreach ($m_goods as &$g) {
$details = str_replace($value['path'], $save_path, $g['details']);
$map = array('id' => $g['id']);
$data = array('details' => $details);
$MGoodsModel->updateOrder($map, $data);
}
foreach ($m_goods as &$g) {
$details = str_replace($value['path'], $save_path, $g['details']);
$map = array('id' => $g['id']);
$data = array('details' => $details);
$GoodsModel->updateOrder($map, $data);
}
}
$map = array(
'id' => $value['id'],
);
$save = array(
'storage_type' => $transfer_to,
'catalog' => $catalog,
'path' => $save_path,
'name' => $name,
);
$ImageGalleryModel->updateData($map, $save);
//删除文件
if ($del_file_state == 1 && $transfer_to != 1) {
if (is_readable('.' . $value['path'])) {
delLocalFile('.' . $value['path']);
}
} else if ($transfer_to != 1) {
if (is_readable($path)) {
delLocalFile($path);
}
}
}
}
if ($del_file_state == 1) {
switch ($transfer_from) {
case '2':
//七牛云
if ($del_file_state == 1) {
delQiNiuFile($list, $config);
}
break;
case '3':
//腾讯云
if ($del_file_state == 1) {
delTencentFile($list, $config);
}
break;
case '4':
//阿里云
if ($del_file_state == 1) {
delOSSFile($list, $config);
}
}
}
$res['count'] = $limit;
if (empty($list) || count($list) < 20) {
$res['status'] = 1;
$res['msg'] = '图片迁移完毕';
} else {
$res['status'] = 2;
$res['msg'] = '请继续迁移';
}
$this->ajaxReturn($res);
}
public function uploadFile()
{
if (!$_FILES) {
$res['status'] = 0;
$res['msg'] = '未上传文件';
} else {
$user = session('jurisdiction_user');
$item = array(
'add_time' => time(),
'from_type_id' => 5,
'storage_type' => I('post.storage_type'),
'localUrl' => 'Admin/ImageGallery',
'user_id' => $user['id'],
'user_type' => 1,
);
$uploads = uploadFiles($item);
if (!empty($uploads['file'])) {
$res['status'] = 1;
$res['msg'] = '上传成功';
} else {
$res['status'] = 0;
$res['msg'] = $uploads['msg'];
}
}
$this->ajaxReturn($res);
}
public function getSelectTypeC()
{
$id = I('post.id');
if (!empty($id)) {
$list = M('upload_image_class')->where(array('user_type' => $id))->select();
array_unshift($list, array('id' => 0, 'name' => '所有', 'user_type' => $id));
$res['status'] = 1;
$res['msg'] = '查询成功';
$res['data'] = $list;
} else {
$res['status'] = 0;
$res['msg'] = '缺少参数';
}
$this->ajaxReturn($res);
}
}