556 lines
19 KiB
JavaScript
556 lines
19 KiB
JavaScript
layui.define(['upload', 'laypage','form'], function(exports) {
|
||
var $ = layui.$
|
||
var upload = layui.upload;
|
||
var admin = layui.admin;
|
||
var listable=layui.listable;
|
||
form = layui.form;
|
||
|
||
|
||
|
||
|
||
var custorm = {
|
||
upload_one: uploadOne,
|
||
upload_more: uploadMore,
|
||
layerpage: laypageList,
|
||
diy_open:diyOpen,
|
||
ajax_area:ajaxArea,
|
||
openjson:openJsonSelecte,
|
||
layer_open:openLayer,
|
||
layer_open_parent:openLayerParent
|
||
}
|
||
layer.photos({
|
||
photos: '#layer-photos-demo'
|
||
,anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
|
||
});
|
||
function uploadOne(obj, accept_type,callback) {
|
||
var upload_type = $(obj).data('type');
|
||
var screen_type = $(obj).data('screen_type');
|
||
accept_type = accept_type || 'images';
|
||
upload_type = upload_type || 'image';
|
||
screen_type = screen_type || '';
|
||
if (accept_type != 'images') {
|
||
accept_type = 'file';
|
||
}
|
||
|
||
|
||
upload.render({
|
||
elem: $(obj + " .upload-event"),
|
||
url: g_upload_url + '?uptype=' + upload_type+'&screen_type='+screen_type,
|
||
data: {
|
||
_token: $('[name="csrf-token"]').attr('content')
|
||
},
|
||
accept: accept_type,
|
||
done: function(res) {
|
||
if (res.success == 1) {
|
||
$(obj + " .layui-upload-drag").hide();
|
||
$(obj + ' .upload_img').parents().show().end().attr('src', res.view_img);
|
||
|
||
|
||
$(obj + " .upload_input").val(res.path);
|
||
if(typeof callback == "function"){
|
||
callback(res.path);
|
||
}
|
||
} else {
|
||
layer.msg(res.state);
|
||
}
|
||
|
||
}
|
||
});
|
||
};
|
||
//
|
||
$(document).on('click', '.upload-close', function(event) {
|
||
/* Act on the event */
|
||
var obj = $(this);
|
||
layer.confirm('确定删除吗?', function(index) {
|
||
obj.parents('.layui-input-inline,.layui-upload-img').hide();
|
||
layer.close(index);
|
||
obj.parents('.layui-input-inline').find('.upload_input').val('');
|
||
obj.parents('.layui-input-inline').find('.upload_img').attr('src', '')
|
||
obj.parents('.layui-input-inline').prev().find('.layui-upload-drag').show();
|
||
obj.parents('.layui-input-inline').prev().show();
|
||
|
||
//判断是否多图上传
|
||
has_more_obj=obj.parents(".item");
|
||
if(has_more_obj.html())
|
||
{
|
||
obj.parents('.item').remove();
|
||
pic_item=obj.data('obj');
|
||
pic_input_item=obj.data('input_obj');
|
||
|
||
jsonThumbs(pic_item,pic_input_item);
|
||
|
||
}
|
||
|
||
});
|
||
});
|
||
$(document).on('click','.table-del-pic',function(event){
|
||
var obj = $(this);
|
||
layer.confirm('确定删除吗?', function(index) {
|
||
obj.parents('.pic-item').remove();
|
||
pic_item=obj.data('obj');
|
||
pic_input_item=obj.data('input_obj');
|
||
jsonThumbs(pic_item,pic_input_item);
|
||
layer.close(index);
|
||
});
|
||
});
|
||
//重新计算图片json
|
||
function jsonThumbs(obj,input_name){
|
||
thumbs_arr=new Array();
|
||
$(obj).each(function(){
|
||
path=$(this).data('path');
|
||
size=$(this).data('size');
|
||
tmpname=$(this).data('tmpname');
|
||
file_type=$(this).data('type');
|
||
thumbs_arr.push({tmpname:encodeURI(tmpname),type:file_type,size:size,path:path});
|
||
});
|
||
json=JSON.stringify(thumbs_arr);
|
||
console.log(json)
|
||
$(input_name).val(json);
|
||
}
|
||
|
||
function uploadMore(obj, more, url,display) {
|
||
url = url || g_upload_more_url;
|
||
more = more || 1;
|
||
config={
|
||
type:2,
|
||
title:'上传多图',
|
||
url:url,
|
||
h:'500px',
|
||
w:'720px',
|
||
btn: ['确定', '取消']
|
||
};
|
||
openLayer(config,function(layero,index){
|
||
var iframeWindow = window['layui-layer-iframe' + index],
|
||
submitID = 'LAY-user-back-submit',
|
||
submit = layero.find('iframe').contents().find('#' + submitID);
|
||
var item_img = layero.find('iframe').contents().find('.on');
|
||
if (more == 1) {
|
||
var html_str = '';
|
||
var upload_thumbs_json=new Array();
|
||
if($(obj + " .upload_input").val())
|
||
{
|
||
upload_thumbs_json=JSON.parse( $(obj + " .upload_input").val());
|
||
|
||
}
|
||
item_img.each(function() {
|
||
var tmpname = $(this).find('p').text();
|
||
var path = $(this).data('src');
|
||
var img_src = $(this).find('img').attr('src');
|
||
var file_type=$(this).data('type');
|
||
var size=$(this).data('size');
|
||
upload_thumbs_json.push({'path':path,size:size,'type':file_type,'tmpname':encodeURI(tmpname)});
|
||
|
||
if(display=='table')
|
||
{
|
||
item_obj=obj + " .layui-upload-list .pic-item";
|
||
input_obj=obj + " .upload_input";
|
||
html_str+='<tr class="pic-item" data-type="'+file_type+'" data-size="'+size+'" data-path="'+img_src+'" data-tmpname="'+tmpname+'"><td>'+tmpname+'</td> ' +
|
||
'<td>'+size+'</td><td><img src="' + img_src + '" title="图片放大" layadmin-event="viewpic2" title="' + tmpname + '" alt="" ></td>' +
|
||
'<td><button class="layui-btn layui-btn-mini layui-btn-danger table-del-pic" data-obj="'+item_obj+'" data-input_obj="'+input_obj+'">删除</button></td></tr>';
|
||
}else
|
||
{
|
||
if(file_type=='vedio')
|
||
{
|
||
html_str += '<div class="layui-upload-img item layui-inline " data-src="' + path + '"> ' +
|
||
'<video class="upload_img" src="'+path+'" controls="controls" style="max-width: 100%;"></video> ' +
|
||
'<p class="layui-">' + tmpname + '</p><a href="javascript:" class="upload-close layui-icon layui-icon-close "></a> </div>';
|
||
}else if(file_type=='image')
|
||
{
|
||
item_obj=obj + " .layui-upload-list .item";
|
||
input_obj=obj + " .upload_input";
|
||
html_str += '<div class="layui-upload-img item layui-inline " data-type="'+file_type+'" data-tmpname="'+tmpname+'" data-size="'+size+'" data-path="' + path + '"> ' +
|
||
'<img src="' + img_src + '" class="" title="' + tmpname + '" layadmin-event="viewpic" alt="" > ' +
|
||
'<p class="layui-">' + tmpname + '</p><a href="javascript:" class="upload-close layui-icon layui-icon-close " data-obj="'+item_obj+'" data-input_obj="'+input_obj+'"></a> </div>';
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
});
|
||
|
||
$(obj + " .upload_input").val(JSON.stringify(upload_thumbs_json));
|
||
if(display!='table')
|
||
{
|
||
$(obj + " .layui-upload-list").append(html_str);
|
||
}else
|
||
{
|
||
$(obj + " .layui-upload-list table tbody").append(html_str);
|
||
}
|
||
|
||
|
||
} else {
|
||
|
||
path = item_img.data('src');
|
||
img_src = item_img.find('img').attr('src');
|
||
img_src = img_src || item_img.find('video').attr('src');
|
||
// console.log(img_src);
|
||
$(obj + " .layui-upload-drag").hide();
|
||
$(obj + ' .upload_img').parents('.layui-input-inline').show().end().attr('src', img_src);
|
||
$(obj + " .upload_input").val(path);
|
||
}
|
||
//sortable('.layui-upload-list');
|
||
layer.close(index); //关闭弹层
|
||
});
|
||
|
||
}
|
||
|
||
function diyOpen(obj, yesFun) {
|
||
|
||
$(obj).click(function() {
|
||
console.log(222);
|
||
w = $(this).data('w');
|
||
h = $(this).data('h');
|
||
title = $(this).data('title');
|
||
url = $(this).data('url');
|
||
url_params=$(this).data('url_params');
|
||
url_params=url_params || '';
|
||
|
||
config={
|
||
type:2,
|
||
title:title,
|
||
url:url+url_params,
|
||
h:h,
|
||
w:w
|
||
};
|
||
openLayer(config,yesFun);
|
||
|
||
});
|
||
|
||
|
||
}
|
||
function layuiPx(w,h){
|
||
var config=new Array();
|
||
if(is_mobile())
|
||
{
|
||
var base_w=1200;
|
||
var view_w=$(window).width();
|
||
var view_h=$(window).height();
|
||
|
||
config[0]=w;
|
||
config[1]=h;
|
||
console.log(config);
|
||
if(w.indexOf('px')!=-1)
|
||
{
|
||
console.log('px 计算');
|
||
w=w.replace(/px/g,"");
|
||
h=h.replace(/px/g,"");
|
||
//如果存在px单位则进行计算
|
||
config[0]=( view_w * 90/100)+'px';
|
||
var mh=( h * view_h / 960);
|
||
|
||
config[1]=( h * view_h / 960)+'px';
|
||
if(mh>100)
|
||
{
|
||
config[0]='100%';
|
||
config[1]='100%';
|
||
}
|
||
|
||
}else{
|
||
config[0]='100%';
|
||
config[1]='100%';
|
||
}
|
||
|
||
}else
|
||
{
|
||
config[0]=w;
|
||
config[1]=h;
|
||
}
|
||
return config;
|
||
|
||
}
|
||
function openLayer(config,yesFun,susFuc,cacFun){
|
||
btn=config.btn || ['确定', '取消'];
|
||
var area_config=layuiPx(config.w, config.h);
|
||
layer.open({
|
||
btnAlign: 'c',
|
||
type: config.type,
|
||
title: config.title,
|
||
content: config.url,
|
||
area: area_config,
|
||
btn: btn,
|
||
|
||
|
||
yes: function(index, layero) {
|
||
if ( typeof(yesFun) == "function") {
|
||
return yesFun(layero, index);
|
||
}else
|
||
{
|
||
layer.close(index)
|
||
}
|
||
},
|
||
success:function(layero,index) {
|
||
|
||
if ( typeof(susFuc) == "function") {
|
||
return susFuc(layero, index);
|
||
}
|
||
},
|
||
cancel:function(index, layero){
|
||
if ( typeof(cacFun) == "function") {
|
||
return cacFun(layero, index);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
function openLayerParent(config,yesFun,susFuc,cacFun){
|
||
btn=config.btn || ['确定', '取消'];
|
||
var area_config=layuiPx(config.w, config.h);
|
||
parent.layer.open({
|
||
btnAlign: 'c',
|
||
type: config.type,
|
||
title: config.title,
|
||
content: config.url,
|
||
area: area_config,
|
||
btn: btn,
|
||
|
||
yes: function(index, layero) {
|
||
if ( typeof(yesFun) == "function") {
|
||
return yesFun(layero, index);
|
||
}else
|
||
{
|
||
layer.close(index)
|
||
}
|
||
},
|
||
success:function(index, layero) {
|
||
|
||
if ( typeof(susFuc) == "function") {
|
||
return susFuc(layero, index);
|
||
}
|
||
},
|
||
cancel:function(index, layero){
|
||
if ( typeof(cacFun) == "function") {
|
||
return cacFun(layero, index);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
//openLayui
|
||
admin.events.openLayer=function(othis){
|
||
w = $(othis).data('w');
|
||
h = $(othis).data('h');
|
||
title = $(othis).data('title');
|
||
url = $(othis).data('url');
|
||
url_params=$(othis).data('url_params');
|
||
url_params=url_params || '';
|
||
btn=$(othis).data('btn');
|
||
btn=btn || ['关闭'];
|
||
|
||
config={
|
||
type:2,
|
||
title:title,
|
||
url:url+url_params,
|
||
h:h,
|
||
w:w,
|
||
btn:btn
|
||
};
|
||
openLayer(config);
|
||
}
|
||
|
||
//图片空间
|
||
admin.events.uploadplace = function(othis) {
|
||
|
||
obj = $(othis).data('obj');
|
||
upload_type = $(othis).data('type');
|
||
|
||
screen_type = $(othis).data('screen_type');
|
||
upload_type = upload_type || 'image';
|
||
screen_type = screen_type || '';
|
||
|
||
|
||
display=$(othis).data('display');
|
||
display=display || '';
|
||
is_more = $(othis).data('more');
|
||
is_more=is_more || 2;
|
||
if(is_more==1)
|
||
{
|
||
uploadMore(obj, is_more, g_upload_more_url + '?uptype=' + upload_type+'&screen_type='+screen_type,display);
|
||
}else
|
||
{
|
||
uploadMore(obj, is_more, g_upload_one_url + '?uptype=' + upload_type+'&screen_type='+screen_type);
|
||
}
|
||
|
||
};
|
||
admin.events.viewpic=function(othis){
|
||
|
||
var src = $(othis).attr('src');
|
||
layer.photos({
|
||
photos: {
|
||
"title": "查看图片" //相册标题
|
||
,
|
||
"data": [{
|
||
"src": src //原图地址
|
||
}]
|
||
},
|
||
shade: 0.01,
|
||
closeBtn: 1,
|
||
anim: 5
|
||
});
|
||
}
|
||
function openJsonSelecte(clallback){
|
||
admin.events.openselected = function(othis) {
|
||
obj = $(othis).data('obj');
|
||
w = othis.data('w');
|
||
h = othis.data('h');
|
||
title = othis.data('title');
|
||
url = othis.data('url');
|
||
layer.open({
|
||
type: 2,
|
||
title: title,
|
||
content: url,
|
||
area: [w, h],
|
||
btn: ['确定', '取消'],
|
||
success: function(layero, index){
|
||
if ( typeof(clallback) == "function") {
|
||
return clallback(layero, index);
|
||
}
|
||
}
|
||
});
|
||
|
||
};
|
||
}
|
||
|
||
|
||
function laypageList(url, pageconfig, handleFun, successCallback, errorCallback) {
|
||
layer.load(3); //风格1的加载
|
||
pageconfig = pageconfig || {};
|
||
var laypage = layui.laypage;
|
||
var laypageparams = {
|
||
obj_id: 'page',
|
||
limit: 10,
|
||
limits: [1, 10, 30, 50, 70],
|
||
layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
|
||
};
|
||
laypageparams = $.extend(laypageparams, pageconfig);
|
||
|
||
$.ajax({
|
||
url: url,
|
||
dataType: 'json',
|
||
type: 'post',
|
||
data: {
|
||
limit: laypageparams.limit,
|
||
_token: $('[name="csrf-token"]').attr('content')
|
||
},
|
||
success: function(data) {
|
||
if (data.total <= 0) {
|
||
return false;
|
||
}
|
||
laypage.render({
|
||
limit: laypageparams.limit,
|
||
elem: laypageparams.obj_id,
|
||
count: data.total,
|
||
limits: laypageparams.limits,
|
||
layout: laypageparams.layout,
|
||
theme: "#62a8ea",
|
||
jump: function(obj, first) {
|
||
if (typeof(handleFun) != "function" && typeof(successCallback) == "function") {
|
||
return successCallback(obj, first);
|
||
}
|
||
|
||
if (first) {
|
||
if (typeof(handleFun) == "function") {
|
||
return handleFun(data);
|
||
} else {
|
||
return $(".tuku-list").empty().append(data.contents);
|
||
}
|
||
|
||
|
||
}
|
||
layer.load(3); //风格1的加载
|
||
$.ajax({
|
||
url: url,
|
||
dataType: 'json',
|
||
type: 'post',
|
||
data: {
|
||
offset: obj.curr,
|
||
limit: obj.limit,
|
||
_token: $('[name="csrf-token"]').attr('content')
|
||
},
|
||
success: function(tdata) {
|
||
if (tdata.total <= 0) {
|
||
return false;
|
||
}
|
||
|
||
if (typeof(handleFun) == "function") {
|
||
|
||
return handleFun(tdata);
|
||
} else {
|
||
console.log(tdata.contents);
|
||
$(".tuku-list").empty().append(tdata.contents);
|
||
return '';
|
||
}
|
||
|
||
},
|
||
complete: function() {
|
||
layer.closeAll('loading');
|
||
}
|
||
})
|
||
}
|
||
});
|
||
|
||
},
|
||
beforeSend: function() {
|
||
|
||
},
|
||
complete: function() {
|
||
layer.closeAll('loading');
|
||
}
|
||
});
|
||
|
||
|
||
}
|
||
|
||
|
||
function ajaxArea(id,setobj,ajaxfun,select_id,input_type){
|
||
input_type=input_type || 'option';
|
||
select_id=select_id || '';
|
||
$.ajax({
|
||
url: g_area_url,
|
||
dataType: 'json',
|
||
type: 'get',
|
||
data: {
|
||
id:id ,
|
||
input_type:input_type,
|
||
select_id:select_id,
|
||
_token: $('[name="csrf-token"]').attr('content')
|
||
},
|
||
success: function(tdata) {
|
||
console.log(tdata);
|
||
$(setobj).empty().append(tdata.data);
|
||
form.render('select', 'layuiadmin-form-role');
|
||
if ( typeof(ajaxfun) == "function") {
|
||
return ajaxfun(tdata);
|
||
}
|
||
|
||
},
|
||
complete: function() {
|
||
layer.closeAll('loading');
|
||
}
|
||
})
|
||
}
|
||
|
||
|
||
var kq_events={
|
||
msg:function(othis){
|
||
var title=othis.data('title');
|
||
var text=othis.data('content');
|
||
console.log(text);
|
||
layer.open({
|
||
title: title?title:'提示',
|
||
btnAlign: 'c'
|
||
,content: text
|
||
});
|
||
|
||
}
|
||
};
|
||
$body = $('body');
|
||
//点击事件
|
||
$body.on('click', '*[kq-event]', function(){
|
||
var othis = $(this)
|
||
,attrEvent = othis.attr('kq-event');
|
||
kq_events[attrEvent] && kq_events[attrEvent].call(this, othis);
|
||
});
|
||
|
||
|
||
exports('custorm', custorm);
|
||
}); |