Authored by 陈峰

js优化

@@ -83,5 +83,4 @@ let indexObj = { @@ -83,5 +83,4 @@ let indexObj = {
83 83
84 $(() => { 84 $(() => {
85 indexObj.init(); 85 indexObj.init();
86 -})  
87 -;  
  86 +});
@@ -13,54 +13,37 @@ let $ = require('yoho-jquery'), @@ -13,54 +13,37 @@ let $ = require('yoho-jquery'),
13 let cartObj = { 13 let cartObj = {
14 init(handle) { 14 init(handle) {
15 let self = this; 15 let self = this;
16 - let cartType = window.cookie('cartType') || 'ordinary';  
17 16
18 self.handle = handle; 17 self.handle = handle;
19 18
20 $('.cart-nav').on('click', 'li', function() { 19 $('.cart-nav').on('click', 'li', function() {
21 - let type = $(this).data('type');  
22 -  
23 - window.setCookie('cartType', type);  
24 - self.handle.refreshPage(''); 20 + self.cartNavClick();
25 }); 21 });
26 - $('.more-box>.down-arrow').on('click', function() {  
27 - $(this).parent().toggleClass('down'); 22 + $('.more-box>.down-arrow').on('click', function(e) {
  23 + self.arrowClick(e);
28 }); 24 });
29 $('.btn-balance').on('click', function() { 25 $('.btn-balance').on('click', function() {
30 - self.toBalance(); 26 + self.balanceClick();
31 }); 27 });
32 - $('.promo-item').on('click', function() {  
33 - let promotionId = $(this).data('id');  
34 - let promotionType = $(this).data('type');  
35 - let promotionTitle = encodeURIComponent($(this).data('title'));  
36 - let title = '促销商品';  
37 -  
38 - if (promotionType === 'Gift') {  
39 - if ($(this).data('status') === 30) {  
40 - window.location.href = `/cart/index/new/gift?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}&edit=1`;  
41 - } else {  
42 - window.location.href = `/cart/index/new/gift?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}`;  
43 - }  
44 -  
45 - } else if (promotionType === 'Needpaygift') {  
46 - if ($(this).data('status') === 30) {  
47 - window.location.href = `/cart/index/new/advanceBuy?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}&edit=1`;  
48 - } else {  
49 - window.location.href = `/cart/index/new/advanceBuy?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}`;  
50 - }  
51 -  
52 - } else {  
53 - window.location.href = `/product/index/index?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}`;  
54 - }  
55 - 28 + $('.promo-item').on('click', function(e) {
  29 + self.promoItemClick(e);
56 }); 30 });
57 $('.all-gift-box').on('click', '.gift-item', (e) => { 31 $('.all-gift-box').on('click', '.gift-item', (e) => {
58 - if ($(e.currentTarget).hasClass('advanceBuy')) {  
59 - window.location.href = '/cart/index/new/advanceBuy?cartType=' + cartType;  
60 - } else {  
61 - window.location.href = '/cart/index/new/gift?cartType=' + cartType;  
62 - } 32 + self.allGiftBoxClick(e);
63 }); 33 });
  34 + self.initPresellTip();
  35 + },
  36 + arrowClick(e) {
  37 + $(e.currentTarget).parent().toggleClass('down');
  38 + },
  39 + cartNavClick() {
  40 + let self = this;
  41 + let type = $(this).data('type');
  42 +
  43 + window.setCookie('cartType', type);
  44 + self.handle.refreshPage('');
  45 + },
  46 + initPresellTip() {
64 if (typeof window.cookie === 'function' && window.cookie('_hasShowCartPresellTip') === 'y') { 47 if (typeof window.cookie === 'function' && window.cookie('_hasShowCartPresellTip') === 'y') {
65 $('#presell-tip').removeClass('show').addClass('hide'); 48 $('#presell-tip').removeClass('show').addClass('hide');
66 } else { 49 } else {
@@ -71,12 +54,45 @@ let cartObj = { @@ -71,12 +54,45 @@ let cartObj = {
71 }, 3000); 54 }, 3000);
72 } 55 }
73 }, 56 },
74 - toBalance() { 57 + promoItemClick(e) {
  58 + let cartType = window.cookie('cartType') || 'ordinary';
  59 + let promotionId = $(e.currentTarget).data('id');
  60 + let promotionType = $(e.currentTarget).data('type');
  61 + let promotionTitle = encodeURIComponent($(e.currentTarget).data('title'));
  62 + let title = '促销商品';
  63 +
  64 + if (promotionType === 'Gift') {
  65 + if ($(e.currentTarget).data('status') === 30) {
  66 + window.location.href = `/cart/index/new/gift?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}&edit=1`;
  67 + } else {
  68 + window.location.href = `/cart/index/new/gift?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}`;
  69 + }
  70 +
  71 + } else if (promotionType === 'Needpaygift') {
  72 + if ($(e.currentTarget).data('status') === 30) {
  73 + window.location.href = `/cart/index/new/advanceBuy?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}&edit=1`;
  74 + } else {
  75 + window.location.href = `/cart/index/new/advanceBuy?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}`;
  76 + }
  77 +
  78 + } else {
  79 + window.location.href = `/product/index/index?promotion_id=${promotionId}&title=${title}&intro_text=${promotionTitle}&cartType=${cartType}`;
  80 + }
  81 + },
  82 + allGiftBoxClick(e) {
75 let cartType = window.cookie('cartType') || 'ordinary'; 83 let cartType = window.cookie('cartType') || 'ordinary';
76 84
  85 + if ($(e.currentTarget).hasClass('advanceBuy')) {
  86 + window.location.href = '/cart/index/new/advanceBuy?cartType=' + cartType;
  87 + } else {
  88 + window.location.href = '/cart/index/new/gift?cartType=' + cartType;
  89 + }
  90 + },
  91 + balanceClick() {
  92 + let cartType = window.cookie('cartType') || 'ordinary';
77 93
78 if (window._yas && window._yas.sendCustomInfo) { 94 if (window._yas && window._yas.sendCustomInfo) {
79 - var productId = Array.from($('.good-item.is-checked').map((i, e) => $(e).data('id'))); 95 + let productId = Array.from($('.good-item.is-checked').map((i, e) => $(e).data('id')));
80 96
81 setTimeout(function() { 97 setTimeout(function() {
82 if (window._yas && window._yas.sendCustomInfo) { 98 if (window._yas && window._yas.sendCustomInfo) {
@@ -17,29 +17,85 @@ let goodObj = { @@ -17,29 +17,85 @@ let goodObj = {
17 let self = this; 17 let self = this;
18 18
19 self.handle = handle; 19 self.handle = handle;
20 -  
21 $('.good-item').on('click', '.num-opt .btn', function(e) { 20 $('.good-item').on('click', '.num-opt .btn', function(e) {
22 - self.modifyNum(e); 21 + self.modifyNumClick(e);
23 }); 22 });
24 $('.good-item').on('click', '.chk.select', function(e) { 23 $('.good-item').on('click', '.chk.select', function(e) {
25 - if (!$(this).hasClass('checked') && $(e.delegateTarget).find('.low-stocks').length > 0) {  
26 - tip.show('库存不足,无法购买');  
27 - return false;  
28 - }  
29 - self.selectGood($(this)); 24 + self.checkSelectClick(e);
30 }); 25 });
31 - $('.good-item').on('click', '.chk.edit', function() {  
32 - $(this).toggleClass('checked'); 26 + $('.good-item').on('click', '.chk.edit', function(e) {
  27 + self.checkClick(e);
33 }); 28 });
34 -  
35 $('.btn-fav').on('click', function() { 29 $('.btn-fav').on('click', function() {
36 - if (!$('.good-item .chk.edit.checked').length) {  
37 - tip.show('请至少选择一件商品');  
38 - return; 30 + self.favClick();
  31 + });
  32 + $('.good-item').on('click', '.size-info', function(e) {
  33 + self.sizeInfoClick(e);
  34 + });
  35 + $('.good-item').on('click', '.name,.color-size-row,.img', function(e) {
  36 + self.goodClick(e);
  37 + });
  38 + $('.check-all').on('click', function() {
  39 + self.checkedAllClick();
  40 + });
  41 + $('.btn-del').on('click', function() {
  42 + self.delClick();
  43 + });
  44 + $('.btn-remove').on('click', function() {
  45 + self.removeInvalidClick();
  46 + });
  47 + self.clearLowStock();
  48 + },
  49 + checkClick(e) {
  50 + $(e.currentTarget).toggleClass('checked');
  51 + },
  52 + removeInvalidClick() {
  53 + dialog.showDialog({
  54 + dialogText: '您确定要清空失效商品吗?',
  55 + hasFooter: {
  56 + leftBtnText: '取消',
  57 + rightBtnText: '确定'
  58 + }
  59 + }, function() {
  60 + ((callback) => {
  61 + let promise = self.removeAllDisabled();
  62 +
  63 + if (promise) {
  64 + promise.then(() => {
  65 + callback();
  66 + }, () => {
  67 + dialog.hideDialog();
  68 + });
  69 + } else {
  70 + return callback();
  71 + }
  72 + })(() => {
  73 + dialog.showDialog({
  74 + dialogText: '清空成功',
  75 + autoHide: true,
  76 + fast: true
  77 + });
  78 + });
  79 +
  80 + });
  81 + },
  82 + delClick() {
  83 + let self = this;
  84 +
  85 + if (!$('.good-item .chk.edit.checked').length) {
  86 + tip.show('请至少选择一件商品');
  87 + return;
  88 + }
  89 + dialog.showDialog({
  90 + dialogText: '您确定要从购物车中删除吗?',
  91 + hasFooter: {
  92 + leftBtnText: '取消',
  93 + rightBtnText: '确定'
39 } 94 }
40 - self.favGood($('.good-item .chk.edit.checked')).then(() => { 95 + }, function() {
  96 + self.delGood($('.good-item .chk.edit.checked')).then(() => {
41 dialog.showDialog({ 97 dialog.showDialog({
42 - dialogText: '收藏成功', 98 + dialogText: '删除成功',
43 autoHide: true, 99 autoHide: true,
44 fast: true 100 fast: true
45 }); 101 });
@@ -47,103 +103,74 @@ let goodObj = { @@ -47,103 +103,74 @@ let goodObj = {
47 dialog.hideDialog(); 103 dialog.hideDialog();
48 }); 104 });
49 }); 105 });
50 - $('.good-item').on('click', '.size-info', function(e) {  
51 - let isGift = $(e.delegateTarget).hasClass('is-gift'); 106 + },
  107 + goodClick(e) {
  108 + if ($(e.delegateTarget).hasClass('in-valid') && !$(e.delegateTarget).hasClass('in-valid-low')) {
  109 + tip.show('商品已下架');
  110 + } else {
  111 + window.location.href = $(e.delegateTarget).data('link');
  112 + }
  113 + },
  114 + checkSelectClick(e) {
  115 + let self = this;
  116 +
  117 + if (!$(e.currentTarget).hasClass('checked') && $(e.delegateTarget).find('.low-stocks').length > 0) {
  118 + tip.show('库存不足,无法购买');
  119 + return false;
  120 + }
  121 + self.selectGood($(e.currentTarget));
  122 + },
  123 + sizeInfoClick(e) {
  124 + let self = this;
  125 + let isGift = $(e.delegateTarget).hasClass('is-gift');
52 126
53 - self.openChosepanel(e, isGift); 127 + self.openChosepanel(e, isGift);
  128 + },
  129 + favClick() {
  130 + let self = this;
  131 +
  132 + if (!$('.good-item .chk.edit.checked').length) {
  133 + tip.show('请至少选择一件商品');
  134 + return;
  135 + }
  136 + self.favGood($('.good-item .chk.edit.checked')).then(() => {
  137 + dialog.showDialog({
  138 + dialogText: '收藏成功',
  139 + autoHide: true,
  140 + fast: true
  141 + });
  142 + }, () => {
  143 + dialog.hideDialog();
54 }); 144 });
55 - $('.good-item').on('click', '.name,.color-size-row,.img', function(e) {  
56 - if ($(e.delegateTarget).hasClass('in-valid') && !$(e.delegateTarget).hasClass('in-valid-low')) {  
57 - tip.show('商品已下架'); 145 + },
  146 + checkedAllClick() {
  147 + if (self.handle.editMode) {
  148 + $(this).find('.chk.edit').toggleClass('checked');
  149 + if ($(this).find('.chk.edit').hasClass('checked')) {
  150 + $('.good-item .chk.edit').addClass('checked');
58 } else { 151 } else {
59 - window.location.href = $(e.delegateTarget).data('link'); 152 + $('.good-item .chk.edit').removeClass('checked');
60 } 153 }
61 - });  
62 - $('.check-all').on('click', function() {  
63 - if (self.handle.editMode) {  
64 - $(this).find('.chk.edit').toggleClass('checked');  
65 - if ($(this).find('.chk.edit').hasClass('checked')) {  
66 - $('.good-item .chk.edit').addClass('checked');  
67 - } else {  
68 - $('.good-item .chk.edit').removeClass('checked');  
69 - }  
70 - } else {  
71 - $(this).find('.chk.select').toggleClass('checked');  
72 - let checked = $(this).find('.chk.select').hasClass('checked'); 154 + } else {
  155 + $(this).find('.chk.select').toggleClass('checked');
  156 + let checked = $(this).find('.chk.select').hasClass('checked');
73 157
74 - if (checked) {  
75 - let promise = self.clearLowStock(); 158 + if (checked) {
  159 + let promise = self.clearLowStock();
76 160
77 - if (promise) {  
78 - tip.show('您全选的商品中存在库存不足商品,已帮您自动取消勾选');  
79 - promise.then(() => {  
80 - self.selectGood($('.good-item:not(.low-stocks) .chk.select'), true);  
81 - });  
82 - } else { 161 + if (promise) {
  162 + tip.show('您全选的商品中存在库存不足商品,已帮您自动取消勾选');
  163 + promise.then(() => {
83 self.selectGood($('.good-item:not(.low-stocks) .chk.select'), true); 164 self.selectGood($('.good-item:not(.low-stocks) .chk.select'), true);
84 - } 165 + });
85 } else { 166 } else {
86 - self.selectGood($('.good-item .chk.select'), false); 167 + self.selectGood($('.good-item:not(.low-stocks) .chk.select'), true);
87 } 168 }
88 -  
89 - }  
90 - });  
91 - $('.btn-del').on('click', function() {  
92 - if (!$('.good-item .chk.edit.checked').length) {  
93 - tip.show('请至少选择一件商品');  
94 - return; 169 + } else {
  170 + self.selectGood($('.good-item .chk.select'), false);
95 } 171 }
96 - dialog.showDialog({  
97 - dialogText: '您确定要从购物车中删除吗?',  
98 - hasFooter: {  
99 - leftBtnText: '取消',  
100 - rightBtnText: '确定'  
101 - }  
102 - }, function() {  
103 - self.delGood($('.good-item .chk.edit.checked')).then(() => {  
104 - dialog.showDialog({  
105 - dialogText: '删除成功',  
106 - autoHide: true,  
107 - fast: true  
108 - });  
109 - }, () => {  
110 - dialog.hideDialog();  
111 - });  
112 - });  
113 -  
114 - });  
115 - $('.btn-remove').on('click', function() {  
116 - dialog.showDialog({  
117 - dialogText: '您确定要清空失效商品吗?',  
118 - hasFooter: {  
119 - leftBtnText: '取消',  
120 - rightBtnText: '确定'  
121 - }  
122 - }, function() {  
123 - ((callback) => {  
124 - let promise = self.removeAllDisabled();  
125 -  
126 - if (promise) {  
127 - promise.then(() => {  
128 - callback();  
129 - }, () => {  
130 - dialog.hideDialog();  
131 - });  
132 - } else {  
133 - return callback();  
134 - }  
135 - })(() => {  
136 - dialog.showDialog({  
137 - dialogText: '清空成功',  
138 - autoHide: true,  
139 - fast: true  
140 - });  
141 - });  
142 172
143 - });  
144 -  
145 - });  
146 - self.clearLowStock(); 173 + }
147 }, 174 },
148 selectGood(eles, selectAll) { 175 selectGood(eles, selectAll) {
149 let self = this; 176 let self = this;
@@ -207,25 +234,31 @@ let goodObj = { @@ -207,25 +234,31 @@ let goodObj = {
207 return ele; 234 return ele;
208 }); 235 });
209 236
210 - return $.ajax({  
211 - type: 'post',  
212 - url: '/cart/index/new/col',  
213 - data: {  
214 - skuList: JSON.stringify(skuData)  
215 - }  
216 - }).then((data) => {  
217 - if (data.code === 200) {  
218 - self.handle.refreshPage(data.data);  
219 - } else if (data.code === 400) {  
220 - tip.show('网络异常');  
221 - } else if (data.code === 401) {  
222 - tip.show(data.message);  
223 - setTimeout(() => {  
224 - window.location.href = data.data;  
225 - }, 1000);  
226 - }  
227 - }, err => {  
228 - tip.show(err.responseJSON ? err.responseJSON.message : '网络异常'); 237 + return new Promise((resolve, reject) => {
  238 + $.ajax({
  239 + type: 'post',
  240 + url: '/cart/index/new/col',
  241 + data: {
  242 + skuList: JSON.stringify(skuData)
  243 + }
  244 + }).then((data) => {
  245 + if (data.code === 200) {
  246 + self.handle.refreshPage(data.data);
  247 + return resolve();
  248 + } else if (data.code === 400) {
  249 + tip.show('网络异常');
  250 + return reject();
  251 + } else if (data.code === 401) {
  252 + tip.show(data.message);
  253 + setTimeout(() => {
  254 + window.location.href = data.data;
  255 + }, 1000);
  256 + return reject();
  257 + }
  258 + }, err => {
  259 + tip.show(err.responseJSON ? err.responseJSON.message : '网络异常');
  260 + return reject();
  261 + });
229 }); 262 });
230 }, 263 },
231 getSelectGoodData(eles, selectAll) { 264 getSelectGoodData(eles, selectAll) {
@@ -282,7 +315,7 @@ let goodObj = { @@ -282,7 +315,7 @@ let goodObj = {
282 } 315 }
283 }); 316 });
284 }, 317 },
285 - modifyNum(e) { 318 + modifyNumClick(e) {
286 let self = this; 319 let self = this;
287 320
288 if (self.handle.posting) { 321 if (self.handle.posting) {