Authored by 郝肖肖

搜索历史记录代码找回及优化

@@ -25,7 +25,6 @@ $('.address-item').on('touchend', function() { @@ -25,7 +25,6 @@ $('.address-item').on('touchend', function() {
25 25
26 orderInfo('addressId', addressId); 26 orderInfo('addressId', addressId);
27 orderInfo('address', address); 27 orderInfo('address', address);
28 - console.log($this.data('href') + (orderInfo('limitUrlSufix') || ''));  
29 window.location.href = $this.data('href') + (orderInfo('limitUrlSufix') || ''); 28 window.location.href = $this.data('href') + (orderInfo('limitUrlSufix') || '');
30 }).on('touchend', '.edit', function() { 29 }).on('touchend', '.edit', function() {
31 window.location.href = $(this).data('href'); 30 window.location.href = $(this).data('href');
@@ -21,28 +21,17 @@ var $historySearch = $('.history-search'); @@ -21,28 +21,17 @@ var $historySearch = $('.history-search');
21 var $clearHistory = $('#clear-history'); 21 var $clearHistory = $('#clear-history');
22 var $buriedpoint = $('.buriedpoint'); 22 var $buriedpoint = $('.buriedpoint');
23 var $search = $('#search'); 23 var $search = $('#search');
  24 +var searchUrl = $search.closest('form').attr('action');
24 25
25 var writeSearch = require('./write-search'); 26 var writeSearch = require('./write-search');
26 27
27 var ranToken = writeSearch.getRanToken(); 28 var ranToken = writeSearch.getRanToken();
  29 +var historyval = writeSearch.getHistoryval();
28 30
29 var chHammer, cHammer; 31 var chHammer, cHammer;
30 32
31 chHammer = new Hammer($clearHistory[0]); 33 chHammer = new Hammer($clearHistory[0]);
32 34
33 -function isLocalStorageSupported() {  
34 - var testKey = 'test',  
35 - storage = Window.prototype.localStorage;  
36 -  
37 - try {  
38 - storage.setItem(testKey, 'testValue');  
39 - storage.removeItem(testKey);  
40 - return true;  
41 - } catch (error) {  
42 - return false;  
43 - }  
44 -}  
45 -  
46 chHammer.on('tap', function() { 35 chHammer.on('tap', function() {
47 dialog.showDialog({ 36 dialog.showDialog({
48 dialogText: '您确定要删除您的最近搜索吗?', 37 dialogText: '您确定要删除您的最近搜索吗?',
@@ -51,8 +40,9 @@ chHammer.on('tap', function() { @@ -51,8 +40,9 @@ chHammer.on('tap', function() {
51 rightBtnText: '确定' 40 rightBtnText: '确定'
52 } 41 }
53 }, function() { 42 }, function() {
54 - if (isLocalStorageSupported()) {  
55 - localStorage.removeItem('historys'); 43 +
  44 + if (localStorage) {
  45 + localStorage.removeItem(historyval);
56 } 46 }
57 47
58 $history.html(''); 48 $history.html('');
@@ -112,8 +102,7 @@ function inputAction() { @@ -112,8 +102,7 @@ function inputAction() {
112 } 102 }
113 103
114 $searchAssociate.find('li').on('touchend', function() { 104 $searchAssociate.find('li').on('touchend', function() {
115 - $buriedpoint.val($(this).find('.keyword').html());  
116 - $search.closest('form').submit(); 105 + GoSearch($(this).find('.keyword').html());
117 }); 106 });
118 }, 107 },
119 error: function() { 108 error: function() {
@@ -123,6 +112,28 @@ function inputAction() { @@ -123,6 +112,28 @@ function inputAction() {
123 }); 112 });
124 } 113 }
125 114
  115 +//跳到搜索页
  116 +function GoSearch(query) {
  117 + //保存搜索的内容
  118 + writeSearch.setHistoryValFun(query);
  119 + document.location.href = searchUrl + '?query=' + query;
  120 +}
  121 +
  122 +//热门搜索、最近搜索事件
  123 +$('.search-items .search-group').on('click', 'li', function(event) {
  124 + var query = '';
  125 +
  126 + if (event.target.nodeName === 'A') {
  127 + query = $(event.target).html();
  128 + }
  129 +
  130 + if (event.target.nodeName === 'LI') {
  131 + query = $(event.target).find('a').html();
  132 + }
  133 +
  134 + GoSearch(query);
  135 +});
  136 +
126 inputAction(); 137 inputAction();
127 138
128 cHammer = new Hammer($clear[0]); 139 cHammer = new Hammer($clear[0]);
@@ -131,13 +142,19 @@ cHammer.on('tap', function() { @@ -131,13 +142,19 @@ cHammer.on('tap', function() {
131 }); 142 });
132 143
133 $search.on('touchend', function() { 144 $search.on('touchend', function() {
134 - if ($buriedpoint.val() === '' && $('#default-terms').val() !== '') { 145 + var $buriedpoint = $form.find('.buriedpoint');
  146 +
  147 + if ($buriedpoint.val() === '') {
135 $buriedpoint.val($('#default-terms').val()); 148 $buriedpoint.val($('#default-terms').val());
136 } 149 }
137 150
  151 + //保存搜索的内容
  152 + writeSearch.setHistoryValFun($buriedpoint.val());
  153 +
138 if (security.hasDangerInput()) { 154 if (security.hasDangerInput()) {
139 return false; 155 return false;
140 } 156 }
  157 +
141 $(this).closest('form').submit(); 158 $(this).closest('form').submit();
142 return false; 159 return false;
143 }); 160 });
@@ -146,13 +163,10 @@ $search.on('touchend', function() { @@ -146,13 +163,10 @@ $search.on('touchend', function() {
146 (function() { 163 (function() {
147 var html = '', 164 var html = '',
148 history, 165 history,
149 - historys, i, num = 1, searchUrl = ''; 166 + historys, i, num = 1;
150 167
151 if (localStorage) { 168 if (localStorage) {
152 - if (isLocalStorageSupported()) {  
153 - historys = localStorage.getItem('historys');  
154 - }  
155 - searchUrl = $form.attr('action'); 169 + historys = localStorage.getItem(historyval);
156 170
157 if (historys && historys.length > 0) { 171 if (historys && historys.length > 0) {
158 historys = historys.split(ranToken); 172 historys = historys.split(ranToken);
@@ -167,7 +181,7 @@ $search.on('touchend', function() { @@ -167,7 +181,7 @@ $search.on('touchend', function() {
167 break; 181 break;
168 } 182 }
169 183
170 - html += '<li><a href="' + searchUrl + '/?query=' + history + '">' + history + '</li>'; 184 + html += '<li><a href="javascript:void(0);">' + history + '</li>';
171 } 185 }
172 186
173 $history.html(html); 187 $history.html(html);
@@ -178,8 +192,4 @@ $search.on('touchend', function() { @@ -178,8 +192,4 @@ $search.on('touchend', function() {
178 window.rePosFooter(); 192 window.rePosFooter();
179 } 193 }
180 } 194 }
181 -}());  
182 -  
183 -if (isLocalStorageSupported()) {  
184 - writeSearch.bindWirteLocal($form);  
185 -} 195 +}());
@@ -4,55 +4,46 @@ @@ -4,55 +4,46 @@
4 * @date: 2015/10/29 4 * @date: 2015/10/29
5 */ 5 */
6 6
7 -var ranToken = ' ??++ ';  
8 -  
9 -function isLocalStorageSupported() {  
10 - var testKey = 'test',  
11 - storage = Window.prototype.localStorage;  
12 -  
13 - try {  
14 - storage.setItem(testKey, 'testValue');  
15 - storage.removeItem(testKey);  
16 - return true;  
17 - } catch (error) {  
18 - return false;  
19 - }  
20 -} 7 +var ranToken = ',',
  8 + historyval = 'historys1';
21 9
22 //获取分隔符 10 //获取分隔符
23 function getRanToken() { 11 function getRanToken() {
24 return ranToken; 12 return ranToken;
25 } 13 }
26 14
  15 +//存变量
  16 +function getHistoryval() {
  17 + return historyval;
  18 +}
  19 +
27 //绑定提交前的存local操作 20 //绑定提交前的存local操作
28 function bindWirteLocal($form) { 21 function bindWirteLocal($form) {
29 $form.on('submit', function() { 22 $form.on('submit', function() {
30 - var query = this.query.value,  
31 - historys; 23 + var query = this.query.value;
32 24
33 - if (localStorage) {  
34 - if (isLocalStorageSupported()) {  
35 - historys = localStorage.getItem('historys');  
36 - } 25 + setHistoryValFun(query);
  26 + });
  27 +}
37 28
38 - historys = historys ? historys : ''; 29 +function setHistoryValFun(query) {
  30 + var historys;
  31 + if (localStorage) {
  32 + historys = localStorage.getItem(historyval);
39 33
40 - if (historys.indexOf(ranToken + query + ranToken) > -1) {  
41 - return;  
42 - } 34 + historys = historys ? historys.replace(new RegExp((query + ranToken), 'g'), '') : '';
43 35
44 - if (historys === '') {  
45 - query = ranToken + query;  
46 - } 36 + if (historys === '') {
  37 + query = ranToken + query;
  38 + }
47 39
48 - historys += query + ranToken;  
49 - if (isLocalStorageSupported()) {  
50 - localStorage.setItem('historys', historys);  
51 - }  
52 - }  
53 - }); 40 + historys += query + ranToken;
  41 + localStorage.setItem(historyval, historys);
  42 + }
54 } 43 }
55 44
56 exports.getRanToken = getRanToken; 45 exports.getRanToken = getRanToken;
  46 +exports.getHistoryval = getHistoryval;
  47 +exports.setHistoryValFun = setHistoryValFun;
57 48
58 exports.bindWirteLocal = bindWirteLocal; 49 exports.bindWirteLocal = bindWirteLocal;
@@ -41,7 +41,6 @@ function nextStep(url, mobileNo, areaCode) { @@ -41,7 +41,6 @@ function nextStep(url, mobileNo, areaCode) {
41 }, 41 },
42 success: function(res) { 42 success: function(res) {
43 43
44 - // console.log(res.data);  
45 if (res.code === 200) { 44 if (res.code === 200) {
46 45
47 // 统计代码:用于统计从哪个渠道登录 46 // 统计代码:用于统计从哪个渠道登录
@@ -103,19 +102,6 @@ $btnNext.on('touchstart', function() { @@ -103,19 +102,6 @@ $btnNext.on('touchstart', function() {
103 nickname: nickname 102 nickname: nickname
104 }, 103 },
105 success: function(res) { 104 success: function(res) {
106 -  
107 - // console.log(res);  
108 -  
109 - //res : {  
110 - // code: 'xxx',  
111 - // data: {  
112 - // isReg: 0,  
113 - // next: 'xxxx'  
114 - // },  
115 - // message: 'xxxx',  
116 - //}  
117 -  
118 -  
119 if (res.code === 200) { 105 if (res.code === 200) {
120 if (res.data.isReg === 1) { 106 if (res.data.isReg === 1) {
121 dialog.showDialog({ 107 dialog.showDialog({
@@ -44,7 +44,7 @@ @@ -44,7 +44,7 @@
44 <ul class="hot clearfix"> 44 <ul class="hot clearfix">
45 {{# hot}} 45 {{# hot}}
46 <li> 46 <li>
47 - <a href="{{url}}" brandName="{{name}}"">{{name}}</a> 47 + <a href="{{url}}" brandName="{{name}}">{{name}}</a>
48 </li> 48 </li>
49 {{/ hot}} 49 {{/ hot}}
50 </ul> 50 </ul>
@@ -29,7 +29,7 @@ @@ -29,7 +29,7 @@
29 <ul class="hot clearfix"> 29 <ul class="hot clearfix">
30 {{# hotTerms}} 30 {{# hotTerms}}
31 <li> 31 <li>
32 - <a href='{{../url}}/?query={{content}}'>{{content}}</a> 32 + <a href='javascript:void(0);'>{{content}}</a>
33 </li> 33 </li>
34 {{/ hotTerms}} 34 {{/ hotTerms}}
35 </ul> 35 </ul>