Commit cbc3674e authored by Damon's avatar Damon

perf: 搜索-虚拟列表

parent 0329c8dc
<template>
<view @click.stop="itemChange(item.id)" class="list-item">
<view :class="['u-flex', 'head', { selected: item.id === arrIndex }]">
<view class="titles">
<text>{{ item.type }}</text
>{{ item.title }}
</view>
<view class="u-flex arrow" v-show="item.id !== arrIndex">
<text>展开</text>
<u-icon name="arrow-down" color="#9EA5BB"></u-icon>
</view>
</view>
<view class="u-flex subject" v-show="item.id === arrIndex">
<view class="content">
<answer-sheet
ref="answerSheet"
:answerList="item.answerList"
:rightKey="item.rightKey"
:multiple="item.type === 3"
:submitStatus="item.submitStatus"
:btnStatus="false"
@change="answerChange"
></answer-sheet>
</view>
<view class="explain">
<answer-explanation
:answer="item.answer"
:explain="item.explain"
></answer-explanation>
</view>
</view>
<view class="u-flex opera" v-show="item.id === arrIndex">
<view class="opera-list" @click="collection(item)">
<u-image
:src="collectImg"
width="36"
height="36"
:fade="false"
:lazy-load="false"
/>
<text>{{ collectionFlag ? '已收藏' : '收藏' }}</text>
</view>
<view class="opera-list" @click.stop="arrowUp(item.id)">
<text>收起</text>
<u-icon name="arrow-up" color="#9EA5BB"></u-icon>
</view>
</view>
</view>
</template>
<script>
import answerSheet from '@/components/answer-sheet/index.vue'
import answerExplanation from '@/components/answer-explanation/index.vue'
export default {
components: {
answerSheet,
answerExplanation,
},
props: ['item', 'arrIndex'],
computed: {
collectImg() {
const name = this.item?.collectionFlag ? 'collected.png' : 'collect.png'
return process.uniEnv.qn_base_url + name
},
collectionFlag() {
return this.item?.collectionFlag || false
},
},
methods: {
itemChange(id) {
this.$emit('arrowDown', id)
},
arrowUp(id) {
this.$emit('arrowUp', id)
},
collection(item) {
this.$emit('collection', item)
},
},
}
</script>
<style lang="scss" scoped>
.list-item {
background: #fff;
padding: 30rpx 20rpx;
margin: 10rpx 0;
border-radius: 30rpx 30rpx 0 0;
.head {
width: 100%;
height: 60rpx;
overflow: hidden;
.titles {
color: #191c27;
font-size: 32rpx;
line-height: 50rpx;
font-weight: bold;
flex: 1;
height: 60rpx;
overflow: hidden;
text {
background: #9ea5bb;
color: #fff;
padding: 8rpx 20rpx;
margin-right: 10rpx;
font-size: 20rpx;
border-radius: 50rpx 0 50rpx 50rpx;
}
}
.arrow {
padding-left: 20rpx;
margin-top: -10rpx;
text {
color: #2953ff;
font-size: 28rpx;
padding-right: 10rpx;
}
}
}
.selected {
height: auto;
overflow: auto;
.titles {
height: auto;
overflow: auto;
}
}
.subject {
flex-flow: column;
.type {
width: 100%;
text-align: left;
padding: 20rpx 0;
}
.content {
width: 100%;
}
.explain,
.btn {
width: 100%;
padding-top: 50rpx;
}
}
.opera {
padding-top: 30rpx;
justify-content: space-between;
color: #5a6073;
font-size: 28rpx;
.opera-list {
display: flex;
&:nth-of-type(1) {
text {
padding-left: 10rpx;
}
}
&:nth-of-type(2) {
text {
padding-right: 10rpx;
color: #2953ff;
}
}
}
}
}
</style>
<template>
<!-- #ifndef APP-NVUE -->
<view class="uni-list uni-border-top-bottom">
<view v-if="border" class="uni-list--border-top"></view>
<slot />
<view v-if="border" class="uni-list--border-bottom"></view>
</view>
<!-- #endif -->
<!-- #ifdef APP-NVUE -->
<list class="uni-list" :class="{ 'uni-list--border': border }" :enableBackToTop="enableBackToTop" loadmoreoffset="15"><slot /></list>
<!-- #endif -->
</template>
<script>
/**
* List 列表
* @description 列表组件
* @tutorial https://ext.dcloud.net.cn/plugin?id=24
* @property {String} border = [true|false] 标题
*/
export default {
name: 'uniList',
'mp-weixin': {
options: {
multipleSlots: false
}
},
props: {
enableBackToTop: {
type: [Boolean, String],
default: false
},
scrollY: {
type: [Boolean, String],
default: false
},
border: {
type: Boolean,
default: true
}
},
// provide() {
// return {
// list: this
// };
// },
created() {
this.firstChildAppend = false;
},
methods: {
loadMore(e) {
this.$emit('scrolltolower');
}
}
};
</script>
<style lang="scss">
.uni-list {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
position: relative;
flex-direction: column;
}
.uni-list--border {
position: relative;
/* #ifdef APP-NVUE */
border-top-color: $uni-border-color;
border-top-style: solid;
border-top-width: 0.5px;
border-bottom-color: $uni-border-color;
border-bottom-style: solid;
border-bottom-width: 0.5px;
/* #endif */
z-index: -1;
}
/* #ifndef APP-NVUE */
.uni-list--border-top {
position: absolute;
top: 0;
right: 0;
left: 0;
height: 1px;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
background-color: $uni-border-color;
z-index: 1;
}
.uni-list--border-bottom {
position: absolute;
bottom: 0;
right: 0;
left: 0;
height: 1px;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
background-color: $uni-border-color;
}
/* #endif */
</style>
......@@ -27,62 +27,12 @@
>道</view
>
<view class="result">
<uni-list :border="false">
<view
v-for="item in lists"
:key="item.id"
@click.stop="itemChange(item.id)"
class="list-item"
>
<view
:class="['u-flex', 'head', { selected: item.id === arrIndex }]"
>
<view class="titles">
<text>{{ item.type }}</text
>{{ item.title }}
</view>
<view class="u-flex arrow" v-show="item.id !== arrIndex">
<text>展开</text>
<u-icon name="arrow-down" color="#9EA5BB"></u-icon>
</view>
</view>
<view class="u-flex subject" v-show="item.id === arrIndex">
<view class="content">
<answer-sheet
ref="answerSheet"
:answerList="item.answerList"
:rightKey="item.rightKey"
:multiple="item.type === 3"
:submitStatus="item.submitStatus"
:btnStatus="false"
@change="answerChange"
></answer-sheet>
</view>
<view class="explain">
<answer-explanation
:answer="item.answer"
:explain="item.explain"
></answer-explanation>
</view>
</view>
<view class="u-flex opera" v-show="item.id === arrIndex">
<view class="opera-list" @click="collection(item)">
<u-image
:src="collectImg"
width="36"
height="36"
:fade="false"
:lazy-load="false"
/>
<text>{{ collectionFlag ? '已收藏' : '收藏' }}</text>
</view>
<view class="opera-list" @click.stop="arrowUp(index)">
<text>收起</text>
<u-icon name="arrow-up" color="#9EA5BB"></u-icon>
</view>
</view>
</view>
</uni-list>
<my-section
v-for="(item, index) in section"
:key="index"
:begin="item.begin"
:end="item.end"
></my-section>
<u-loadmore :status="loadStatus" :margin-bottom="40" />
</view>
</view>
......@@ -99,14 +49,11 @@
</template>
<script>
import answerSheet from '@/components/answer-sheet/index.vue'
import answerExplanation from '@/components/answer-explanation/index.vue'
import uniList from '@/components/uni-list/uni-list.vue'
import { list } from './list.js'
import mySection from './my-section.vue'
export default {
components: {
answerSheet,
answerExplanation,
uniList,
mySection,
},
data() {
return {
......@@ -117,26 +64,21 @@ export default {
pageSize: 10,
loadStatus: 'loadmore',
isLoadMore: false, //是否加载中
total: 0
total: 0,
section: [],
}
},
onLoad(e) {
this.keyword = e ? decodeURIComponent(e.keyword) : ''
this.lists = list
},
onReady() {
this.init()
},
computed: {
defaultImg() {
return process.uniEnv.qn_base_url + 'no-search.png'
},
collectImg() {
const info = this.lists.find((v) => v.index === this.arrIndex)
const name = info?.collectionFlag ? 'collected.png' : 'collect.png'
return process.uniEnv.qn_base_url + name
},
collectionFlag() {
const info = this.lists.find((v) => v.index === this.arrIndex)
return info ? info.collectionFlag : false
},
}
},
onReachBottom() {
//上拉触底函数
......@@ -156,55 +98,56 @@ export default {
size: this.pageSize,
}
uni.showLoading({
title: '加载中',
})
this.loadStatus = 'loading'
this.$u.api.searchQuestion(data).then((res) => {
uni.hideLoading()
this.total = res.total
const lists =
res?.records.length > 0
? res?.records.map(v => {
const rightKey = v.answer.split(',')
const content = JSON.parse(v.content)
const answerList = content.choiceList.map((item) => {
return {
sort: item.value,
text: item.label,
checked: rightKey.includes(item.value),
confirm: rightKey.includes(item.value),
}
})
const records = res?.records || []
if (records.length === 0) {
this.loadStatus = 'nomore'
return
}
return {
id: v.id,
answer: v.answer,
title: content.title,
type: v.echoMap.questionTypeId,
answerList: answerList,
explain: v.analysis,
rightKey: rightKey,
collectionFlag: v.collectionFlag,
submitStatus: true,
}
})
: []
for (let i = 0; i < records.length; i++) {
const v = records[i]
const rightKey = v.answer.split(',')
const content = JSON.parse(v.content)
const answerList = content.choiceList.map((item) => {
return {
sort: item.value,
text: item.label,
checked: rightKey.includes(item.value),
confirm: rightKey.includes(item.value),
}
})
this.lists = this.lists.concat(lists)
if (res.records.length < this.pageSize) {
this.lists.push({
id: v.id,
answer: v.answer,
title: content.title,
type: v.echoMap.questionTypeId,
answerList: answerList,
explain: v.analysis,
rightKey: rightKey,
collectionFlag: v.collectionFlag,
submitStatus: true,
})
}
this.section.push({
begin: this.lists.length - records.length,
end: this.lists.length,
})
if (records.length < this.pageSize) {
//判断接口返回数据量小于请求数据量,则表示此为最后一页
this.isLoadMore = true
this.loadStatus = 'nomore'
} else {
this.loadStatus = 'loadmore'
this.isLoadMore = false
}
this.status = 'nomore'
})
},
itemChange(e) {
this.arrIndex = e
},
search(val) {
if (val) {
this.keyword = val
......@@ -214,23 +157,12 @@ export default {
history.splice(9, 1)
}
this.$u.vuex('searchHistory', history)
this.currentPage = 1
this.lists = []
this.section = []
this.init()
}
},
collection(item) {
const action = item.collectionFlag ? 'removeCollection' : 'joinCollection'
this.$u.api[action]({ questionId: item.id }).then((res) => {
if (res) {
const msg = item.collectionFlag ? '取消收藏成功' : '收藏成功'
this.$u.toast(msg)
item.collectionFlag = !item.collectionFlag
}
})
},
arrowUp() {
this.arrIndex = ''
},
}
},
}
</script>
......@@ -264,87 +196,6 @@ export default {
.result {
margin-top: 30rpx;
width: 100%;
.list-item {
background: #fff;
padding: 30rpx 20rpx;
margin: 10rpx 0;
border-radius: 30rpx 30rpx 0 0;
.head {
width: 100%;
height: 60rpx;
overflow: hidden;
.titles {
color: #191c27;
font-size: 32rpx;
line-height: 50rpx;
font-weight: bold;
flex: 1;
height: 60rpx;
overflow: hidden;
text {
background: #9ea5bb;
color: #fff;
padding: 8rpx 20rpx;
margin-right: 10rpx;
font-size: 20rpx;
border-radius: 50rpx 0 50rpx 50rpx;
}
}
.arrow {
padding-left: 20rpx;
margin-top: -10rpx;
text {
color: #2953ff;
font-size: 28rpx;
padding-right: 10rpx;
}
}
}
.selected {
height: auto;
overflow: auto;
.titles {
height: auto;
overflow: auto;
}
}
.subject {
flex-flow: column;
.type {
width: 100%;
text-align: left;
padding: 20rpx 0;
}
.content {
width: 100%;
}
.explain,
.btn {
width: 100%;
padding-top: 50rpx;
}
}
.opera {
padding-top: 30rpx;
justify-content: space-between;
color: #5a6073;
font-size: 28rpx;
.opera-list {
display: flex;
&:nth-of-type(1) {
text {
padding-left: 10rpx;
}
}
&:nth-of-type(2) {
text {
padding-right: 10rpx;
color: #2953ff;
}
}
}
}
}
}
.default {
flex-flow: column;
......
export const list = []
\ No newline at end of file
<template>
<view>
<search-item
v-for="(item, index) in list"
:key="index"
:item="item"
:arrIndex.sync="arrIndex"
@arrowDown="arrowDown"
@arrowUp="arrowUp"
@collection="collection($event, index)"
></search-item>
</view>
</template>
<script>
import { list } from './list.js'
import searchItem from '@/components/search-item/index.vue'
export default {
components: {
searchItem,
},
props: ['begin', 'end'],
data() {
return {
list: [],
arrIndex: '',
}
},
created() {
this.list = list.slice(this.begin, this.end)
},
methods: {
arrowDown(e) {
this.arrIndex = e
},
arrowUp() {
this.arrIndex = ''
},
collection(item, index) {
const action = item.collectionFlag ? 'removeCollection' : 'joinCollection'
this.$u.api[action]({ questionId: item.id }).then((res) => {
if (res) {
const msg = item.collectionFlag ? '取消收藏成功' : '收藏成功'
this.$u.toast(msg)
this.$set(this.list[index], 'collectionFlag', !item.collectionFlag)
}
})
},
},
}
</script>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment