Authored by liuyue

upload

... ... @@ -165,7 +165,7 @@ edit.prototype={
_w.prepend('<img width="76" height="80" src="'+response.data+'">');
}
$(this).attr("value",response.data);
that.__listen("callback",{key:"file_onComplete_"+$(this).attr("id")});
that.__listen("callback",{key:"file_onComplete_"+$(this).attr("id"),data:response.data});
},
'onStart': function() {
that.__listen("callback",{key:"file_onStart_"+$(this).attr("id")});
... ... @@ -260,7 +260,7 @@ edit.prototype={
var value =$.trim($(this).val());
var name=$(this).attr("placeholder")&&!(/^\d+$/.test($(this).attr("placeholder")))?$(this).attr("placeholder").replace(/请填写/g,''):$(this).parents(".form-group").find("label").text().replace(/\*/g,'');
if (id&&$(this).attr("required")&&+value==-1) {
if (id&&$(this).attr("required")&&(+value==-1||+value=="")) {
err.push("<p>请选择 "+name+"</p>");
}
});
... ... @@ -330,7 +330,9 @@ edit.prototype={
}
};
callback&&callback(options);
$.ajax(options);
if(!options.debug){
$.ajax(options);
}
//return false;
},
__listen:function(key,o,callback){
... ...
var $ = require('jquery');
var $=require('jquery');
$.fn.ajaxfileupload = function(options) {
var settings = {
params: {},
action: '',
onStart: function() {},
onComplete: function(response) {},
onCancel: function() {},
validate_extensions: true,
valid_extensions: ['gif', 'png', 'jpg', 'jpeg'],
submit_button: null
};
var settings = {
params: {},
action: '',
onStart: function() { },
onComplete: function(response) { },
onCancel: function() { },
validate_extensions : true,
valid_extensions : ['gif','png','jpg','jpeg','mp4'],
submit_button : null
};
var uploading_file = false;
var uploading_file = false;
if (options) {
$.extend(settings, options);
}
if ( options ) {
$.extend( settings, options );
}
return this.each(function() {
var $element = $(this);
if ($element.data('ajaxUploader-setup') === true) return;
return this.each(function() {
var $element = $(this);
if($element.data('ajaxUploader-setup') === true) return;
$element.change(function() {
uploading_file = false;
if (settings.submit_button == null) {
upload_file();
}
});
$element.change(function()
{
uploading_file = false;
if (settings.submit_button == null)
{
upload_file();
}
});
if (settings.submit_button == null) {} else {
settings.submit_button.click(function(e) {
e.preventDefault();
if (!uploading_file) {
upload_file();
}
});
}
if (settings.submit_button == null)
{
} else
{
settings.submit_button.click(function(e)
{
e.preventDefault();
if (!uploading_file)
{
upload_file();
}
});
}
var upload_file = function() {
if ($element.val() == '') return settings.onCancel.apply($element, [settings.params]);
var upload_file = function()
{
if($element.val() == '') return settings.onCancel.apply($element, [settings.params]);
var ext = $element.val().split('.').pop().toLowerCase();
if (true === settings.validate_extensions && $.inArray(ext, settings.valid_extensions) == -1) {
settings.onComplete.apply($element, [{
status: false,
message: '文件类型无效.只支持' + settings.valid_extensions.join(',')
}, settings.params]);
} else {
uploading_file = true;
wrapElement($element);
var ret = settings.onStart.apply($element, [settings.params]);
if (ret !== false) {
$element.parent('form').submit(function(e) {
e.stopPropagation();
}).submit();
}
}
};
$element.data('ajaxUploader-setup', true);
var handleResponse = function(loadedFrame, element) {
var response, responseStr = $(loadedFrame).contents().text();
try {
response = JSON.parse(responseStr);
} catch (e) {
response = responseStr;
}
element.siblings().remove();
element.unwrap();
var ext = $element.val().split('.').pop().toLowerCase();
if(true === settings.validate_extensions && $.inArray(ext, settings.valid_extensions) == -1)
{
settings.onComplete.apply($element, [{status: false, message: '文件类型无效.只支持'+settings.valid_extensions.join(',')}, settings.params]);
} else
{
uploading_file = true;
wrapElement($element);
var ret = settings.onStart.apply($element, [settings.params]);
if(ret !== false)
{
$element.parent('form').submit(function(e) { e.stopPropagation(); }).submit();
}
}
};
$element.data('ajaxUploader-setup', true);
var handleResponse = function(loadedFrame, element) {
var response, responseStr = $(loadedFrame).contents().text();
try {
response = JSON.parse(responseStr);
} catch(e) {
response = responseStr;
}
element.siblings().remove();
element.unwrap();
uploading_file = false;
uploading_file = false;
settings.onComplete.apply(element, [response, settings.params]);
var _$element = $element.clone();
var _$parent = $element.parent();
$element.remove();
_$parent.append(_$element);
_$element.ajaxfileupload(settings);
};
var wrapElement = function(element) {
var frame_id = 'ajaxUploader-iframe-' + Math.round(new Date().getTime() / 1000)
$('body').after('<iframe width="0" height="0" style="display:none;" name="' + frame_id + '" id="' + frame_id + '"/>');
$('#' + frame_id).get(0).onload = function() {
handleResponse(this, element);
};
element.wrap(function() {
return '<form action="' + settings.action + '" method="POST" enctype="multipart/form-data" target="' + frame_id + '" />'
})
.before(function() {
var key, html = '';
for (key in settings.params) {
var paramVal = settings.params[key];
if (typeof paramVal === 'function') {
paramVal = paramVal();
}
html += '<input type="hidden" name="' + key + '" value="' + paramVal + '" />';
settings.onComplete.apply(element, [response, settings.params]);
var _$element=$element.clone();
var _$parent=$element.parent();
$element.remove();
_$parent.append(_$element);
_$element.ajaxfileupload(settings);
};
var wrapElement = function(element) {
var frame_id = 'ajaxUploader-iframe-' + Math.round(new Date().getTime() / 1000)
$('body').after('<iframe width="0" height="0" style="display:none;" name="'+frame_id+'" id="'+frame_id+'"/>');
$('#'+frame_id).get(0).onload = function() {
handleResponse(this, element);
};
element.wrap(function() {
return '<form action="' + settings.action + '" method="POST" enctype="multipart/form-data" target="'+frame_id+'" />'
})
.before(function() {
var key, html = '';
for(key in settings.params) {
var paramVal = settings.params[key];
if (typeof paramVal === 'function') {
paramVal = paramVal();
}
html += '<input type="hidden" name="' + key + '" value="' + paramVal + '" />';
}
html+='<input type="hidden" name="filename" value="' + $element.attr("name") + '" />'
return html;
});
}
html += '<input type="hidden" name="filename" value="' + $element.attr("name") + '" />'
return html;
});
}
});
}
});
}
module.exports = $;
\ No newline at end of file
module.exports=$;
\ No newline at end of file
... ...
... ... @@ -14,12 +14,15 @@ var util={
success: function(res) {
res=res.data;
if (("" + res.code) === "200") {
notip?callback.bind(this,res)():util.__tip(res.message,"success",callback.bind(this,res));
notip?(callback&&callback.bind(this,res)()):util.__tip(res.message,"success",callback&&callback.bind(this,res)());
return true;
} else {
(!notip)&&util.__tip(res.message,"danger");
return false;
}
},
error:function(){
return false;
}
});
}
... ...
... ... @@ -10,15 +10,19 @@ var $ = require('jquery'),
require('../common/bootstrap-wysihtml5');
var ENMA={
attachType:{1:"图片",2:"视频",3:"文本"},
type:{}
attachType:{1:"图片",2:"视频",3:"文本"}
}
//日期插件
$('#saleTime').fdatepicker({
format: 'yyyy-mm-dd'
});
function getLocalTime(nS) {
var date= new Date(parseInt(nS) * 1000);
var mm=date.getMonth()+1;
var dd=date.getDate();
return date.getFullYear()+"-"+(mm<10?"0"+mm:mm)+"-"+(dd<10?"0"+dd:dd);
}
if($("#saleTime").data("time")){
var date=getLocalTime($("#saleTime").data("time"));
$("#saleTime").val(date);
}
//测试数据
var g=new common.grid({
... ... @@ -31,8 +35,9 @@ var g=new common.grid({
return ENMA.attachType[item.attachType];
}},
{display:"内容",render:function(item){
item.orderBy==item.__index;
if(item.isDefault==1){
return '<input type="file" name="coverImg" id="coverImg" placeholder="封面图" required/>';
return '<input type="file" name="coverImg" id="coverImg" value="'+item.attachUrl+'" placeholder="封面图" required/>';
}else{
if(item.attachType==1){
return "<img src='"+item.attachUrl+"' style='width:120px;height:80px' />";
... ... @@ -46,60 +51,167 @@ var g=new common.grid({
{display:"操作",render:function(item){
var htmlArr=[];
if(!item.isDefault){
htmlArr.push("<button type='button' data-index='0' class='delete btn btn-danger btn-xs'>删除</button>");
htmlArr.push("<button type='button' data-index='0' class='delete btn btn-info btn-xs'>编辑</button>");
htmlArr.push("<button type='button' data-index='"+item.__index+"' class='delete btn btn-danger btn-xs'>删除</button>");
htmlArr.push("<button type='button' data-index='"+item.__index+"' class='edit btn btn-info btn-xs'>编辑</button>");
}
return htmlArr.join(',');
}}
]
});
g.__rows=[
{
"attachType": 1,
"attachUrl": "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1444256491,538988776&fm=80",
"isDefault": 1,
"orderBy": 1
},
var __ac=$.trim($("#attachmentContent").val());
g.__rows=__ac?JSON.parse(__ac):[
{
"attachType": 1,
"attachUrl": "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1444256491,538988776&fm=80",
"isDefault": 0,
"orderBy": 2
},
{
"attachType": 3,
"intro": "限售商品详情",
"isDefault": 0,
"orderBy": 3
"attachUrl": "",
"isDefault": 1
}
]
];
g.init(g.__rows);
var e1 = new common.edit('#baseTable');
e1.on("callback",function(obj){
if(obj.key=="file_onComplete_coverImg"){
g.__rows[0].attachUrl=obj.data;
}
});
e1.init();
//初始化form表单
var $editorgroup = $('#editor-group');
$editorgroup.init();
var e = new common.edit('#editor-group');
e.on("validate",function(){
var param={
batchNo: $('#batchNo').val(),
id:$("#id").val()
};
return common.util.__ajax({
url:'/limit/checkBatch',
async:true,
data:param
},null,true);
});
e.on("validate",function(){
if(!g.__rows[0].attachUrl){
return "请上传默认封面";
}
});
e.init();
//添加修改
$('.limit-add-btn').click(function (option) {
e.submit($('#limit-add-form').attr('action'),function(option){
//option.debug=true;
option.data.attachmentContent=JSON.stringify(g.__rows);
option.data.saleTime = new Date(option.data.saleTime.replace('-',',')).getTime() / 1000;
option.success=function(res){
res=res.data;
if(res.code=="200"){
e.$tip('提交成功',function(){
location.href="/limit/product/index";
},'growl-success');
}else{
e.$tip(res.message);
}
return false;
}
option.error=function(res){
e.$tip("提交失败");
};
});
});
// var $editor = $('#editor'), //文字,图片,视频容器
// type = $('#limit-add-form').attr('type');
// //添加文字
// $('body').on('click', '#textDialog', function() {
// common.dialog.confirm("添加文本",'<textarea id="text-editor"></textarea>',function(){
// var textStr = $('#text-editor').val();
// var $oText = $('<div class="quota-content quota-text" data-type="3"><span>' + textStr + '</span><a href="javascript:;" class="editor-delete btn btn-default">删除</a></div>');
var Bll={
imgVideo:function(title,item,index){
common.dialog.confirm(title,common.util.__template($("#picDialogTemp").html(),item),function(){
var _html=$.trim($("#imgVideoText").val());
if(_html){
if(item.attachType){
//修改
g.__rows[index].attachUrl=_html;
}
else{
//添加
g.__rows.push({
"attachType": item.__attachType,
"attachUrl": _html,
"isDefault": 0,
"attachName":$("#attachName").val()
});
}
g.reload();
e1.init();
}
});
var a = new common.edit("#upload-wrapper");
a.on("callback",function(obj){
if(obj.key=="file_onComplete_imgVideo"){
$("#imgVideoText").val(obj.data);
}
});
a.init();
},
wenZi:function(title,item,index){
common.dialog.confirm(title,common.util.__template('<textarea id="text-editor">{attachUrl}</textarea>',item),function(){
var _html=$.trim($("#text-editor").val());
if(_html){
if(item.attachType){
//修改
g.__rows[index].attachUrl=_html;
}
else{
//添加
g.__rows.push({
"attachType": 3,
"attachUrl": _html,
"isDefault": 0
});
}
g.reload();
e1.init();
}
});
$('#text-editor').wysihtml5();
}
}
// $oText.appendTo($editor);
//添加文字
$('body').on('click', '#textDialog', function() {
Bll.wenZi("添加文字",{});
return false;
});
$('body').on('click', '#picDialog', function() {
Bll.imgVideo("添加图片",{name:"图片",__attachType:1});
return false;
});
$('body').on('click', '#videoDialog', function() {
Bll.imgVideo("添加视频",{name:"视频",__attachType:2});
return false;
});
//删除
$('body').on('click',".delete",function(){
var index=$(this).data("index");
g.__rows.splice(index,1);
g.reload();
e1.init();
});
//修改
$('body').on('click',".edit",function(){
var item=g.rows[$(this).data("index")];
Bll[item.attachType==3?"wenZi":"imgVideo"]("修改"+ENMA.attachType[item.attachType],item,$(this).data("index"));
});
// });
// $('#text-editor').wysihtml5();
// return false;
// });
// //添加图片
// $('body').on('click', '#picDialog', function() {
... ...
... ... @@ -3,6 +3,7 @@ var fs=require('fs');
var domain = require('../../config/common.js').domain;
//var domain = 'http://172.16.6.227:8083/yohobuy-platform-web';
//var domain = 'http://192.168.102.202:8088/platform';
//var domain = 'http://172.16.6.240:8088/platform';
var env = process.env.NODE_ENV || 'development';
exports.uploadFile=function(req,res){
... ...
... ... @@ -19,77 +19,77 @@
<form id="limit-add-form" class="form-horizontal form-bordered" type="{{type}}" action="{{action}}">
{{# data}}
<div class="panel-group" id="editor-group">
<div class="panel-group">
<div class="pannel">
<div class="panel-heading">
<h4>限购商品基本信息</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="form-group">
<label class="col-sm-2 control-label">商品名称<span class="red">*</span></label>
<div class="col-sm-8">
<input type="text" id="productName" placeholder="商品名称" class="form-control" required maxlength="30" value="{{productName}}">
<div class="panel-group" id="editor-group">
<input type="hidden" id="id" value="{{id}}">
<div class="pannel">
<div class="panel-heading">
<h4>限购商品基本信息</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="form-group">
<label class="col-sm-2 control-label">商品名称<span class="red">*</span></label>
<div class="col-sm-8">
<input type="text" id="productName" placeholder="商品名称" class="form-control" required maxlength="30" value="{{productName}}">
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">限购码批次<span class="red">*</span></label>
<div class="col-sm-8">
<input type="text" id="batchNo" data-id="{{id}}" placeholder="限购码批次" class="form-control" required maxlength="30" value="{{batchNo}}">
<div class="form-group">
<label class="col-sm-2 control-label">限购码批次<span class="red">*</span></label>
<div class="col-sm-8">
<input type="text" id="batchNo" placeholder="限购码批次" class="form-control" required maxlength="30" value="{{batchNo}}">
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">发售日期<span class="red">*</span></label>
<div class="col-sm-4">
<input id="saleTime" data-time="{{saleTime}}" value="" type="text" class="form-control" placeholder="发售日期" readonly required>
</div>
<div class="col-md-3">
<label>是否显示日期:</label>
<label class="radio-inline"><input type="radio" name="dayFlag" value="1"></label>
<label class="radio-inline"><input type="radio" name="dayFlag" value="0"></label>
<input type="hidden" value="{{dayFlag}}" id="dayFlag" for="radio">
<div class="form-group">
<label class="col-sm-2 control-label">发售日期<span class="red">*</span></label>
<div class="col-sm-4">
<input id="saleTime" data-time="{{saleTime}}" value="" type="text" class="form-control" jsaction="date" placeholder="发售日期" readonly required>
</div>
<div class="col-md-3">
<label>是否显示日期:</label>
<label class="radio-inline"><input type="radio" name="dayFlag" value="1"></label>
<label class="radio-inline"><input type="radio" name="dayFlag" value="0"></label>
<input type="hidden" value="{{dayFlag}}" id="dayFlag" for="radio">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="pannel">
<div class="panel-heading">
<h4>限购商品排序</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<label>是否热门:</label>
<label class="radio-inline"><input type="radio" name="hotFlag" value="1"></label>
<label class="radio-inline"><input type="radio" name="hotFlag" value="0"></label>
<input type="hidden" value="{{hotFlag}}" id="hotFlag" for="radio">
</div>
<div class="col-md-3 height60">
<label>热门发售排序:</label>
<label><input id="orderBy" value="{{orderBy}}" type="text" class="form-control" placeholder=""></label>
<p>(提示:数字越大越靠前)</p>
</div>
<div class="col-md-3">
<label>未发售排序:</label>
<label><input id="notSaleOrderBy" value="{{notSaleOrderBy}}" type="text" class="form-control" placeholder=""></label>
<div class="pannel">
<div class="panel-heading">
<h4>限购商品排序</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-3">
<label>是否热门:</label>
<label class="radio-inline"><input type="radio" name="hotFlag" value="1"></label>
<label class="radio-inline"><input type="radio" name="hotFlag" value="0"></label>
<input type="hidden" value="{{hotFlag}}" id="hotFlag" for="radio">
</div>
<div class="col-md-3 height60">
<label>热门发售排序:</label>
<label><input id="orderBy" value="{{orderBy}}" type="text" class="form-control" placeholder=""></label>
<p>(提示:数字越大越靠前)</p>
</div>
<div class="col-md-3">
<label>未发售排序:</label>
<label><input id="notSaleOrderBy" value="{{notSaleOrderBy}}" type="text" class="form-control" placeholder=""></label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="pannel">
<div class="panel-heading">
<h4>限量详情内容</h4>
</div>
<div id="baseTable"></div>
<input type="hidden" value="{{attachment}}" id="attachment" />
<input type="hidden" value="{{attachmentContent}}" id="attachmentContent" />
<div class="row">
<div class="col-md-8"></div>
<div class="col-md-4">
<div class="col-md-12">
<button class="btn btn-info mr5" id="textDialog">文本</button>
<button class="btn btn-info mr5" id="picDialog">图片</button>
<button class="btn btn-info mr5" id="videoDialog">视频</button>
... ... @@ -97,11 +97,14 @@
</div>
</div>
<div class="pannel">
<div class="col-md-10"></div>
<div class="col-md-2">
<input id="limit-add-btn" class="btn btn-success limit-add-btn" type="button" value="保存"></input>
</div>
</div>
</div>
<input type="hidden" id="id" value="{{id}}">
<div class="mt20">
<input id="limit-add-btn" class="btn btn-primary limit-add-btn" type="button" value="保存"></input>
</div>
</form>
{{/ data}}
</div>
... ... @@ -110,20 +113,20 @@
<script type="text/template" id="picDialogTemp">
<div class="row">
<div id="upload-wrapper" class="col-md-3">
<input id="{fileId}" type="file" class="btn btn-primary ml10" value="">
<input id="imgVideo" type="file" class="btn btn-primary ml10" value="{attachUrl}">
</div>
<div class="col-md-9">
<div class="row">
<label class="pull-left control-label height40">{name}链接</label>
<div class="pull-left ml10">
<input class="form-control" type="text" placeholder="{name}链接">
<input class="form-control" id="imgVideoText" type="text" placeholder="{name}链接" value="{attachUrl}">
</div>
</div>
<div class="row">
<label class="pull-left control-label height40">{name}描述</label>
<div class="pull-left ml10">
<input class="form-control" type="text" placeholder="{name}描述">
<input class="form-control" type="text" id="attachName" value="{attachName}" placeholder="{name}描述">
</div>
</div>
</div>
... ...