labourUnionSearch.vue 15.1 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
<template>
	<div class="search-page">
		
		<!-- 容器 -->
		<div class="container">
			
			<!-- 主体区域 -->
			<div class="main-box">
						
				<!-- 导航栏容器 -->
				<div class="nav-container" :style="'padding-top: ' + $parent.top.value + 'px'">
					
					<!-- 导航栏 -->
					<nav-search-bar ref="navSearchBar" @back="back" @doSearch="doSearch" @clearInput="clearInput" @exceptions="exceptions"
						:navs="navsSearch">
					</nav-search-bar>
				</div>
				
				<!-- nav-pl -->
				<div class="nav-pl" :style="'padding-top: ' + $parent.top.value + 'px'"></div>

				<!-- 滚动组件 -->
				<div class="scroll-warp">
					
					<!-- 主内容浮动区域 -->
					<div class="section-box">
						
						<!-- 搜索组件框 -->
						<div class="section">
							
							<!-- 判断列表数组是否有数据,有再显示 -->
							<template v-if="lists && lists.length">
								
								<!-- 上拉加载 -->
			                    <van-list class="list-container" v-model="tabs.loading" :finished="tabs.finished" finished-text="~没有更多了~" @load="loadMore">
									
									<!-- 列表项 -->
									<div class="list-item" v-for="(item, index) in lists" :key="index">
										
										<!-- 商品样式 III 列表 -->
										<goods-item-i-i-i @jump="toDetail" @primary="primary"
											:item="item">
											
											<!-- 角标 -->
											<div class="discount-flag" slot="flag" v-if="item.is_gh != '1'">
												非工会选品
											</div>
										</goods-item-i-i-i>
									</div>
								</van-list>
							</template>
							
							<!-- 没有搜索数据 -->
							<template v-else-if="tabs && tabs.keyword && initialized">
								
								<!-- 未找到您搜索的内容 -->
								<div class="no-data">
									
									<!-- 图片 -->
									<img src="https://wjj.ys2.cnliveimg.com/769/img_new/1659946355366.png" class="icon-empty" />
									
									<!-- 提示文字 -->
									<div class="text">搜索结果为空~</div>
								</div>
							</template>
							
							<!-- 未搜索时,显示历史搜索 -->
							<template v-else>
								
								<!-- 历史搜索区块 -->
								<div class="his-box" v-if="searchHistoryArr && searchHistoryArr.length">
								
									<!-- 标题 -->
									<div class="title-box">
										<div class="title">历史搜索</div>
									</div>
									
									<!-- 历史搜索结果 -->
									<div class="item-box">
										<div class="item" v-for="(item, index) in searchHistoryArr" :key="index" @click="doSearch(item)">
											{{item}}
										</div>
									</div>
									
									<!-- 清除历史搜索结果 -->
									<div class="clear-his" @click="clearSearchHistoryArr">清除搜索历史</div>
								</div>
							</template>
						</div>
					</div>
					
					<!-- 安全底部距离 -->
					<div class="safe-area-inset-bottom"></div>
				</div>
			</div>
		</div>
		
		<!-- 回到顶部按钮 -->
        <goto-top
            :top="parseInt(150)">
        </goto-top>
	</div>
</template>

<script>
	
	// 引入组件
	import Vue from "vue";
    import { Toast, Indicator } from "mint-ui";
    import { List } from "vant";
	import navSearchBar from '@/components/navSearchBar/navSearchBar';
	import goodsItemIII from '@/components/goodsItemIII/goodsItemIII';
	import gotoTop from "@/components/gotoTop/gotoTop";
	
	Vue.use(List);

	export default {
		name: "searchPage",
		data() {
			return {
				title: '',
				statusBarHeight: 0,				// 状态栏高度
				scrollViewHeight: 0,			// scroll-view高度
				threshold: 230,					// 出现返回顶部按钮的滚动条位置
				scrollTop: 0,					// 滚动到顶部位置,用于changeTab时,滚动条滚动到顶部
				currScrollTop: 0,				// 滚动条当前位置,用于changeTab时,滚动条滚动到顶部
				CALL_STATUS: {
					INIT: "init",				// 初始化页面
					LOAD_MORE: "loadMore"		// 加载更多
				},
				navsSearch: null,				// 导航栏对象
				searchHistoryArr: [],			// 搜索历史记录数组
				tabs: {},						// tabsBar数据对象
				initialized: false,				// 已初始化过标识,默认false
				interface: 'goodsSearch',		// 列表页接口名称
				lists: []						// 列表数据
			}
		},
		components: {
			navSearchBar,
			goodsItemIII,
			gotoTop
		},
		created() {

			// 设置标题
			this.$set(this, 'title', '搜索');
			
			// 设置navs
			this.$set(this, 'navsSearch', {
				back: {
					icon: 'static/img/icon_back.png'
				},
				search: {
					icon: 'static/img/icon_search.png',
					pl: '请输入关键字',
					enabled: true
				}
			});
			
			// 初始化tabs对象
			this.setTabs();

			// 获取历史搜索结果Storage
            this.getSearchHistoryArr();
		},
		methods: {
			
			/**
			 * scroll-view组件滚动监听
			 * @param {Object} e [scroll-view滚动事件对象]
			 */
			scroll(e) {
				
				// 设置currScrollTop
				this.$set(this, 'currScrollTop', e.detail.scrollTop);
			},
			
			/**
			 * 加载更多
			 */
			loadMore() {

				// 判断是否可加载
				if (this.tabs.loadingNow === false && this.tabs.finished === false) {
					
					// 设置加载中状态
                    this.$set(this.tabs, 'loadingNow', true);

					if (this.tabs.page == 1 || this.tabs.page === undefined) {
						
						// 设置初始化页码
						this.$set(this.tabs, 'page', 1);
						
						// 初始化列表
						this.getLists(this.tabs, this.CALL_STATUS.INIT);
					} else {
						
						// 判断页面是数字再执行
						if (!isNaN(parseInt(this.tabs.page)) && this.tabs.page > 1) {

							// 加载更多
							this.getLists(this.tabs, this.CALL_STATUS.LOAD_MORE);
						}
					}
				}
			},
			
			/**
			 * 获取数据列表
			 * @param {Object} tabs [tabs对象,获取数据必要参数对象]
			 * @param {Object} CALL_STATUS [何处触发请求]
			 */
			getLists(tabs, CALL_STATUS = this.CALL_STATUS.INIT) {

				// 判断必要参数
				if (typeof tabs === 'object' && tabs.interface && tabs.page >= 1 && tabs.finished === false) {

					// 设置loading状态
					this.$set(tabs, 'loading', true);
					
					// 加载中提示
                	Indicator.open("加载中...");
					
					// 设置请求参数
					let reqParams = {
						keyword: tabs.keyword,
						uid: (this.$route.query.uid || 0),
						page: tabs.page
					};
					
					// 请求接口
					this.http('/Api/' + (this.getApiVersion().index) + '/' + tabs.interface, reqParams, this.getListsCallback, {
						method: 'POST',
						tabs,
						CALL_STATUS
					});
				}
			},
			
			/**
			 * 获取数据列表回调
			 * @param {Object} res [服务器返回数据]
			 * @param {Object} ext [扩展参数]
			 */
			getListsCallback(res, ext) {
				
				// 加载完成
                Indicator.close();

				// 如果是初始化,清空lists数据
				if (ext.CALL_STATUS === this.CALL_STATUS.INIT) {
					
					// 清空lists数据
					this.$set(this, 'lists', []);
				}
				
				// 返回处理
				if (res.code == 200) {
					
					// 解构数据
					let {data: datas} = res;
					
					/* 设置列表数据 开始 */
					
					// 判断如果有数据
					if (datas && datas.goods && datas.goods.length) {

						switch (ext.CALL_STATUS) {
							case this.CALL_STATUS.INIT:

								// 设置数据数组列表
								this.$set(this, 'lists', datas.goods);
								break;
							case this.CALL_STATUS.LOAD_MORE:

								// 追加数据数组列表
								this.$set(this, 'lists', [...this.lists, ...datas.goods]);
								break;
						}
						
						// 设置页码
						this.$set(ext.tabs, 'page', ++ext.tabs.page);
						
						// 取消Loading中状态,设置加载未完成,可以再加载更多
						this.$set(ext.tabs, 'loading', false);
						this.$set(ext.tabs, 'loadingNow', false);
						this.$set(ext.tabs, 'finished', false);
					} else {
					
						// 取消Loading中状态,设置加载已完成,不能再加载更多
						this.$set(ext.tabs, 'loading', false);
						this.$set(ext.tabs, 'loadingNow', false);
						this.$set(ext.tabs, 'finished', true);
					}
					
					/* 设置列表数据 结束 */
					
				} else if (res.code == 400) {
					
					// 获取数据列表失败
					Toast(res.message);
				} else {
					
					// 获取数据列表失败
					Toast('获取数据列表失败');
				}

				// 设置已初始化过
				this.$set(this, 'initialized', true);
			},
			
			/**
			 * 初始化tabs对象
			 * @param {String} keyword [搜索关键字,默认为空字符串]
			 */
			setTabs(keyword = '') {
				
				if (this.debug) {
					console.log('setTabs', keyword);
				}
				
				/* 初始化tabs数据 开始 */
				
				// tabsBar数据对象
				this.$set(this, 'tabs', {
					interface: this.interface,	// 列表页接口名称
					keyword: keyword,			// 搜索关键字
					page: 1,					// 页码
					loading: false,				// 加载中状态
					loadingNow: false,
					finished: false				// 全部加载完成
				});
				
				/* 初始化tabs数据 结束 */
				
				if (this.debug) {
					console.log('setTabs', this.tabs);
				}
			},
			
			/**
			 * 搜索事件监听
			 * @param {String} keyword [搜索关键字]
			 */
			doSearch(keyword) {
				
				if (this.debug) {
					console.log('doSearch', keyword);
				}
				
				// 判断必要参数
        		if (keyword && typeof keyword === 'string' && keyword.trim()) {

					// 清空列表
					this.$set(this, 'lists', []);

					// 初始化tabs对象
					this.setTabs(keyword);

					/* 记录搜搜历史 开始 */

					// 最多8个历史搜索结果
					if (this.searchHistoryArr.length >= 8) {

						// 弹出一个数据
						this.searchHistoryArr.pop();
					}

					// 向前插入搜索历史
					this.searchHistoryArr.unshift(keyword);

					// 搜索历史放入Storage中
					this.setSearchHistoryArr(keyword);

					/* 记录搜搜历史 结束 */

					// 下一周期执行
					this.$nextTick(() => {

						// 请求搜索接口
						this.getLists(this.tabs, this.CALL_STATUS.INIT);
					});
				}
			},
      	
			/**
			 * 搜索异常
			 */
			exceptions() {
				
				// 提示
        		Toast("请输入要搜索的商品");

				// 设置未初始化过
				this.$set(this, 'initialized', false);
      		},
			
			/**
			 * 清除输入框事件监听
			 */
			clearInput() {
				
				if (this.debug) {
					console.log('clearInput');
				}
				
				// 清空列表
				this.$set(this, 'lists', []);
					
				// 初始化tabs对象
				this.setTabs();

				// 设置未初始化过
				this.$set(this, 'initialized', false);
			},

			/**
             * 获取历史搜索结果Storage
             */
            getSearchHistoryArr() {

                // 处理搜索历史Storage数组
                if (window.localStorage.getItem("searchHistoryArr")) {
                    
                    // 获取搜索历史数组
                    this.$set(this, "searchHistoryArr", window.localStorage.getItem("searchHistoryArr").split(","));
                }
            },

            /**
             * 设置历史搜索结果Storage
             * @param {String} keyword [搜索关键字]
             */
            setSearchHistoryArr(keyword) {

                // 设置会话keyword
                window.sessionStorage.setItem("keyword", keyword);

                this.$nextTick(() => {

                    // 设置input值,通过历史记录搜索时,会用到
                    this.$refs.navSearchBar.setKeyword(keyword);
                });

                // 处理搜索历史Storage数组
                if (!window.localStorage.getItem("searchHistoryArr")) {
                    window.localStorage.setItem("searchHistoryArr", "");
                }

                // 判断keyword是否有值
                if (keyword) {

                    // 数组压栈
                    this.searchHistoryArr.unshift(keyword);

                    // 数组去重
                    this.$set(this, "searchHistoryArr", Array.from(new Set(this.searchHistoryArr)));

                    // 判断数组大小,最多存8个
                    if (this.searchHistoryArr.length > 8) {
                        
                        // 循环弹出数组多余元素
                        for (let i = this.searchHistoryArr.length; i > 8 ; i--) {
                            this.searchHistoryArr.pop();
                        }
                    }

                    // 将数组信息序列化,存入Storage中
                    window.localStorage.setItem("searchHistoryArr", this.searchHistoryArr.join(","));
                }
            },

            /**
             * 清空搜索历史
             */
            clearSearchHistoryArr() {

                // 清除搜索历史Storage数组
                if (window.localStorage.getItem("searchHistoryArr")) {
                    window.localStorage.setItem("searchHistoryArr", "");
                }

                // 清除searchHistoryArr数组
                this.$set(this, "searchHistoryArr", []);
            },
			
            /**
             * 跳转商品详情
             * @param {Object} item [数据对象]
             */
            toDetail(item) {

                // 判断App内
                if (this.isCNLive.value) {

                    // 判断通用跳转函数是否存在
                    if (typeof this.$parent.gotoDetail === 'function') {

                        // 跳转参数
                        let params = {
                            type: '2',
                            to: item.id,
                            shop_type: '3'
                        };

                        // 通用跳转
                        this.$parent.gotoDetail(params, {
                            activityCode: this.activityCode ? this.activityCode : ''		// 添加页面渠道码
                        });
                    }
                } else {

                    // App外,唤起App
                    this.openApp();
                }
            },

            /**
             * 项目按钮事件
             * @param {Object} item [数据对象]
             */
            primary(item) {

                // 跳转商品详情
                this.toDetail(item);
            },

			/**
             * 后退
             */
            back() {

                // 判断是否有处理函数
                if (typeof this.$parent.back === 'function') {

                    // 后退
                    this.$parent.back();
                } else {

                    // 后退
                    window.history.go(-1);
                }
            }
		},
		head: {
            title () {
                return {
                    inner: this.title
                }
            }
        },
        watch: {

            // 设置页面标题
            title(newVal) {
                if (this.$store.state.browser.toLowerCase() == "qq" && this.$store.state.deviceType.toLowerCase() == "iphone") {
                    this.$iFrame.insertIFrame(newVal);
                } else {
                    this.$emit("updateHead");
                }
            }
        },
		mounted() {
			
		}
	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style rel="stylesheet/less" lang="less" scoped>
	@import 'labourUnionSearch';
</style>