Skip to content

Commit ed37b8c

Browse files
committed
将探索页面改成分页设计
1 parent 7e55ffd commit ed37b8c

File tree

1 file changed

+85
-49
lines changed

1 file changed

+85
-49
lines changed

src/views/explore/index.vue

Lines changed: 85 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,41 @@
2323
<v-col lg="4" md="4" sm="6" cols="12">
2424
<span>预处理:</span>
2525
<v-select :items="prepList" clearable solo :menu-props="{ offsetY: true }" v-model="searchForm.prep"
26-
hide-details @change="search">
26+
hide-details @change="newSearch">
2727
</v-select>
28-
<!-- <v-combobox v-model="searchForm.tags" :items="tagList" clearable multiple chips solo hide-selected
29-
label="选择标签..." hide-details>
30-
<template v-slot:selection="{ attrs, item, select, selected }">
31-
<v-chip v-bind="attrs" :input-value="selected" close @click="select" @click:close="removeTag(item)">
32-
<strong>{{ item }}</strong>
33-
</v-chip>
34-
</template>
35-
</v-combobox> -->
3628
</v-col>
3729
<v-col lg="4" md="4" sm="6" cols="12">
3830
<span>排序:</span>
3931
<v-select :items="sortList" solo :menu-props="{ offsetY: true }" v-model="searchForm.sort" item-text="text"
40-
item-value="value" hide-details @change="search">
32+
item-value="value" hide-details @change="newSearch">
4133
</v-select>
4234
</v-col>
4335
</v-row>
4436
</div>
45-
<div class="find-tip flex-jcc" v-show="init&&nothing&&!instanceList.length">
37+
<div class="find-tip flex-jcc" v-show="showNothingTip">
4638
<div class="tip-content d-flex flex-clo flex-ai">
4739
<span class="emoji">🧐</span>
4840
<span class="text-describe">哎呀,什么都没找到诶~~</span>
4941
<span class="text-describe">但我想,也许你可以为这里开拓一片新天地?</span>
5042
</div>
5143
</div>
52-
<div class="init-tip flex-jcc" v-show="!init">
44+
<!-- <div class="init-tip flex-jcc" v-show="!init">
5345
<div class="d-flex flex-jcc text-describe">
5446
<span>请在搜索框输入关键字寻找你想要的实例</span>
5547
</div>
56-
</div>
57-
<div v-show="init&&!nothing">
48+
</div> -->
49+
<div v-show="!nothing">
5850
<div class="explore-instance-list">
59-
<div class="explore-list-item" v-for="item in instanceList" :key="item.exampleId">
60-
<instance-card :info="item"></instance-card>
51+
<div class="explore-list-item" v-for="(item, index) in instanceList" :key="item.exampleId">
52+
<instance-card :info="item" :cardIndex="index" @setFollow="setFollow" @setFav="setFav"></instance-card>
6153
</div>
6254
<div class="skeleton-list-item" v-for="(item, index) in 12" :key="index" v-show="listLoading">
6355
<instance-skeleton></instance-skeleton>
6456
</div>
6557
</div>
66-
<div class="view-more flex-jcc" v-show="!isLastPage&&instanceList.length">
67-
<v-btn color="info" x-large @click="searchMore">
68-
<v-icon left>mdi-eye-outline</v-icon>查看更多
69-
</v-btn>
70-
</div>
71-
<div class="no-more-tip flex-jcc" v-show="isLastPage&&instanceList.length">
72-
<span class="text-describe text-sm">别划了,真的一个也没有了😥</span>
58+
<div class="page-opt flex-jcc" v-show="!isFirstPage||!isLastPage">
59+
<v-btn class="before-btn" @click="switchPage(-1)" :disabled="isFirstPage">上一页</v-btn>
60+
<v-btn color="primary" class="after-btn" @click="switchPage(1)" :disabled="isLastPage">下一页</v-btn>
7361
</div>
7462
</div>
7563
</div>
@@ -82,6 +70,7 @@ import InstanceCard from '@components/instanceCard'
8270
import GoToTop from '@components/goToTop'
8371
import { defPrepOpts } from '@utils/publicData'
8472
import { judgeMode } from '@utils/editor/judgeMode'
73+
import * as p2b from '@utils/paramsToBase64'
8574
export default {
8675
name: 'Explore',
8776
data() {
@@ -95,15 +84,17 @@ export default {
9584
searchForm: {
9685
keyword: '',
9786
prep: '',
98-
sort: 0,
87+
sort: 2,
9988
},
89+
page: 1,
10090
instanceList: [],
10191
nothing: false,
92+
showNothingTip: false,
10293
searchLoading: false,
10394
showFilter: false,
10495
listLoading: false,
105-
page: 1,
10696
isLastPage: false,
97+
isFirstPage: false,
10798
init: false,
10899
}
109100
},
@@ -112,46 +103,81 @@ export default {
112103
for (let key in defPrepOpts) {
113104
prepList.push(...defPrepOpts[key])
114105
}
106+
this.initData()
115107
},
116108
methods: {
117-
// removeTag(item) {
118-
// const form = this.searchForm
119-
// form.tags.splice(form.tags.indexOf(item), 1)
120-
// form.tags = [...form.tags]
121-
// },
122-
searchMore() {
123-
this.page++
109+
initData() {
110+
let page = 1
111+
let sort = 2
112+
let prep = ''
113+
let keyword = ''
114+
let f = this.$route.query.f
115+
if (f) {
116+
const {
117+
page: fPage,
118+
sort: fSort,
119+
prep: fPrep,
120+
keyword: fKeyword,
121+
} = p2b.decode(f)
122+
page = parseInt(fPage)
123+
sort = parseInt(fSort)
124+
keyword = fKeyword
125+
prep = fPrep
126+
console.log(p2b.decode(f))
127+
}
128+
this.searchForm = { sort, prep, keyword }
129+
this.page = page
124130
this.getInstance()
125131
},
132+
newSearch() {
133+
// 重新从第一页开始搜索
134+
this.page = 1
135+
this.switchRoute()
136+
},
137+
switchPage(changeNum) {
138+
this.page += changeNum
139+
this.switchRoute()
140+
},
141+
switchRoute() {
142+
// 切换路由,如果没有name就只更新query查询信息
143+
const f = p2b.encode({ ...this.searchForm, page: this.page })
144+
if (this.$route.query.f === f) {
145+
this.getInstance()
146+
} else {
147+
const routeObj = { name: 'Explore', query: { f } }
148+
this.$router.push(routeObj).catch((err) => err)
149+
}
150+
},
126151
search() {
127152
this.$refs.searchField.blur()
128153
// 每次改变条件或点击按钮查询都清空列表再查
129154
this.instanceList = []
130-
this.page = 1
131-
this.getInstance()
155+
this.switchRoute()
132156
},
133157
async getInstance() {
134158
try {
135159
const { keyword, sort, prep } = this.searchForm
136-
if (keyword === '') return void 0
160+
// if (keyword === '') return void 0
137161
this.searchLoading = true
138162
this.listLoading = true
139163
this.nothing = false
140164
this.init = true
141165
const prepKey = judgeMode(prep)
142166
const res = await this.$http.searchWorksByContent({
143167
currentPage: this.page,
144-
queryContent: keyword,
168+
queryContent: keyword || '',
145169
orderCondition: sort,
146170
htmlStyle: prepKey === 'HTML' ? prep : '',
147171
cssStyle: prepKey === 'CSS' ? prep : '',
148172
jsStyle: prepKey === 'JavaScript' ? prep : '',
149173
})
150174
if (res.state) {
151175
this.$message.success('查询成功!')
152-
const { list, isLastPage } = res.data
176+
const { list, isLastPage, isFirstPage } = res.data
153177
this.nothing = list.length === 0
154-
this.instanceList.push(...list)
178+
this.showNothingTip = list.length === 0
179+
this.instanceList = list
180+
this.isFirstPage = isFirstPage
155181
this.isLastPage = isLastPage
156182
} else {
157183
this.nothing = true
@@ -163,18 +189,31 @@ export default {
163189
this.searchLoading = false
164190
this.listLoading = false
165191
},
192+
setFollow(isFollow, index) {
193+
this.instanceList[index].myFollow = isFollow
194+
},
195+
setFav(isFav, index) {
196+
const item = this.instanceList[index]
197+
item.myFavorites = isFav
198+
item.favorites += isFav ? 1 : -1
199+
},
166200
},
167201
components: {
168202
InstanceSkeleton,
169203
InstanceCard,
170204
GoToTop,
171205
},
172-
beforeRouteLeave (to, from, next) {
206+
beforeRouteUpdate(to, from, next) {
207+
next()
208+
this.instanceList = []
209+
this.getInstance()
210+
},
211+
beforeRouteLeave(to, from, next) {
173212
if (from.name === 'Explore' && to.name !== 'Work') {
174213
this.$destroy()
175214
}
176215
next()
177-
}
216+
},
178217
}
179218
</script>
180219
<style lang="scss">
@@ -221,16 +260,13 @@ export default {
221260
margin-top: 30px;
222261
display: grid;
223262
grid-gap: 30px;
224-
.skeleton-list-item {
225-
}
226-
}
227-
.view-more {
228-
display: flex;
229-
margin-top: 35px;
230263
}
231-
.no-more-tip {
232-
padding: 50px 0;
233-
display: flex;
264+
}
265+
.page-opt {
266+
display: flex;
267+
margin-top: 50px;
268+
.before-btn {
269+
margin-right: 15px;
234270
}
235271
}
236272
}

0 commit comments

Comments
 (0)