Merge branch 'develop' of git.dev.yoho.cn:platform/yohobuy-portal-fe into develop
Showing
1 changed file
with
191 additions
and
179 deletions
1 | -var $ = require('jquery'); | ||
2 | -var util = { | ||
3 | - __ajax: function(options, callback, notip) { | ||
4 | - if (typeof options.data == "string") { | ||
5 | - util.__tip(options.data, "warning"); | ||
6 | - return false; | ||
7 | - } else { | ||
8 | - $.ajax({ | ||
9 | - type: options.type || 'POST', | ||
10 | - url: options.url, | ||
11 | - async: options.async, | ||
12 | - dataType: 'json', | ||
13 | - data: options.data || {}, | ||
14 | - success: function(res) { | ||
15 | - res = res.data; | ||
16 | - if (("" + res.code) === "200") { | ||
17 | - notip ? (callback && callback.bind(this, res)()) : util.__tip(res.message, "success", callback && callback.bind(this, res)()); | ||
18 | - return true; | ||
19 | - } else { | 1 | +var $=require('jquery'); |
2 | +var util={ | ||
3 | + __ajax:function(options,callback,notip){ | ||
4 | + if(typeof options.data=="string"){ | ||
5 | + util.__tip(options.data,"warning"); | ||
6 | + return false; | ||
7 | + }else{ | ||
8 | + $.ajax({ | ||
9 | + type: options.type||'POST', | ||
10 | + url: options.url, | ||
11 | + async: options.async, | ||
12 | + dataType: 'json', | ||
13 | + data:options.data||{}, | ||
14 | + success: function(res) { | ||
15 | + res=res.data; | ||
16 | + if (("" + res.code) === "200") { | ||
17 | + notip?(callback&&callback.bind(this,res)()):util.__tip(res.message,"success",callback&&callback.bind(this,res)()); | ||
18 | + return true; | ||
19 | + } else { | ||
20 | (!notip) && util.__tip(res.message, "danger"); | 20 | (!notip) && util.__tip(res.message, "danger"); |
21 | + return false; | ||
22 | + } | ||
23 | + }, | ||
24 | + error:function(err){ | ||
25 | + console.log("ajax.ERROR",err); | ||
21 | return false; | 26 | return false; |
22 | } | 27 | } |
23 | - }, | ||
24 | - error: function(err) { | ||
25 | - console.log("ajax.ERROR", err); | ||
26 | - return false; | ||
27 | - } | ||
28 | - }); | ||
29 | - } | ||
30 | - }, | ||
31 | - __ajax2: function(options, callback) { | ||
32 | - if (typeof options.data == "string") { | ||
33 | - util.__tip(options.data, "warning"); | ||
34 | - } else { | ||
35 | - $.ajax({ | ||
36 | - type: 'POST', | ||
37 | - url: options.url, | ||
38 | - async: options.async, | ||
39 | - dataType: 'json', | ||
40 | - data: options.data || {}, | ||
41 | - success: function(res) { | ||
42 | - res = res.data; | ||
43 | - if (res.code != 200) { | ||
44 | - util.__tip(res.message, "danger"); | ||
45 | - } else { | ||
46 | - return callback.bind(this, res)(); | ||
47 | - } | ||
48 | - } | ||
49 | - }); | ||
50 | - } | ||
51 | - }, | ||
52 | - __tip: function(message, callback) { | ||
53 | - //danger | ||
54 | - var options = {}; | ||
55 | - options.class_name = "growl-danger"; | ||
56 | - options.title = arguments[0]; | ||
57 | - if (arguments.length > 1) { | ||
58 | - if (typeof(arguments[1]) === "string") { | ||
59 | - options.class_name = "growl-" + arguments[1]; | ||
60 | - } else { | ||
61 | - options.class_name = "growl-success"; | 28 | + }); |
62 | } | 29 | } |
63 | - var lastargs = Array.prototype.slice.call(arguments, arguments.length - 1); | ||
64 | - lastargs = lastargs[0]; | ||
65 | - if (typeof lastargs === "function") { | ||
66 | - options.after_open = setTimeout(function() { | ||
67 | - lastargs && lastargs(); | ||
68 | - }, 600); | 30 | + }, |
31 | + __ajax2:function(options,callback){ | ||
32 | + if(typeof options.data=="string"){ | ||
33 | + util.__tip(options.data,"warning"); | ||
34 | + }else{ | ||
35 | + $.ajax({ | ||
36 | + type: 'POST', | ||
37 | + url: options.url, | ||
38 | + async: options.async, | ||
39 | + dataType: 'json', | ||
40 | + data:options.data||{}, | ||
41 | + success: function(res) { | ||
42 | + res=res.data; | ||
43 | + if (res.code != 200) { | ||
44 | + util.__tip(res.message,"danger"); | ||
45 | + } else { | ||
46 | + return callback.bind(this,res)(); | ||
47 | + } | ||
48 | + } | ||
49 | + }); | ||
69 | } | 50 | } |
70 | - } | ||
71 | - options.sticky = false; | ||
72 | - options.time = 1000; | 51 | + }, |
52 | + __tip:function(message,callback){ | ||
53 | + //danger | ||
54 | + var options={}; | ||
55 | + options.class_name="growl-danger"; | ||
56 | + options.title=arguments[0]; | ||
57 | + if (arguments.length > 1) { | ||
58 | + if (typeof (arguments[1]) === "string") { | ||
59 | + options.class_name="growl-"+arguments[1]; | ||
60 | + }else{ | ||
61 | + options.class_name="growl-success"; | ||
62 | + } | ||
63 | + var lastargs = Array.prototype.slice.call(arguments, arguments.length-1); | ||
64 | + lastargs=lastargs[0]; | ||
65 | + if(typeof lastargs==="function"){ | ||
66 | + options.after_open=setTimeout(function() { | ||
67 | + lastargs&&lastargs(); | ||
68 | + },600); | ||
69 | + } | ||
70 | + } | ||
71 | + options.sticky=false; | ||
72 | + options.time=1000; | ||
73 | 73 | ||
74 | - $.gritter.add(options); | ||
75 | - }, | ||
76 | - __template: function() { | ||
77 | - var args = arguments, | ||
78 | - result; | ||
79 | - if (args.length > 0) { | ||
80 | - if (typeof args[0] === "string") { | ||
81 | - result = args[0]; | ||
82 | - if (args.length == 2 && typeof args[1] == "object") { | ||
83 | - for (var key in args[1]) { | ||
84 | - if (args[1][key] != undefined) { | ||
85 | - var regkey = ((+key) == (+key)) ? "[" + key + "]" : key; | ||
86 | - var reg = new RegExp("({" + regkey + "})", "g"); | ||
87 | - result = result.replace(reg, args[1][key]); | 74 | + $.gritter.add(options); |
75 | + }, | ||
76 | + __template: function () { | ||
77 | + var args = arguments, result; | ||
78 | + if (args.length > 0) { | ||
79 | + if (typeof args[0]==="string") { | ||
80 | + result = args[0]; | ||
81 | + if (args.length == 2 && typeof args[1]=="object") { | ||
82 | + for (var key in args[1]) { | ||
83 | + if (args[1][key] != undefined) { | ||
84 | + var regkey=((+key)==(+key))?"["+key+"]":key; | ||
85 | + var reg = new RegExp("({" + regkey + "})", "g"); | ||
86 | + result = result.replace(reg, args[1][key]); | ||
87 | + } | ||
88 | } | 88 | } |
89 | - } | 89 | + } |
90 | + var reg = new RegExp("({.*})", "g"); | ||
91 | + result = result.replace(reg, ''); | ||
90 | } | 92 | } |
91 | - var reg = new RegExp("({.*})", "g"); | ||
92 | - result = result.replace(reg, ''); | ||
93 | } | 93 | } |
94 | - } | ||
95 | - return result; | 94 | + return result; |
96 | }, | 95 | }, |
97 | - __template2: function(str, source) { | ||
98 | - var result = ""; | ||
99 | - var script = []; | ||
100 | - script.push('var each=function(obj,fn){'); | ||
101 | - script.push(' var that=this;'); | ||
102 | - script.push(' if({}.toString.call(obj)=="[object Array]"){'); | ||
103 | - script.push(' for(var i=0;i<obj.length;i++){'); | ||
104 | - script.push(' fn.call(i,i,obj[i]);'); | ||
105 | - script.push(' }'); | ||
106 | - script.push(' return;'); | ||
107 | - script.push(' }'); | ||
108 | - script.push(' if(typeof obj=="function"||typeof obj=="object"&&!!obj){'); | ||
109 | - script.push(' for(var i in obj){'); | ||
110 | - script.push(' fn.call(i,i,obj[i]);'); | ||
111 | - script.push(' }'); | ||
112 | - script.push(' return;'); | ||
113 | - script.push(' }'); | ||
114 | - script.push('}'); | ||
115 | - result = script.join('') + ";\n"; | 96 | + __template2:function(str,source){ |
97 | + // console.log(str); | ||
98 | + var result=""; | ||
99 | + var script=[]; | ||
100 | + script.push('var each=function(obj,fn){'); | ||
101 | + script.push(' var that=this;'); | ||
102 | + script.push(' if({}.toString.call(obj)=="[object Array]"){'); | ||
103 | + script.push(' for(var i=0;i<obj.length;i++){'); | ||
104 | + script.push(' fn.call(i,i,obj[i]);'); | ||
105 | + script.push(' }'); | ||
106 | + script.push(' return;'); | ||
107 | + script.push(' }'); | ||
108 | + script.push(' if(typeof obj=="function"||typeof obj=="object"&&!!obj){'); | ||
109 | + script.push(' for(var i in obj){'); | ||
110 | + script.push(' fn.call(i,i,obj[i]);'); | ||
111 | + script.push(' }'); | ||
112 | + script.push(' return;'); | ||
113 | + script.push(' }'); | ||
114 | + script.push('}'); | ||
115 | + result=script.join('')+";\n"; | ||
116 | 116 | ||
117 | - var formstr = function(code) { | 117 | + var formstr=function (code) { |
118 | return "'" + code | 118 | return "'" + code |
119 | - .replace(/('|\\)/g, '\\$1') | ||
120 | - .replace(/\r/g, '\\r') | ||
121 | - .replace(/\n/g, '\\n') + "'"; | ||
122 | - } | ||
123 | - //获取参数 | ||
124 | - var keys = []; | ||
125 | - for (var key in source) { | ||
126 | - if (typeof source[key] == "boolean") { | ||
127 | - keys.push(key + "=" + source[key]); | ||
128 | - } else { | ||
129 | - keys.push(key + "=" + JSON.stringify(source[key])); | 119 | + .replace(/('|\\)/g, '\\$1') |
120 | + .replace(/\r/g, '\\r') | ||
121 | + .replace(/\n/g, '\\n') + "'"; | ||
130 | } | 122 | } |
123 | + //获取参数 | ||
124 | + | ||
131 | 125 | ||
132 | - } | 126 | + var params=[]; |
133 | 127 | ||
134 | - //result+="var "+keys.join(',')+";"; | ||
135 | - result += "var obj=" + JSON.stringify(source) + ";"; | ||
136 | - var html = ""; | ||
137 | - html = str.replace(/\[\[(.*?)\]\]/igm, function(name, $1) { | ||
138 | - var trim = $1.replace(/^\s+|\s+$/, ''); | ||
139 | - if (/^each/.test(trim)) { | ||
140 | - trim = trim.replace(/^each\s+([a-zA-Z0-1.]+)\s+as\s+(\w+)\s+(\w+)/, function(s, $1, $2, $3) { | ||
141 | - if (!/\w+\.\w+/g.test($1)) { | ||
142 | - return 'each\( obj.' + $1 + ',function(' + $3 + ',' + $2 + '){'; | ||
143 | - } | ||
144 | - return 'each\( ' + $1 + ',function(' + $3 + ',' + $2 + '){'; | ||
145 | - }); | ||
146 | - return "[[" + trim + "]]"; | ||
147 | - } else | ||
148 | - if (/^\/each/.test(trim)) { | ||
149 | - trim = trim.replace(/^\/each/, '});') | ||
150 | - return "[[" + trim + "]]"; | ||
151 | - } else if (/^if/.test(trim)) { | ||
152 | - trim = trim.replace(/^if(.+)/, function(s, $1) { | ||
153 | - var aa = $1.replace(/^\s+|\s+$/, ''); | ||
154 | - if (!/\w+\.\w+/g.test(aa)) { | ||
155 | - return 'if(obj.' + aa + '){'; | ||
156 | - } | ||
157 | - return 'if('+$1+'){'; | ||
158 | - }); | ||
159 | - return "[[" + trim + "]]"; | ||
160 | - } else if (/^\/if/.test(trim)) { | ||
161 | - trim = trim.replace(/^\/if/, '};') | ||
162 | - return "[[" + trim + "]]"; | ||
163 | - } else if (/^else$/.test(trim)) { | ||
164 | - trim = trim.replace(/else/, '}else{'); | ||
165 | - return "[[" + trim + "]]"; | ||
166 | - } else { | ||
167 | - var aa = $1.replace(/^\s+|\s+$/, ''); | ||
168 | - if (!/\w+\.\w+/g.test(aa)) { | ||
169 | - if (!source[aa]) { | ||
170 | - return ""; | ||
171 | - } | ||
172 | - return "[[+obj." + aa + "+]]"; | 128 | + |
129 | + var html=""; | ||
130 | + html=str.replace(/\[\[(.*?)\]\]/igm,function(name,$1){ | ||
131 | + var trim=$1.replace(/^\s+|\s+$/,''); | ||
132 | + if(/^each/.test(trim)) | ||
133 | + { | ||
134 | + trim=trim.replace(/^each\s+([a-zA-Z0-1.]+)\s+as\s+(\w+)\s+(\w+)/,function($0,$1,$2,$3){ | ||
135 | + params.push($1); | ||
136 | + return 'each\( '+$1+',function('+$3+','+$2+'){'; | ||
137 | + }); | ||
138 | + return "[["+trim+"]]"; | ||
139 | + }else | ||
140 | + if(/^\/each/.test(trim)){ | ||
141 | + trim=trim.replace(/^\/each/,'});') | ||
142 | + return "[["+trim+"]]"; | ||
143 | + } | ||
144 | + else if(/^if/.test(trim)){ | ||
145 | + trim=trim.replace(/^if(.+)/,function($0,$1){ | ||
146 | + params.push($1); | ||
147 | + return 'if('+$1+'){'; | ||
148 | + }); | ||
149 | + return "[["+trim+"]]"; | ||
150 | + } | ||
151 | + else if(/^\/if/.test(trim)){ | ||
152 | + trim=trim.replace(/^\/if/,'};') | ||
153 | + return "[["+trim+"]]"; | ||
154 | + } | ||
155 | + else if(/^else$/.test(trim)){ | ||
156 | + trim=trim.replace(/else/,'}else{'); | ||
157 | + return "[["+trim+"]]"; | ||
158 | + } | ||
159 | + else{ | ||
160 | + params.push(trim); | ||
161 | + return "[[+"+trim+"+]]"; | ||
162 | + } | ||
163 | + }); | ||
164 | + console.log(params); | ||
165 | + //.match(/(\w+)+/g) | ||
166 | + for(var i in params){ | ||
167 | + var aps=params[i].split(/\+|\-|\*|\/|\=|\!|\~/g); | ||
168 | + for(var j in aps){ | ||
169 | + var ap=aps[j].replace(/^\s+|\s+$/,''); | ||
170 | + if(!/^[0-9]+$/.test(ap)&&!/^\s*$/.test(ap)&&!/\./g.test(ap)){ | ||
171 | + if(!source.hasOwnProperty(ap)){ | ||
172 | + source[ap]=""; | ||
173 | } | 173 | } |
174 | - return "[[+" + trim + "+]]"; | ||
175 | - } | ||
176 | - }); | ||
177 | - html = html.replace(/(^|\]\])(.*?)(\[\[|$)/igm, function(name, $1, $2) { | ||
178 | - var trim = $2.replace(/^\s+|\s+$/, ''); | ||
179 | - return trim ? formstr($2) : ''; | ||
180 | - }); | ||
181 | - // var result="var result='';result+="; | ||
182 | - var arr = html.split(/\n+/igm); | ||
183 | - result += "var p=[];\n"; | ||
184 | - for (var i = 0; i < arr.length; i++) { | ||
185 | - if (/^'/.test(arr[i])) { | ||
186 | - result += "p.push(" + (arr[i]) + ");\n"; | ||
187 | - } else { | ||
188 | - result += (arr[i]); | ||
189 | } | 174 | } |
190 | } | 175 | } |
191 | - result += "return p.join('');\n"; | 176 | + } |
177 | + | ||
178 | + var keys=[]; | ||
179 | + for(var key in source){ | ||
180 | + if(typeof source[key]=="boolean"){ | ||
181 | + keys.push(key+"="+source[key]); | ||
182 | + }else{ | ||
183 | + keys.push(key+"="+JSON.stringify(source[key])); | ||
184 | + } | ||
185 | + } | ||
186 | + var header="var "+keys.join(',')+";"; | ||
187 | + | ||
188 | + | ||
189 | + html=html.replace(/(^|\]\])(.*?)(\[\[|$)/igm,function(name,$1,$2){ | ||
190 | + var trim=$2.replace(/^\s+|\s+$/,''); | ||
191 | + return trim?formstr($2):''; | ||
192 | + }); | ||
193 | + // var result="var result='';result+="; | ||
194 | + var arr=html.split(/\n+/igm); | ||
195 | + result+="var p=[];\n"; | ||
196 | + for(var i=0;i<arr.length;i++){ | ||
197 | + if(/^'/.test(arr[i])){ | ||
198 | + result+="p.push("+(arr[i])+");\n"; | ||
199 | + }else{ | ||
200 | + result+=(arr[i]); | ||
201 | + } | ||
202 | + } | ||
203 | + result+="return p.join('');\n"; | ||
192 | 204 | ||
193 | 205 | ||
194 | - var fn = new Function(result); | ||
195 | - var b = fn.call(source); | ||
196 | - return b; | 206 | + var fn=new Function(header+result); |
207 | + var b=fn.call(source); | ||
208 | + return b; | ||
197 | }, | 209 | }, |
198 | - __input: function(id) { | ||
199 | - return !!~$.trim($('#' + id).val()) ? $('#' + id).val() : "" | 210 | + __input:function(id){ |
211 | + return !!~$.trim($('#'+id).val())?$('#'+id).val():"" | ||
200 | } | 212 | } |
201 | } | 213 | } |
202 | -module.exports = util; | ||
214 | +module.exports=util; |
-
Please register or login to post a comment