Goods.js 19.5 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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
(function($) {
	$.extend({
		product_id : 0,
		is_advance : 'N',
		is_special : 'N',
		is_outlets : 'N',
		is_new : 'N',
		is_replenishment : 0,
		is_favorite : 0,
		is_limited : 'N',
		attribute : 1,
		price : '',
		goodsList : '',
		goods_id : 0,
		size_id : 0,
		vip_level : 0,
		is_login : 0,
		product_name: '',
		goods_status : 1,
		product_status : 1,
		user : '',
		brand_fav : 0,
		is_notify : 0,
		is_sku : 0,
		expect_arrival_time : '',
		limit_num : 0,
		sell_channels : 0,
		isspecial : 'N',
		goods : function(options){
			this.product_id = options.product_id || 0;
			this.goods_id = options.goods_id || 0;
			this.image = options.image || {};
			this.product_skn = options.product_skn || 0;
			this.getGoodsData();
		},
		getGoodsData : function(){
			if(parseInt(this.product_id) == 0){
				return false;
			}
			$.getJSON('/product/index/goodsinfo',{'product_id' : this.product_id, 'goods_id' : this.goods_id}, function(eJson){
				if(eJson.code != 200){
					console.log(eJson.message);
					return false;
				}
				var data = eJson.data;
				$.price = data['price'] || '';
				$.goodsList = data['goods'] || '';
				var promotion = data['promotion'] || '';
				var banner = data['banner'] || '';
				$.is_advance = data['is_advance'] || 'N';
				$.is_special = data['is_special'] || 'N';
				$.isspecial = data['isspecial'] || 'N';
				$.is_outlets = data['is_outlets'] || 'N';
				$.is_limited = data['is_limited'] || 'N';
				$.is_new = data['is_new'] || 'N';
				$.attribute = data['attribute'] || 1;
				$.vip_level = data['vip_level'] || 0;
				$.is_login = data['is_login'] || 0;
				$.goods_id = data['goods_id'] || $.goods_id;
				$.product_status = data['status'] || 0;
				$.is_favorite = data['is_favorite'] || 0;
				$.goods_status = data['goods_status'] || 0;
				$.is_replenishment = data['is_replenishment'] || 0;
				$.is_yohood = data['is_yohood'] || 'N';
				$.can_buy = data['can_buy'] || 'N';
				$.expect_arrival_time = data['expect_arrival_time'] || '';
				$.brand_fav = data['brand_fav'] || 0;
				$.is_notify = data['is_notify'] || 0;	
				$.limit_num = data['limit_num'] || 0;
				if ($.attribute == 3) {
					$.limit_num = 1;
				}
				$.outlets_discount = data['outlets_discount'] || 1;
				$.sell_channels = data['sell_channels'] || 0;
				$.user = data['user'] || {'headIco' : '' ,'username' : ''}
				$._price();
				$._image();
				$._color();
				$._brand();
				$._promotion(banner, promotion);
				$._outlets();
				if($.sell_channels != 2 && $.attribute != 3){
					$('.amount-sub').click(function(){
						$._augment();
					});
					$('.amount-add').click(function(){
						$._minus();
					});
				}else{
					$._client();
				}
				if ($.attribute == 3) {
					$('#size_name_text').text('选日期:');
					$('#add_min_btn').hide();
				}
				if ($.can_buy == 'N') {
		            $('.info-other').before('<div class="yohoodNotice"><h3><b>YO‘HOOD会场限定商品</b></h3><p>该商品将优先于9月20-21日上海活动会场售卖,您可在YO’HOOD展会现场通过APP扫描商品二维码进行购买。9月22日,可在线购买。</p></div>');
		        }
				return true;
			});
		},
		_price : function(){
			if($.price == ''){
				console.log('价格为空');
				return false;
			}
			$.price['is_advance'] = $.is_advance;
			$.price['expect_arrival_time'] = $.expect_arrival_time;
			$.price['attribute'] = $.attribute;
			$('.info-price').html($.tmpl($('#priceTpml').html(), $.price));
			if($.is_advance == 'N' && $.isspecial == 'N' && $.is_outlets == 'N' && $.attribute == 1)
			{
				var param = {
					'vip' : $.price['vip'],
					'vip_level' : $.vip_level,
					'is_login' : $.is_login
				};
				$('.info-price').append($.tmpl($('#vip-price').html(), param));
			}
		},
		_color : function(){
			if($.goodsList == ''){
				console.log('商品列表为空');
				return false;
			}
			var params = {
				'goods' : $.goodsList,
				'goods_id' : $.goods_id,
				'product_status' : $.product_status
			};
			$('#goodsList').html($.tmpl($('#goods-tpml').html(), params));
			$('#colorBox a').each(function(){
				$(this).click(function(){
					$.is_sku = 0;
					$.changeGoods(this);
					$._image();
					$('.color dd:last-child').hide();
				});
			});
			$('#goods_id').val($.goods_id);
			var colorName = '';
			for(k in $.goodsList){
				if($.goodsList[k]['id'] == $.goods_id){
					colorName = $.goodsList[k]['color_name'];
				}
			}
			$('#goodsColor').html('<span>颜色:</span>' + colorName);
			$._size();
		},
		changeGoods : function(obj){
			$.goods_id = $(obj).attr('goods_id');
			$('#goods_id').val($.goods_id);
			var storage = $(obj).attr('storage');
			$('#colorBox a').removeClass('cur');
			$(obj).addClass('cur');
			var colorName = $(obj).attr('color_name');
			if(parseInt(storage) < 1){
				$('.amount-unit').hide();
				$('.amount-tips').children('span').html('该颜色已售罄!');
			}else{
				$('.amount-unit').show();
				$('.amount-tips').children('span').html('');
			}
			$('#goodsColor').html('<span>颜色:</span>' + colorName);
			$._size();
		},
		_image : function(){
			if(parseInt($.goods_id) < 1){
				console.log('商品ID为空');
				return false;
			}
			if($.image == ''){
				console.log('商品图片列表为空');return false;}
			var imageList = {};
			for(k in $.image){
				if($.goods_id == $.image[k]['goods_id']){
					imageList = $.image[k]['image'];
				}
			}
			if ($.is_yohood == 'Y') {
				$('.goods-tag-small').before('<div class="absolute yohood-tag"></div>');
			}
			$('.goods-tag-small').html($.tmpl($('#imageTags-tmpl').html(), {'is_advance' : $.is_advance,'is_special' : $.is_special,'is_new' : $.is_new,'is_limited' : $.is_limited}));
			$('#bigImage').attr('src', imageList[0]['big']);
			$('.goods-img-small').html($.tmpl($('#image-tmpl').html(), imageList));
			$('.goods-img-small a').QINSlider({changeType : 'mouseenter',element : '#bigImage',className : ''});},
		_size : function(){
			if(parseInt($.goods_id) < 1){
				console.log('商品ID为空');
				return false;
			}
			if($.goodsList == ''){
				console.log('商品列表为空');
				return false;
			}
			$._clearParams();
			var sku = {};
			for(k in $.goodsList){
				if($.goodsList[k]['id'] == $.goods_id){
					$('#storage').val($.goodsList[k]['storage']);
					$.goods_status = $.goodsList[k]['status'];
					sku = $.goodsList[k]['sku'];
				}
			}
			var params = {
				'product_status' : $.product_status,
				'goods_status' : $.goods_status,
				'sku' : sku,
				'attribute':$.attribute
			};
			$('#sizeList').html($.tmpl($('#size-tpml').html(), params));
			$('#sizeList a').not(':last').each(function(){
				$(this).click(function(){
					$('#sizeList a').removeClass('cur');
					$(this).addClass('cur');
					$('#sizeList').children('span').hide();
					$._parameter(this);
				});
			});
			if($('#sizeList a').length == 1){
				var select = $('#sizeList a').eq(0);
				var storage = select.attr('num');
				select.addClass('cur');
				$._parameter(select);
			}
			$._button();
		},
		_parameter : function(obj){
			$.is_sku = 1;
			var storage = $(obj).attr('num');
			$('#sku_id').val(parseInt($(obj).attr('sku')));
			$('#size_id').val(parseInt($(obj).attr('size')));
			$('#size_name').val($(obj).attr('sizename'));
			$('#storage').val($(obj).attr('num'));
			$('#num').val(1);
			$.size_id = $('#size_id').val();
			$('.amount-unit').show();
			var text = '';
			if($.limit_num > 0){
				text += '限购' + $.limit_num + '件';
			}
			if(storage == 0){
				$('.amount-unit').hide();
				text = '该尺码已售罄!';
			}else if(storage <= 3){
				text += text != '' ? ',仅剩'+storage+'件' : '仅剩'+storage+'件';
			}
			$('.amount-tips').children('span').html(text);
			$._button();
			if($.is_replenishment == 1){$('#supply').click(function(){$._replenishment();});}
		},
		_clearParams : function(){
			$('#sku_id').val(0);
			$('#size_id').val(0);
			$('#size_name').val('');
			$('#storage').val(0);
			$('#num').val(1);
		},
		_addFavorite : function(_id){
			if($.is_login == 0){
				location.href = 'http://www.yohobuy.com/signin.html';
				return false;
			}
			$.getData(apiDomain, {method : 'open.favorite.product','product_id' : $.product_id},function(data){
				if(data.result == -1){
					location.href = 'http://www.yohobuy.com/signin.html';
					return false;
				}else if(data.result == 0){
					alert('收藏失败,请稍后再试。');
					return false;
				}
				$.is_favorite = 1;
				$._favorite(_id);
			});
		},
		_cancelFavorite : function(_id){
			if($.is_login == 0){
				location.href = 'http://www.yohobuy.com/signin.html';
				return false;
			}
			$.getData(apiDomain, {method : 'open.favorite.cancelproduct','product_id' : $.product_id},function(data){
				if(data.result == -1){
					location.href = 'http://www.yohobuy.com/signin.html';
					return false;
				}else if(data.result == 0){
					alert('取消收藏失败,请稍后再试。');
					return false;
				}
				$.is_favorite = 0;
				$._favorite(_id);
			});
		},
		_favorite : function(_id){
			var id = _id || '.button-favor';
			$(id).unbind('mouseenter').unbind('mouseleave').unbind('click');
			if($.is_favorite == 1){
				var html = '<span class="ifont favored">f</span>已收藏';
				$(id).html(html).bind('mouseenter',function(){
					$(this).html('<span class="ifont favored">f</span>取消收藏');
				}).bind('mouseleave', function(){
					$(this).html(html);
				}).click(function(){$._cancelFavorite(id);});
			}else{
				var html = '<span class="ifont">f</span>收藏商品';
				$(id).html(html).click(function(){
					$._addFavorite(id);
				});
			}
		},
		_button : function()
		{
			var params = {
				'product_status' : $.product_status,
				'goods_status' : $.goods_status,
				'is_replenishment' : $.is_replenishment,
				'storage' : $('#storage').val(),
				'is_favorite' : parseInt($.is_favorite),
				'is_notify' : $.is_notify,
				'is_sku' : $.is_sku,
				'attribute' : $.attribute,
				'is_yohood' : $.is_yohood,
				'can_buy' : $.can_buy
			};

			var html = $.tmpl($('#sale-btn').html(), params);
			$('.info-submit').children('div').html(html);
			$._favorite();
			if(params['storage'] > 0){
				$('.button-cart').click(function(){
					$._cart();
                    /** 购物车统计数据 **/
                    var _analysis = new QAnalysis;
                    _analysis.dataCollect({
                        'product_skn':__custom['product_skn']
                    });

				});
			}
		},
		_cart : function(){
			var product_id = $('#product_id').val();
			var goods_id = $('#goods_id').val();
			var size_id = $('#size_id').val();
			var num = $('#num').val();
			var promotion_id = $('#promotion_id').val();
            var sku = $('#sku_id').val();
			if(parseInt(goods_id) < 1){
				$('.color dd:last-child').show();
				return false;
			}
			if(parseInt(size_id) < 1){
				$('#sizeList').children('span').show();
				return false;
			}
			if(!/^\d+$/.test(num) || num < 1)
			{
				alert('商品数量必须是大于零的整数.');
				$('#num').val(1);
				return false;
			}
            if ($.attribute == 3) {
                window.open('http://shopping.yohobuy.com/ticket?product_sku='+sku+'&buy_number=1');
                return true;
            }
			cart.AddToCart(product_id, goods_id, size_id, num, promotion_id, "$._cartResult");
		},
		_cartResult : function(code, totalNum, totalCost, t, message){
			if(code == 200){
				$('.info-other').hide();
				$('.info-buy').children('div').eq(0).siblings();
				var param = {'total' : totalNum};
				$('.info-buy').children('div').eq(1).html($.tmpl($('#cart-result-tmpl').html(), param)).slideDown('slow').siblings().slideUp('slow');
				$('#shopping').click(function(){
					$('.info-other').show();
					$('.info-buy').children('div').eq(1).siblings();
					$('.info-buy').children('div').eq(0).slideDown('slow').siblings().slideUp('slow');
				});
			}else{
				alert(message);
				return false;
			}
		},
		_client : function(){
			$('#mobileClient').click(function(){
				$('.buy-phone').children('div').eq(0).siblings();
				$('.buy-phone').children('div').eq(1).slideDown('slow').siblings().slideUp('slow');
			});
			$('#closeMobileClient').click(function(){
				$('.buy-phone').children('div').eq(1).siblings();
				$('.buy-phone').children('div').eq(0).slideDown('slow').siblings().slideUp('slow');
			});
			$._favorite('#button-favor');
		},
		_replenishment : function(){
			if($.is_notify == 1){
				$('#supply').removeClass('button-supply').addClass('button-wait').html('<span class="ifont font16">T</span>请等待补货');
				return false;
			}
			$('.info-other').hide();
			$('.info-buy').children('div').eq(0).siblings();				 
			$('.info-buy').children('div').eq(1).html($('#mobile-message-tmpl').html()).show().slideDown('slow').siblings().slideUp('slow');
			$('#notifyBtn').click(function(){
				var mobile = $('#mobile').val();
				if(mobile == ''){
					return false;
				}
				if(!mobile.match(/^1[3|4|5|8][0-9]\d{4,8}$/)){
					return false;
				}
				$.getJSON('/product/index/notify',{'mobile' : mobile, 'product_id' : $.product_id, 'goods_id' : $.goods_id, 'product_sku' : $('#sku_id').val()}, function(data){
					var _data = data.data;
					if(_data.result == 1 || _data.result == 2){
						$.is_notify = 1;
						$('#supply').removeClass('button-supply').addClass('button-wait').html('<span class="ifont font16">T</span>请等待补货');
						$('#notifyBtn').click(function(){
							return false;
						});
						$('.info-other').show();
						$('.info-buy').children('div').eq(1).siblings();				 
						$('.info-buy').children('div').eq(0).slideDown('slow').siblings().slideUp('slow');
						if(_data.result == 2){
							alert('您已经提交了补货通知,请不要重复提交.');
						}
						return false;
					}
					alert('补货通知提交失败,请稍后再试');
					return false;
				});
			});
		},
		_augment : function()
		{
			var num = parseInt($('#num').val());
			var storage = parseInt($('#storage').val()) - 1;
			var text = '您要购买的数量已经超过了该商品的库存!';
			if($.limit_num > 0)
			{
				text = '您要购买的数量已经超过了该商品的限购数量!';
				if(storage > $.limit_num - 1){
					storage = $.limit_num - 1;
				}
			}
			if(num > storage){
				alert(text);
				return false;
			}
			num = num+1;
			$('#num').val(num);
		},
		_minus : function(){
			var num = parseInt($('#num').val());
			if(num == 1){
				alert('已经是最后一件了,就不要再减了吧!');
				return false;
			}
			num = num-1;
			$('#num').val(num);
		},
		_promotion : function(banner, promotion){
			if($.isspecial == 'Y' || $.is_outlets == 'Y' || $.attribute == 3){
				return false;
			}
			var params = {};
			params['banner'] = banner;
			params['promotion'] = promotion;
			$('.info-activity').html($.tmpl($('#promotion').html(), params));
		},
		_outlets : function () {
			if ($.is_outlets != "Y") {
				return false;
			}
			var params = {};
			params['up'] = $.outlets_discount*10;
			$('.info-activity').html($.tmpl($('#outlet').html(), params));
		},
		items : function (options){
			var defaults = {
				'url' : '',
				'onSuccess' : ''
			};
			var params = $.extend(defaults, options);
			if(params.url == ''){
				return false;
			}
			var data = {};
			for (k in params){
				if(k != 'url' && k != 'onSuccess'){
					data[k] = params[k];
				}
			}
			$.getJSON(params.url,data,function(eData){
				if(params.onSuccess != ''){
					eval(params.onSuccess(eData.data));
					return false;
				}
			});
		},
		comments : function(options){
			var page = 1;
			var total = 1;
			var defaults = {
				'nums' : '#commentsNums',
				'num' : '#commentsNum',
				'url' : '/product/index/comments',
				'btnBox' : '#commentsBtnBox',
				'tmpl' : '#comments-tpml',
				'product_id': 0,
				'parent' : '.comments',
				'changeShow' : '#userComments',
				'itemBox' : '#commentsItem',
				'showFormBtn' : '',
				'formCallback' : ''
			};
			var params = $.extend(defaults, options);
			if(params.product_id == 0){
				return false;
			}
			function item(){
				$.items({url : params.url,product_id:params.product_id,onSuccess:function(data){
					$(params.nums).html(data['total'] || 0);
					$(params.num).html(data['total'] || 0);
					total = data['pageNum'] || 1;
					var html = (data['items'] == '' || data['items'].length < 1) ? '' : $.tmpl($(params.tmpl).html(), data['items']);
					$(html).insertBefore($(params.btnBox));
				}});
			}
			item();
			if(parseInt(total) > 1 && parseInt(total) > page){
				$(params.btnBox).show();
				$(params.btnBox).children('a').eq(0).click(function(){
					item();
				});
			}else{
				$(params.btnBox).hide();
			}
			$(params.changeShow).click(function(){
				$(this).parent().parent().children().removeClass('cur');
				$(this).parent().addClass('cur');
				$(params.parent).children('dl').addClass('hidden');
				$(params.itemBox).removeClass('hidden');
			});
			if(params.showFormBtn != '' && params.formCallback != ''){
				$(params.showFormBtn).click(function(){
					eval(params.formCallback());
				});
				return false;
			}
		},
		consult : function(){
			if($.is_login == 0){
				location.href= '/signin.html?refer=' + document.location;
				return false;
			}
			$('#consultItem dd:last-child').html($.tmpl($('#counsultForm-tmpl').html(), $.user)).show();
			var text = '请输入咨询内容...';
			$('#consultContent').val(text);
			$('#consultContent').focus(function(){if($(this).val() == text){$(this).val('');}}).blur(function(){if($(this).val() == ''){$(this).val(text);}else{$(this).parent().children('.commit-tips').hide();}});
			$('.commit').children('a').eq(0).click(function(){
				var consultContent = $.trim($('#consultContent').val());
				if(consultContent == text || consultContent == ''){$('#consultContent').parent().children('.commit-tips').show();return false;}else{$('#consultContent').parent().children('.commit-tips').hide();}
				var auth_code = $.trim($('#auth_code').val());
				if(auth_code == ''){$(this).parent().children('.commit-tips').show();return false;}else{$(this).parent().children('.commit-tips').hide();}
				$.getJSON('/product/index/consult',{'consultContent' : consultContent, 'auth_code' : auth_code, 'product_id' : $.product_id},function(eData){
					if(eData.code == 200){$('#consultItem dd:last-child').html($.tmpl($('#consult-success-tmpl').html(), $.user));return false;}
					alert(eData.message);return false;
				});
			});
		},
		live : function(){
			$.items({url : '/product/index/recommends',product_id:$.product_id,onSuccess:function(data){
				var html = $.tmpl($('#like-tmpl').html(), data);
				$('#like').html(html);
			}});
		},
		recommend : function(){
			$.items({url : '/product/index/recommends',product_skn:$.product_skn,onSuccess:function(data){
                if(!data){
                    $('#like').hide();
                    return;
                }
				var html = $.tmpl($('#like-tmpl').html(), data);
				$('#like').html(html);
			}});
		},
		_brand : function(){
			var t = $.brand_fav == 0 ? '' : 'favored';
			$('#brandFavor').addClass(t).click(function(){
				var method = $.brand_fav == 0 ? 'open.favorite.brand' : 'open.favorite.cancelFavorite';
				var className = $.brand_fav == 0 ? 'favored' : '';
				if($.is_login == 0){
					location.href = 'http://www.yohobuy.com/signin.html';
					return false;
				}
				var brand_id = $(this).attr('brand');
				$.getData(apiDomain, {'method' : method, 'brand_id' : brand_id},function(data){
					if(data.result != 1){
						if($.brand_fav == 1){
							alert('取消失败,请稍后再试');
							return false;
						}
						alert('收藏品牌失败,请稍后再试');
						return false;
					}
					$.brand_fav == 0 ? $.brand_fav = 1 : $.brand_fav = 0;$('#brandFavor').removeClass('favored').addClass(className);return false;
				});
				return false;
			});
		}
	});
})(jQuery);