limit-add.js
4.76 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
*@time: 2016/2/15
*@author: chenglong
*/
var $ = require('jquery'),
common=require('../common/common');
require('../common/bootstrap-wysihtml5');
require('../util/datepicker');
//日期插件
$('#saleTime').fdatepicker({
format: 'yyyy-mm-dd'
});
var $editor = $('#editor'); //文字,图片,视频容器
//添加文字
$('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>');
$oText.appendTo($editor);
});
$('#text-editor').wysihtml5();
return false;
});
//添加图片
$('body').on('click', '#picDialog', function() {
common.dialog.confirm("添加图片",common.util.__template($("#picDialogTemp").html(),{name:"图片", fileId: "imgUpload"}),function(){
var imgStr = '<img src="' + $('#imgUpload').attr('value') + '">';
var $oImg = $('<div class="quota-content quota-img" data-type="1">' + imgStr + '<a href="javascript:;" class="editor-delete btn btn-default">删除</a></div>');
$oImg.appendTo($editor);
});
var a = new common.edit("#upload-wrapper");
a.init();
return false;
});
//添加视频
$('body').on('click', '#videoDialog', function() {
common.dialog.confirm("添加视频",common.util.__template($("#picDialogTemp").html(),{name:"视频"}),function(){
/*var imgStr = '<img src="http://img11.static.yhbimg.com/goodsimg/2015/07/07/08/01315b5bbfd72bc5b59fa2fda6c28b9ae6.jpg">';
var $oVideo = $('<div class="quota-imf"><img src="' + imgStr + '" alt="" /></div>');
$('.quota-video').html(imgStr);*/
});
var a = new common.edit("#upload-wrapper");
a.init();
return false;
});
// 表单验证
var limitAddForm = new common.edit("#limit-add-form");
limitAddForm.init();
//表单提交
$('.limit-add-btn').click(function () {
var $this = $(this);
limitAddForm.submit($('#limit-add-form').attr('action'), function(option, that) {
option.beforeSend = function () {
$this.attr('disabled', 'disabled');
};
option.success=function(res){
if (res.data.code == 200) {
//成功返回列表页
limitAddForm.$tip('提交成功',function(){
//window.location.href = '/limit/product/index';
}, 'growl-success');
$this.removeAttr('disabled');
}
};
option.error=function(res){
limitAddForm.$tip(res.message);
$this.removeAttr('disabled');
};
//文字,图片,视频上传数据
option.data.attachmentContent = [];
option.data.attachmentContent.push({
attachUrl:option.data.coverImg,
isDefault: 1,
attachType: 1,
orderBy: 0
});
if ($('.quota-content').size() > 0) {
$('.quota-content').each(function(i) {
var type = $(this).data('type'),
content = '',
obj = {
attachType: type, //上传内容类型,1:图片, 2:视频,3:文字
isDefault: 0, //是否封面图
orderBy: i //排序
};
console.log(type);
if (type == 3) {
content = $(this).find('span').text();
obj.intro = content; //文字key为intro
} else if (type == 1) {
content = $(this).find('img').attr('src');
obj.attachUrl = content; //图片key为attachUrl
}
option.data.attachmentContent.push(obj)
});
}
//因为只能传字符串,转换为字符串
option.data.attachmentContent = JSON.stringify(option.data.attachmentContent);
//时间上传为时间戳
option.data.saleTime = new Date(option.data.saleTime.replace('-',',')).getTime() / 1000;
console.log(option.data);
});
});
//编辑页,时间戳转换
$('#saleTime').val(function() {
if ($(this).data('time')) {
var date = new Date($(this).data('time') * 1000);
return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
}
});
//富文本删除事件
$('#limit-add-form').on('click', '.editor-delete', function() {
$(this).parents('.quota-content').remove();
});