import { getFactoryPrice, getProxyOrderList } from "../../../api/ztb" import { getPreferList, getInformationList } from "../../../api/moment" const storage = require('../../../utils/storage') const util = require('../../../utils/util') const tdsdk = require('../../../libs/tdweapp.js') const app = getApp() Page({ /** * 页面的初始数据 */ data: { height: app.globalData.fragmentHeight - 100, focus: true, rearchStringList: [], tabList: [ '交易', '行情', '订单' , '资讯'], tabIndex: 0, form: { searchKey: '', pageSize: 10, pageNum: 1 }, loading: false, requesting: false, finished: false, taskList: [] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if(options.tab) { this.data.tabIndex = Number(options.tab) } var min = new Date() min.setHours(0, 0, 0) this.data.form.date = util.formatDate(min, 'Y-M-D') this.setData({height: app.globalData.fragmentHeight - 100, tabIndex: this.data.tabIndex}) var searchString = storage.get('mall-search') if(!util.isEmpty(searchString)){ this.setData({rearchStringList: searchString.split(',').slice(0, 24)}) } }, clearHistory: function(){ var that = this wx.removeStorage({ key: 'mall-search', success (res) { that.setData({ rearchStringList: [] }) } }) }, onFocusChange: function(){ this.setData({ focus: true }) }, onBlurChange: function(){ if(util.isEmpty(this.data.form.searchKey)){ return } this.setData({ focus: false }) }, onTabChange: function({detail}){ if(this.data.tabIndex == Number(detail.index)){ return } this.data.tabIndex = Number(detail.index) if(util.isEmpty(this.data.form.searchKey)){ return } this.onSearchList({ detail:{value: this.data.form.searchKey} }) }, onSearchList: function({detail}){ if (!app.globalData.userInfo && this.data.tabIndex == 3) { wx.navigateTo({ url: '/pages/login/index' }) return } this.data.form.searchKey = detail.value if(util.isEmpty(this.data.form.searchKey)){ util.showToast('请输入要搜索的关键字') return } var strList = this.data.rearchStringList if(!util.isEmpty(this.data.form.searchKey) && strList.indexOf(this.data.form.searchKey) < 0){ strList.splice(0, 0, this.data.form.searchKey) } this.data.form.pageNum = 1 this.setData({ taskList: [], loading: true, finished: false, rearchStringList: strList.slice(0, 24), focus: false, ['form.searchKey']: detail.value }) this.fetchTaskList() }, fetchTaskList: function(){ if (this.data.requesting || this.data.finished) { return } if (this.data.loading) { this.setData({ loading: true }) } else { this.setData({ requesting: true }) } if(this.data.tabIndex == 0){ getFactoryPrice(this.data.form).then(result => { if (result.data && result.data.records.length) { this.handRespList(result.data) } else { this.setData({ finished: true, requesting: false, loading: false, tabIndex: this.data.tabIndex }) } }).catch(err => { this.setData({ requesting: false, loading: false, tabIndex: this.data.tabIndex}) util.showToast(err) }) } else if(this.data.tabIndex == 1){ getInformationList(this.data.form).then(result => { if (result.data && result.data.records.length) { this.handRespList(result.data) } else { this.setData({ finished: true, requesting: false, loading: false, tabIndex: this.data.tabIndex }) } }).catch(err => { this.setData({ requesting: false, loading: false, tabIndex: this.data.tabIndex }) util.showToast(err) }) } else if(this.data.tabIndex == 2){ getProxyOrderList(this.data.form).then(result => { if (result.data && result.data.records.length) { this.handRespList(result.data) } else { this.setData({ finished: true, requesting: false, loading: false, tabIndex: this.data.tabIndex }) } }).catch(err => { this.setData({ requesting: false, loading: false, tabIndex: this.data.tabIndex }) util.showToast(err) }) } else if(this.data.tabIndex == 3){ getPreferList(this.data.form).then(result => { if (result.data && result.data.records.length) { this.handRespList(result.data) } else { this.setData({ finished: true, requesting: false, loading: false, tabIndex: this.data.tabIndex }) } }).catch(err => { this.setData({ requesting: false, loading: false, tabIndex: this.data.tabIndex }) util.showToast(err) }) } }, handRespList: function(data){ let nowList = `taskList[${this.data.taskList.length}]` var num = this.data.form.pageNum var finished = this.data.form.pageNum >= data.pages if (this.data.form.pageNum == 1) { this.setData({ [nowList]: data.records, total: data.total, ['form.pageNum']: (num + 1), top: 0, finished, requesting: false, loading: false, tabIndex: this.data.tabIndex }) } else { this.setData({ [nowList]: data.records, total: data.total, ['form.pageNum']: (num + 1), finished, requesting: false, loading: false, tabIndex: this.data.tabIndex }) } }, tagClick: function(e){ this.data.form.searchKey = this.data.rearchStringList[e.currentTarget.dataset.index] this.onSearchList({detail:{value: this.data.form.searchKey}}) }, onShow: function () { tdsdk.Page.onShow() }, onHide: function(){ tdsdk.Page.onHide() }, onUnload: function() { tdsdk.Page.onUnload() if(this.data.rearchStringList.length){ storage.put('mall-search', this.data.rearchStringList.toString()) } } })