纸通宝小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

209 lines
5.4 KiB

// pages/moment/price/index.js
import { getCategoryPrice } from "../../api/moment"
const util = require('../../../utils/util')
const math = require('../../../utils/math')
const storage = require('../../../utils/storage')
const event = require('../../../utils/event')
var wxCharts = require('../../../utils/wxcharts')
const app = getApp()
var lineChart = null
const colors = ['#FF4229', '#FFAD2A', '#297BFF', '#29FF46', '#AD29FF']
Page({
/**
* 页面的初始数据
*/
data: {
height: app.globalData.fragmentHeight,
chartList: null,
idList: '',
loading: true,
requesting: false,
finished: false,
orderList: [],
form: { pageNum: 1 }
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
event.on('EventMessage', this, this.onEvent)
this.fetchChartList()
},
onEvent: function (message) {
if (message.what == 502) {
this.fetchChartList()
}
},
// 下拉刷新...
onRefreshList: function () {
this.setData({
orderList: [],
['form.pageNum']: 1,
loading: true,
height: app.globalData.fragmentHeight,
finished: false
})
this.fetchPapersList()
},
// 获取特价列表
fetchPapersList: function () {
if (this.data.requesting || this.data.finished) {
return
}
if (this.data.loading) {
this.data.requesting = true
} else {
this.setData({ requesting: true })
}
getCategoryPrice().then(result => {
if (result.data.records.length) {
var respList = result.data.records
let nowList = `orderList[${this.data.orderList.length}]`
var num = result.data.current
var finished = result.data.current >= result.data.pages
if(this.data.form.pageNum == 1){
this.setData({
[nowList]: respList,
total: result.data.total,
['form.pageNum']: (num + 1),
top: 0,
finished,
requesting: false,
loading: false
})
} else {
this.setData({
[nowList]: respList,
total: result.data.total,
['form.pageNum']: (num + 1),
finished,
requesting: false,
loading: false
})
}
} else {
this.setData({
finished: true,
requesting: false,
loading: false
})
}
}).catch(err => {
//异常回调
this.setData({
requesting: false,
finished: true,
loading: false
})
})
},
//***********************************fetchChartList*****************************************//
fetchChartList: function(){
var cateIds = storage.get('cateIdList' + app.globalData.userInfo.userId)
if(!util.isEmpty(cateIds)){
this.data.idList = cateIds
}
getCategoryPrice({idList: this.data.idList}).then(result => {
if(result.data.length <= 0){
return
}
var respList = result.data
var categoriesList = []
var serieList = []
var min = Number.MAX_VALUE
var max = 0
for (let index = 0; index < respList.length; index++) {
const element = respList[index]
var dataList = []
if(index == 0){
element.list.forEach(item => {
categoriesList.push(item.data.substring(5))
var price = math.times(item.unitPrice, 1000)
if(min > price){
min = price
}
if(max < price){
max = price
}
dataList.push(price)
})
} else {
element.list.forEach(item => {
var price = math.times(item.unitPrice, 1000)
if(min > price){
min = price
}
dataList.push(price)
})
}
var series = {
name: element.list[0].name,
data: dataList,
format: function (val, name) {
return val + '元/吨'
},
color: colors[index]
}
serieList.push(series)
}
this.setData({ chartList: { categories: categoriesList, serieList: serieList } })
// if(min < 50){
// min = 50
// }
this.initChart(min - 50, max + 50)
})
},
touchHandler: function (e) {
if(lineChart){
lineChart.scrollStart(e)
}
},
moveHandler: function (e) {
if(lineChart){
lineChart.scroll(e)
}
},
touchEndHandler: function (e) {
if(lineChart){
lineChart.scrollEnd(e)
lineChart.showToolTip(e, {
format: function (item, category) {
return category + ' ' + item.name + ':' + item.data
}
})
}
},
initChart: function (min, max) {
lineChart = new wxCharts({
canvasId: 'lineChart',
type: 'line',
categories: this.data.chartList.categories,
animation: false,
title: { name: '纸品单价 (元/吨)' },
series: this.data.chartList.serieList,
xAxis: { disableGrid: false },
yAxis: {
// title: '纸品单价 (元/吨)',
format: function (val) {
return val
},
min,
max
},
width: 350,
height: 200,
dataLabel: false,
dataPointShape: 'circle',
enableScroll: true,
extra: { lineStyle: 'curve' }
}, this)
},
toPaperList: function(){
wx.navigateTo({ url: '/pages/moment/paper/index' })
},
onUnload: function(){
event.remove('EventMessage', this)
}
})