__coupon__bak.js
9.18 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var $ = require('jquery');
require('../util/datepicker')($);
var Validator = require('../util/jquery.validate');// 浏览器就不用这句了,Validator是全局变量
/*获取优惠码信息渲染页面*/
exports.renderview = function(id, callback) {
var _content = $("#basicForm");
var options = {
type: 'post',
url: '/getPromotionCode',
data: {
id: id
},
dataType: "json",
success: function(res) {
res = res.data;
if (res.code == 200) {
for (var key in res.data) {
if (key == 'promotionInfo') {
var __promotionInfo = JSON.parse(res.data["promotionInfo"]);
$("#promotionInfo-type").val(__promotionInfo["type"]);
$("#promotionInfo-condition-amount_at_least").val(__promotionInfo["condition"]["amount_at_least"]);
$("#promotionInfo-condition-count_at_least").val(__promotionInfo["condition"]["count_at_least"]);
$("#promotionInfo-action-discount").val(__promotionInfo["action"]["discount"]);
$("#promotionInfo-action-discount_at_most").val(__promotionInfo["action"]["discount_at_most"]);
if ($("#promotionInfo-type").val() == "3") {
$("#resultdiscount").hide();
}
} else if (key == 'reqDepartment') {
$("#" + key).val(res.data[key]);
var arr = String.prototype.split.call(res.data["reqDepartment"], '/');
$("#department1").val(arr[0]);
if (arr[1]) {
$("#department2").val(arr[1]).show();
} else {
$("#department2").hide();
}
} else {
$("#" + key).val(res.data[key]);
if (key == "staff") {
$("#" + key).html(res.data[key]);
}
}
}
$("input:hidden", _content).each(function() {
var _value = $(this).val();
var _name = $(this).attr("id");
$("#" + _name).prev("div").find("[name='" + _name + "']").each(function() {
if (_value.indexOf($(this).val()) > -1) {
$(this).prop("checked", true);
}
});
});
callback && callback(res.data)
}
},
error: function(err) {
console.log(err);
}
};
$.ajax(options);
};
/*点击按钮提交(优惠码添加修改)*/
exports.submit = function(url, callback) {
var $submit = $('#basicForm'),
$startTime = $('#limitDateFrom'),
$endTime = $("#limitDateTo");
//初始化时间控件
//.add($endTime)
var a = $startTime.fdatepicker({
format: 'yyyy-mm-dd hh:ii:ss',
pickTime: true
}).data('datepicker');
$startTime.click(function() {
a.setEndDate($endTime.val());
});
var b = $endTime.fdatepicker({
format: 'yyyy-mm-dd hh:ii:ss',
pickTime: true
}).data('datepicker');
$endTime.click(function() {
b.setStartDate($startTime.val());
});
//点击radio,checkbox,获取相关数据,录入相关隐藏控件中
$discount = $("#promotionInfo-action-discount");
$("input:checkbox").add("input:radio").change(function() {
var _name = $(this).attr("name"),_value = [];
var $checked = $("input[name=" + _name + "]:checked");
if ($checked.prop("name") == "promotionInfo-type") {
if ($checked.val() == "3") {
$discount.prop("disabled", true).val("0.00").parents("#resultdiscount").hide();
} else {
$discount.prop("disabled", false).parents("#resultdiscount").show();
}
}
$checked.each(function() {
_value.push($(this).val());
});
$("#" + _name).val(_value.join(','));
});
//选择部门
var departmentArr = String.prototype.split.call($("#reqDepartment").val(), '/'),
$department1 = $("#department1"),
$department2 = $("#department2");
$("select").change(function() {
if ($(this).is($department1)) {
departmentArr.length = 0;
departmentArr[0] = $(this).val();
if ($(this).val() === "零售运营部") {
$department2.show();
departmentArr[1] = $department2.val();
} else {
$department2.hide();
}
} else {
departmentArr[1] = $(this).val();
}
$("#reqDepartment").val(departmentArr.join('/'));
});
function __isRepeat(arr) {
var hash = {};
for (var i in arr) {
if (hash[arr[i]]) {
return true;
}
hash[arr[i]] = true;
}
return false;
}
function validate() {
var rules = {},data = {};
$("input:text").each(function() {
var id = $(this).attr("id");
var value = $(this).val();
var rule = [];
if ($(this).attr("required")) {
rule.push("required");
}
if ($(this).attr("maxlength")) {
rule.push("max:" + $(this).attr("maxlength"));
}
if ($(this).attr("minlength")) {
rule.push("max:" + $(this).attr("minlength"));
}
if ($(this).attr("pattern")) {
rule.push("regex:" + $(this).attr("pattern"));
}
rules[id] = rule.join('|');
data[id] = value;
});
console.log(rules);
console.log(data);
var v = Validator.make(data, rules);
if (v.fails()) {
console.log(v.messages());
return false;
}
if (v.passes()) {
if ($startTime.val() && $endTime.val()) {
var startTime = new Date($startTime.val().replace(/-/g, "/"));
var endTime = new Date($endTime.val().replace(/-/g, "/"));
if (endTime > startTime) {
return true;
}
}
var arr = String.prototype.split.call($("#code").val(), ',');
if (__isRepeat(arr)) {
validity.valid = false;
return validity;
}
$.ajax({
type: 'POST',
url: '/checkPromotionCode',
data: {
code:$("#code").val()
},
dataType: 'json'
}).then(function(data) {
data = data.data;
if (data.code == "20001") {
var badcode = [];
$.each(data.data, function(index, item) {
if (item.flag == "1") {
badcode.push(item.code);
}
});
if (badcode.length == 0) {
return true
}
}
return false;
}, function() {
console.log("验证codeAjax", false);
return false;
});
return false;
}
}
$("#save_brand").click(function() {
if (validate()) {
var data = {},key2,data2 = {};
$submit.find("input").each(function() {
var _key = $(this).attr("id");
if (_key) {
var _value = $(this).val();
if (_key.indexOf('-') > -1) {
var arr = _key.split('-');
key2 = arr[0];
data[arr[0]] = data[arr[0]] || {};
if (arr.length == 2) {
data2[arr[1]] = _value;
}
if (arr.length == 3) {
data2[arr[1]] = data2[arr[1]] || {};
data2[arr[1]][arr[2]] = _value;
}
} else {
data[_key] = _value;
}
}
});
data[key2] = JSON.stringify(data2);
if (data.status === "2") {
data.status = "0";
}
var options = {
type: 'post',
url: url,
data: data,
dataType: "json",
success: function(res) {
location.href = "/list";
callback && callback(res);
},
error: function(err) {
console.log(err);
}
};
//console.log("AJAX");
$.ajax(options);
return false;
} else {
console.log("验证不通过");
}
});
}