123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- import {
- getCategoryWorks,
- getCategoryLowerList,
- searchWorks
- } from '~/api/works';
- import {
- setUserInfo
- } from '~/api/user'
- import reachBottom from '~/mixins/reachBottom';
- import {
- createStoreBindings
- } from 'mobx-miniprogram-bindings';
- import event from '~/mixins/event'
- import {
- store
- } from '~/store/index';
- let storeBindings;
- Page({
- behaviors: [reachBottom, event],
- startPageX: 0,
- data: {
- // class为二级,search为搜索
- type: 'class',
- placeholderText: '请输入搜索内容',
- categoryList: [],
- childType: '',
- currentIndex: 0,
- scrollTop: 0,
- text: '',
- currentId: '',
- navBarTitle: '',
- historySearch: [],
- },
- /**
- * 生命周期函数--监听页面加载
- */
- async onLoad(options) {
- console.log(options, 'options');
- if (options.id) {
- let categoryList = await getCategoryLowerList(options.id)
- if (categoryList.length == 0) {
- this.setData({
- childType: options.id
- });
- } else {
- this.setData({
- categoryList
- });
- }
- this.resetData();
- }
- wx.setNavigationBarTitle({
- title: options.title || '搜索'
- });
- this.setData({
- type: options.type,
- historySearch: wx.getStorageSync('search'),
- navBarTitle: options.title
- });
- this.storeBindings = createStoreBindings(this, {
- store,
- fields: {
- userInfo: 'userInfo'
- }
- });
- // 立刻更新
- this.storeBindings.updateStoreBindings();
- setTimeout(() => {
- if (options.grade && !this.data.userInfo.grade) {
- setUserInfo({
- grade: options.grade
- }, 'put')
- }
- }, 1000)
- },
- // 获取分类的内容
- loadMore() {
- if (this.data.type == 'search') {
- return;
- }
- let columnId = this.data.childType ? this.data.childType : this.data.categoryList[this.data.currentIndex].id;
- this.getData(getCategoryWorks, {
- columnId
- });
- },
- setClass({
- currentTarget
- }) {
- this.setData({
- scrollTop: 0,
- navBarTitle: currentTarget.dataset.title,
- currentIndex: currentTarget.dataset.index,
- currentId: `class${currentTarget.dataset.index}`
- });
- this.resetData();
- },
- setSearch({
- detail
- }) {
- if (!detail.value) {
- this.setData({
- nullList: false,
- list: []
- });
- }
- this.setData({
- text: detail.value
- });
- },
- async search() {
- if (!this.data.text) {
- this.setData({
- list: []
- });
- return;
- }
- let list = await searchWorks({
- title: this.data.text,
- grade: this.data.userInfo.grade
- });
- if (!this.data.historySearch.includes(this.data.text)) {
- this.setData({
- historySearch: [this.data.text, ...this.data.historySearch].slice(0, 20)
- });
- wx.setStorageSync('search', this.data.historySearch);
- }
- this.setData({
- list,
- nullList: list.length == 0
- });
- },
- historySearch({
- currentTarget
- }) {
- this.setData({
- text: currentTarget.dataset.text
- });
- this.search();
- },
- deleteHistory({
- currentTarget
- }) {
- let newList = this.data.historySearch.filter(item => {
- return item != currentTarget.dataset.text;
- });
- this.setData({
- historySearch: newList.slice(0, 20)
- });
- wx.setStorageSync('search', this.data.historySearch);
- },
- clearHistory() {
- wx.showModal({
- title: '温馨提示',
- content: '历史记录清除后无法恢复,是否清除全部记录',
- success: res => {
- if (res.confirm) {
- this.setData({
- historySearch: []
- });
- wx.setStorageSync('search', this.data.historySearch);
- }
- }
- });
- },
- goRead({
- currentTarget
- }) {
- wx.navigateTo({
- url: `/pages/reading/index?videoId=${currentTarget.dataset.id}&navBarTitle=${this.data.navBarTitle || ''}`
- });
- },
- touchStart(e) {
- this.startPageX = e.changedTouches[0].pageX;
- },
- touchEnd(e) {
- const moveX = e.changedTouches[0].pageX - this.startPageX;
- if (Math.abs(moveX) >= 100) {
- if (moveX > 0) {
- if (this.data.currentIndex > 0) {
- let data = this.data.categoryList[this.data.currentIndex - 1]
- let index = data.sort - 1
- this.setClass({
- currentTarget: {
- dataset: {
- title: data.title,
- index
- }
- }
- })
- }
- } else {
- if (this.data.currentIndex < this.data.categoryList.length - 1) {
- let data = this.data.categoryList[this.data.currentIndex + 1]
- this.setClass({
- currentTarget: {
- dataset: {
- title: data.title,
- index: data.sort - 1
- }
- }
- })
- }
- }
- }
- },
- cleanPlaceholder() {
- this.setData({
- placeholderText: ''
- })
- },
- setPlaceholder() {
- this.setData({
- placeholderText: '请输入搜索内容'
- })
- },
- onUnload() {
- this.storeBindings.destroyStoreBindings();
- }
- });
|