common.js
16.8 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
* @fileOverview 公共模块
* @author:Hbomb(zhouqq@yoho.cn)
* @date:2012-07-20
*/
define(
'common',
function(require, exports)
{
var $ = require("jquery");// jquery
require("lib/ui/jquery.ui");// jqueryui
var tools = require("lib/util/tools");
require("lib/util/json");
var dialog = require('dialog');
require('lib/util/jquery.imagesLoaded');
$.ajaxSetup({"cache":false});
/**
* URL生成方法
*
* @param string udi
* @param array params 参数
* @param banAgent 是否禁止代理
* @return string
*/
exports.U = function(udi, params, banAgent)
{
var module = 'default';
var namespace = 'default';
var controller = 'default';
var action = 'index';
var website = YohoConfig.baseUrl + 'index.php';
var namespace_end = udi.indexOf('::');
var module_begin = udi.indexOf('@');
if (module_begin != -1)
{
module = udi.substr(module_begin + 1);
}
else
{
module_begin = udi.length;
}
if (namespace_end != -1)
{
namespace = udi.substr(0, namespace_end);
}
else
{
namespace_end = 0;
}
var url = udi.substring(namespace_end + 2, module_begin);
url = url.split('/');
if (url[0])
{
controller = url[0];
}
if (url[1])
{
action = url[1];
}
if(banAgent != true)
{
//当二级域名时,改变地址
var href = window.location.host ;
var host = href.split('.');
if(host[0] && host[0] == 'my')
{
module = 'default' ;
namespace = 'my';
controller = 'ajax' ;
action = 'index' ;
params = tools.isArray(params)?params:[];
params.push("forward="+udi);
}
}
website = website + '?module=' + module + '&namespace='
+ namespace + '&controller=' + controller + '&action='
+ action;
if (params)
{
params = params.join('&');
website = website + '&' + params;
}
return website;
};
/**
* 前端判断是否登录
*
* @returns
*/
exports.isLogin = function()
{
var cookie = tools.cookie('yh_merge_new');
if (typeof (cookie) == 'undefined' || !cookie)
{
return false;
}
else
{
var temp = cookie.split(",");
var _UID = temp[0];
if (typeof (_UID) != 'undefined' && _UID)
{
this.QIS_LOGIN = true;
}
return _UID ? _UID : false;
}
};
/**
* 获取图片地址
* @param string imageUrl 图片地址
* @param string type 尺寸
* @param string project 图片类型
* @param int cropType 切图方式(整数)
*/
exports.getImages = function(imageUrl, type, project, cropType)
{
var qiniuRules = {
'0' : '?imageMogr2/thumbnail/!{size}r/crop/{size}/strip/quality/80',
'1' : '?imageMogr2/thumbnail/{size}/strip/quality/80', // 缩略图方式
'2' :'?imageMogr2/thumbnail/{size}/strip/quality/80', // 贴图方式
'3' : '?imageMogr2/crop/{size}/strip/quality/80', // 剪切方式
'4' :'?imageMogr2/thumbnail/{size}/strip/quality/80', // 大小缩放方式
'5' : '?imageMogr2/thumbnail/!{size}r/gravity/Center/crop/{size}/strip/quality/80', // 缩略剪切方式
'6' : '?imageMogr2/thumbnail/{size}/strip/quality/80', // 自定义的, 活动瀑布流的图片切图方式
'primary' : '?imageMogr2/thumbnail/!{size}r/crop/{size}/strip/quality/80' // 内部自定义
};
if (!imageUrl)
{
return '';
}
// 地址: 站内站外的绝对地址
if (imageUrl.indexOf('http://')>-1 || imageUrl.indexOf('https://')>-1)
{
return imageUrl;
}
// 地址: 老社区用户的头像或用户相册的照片
if (imageUrl.toLowerCase().indexOf('/user/')>-1 || imageUrl.toLowerCase().indexOf('/photo/')>-1)
{
return 'http://www.yoho.cn' + imageUrl;
}
if(!type)
{
type = '9999x9999' ;
}
var imgArr = imageUrl.split("/");
if (imgArr.length < 5)
{
return '';
}
if(imageUrl.substr(0,4) == 'http')
{
return imageUrl ;
}
//组装图片服务器地址
var newImages = 'http://img'
+ '0'+( parseInt (imgArr[imgArr.length - 1].substr(1, 1))+2) +'.'+ YohoConfig.resdomain;
if (type == 'source')
{
newImages = 'http://img'
+ '0'+parseInt (imgArr[imgArr.length - 1].substr(1, 1)) +'.'+ YohoConfig.resdomain+"/";
newImages += project + imageUrl;
}
else
{
var typeArr = type.split("x");
if (typeArr[0].length > 4 || typeArr[1].length > 4)
{
return '';
}
//填充尺寸,补齐四位
var prefix = '0';
if(typeArr[0].length < 4)
{
for(var li = 1 ; li <= (4-typeArr[0].length) ;li++)
{
typeArr[0] = prefix + typeArr[0] ;
}
}
if(typeArr[1].length < 4)
{
for(var li = 1 ; li <= (4-typeArr[1].length) ;li++)
{
typeArr[1] = prefix + typeArr[1] ;
}
}
//处理切图方式
if(! cropType)
{
cropType = '0';
}
try
{
var imginfo = imageUrl.split('.');
//var filename = '/' + project + imginfo[0];
//filename += '-' + type + '-' + cropType + '.'+imginfo[1];
//七牛云
var rule = '';
$.each(qiniuRules, function(key, val)
{
if(key == cropType)
{
rule = val.replace(/\{size\}/g, type);
return;
}
});
var filename = '/' + project + imginfo[0] + '.'+ imginfo[1] + rule;
newImages += filename;
}
catch (err)
{
newImages = "";
}
}
return newImages;
};
/**
* 提示
*/
exports.alert = function(str,target)
{
var id = tools.genDefID('alert');
dialog.open(
{
con:'<div style="padding:12px;width:250px">'+str+'</div>',
title:'提示',
buttons:'<div class="rel_footer">'+
'<a href="javascript:void(0)" id="alert_ok" class="btn btn27">确定</a></div>',
noclose:true,
id:id,
alpha:0,
zindex:1000000,
width:260
});
$("#alert_ok").click(function()
{
dialog.close(id);
});
};
/**
* 登陆弹出层
*/
exports.loginLayer = function(str,target)
{
dialog.open(
{
con:'<div style="padding:12px;width:300px;margin: 0 auto;">'+str+'</div>',
title:'你还未登录',
buttons:'',
noclose:true,
id: "loginLayer",
alpha:60,
zindex:1000000,
width:530
});
$("#alert_ok").click(function()
{
dialog.close(id);
});
};
/**
* 对话弹出层
*/
exports.confirm = function(str,callback,isdel)
{
var id = tools.genDefID('confirm');
var but= '<div class="rel_footer">'+
'<a href="javascript:void(0)" id="alert_ok" class="btn btn27">确定</a>'+
'<a href="javascript:void(0)" id="alert_cancel" class="btn btn4">取消</a></div>';
var but1 = '<div class="rel_footer">'+
'<a href="javascript:void(0)" id="alert_ok" class="btn btn5">删除</a>'+
'<a href="javascript:void(0)" id="alert_cancel" class="btn btn4">取消</a></div>';
dialog.open(
{
con:'<div style="padding:12px;width:360px;text-align: center; padding: 0 10px;margin: 50px 0; "><span style="background: url('+YohoConfig.resUrl+
'/res/www/images/ui/tip/icons_3.jpg) no-repeat 0px 0px;padding-left: 20px;display: inline;">'+
str+'</span></div>',
title:'提示',
buttons:isdel?but1:but,
noclose:true,
id:id,
alpha:0,
zindex:1000000,
width:360
});
$("#"+id).css("background",'#fff');
$("#alert_ok").click(function()
{
callback();
dialog.close(id);
});
$("#alert_cancel").click(function()
{
dialog.close(id);
});
};
/**
* 验证值是否在数组中
*/
exports.in_array = function(needle, haystack)
{
if(typeof needle == 'string' || typeof needle == 'number')
{
for(var i in haystack)
{
if(haystack[i] == needle)
{
return true;
}
}
}
return false;
};
exports.httpHtml = function(str)
{
if(str)
{
var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
return str.replace(reg, '<a href="$1$2">$1$2</a>');
}
};
exports.errorProc = function()
{
$("img").imagesLoaded(function()
{
$(this).each(function()
{
var error = false;
if (!this.complete)
{
error = true;
}
if (typeof this.naturalWidth != "undefined" && this.naturalWidth == 0)
{
error = true;
}
if(error)
{
function procerror(obj)
{
$(obj).attr("old_src",obj.src);
obj.src = YohoConfig.resUrl+'/res/www/images/ui/space.gif';
$(obj).unbind('error.replaceSrc');
//if(window.location.href.indexOf("/channel/photo.html")>-1
//||window.location.href.indexOf("/channel/market.html")>-1)
{
var big_view = $(obj).parents(".shoot_photobox");
if(big_view.length>0)
{
big_view.remove();
}
else
{
if($("#photo_pan").freetile)
{
$(obj).parents(".state_box").remove();
$("#photo_pan").freetile({animate: false,elementDelay: 0});
}
}
}
if($("#detail_page").length>0||$(".i_book").length>0)
{
$(obj).parents("li").remove();
}
}
$(this).bind('error.replaceSrc',function()
{
procerror(this);
});
$(this).attr("src",$(this).attr("src")+"?r="+Math.random());
}
});
});
};
//自主采集代码
exports.analysisLog = function(event, params)
{
var json = require("lib/util/json");
try
{
var formID = "logform_"+Math.floor(Math.random() * 100000);
var iframe = '<iframe name="analysis_yoho" id="analysis_yoho" style="display:none"></iframe>';
var form = '<form action="http://analysis.yoho.cn/log.php" id="'+formID+'" name="'+formID+'" method="post" target="analysis_yoho">'
+ '<input type="hidden" name="event" value="{event}"/><input type="hidden" name="params" value={params} /></form>';
$('body').append(iframe);
window.setTimeout(function()
{
form = form.replace("{event}", event).replace("{params}", json.stringify(params));
$("#analysis_yoho").contents().find('#'+formID).remove();
$("#analysis_yoho").contents().find('body').append(form);
$("#analysis_yoho").contents().find('#'+formID).submit();
}, 10);
} catch(err)
{
return true;
}
};
//事件数据采集
exports.evCollect = function(category, action, opt_label)
{
try
{
_gaq.push(['_trackEvent', category, action, opt_label]);
_hmt.push(['_trackEvent', category, action, opt_label]);
}
catch(err)
{
//nothing
}
};
//自定义变量采集
exports.varCollect = function(index, name, value, opt_scope)
{
try
{
_hmt.push(['_setCustomVar',index, name, value, opt_scope]);
_gaq.push(['_setCustomVar',index, name, value, opt_scope]);
}
catch(err)
{
//nothing
}
};
});