新年好!我的公共方法,只能被两次引用中的一个引用,这是为什么呢?谢谢!
抱歉我的代码就是这么长,我的疑问在如下代码的注释里,请大神们耐心看一下哈!感谢!
const db = wx.cloud.database()
const userOrder = db.collection('userOrder')
const _ = db.command
const $ = _.aggregate
Page({
data: {
timeMode: 'twoDay',
statusIndex: 0
},
onLoad() {
var that = this
userOrder
.watch({
onChange: function (e) {
let list = e.docs
console.log('onLoad',list)
let twoDayList = that.twoDayList(list)
console.log('onLoad',twoDayList)
that.handleData(twoDayList)
that.handleData(twoDayList)
that.setData({
list: list,
// newOrderList:twoDayList.reverse(),
chsDate: that.YYYYMMDD(new Date())
})
},
onError: function (err) {
console.error('the watch closed because of error', err)
}
})
},
//为什么twoDayList只可以被引用一次?
twoDay() {
let list = this.data.list
let two = this.twoDayList(list) // 是否twoDay的list根本就没有被引用到?
console.log('twoDay',two)
let twoDayList = []
for (let i in list) {
if (new Date(list[i].TS) >= this.yesterdayFirstSec() && new Date(list[i].TS) <= this.todayLastSec()) {
twoDayList.push(list[i])
}
}
this.handleData(twoDayList)
console.log(twoDayList)
this.setData({
newOrderList:twoDayList.reverse(),
timeMode: 'twoDay'
})
},
newOrder() {
this.setData({
statusIndex: 0
})
},
finish() {
var arr = this.data.arr
var finishList = []
for (var i in arr) {
if (arr[i].handled == true)
finishList.push(arr[i])
}
this.setData({
statusIndex: 1
})
},
orderFinish(e) {
var arr = this.data.arr
var index = e.currentTarget.dataset.index
var id = arr[index]._id
var TS = arr[index].TS
userOrder.where({
_id: id,
TS: TS
}).update({
data: {
handled: true,
handleTime: new Date()
}
})
arr[index].handled = true
arr[index].handleTime = this.time(new Date())
this.setData({
arr: arr
})
},
chsDate(e) {
var list = this.data.list
var chsDate = e.detail.value
var chsDateList = []
// console.log(list)
//在这里新数组是有必要的
for (var i in list) {
if (this.YYYYMMDD(list[i].TS) == chsDate) {
chsDateList.push(list[i])
}
}
this.handleData(chsDateList)
this.setData({
timeMode: 'chsDate',
chsDate: chsDate,
newOrderList:chsDateList.reverse()
// chsDateList: chsDateList.reverse()
})
},
// ------------------------------------公共方法-----------------------------------
testPublic(mList){
var arr = []
arr.push(mList[0])
return arr
},
twoDayList(mList){
let mTwoDayList = []
for (let i in mList) {
if (new Date(mList[i].TS) >= this.yesterdayFirstSec() && new Date(mList[i].TS) <= this.todayLastSec()) {
mTwoDayList.push(mList[i])
}
}
return mTwoDayList
},
handleData(mList) {
for (var i = 0; i < mList.length; i++) {
mList[i].OID = this.OID(mList[i].TS, mList[i]._openid)
mList[i].TS = this.time(mList[i].TS)
mList[i].rank = i + 1
mList[i].nickName = this.handleName(mList[i].nickName)
if (mList[i].gender == 1) {
mList[i].gender = '先生'
} else {
mList[i].gender = '女士'
}
}
return mList
},
OID(mDate, mID) {
var mId
var date = new Date(mDate) / 1 * 1
var four = mID.substring(mID.length - 4)
var result = date + four
return result
},
time(mDate) {
var date = new Date(mDate)
var M = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
var now = M + '-' + D + ' ' + h + ':' + m
return now
},
nowDate(mDate) {
var date = new Date(mDate)
var date2 = new Date()
var Y = date.getFullYear();
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
var D = date.getDate()
var checkDate = Y + '' + M + '' + D
var Y2 = date2.getFullYear();
var M2 = (date2.getMonth() + 1 < 10 ? '0' + (date2.getMonth() + 1) : date2.getMonth() + 1);
var D2 = date2.getDate()
var checkDate2 = Y2 + '' + M2 + '' + D2
return checkDate == checkDate2
},
todayLastSec() {
var date = new Date()
var Y = date.getFullYear()
var M = date.getMonth()
var D = date.getDate()
var result = new Date(Y, M, D, '23', '59', '59')
return result
},
yesterdayFirstSec() {
var date = new Date()
var Y = date.getFullYear()
var M = date.getMonth()
var D = date.getDate();
var result = new Date(Y, M, D - 1, '00', '00', '01')
return result
},
YYYYMMDD(mDate) {
var date = new Date(mDate)
var Y = date.getFullYear();
var M = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
var result = Y + '-' + M + '-' + D
return result
},
handleName(mName) {
var result = mName.replace(/[^a-zA-Z\u4e00-\u9fa5\d\s]+/g, '')
var REChina = /^[\u4E00-\u9FA5\s]{0,}$/ // 中文
var REEng = /^[A-Za-z\s]+$/ // 英文
var REMath = /^[0-9\s]*$/ // 数字
if (REChina.test(result)) {
return result.substring(0, 18)
} else if (REEng.test(result)) {
return result.substring(0, 30)
} else if (REMath.test(result)) {
return result.substring(0, 30)
} else {
return result.substring(0, 18)
}
}
})