grass_article.js
4.37 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
function handleGoodsBeforeSubmit(html) {
var div=document.createElement("div");
div.innerHTML = html;
var node = div.querySelectorAll(".yhproduct");
for (var i = 0 ;i<node.length;i++){
var good = document.createElement("div");
var anode = node[i];
good.setAttribute("class","yhproduct");
good.setAttribute("data-option", anode.getAttribute("data-option"));
var product = '<product-group-item v-bind="getNoteProduct(\''+anode.getAttribute("data-option")+'\')"></product-group-item>';
good.innerHTML = product;
// if(anode.parentNode.parentNode){
// anode.parentNode.parentNode.insertBefore(good, anode.parentNode);
// anode.parentNode.parentNode.removeChild(anode.parentNode);
// }else{
// }
anode.parentNode.insertBefore(good, anode);
anode.parentNode.removeChild(anode);
}
return div.innerHTML;
}
function handleGoodsAfterEdit(html) {
var productSkns = [];
var yohoproducts = {};
var ufoproducts = {};
var div=document.createElement("div");
div.innerHTML = html;
var node = div.querySelectorAll(".yhproduct");
for (var i = 0 ;i<node.length;i++){
productSkns.push(node[i].getAttribute("data-option"));
}
if(productSkns.length==0){
return html;
}
yohoproducts = initGoods(productSkns,1);
ufoproducts = initGoods(productSkns,2);
var productHtml = {};
for (var k = 0 ;k<productSkns.length;k++){
productHtml[productSkns[k]] = buildgood(productSkns[k],yohoproducts,ufoproducts);
}
for (var n = 0 ;n<node.length;n++){
var skn = node[n].getAttribute("data-option");
node[n].parentNode.insertBefore(createDocument(productHtml[skn]), node[n]);
// node[n].parentNode.insertBefore(document.createElement("br"), node[n]);
node[n].parentNode.removeChild(node[n]);
}
return div.innerHTML;
}
function initGoods(skns,type) {
var products = {};
var url=serverContextPath+"/grassArticle/queryGoods";
// var url = contextPath+"/search/queryGoods";
var query ='';
var params ='';
for (var i=0;i<skns.length;i++){
if(query==''){
query = skns[i];
}else{
query = query+","+skns[i];
}
}
if(type==1){
params = "?query="+query;
}
if(type==2){
params = "?query="+query+"&type=UFO";
}
$.ajax({
type : "post",
url : url+params,
async : false,
success : function(data){
if(data.code == 200){
for (var i=0; i<data.data.list.length;i++){
var rowdata = data.data.list[i]
products[rowdata.product_skn] = rowdata;
}
}
}
});
return products;
}
function buildgood(skn, yohoproducts,ufoprodcut) {
debugger;
var data = yohoproducts[skn];
if(!data){
data = ufoprodcut[skn];
}
if(data){
return gooddiv(data);
}
return "";
}
function gooddiv(data) {
// return '商品图:<img width=\"120\" height=\"60\" src="'+getImageUrl(data.default_images)+ '">' +
// '<h5 style=\"margin-top: 0px;margin-bottom: 0px\">商品名:'+ data.product_name +'</h5>\n'+
// '<h5 style=\"margin-top: 0px;margin-bottom: 0px\">价格:'+data.sales_price+'</h5>\n';
return '<a href="#" title="商品" class="yhproduct" data-option="'+data.product_skn+'"'+'>'+ data.product_name.substring(10)+'(skn:'+data.product_skn+')'+ '</a>'+'<br>';
}
function getImageUrl(image) {
var result='';
if (image) {
if(image.indexOf('http://') == 0){
result = image.split("?")[0];
}else{
var fileMode = image.substring(15, 17);
if(fileMode=='01'){
result= "http://"+"img11.static.yhbimg.com"+"/goodsimg"+image;
}
if(fileMode=='02'){
result= "http://"+"img12.static.yhbimg.com"+"/goodsimg"+image;
}
}
}
else {
result = ""
}
if (result) {
return result + "?imageMogr2/thumbnail/320x320/extent/320x320/background/d2hpdGU=/position/center/quality/90";
} else {
return "";
}
}
function createDocument(txt) {
var template =txt;
var doc = new DOMParser().parseFromString(template, 'text/html');
var div = doc.querySelector('.yhproduct');
return div;
}