chengkun
2025-05-23 a6f7b382623096b6a00924f418447cf5204e825e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
 * pc文件上传
*/
layui.use('upload', function(){
    var $ = layui.$
        ,upload = layui.upload
        ,layer = layui.layer
        ,device = layui.device();
    
    var layupload_type = $("#layupload_type").val();   //文件上传方式   2、选完文件后自动上传
    var laynoupload = $("#laynoupload").val();            //1、选完不上传            
    //选完不上传,url暂未用到,只是需要其样式
    
    if (laynoupload == 1){
        var layfiletype = $("#layfiletype").val();
        //上传文件类型
        if (layfiletype == 2){
            var layaccept = 'file', layexts = 'doc|docx|rar|zip|pdf|xls|xlsx';
        }else{
            var layaccept = 'images', layexts = 'jpg|png|gif|bmp|jpeg';
        }
        upload.render({
            elem: '.noupload'
            ,auto: false
            ,bindAction: '#test9'   //触发上传的对象,暂未用到
            ,accept: layaccept
            ,exts: layexts
            ,choose: function(obj){
                if(this.imgid){
                    //预读本地文件示例,不支持ie8/9
                    var imgid = null,
                        parentid = null;
                    if(this.imgid){
                        imgid = this.imgid;
                    }
                    if(this.parentid){
                        parentid = this.parentid;
                    }
                    obj.preview(function(index, file, result){
                        if (parentid && $('#'+parentid).length>0){
                            $('#'+parentid).removeClass('none');
                            $('#'+imgid).attr('src', result); 
                        }else if(imgid && $('#'+imgid).length>0){
                            $('#'+imgid).removeClass('none');
                            $('#'+imgid).attr('src', result); //图片链接(base64)
                        }
                        $('#checka').hide();
                    });
                }
            }
        });
    }
    if (layupload_type == 2){
        if($(".adminupload").length>0){
            var newData = {};
            var pytoken = $("input[name^='pytoken']").val();
            var notoken = $("#notoken").val();
            var url = '';
 
            if(pytoken && notoken != 1){
 
                url = 'index.php?m=config&c=layui_upload';
            }else{
 
                url = weburl+'/index.php?m=ajax&c=layui_upload';
            }
            
            var uploadInst = upload.render({
                elem: '.adminupload' 
                ,url: url
                ,data: newData
                ,choose: function(obj){
                    if(this.name){
                        newData.name = this.name;
                    }
                    if(this.path){
                        newData.path = this.path;
                    }
                    if(this.imgid){
                        newData.imgid = this.imgid;
                    }
                    if(this.uid){
                        newData.uid = this.uid;
                    }
                    if(this.usertype){
                        newData.usertype = this.usertype;
                    }
                    if(pytoken){
                        newData.pytoken = pytoken;
                    }
                    if(notoken){
                        newData.notoken = notoken;
                    }
                }
                ,before: function(obj){
                    layer.load();
                }
                ,done: function(res){
                    layer.closeAll('loading');
                    if(res.code > 0){                //上传失败,返回失败原因
                        return layer.msg(res.msg,{icon: 5, time: 2000});
                    }else{
                        if(res.msg){
                            layer.msg(res.msg,{icon: 6, time: 2000});
                        }
                        if(this.name=='pic'){
                            $('input[name="'+ this.name +'"]').val(res.data.url);
                        }
                        //图片外层有其他元素
                        if ($('#'+this.parentid).length>0){
                            $('#'+this.parentid).removeClass('none');
                            $('#'+this.imgid).attr('src', res.data.url); 
                        }else if(this.imgid){
                            $('#'+this.imgid).removeClass('none');
                            $('#'+this.imgid).attr('src', res.data.url);
                        }
 
                        if(document.getElementById('newbind')){
                            $('#newbind').removeClass('none');
                        }
                    }
                }
            });
        }
    }
});