mirror of
				https://gitee.com/zoujingli/ThinkAdmin.git
				synced 2025-11-04 05:32:09 +08:00 
			
		
		
		
	修改文件上传,增加文件管理
This commit is contained in:
		
							parent
							
								
									526c13e8ac
								
							
						
					
					
						commit
						bb3ab695a7
					
				@ -17,6 +17,8 @@
 | 
			
		||||
namespace app\admin\controller\api;
 | 
			
		||||
 | 
			
		||||
use think\admin\Controller;
 | 
			
		||||
use think\admin\model\SystemFile;
 | 
			
		||||
use think\admin\service\AdminService;
 | 
			
		||||
use think\admin\Storage;
 | 
			
		||||
use think\admin\storage\AliossStorage;
 | 
			
		||||
use think\admin\storage\LocalStorage;
 | 
			
		||||
@ -66,10 +68,21 @@ class Upload extends Controller
 | 
			
		||||
    {
 | 
			
		||||
        [$name, $safe] = [input('name'), $this->getSafe()];
 | 
			
		||||
        $data = ['uptype' => $this->getType(), 'safe' => intval($safe), 'key' => input('key')];
 | 
			
		||||
        $file = SystemFile::mk()->data($this->_vali([
 | 
			
		||||
            'path.value'   => $data['key'],
 | 
			
		||||
            'type.value'   => $this->getType(),
 | 
			
		||||
            'uuid.value'   => AdminService::instance()->getUserId(),
 | 
			
		||||
            'name.require' => '名称不能为空!',
 | 
			
		||||
            'hash.require' => '哈希不能为空!',
 | 
			
		||||
            'xext.require' => '后缀不能为空!',
 | 
			
		||||
            'size.require' => '大小不能为空!',
 | 
			
		||||
            'mime.require' => "类型不能为空!",
 | 
			
		||||
            'status.value' => 1
 | 
			
		||||
        ]));
 | 
			
		||||
        if ($info = Storage::instance($data['uptype'])->info($data['key'], $safe, $name)) {
 | 
			
		||||
            $data['url'] = $info['url'];
 | 
			
		||||
            $data['key'] = $info['key'];
 | 
			
		||||
            $this->success('文件已经上传', $data, 200);
 | 
			
		||||
            $file->save(['xurl' => $info['url'], 'is_fast' => 1, 'is_safe' => $data['safe']]);
 | 
			
		||||
            $extr = ['id' => $file->id ?? 0, 'url' => $info['url'], 'key' => $info['key']];
 | 
			
		||||
            $this->success('文件已经上传', array_merge($data, $extr), 200);
 | 
			
		||||
        } elseif ('local' === $data['uptype']) {
 | 
			
		||||
            $data['url'] = LocalStorage::instance()->url($data['key'], $safe, $name);
 | 
			
		||||
            $data['server'] = LocalStorage::instance()->upload();
 | 
			
		||||
@ -100,7 +113,25 @@ class Upload extends Controller
 | 
			
		||||
            $data['authorization'] = $token['authorization'];
 | 
			
		||||
            $data['server'] = UpyunStorage::instance()->upload();
 | 
			
		||||
        }
 | 
			
		||||
        $this->success('获取上传授权参数', $data, 404);
 | 
			
		||||
        $file->save(['xurl' => $data['url'], 'is_fast' => 0, 'is_safe' => $data['safe']]);
 | 
			
		||||
        $this->success('获取上传授权参数', array_merge($data, ['id' => $file->id ?? 0]), 404);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 更新文件状态
 | 
			
		||||
     * @login true
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function done()
 | 
			
		||||
    {
 | 
			
		||||
        $data = $this->_vali(['id.require' => '编号不能为空!']);
 | 
			
		||||
        $file = SystemFile::mk()->where($data)->findOrEmpty();
 | 
			
		||||
        if ($file->isEmpty()) $this->error('文件不存在!');
 | 
			
		||||
        if ($file->save(['status' => 2])) {
 | 
			
		||||
            $this->success('更新成功!');
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->error('更新失败!');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -95,10 +95,11 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
 | 
			
		||||
    // 文件上传
 | 
			
		||||
    Adapter.prototype.request = function (file, done) {
 | 
			
		||||
        var that = this, data = {key: file.xkey, safe: that.option.safe, uptype: that.option.type};
 | 
			
		||||
        data.size = file.size, data.name = file.name, data.hash = file.xmd5;
 | 
			
		||||
        data.size = file.size, data.name = file.name, data.hash = file.xmd5, data.mime = file.type, data.xext = file.xext;
 | 
			
		||||
        jQuery.ajax("{:url('admin/api.upload/state')}", {
 | 
			
		||||
            data: data, method: 'post', success: function (ret) {
 | 
			
		||||
                file.xurl = ret.data.url, file.xsafe = ret.data.safe, file.xpath = ret.data.key, file.xtype = ret.data.uptype;
 | 
			
		||||
                file.id = ret.data.id || 0, file.xurl = ret.data.url;
 | 
			
		||||
                file.xsafe = ret.data.safe, file.xpath = ret.data.key, file.xtype = ret.data.uptype;
 | 
			
		||||
                if (parseInt(ret.code) === 404) {
 | 
			
		||||
                    var uploader = {};
 | 
			
		||||
                    uploader.url = ret.data.server;
 | 
			
		||||
@ -179,6 +180,7 @@ define(['md5', 'notify'], function (SparkMD5, Notify, allowMime) {
 | 
			
		||||
        /*! 检查单个文件上传返回的结果 */
 | 
			
		||||
        if (ret.code < 1) return $.msg.tips(ret.info || '文件上传失败!');
 | 
			
		||||
        if (typeof file.xurl !== 'string') return $.msg.tips('无效的文件上传对象!');
 | 
			
		||||
        jQuery.post("{:url('admin/api.upload/done')}", {id: file.id, hash: file.xmd5});
 | 
			
		||||
        /*! 单个文件上传成功结果处理 */
 | 
			
		||||
        if (typeof done === 'function') {
 | 
			
		||||
            done.call(this.option.elem, file.xurl, this.files['id']);
 | 
			
		||||
 | 
			
		||||
@ -29,7 +29,7 @@
 | 
			
		||||
    "topthink/framework": "^6.0",
 | 
			
		||||
    "topthink/think-view": "^1.0",
 | 
			
		||||
    "zoujingli/ip2region": "^1.0",
 | 
			
		||||
    "zoujingli/think-library": "^6.0",
 | 
			
		||||
    "zoujingli/think-library": "6.0.x-dev",
 | 
			
		||||
    "zoujingli/wechat-developer": "^1.2"
 | 
			
		||||
  },
 | 
			
		||||
  "autoload": {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user