- WXS日期转换?
想要实现,将数据库的时间转换为,今天,明天,后天,具体日期等。wxs写了段代码,打印结果是获取的数据库时间为Invalid Date 。数据库保存的时间两种方式和打印结果如图:在发布信息时,无论选择哪天,显示的都是今天。数据库保存的时间类型会影响转换吗? [图片][图片] 第二种 [图片] [图片] wxs代码: var timeFormat = function (dateString) { var date = getDate(dateString);// 传递日期字符串,得到日期 console.log('数据库的日期',date); var today = getDate() // 当前的时间 console.log('当前的日期',today) var timeStr = ''; var daysDifference = Math.abs(date.getTime() - today.getTime()); // console.log(daysDifference) var daysBetween = Math.floor(daysDifference/(1000*60*60*24)); console.log('天数差',daysBetween) var year = date.getFullYear(); var month = ('0' + (date.getMonth() + 1)).slice(-2); var day = ('0' + date.getDate()).slice(-2); var isToday = daysBetween === 0 ? '今天':''; var isTomorrow = daysBetween === 1 ? '明天':''; var isDayAfterTomorrow = daysBetween === 2 ? '后天':''; if(isToday){ timeStr = isToday; }else if(isTomorrow){ timeStr = isTomorrow; }else if(daysBetween = isDayAfterTomorrow){ timeStr = isDayAfterTomorrow; } else{ timeStr = year + "/" + month + "/" + day // timeStr = dateString } return timeStr; }
13小时前 - 如何让页面显示的后天随时间推移变成明天?
下面三张图,如何让页面中的后天,随着时间的推移,让后天变成明天,让日期变成后天,比如说,第一天是后天,第二天应该变成明天,后面日期(2024-11-20)变成后天在页面中显示。 目前的问题是,日期选择器含有今天,明天,后天和日期,保存到数据库也是今天,明天,后天,日期,显示到页面中的也是今天,明天,后天,但是页面中显示的不能随时间推移而改变。 想要实现的效果,日期选择器含有今天,明天,后天和日期。保存到数据库应该是什么?在页面中显示的应该随着时间的推移而改变。要怎么实现 [图片][图片][图片]
星期日 18:36 - 下拉刷新失效?
如图布局图,在点击查询后,在查询的状态下,下拉刷新,没有退出查询状态,下拉刷新失效,在第二次下拉才生效。在查询状态下,切换到其他页面,再回到该页面,没有回到初始页面,一直保持在查询状态下。 想要的效果是,在查询状态下,下拉刷新退出查询状态,回到初始页面状态,从其他页面切换到该页面,回到初始页面。 [图片] 代码: /** * 获取选项卡 */ onItemChengdeEvent(event) { var index = event.detail.index; // console.log(index); this.setData({ currentIndex: index }); if (!this.data.isQuerying) { this.lowdrelease(); } else { this.onchaxun() } }, /** * 获取数据库数据 */ async loadData(start = 0, vehicle,newQuery = {}) { const that = this; const index = that.data.currentIndex; console.log(index); let query = {}; switch (index) { case 0: break; case 1: query = {carpool: '我要找车'}; break; case 2: query = {carpool: '我要找人'}; break; case 3: query = {carpool: '我找货车'}; break; case 4: query = {carpool: '我要找货'}; break; } console.log(query) if (that.data.isQuerying) { newQuery = _.and(query,{ startPoint: db.RegExp({ regexp: '^' + that.data.startPoint }), goal: db.RegExp({ regexp: '^' + that.data.goal }), }) } else { newQuery = query } console.log('条件查询', newQuery) try { let promise = db.collection(vehicle).where(newQuery); if (start > 0) { promise = promise.skip(start); } promise = promise.limit(2) .orderBy("redden.reddenStartTime", 'desc') .orderBy("topping.toppingStarttime", "desc") .orderBy("create_time", "desc"); const vehiclesRes = await promise.get(); const vehicles = vehiclesRes.data; console.log('获取数据库数据', vehicles) const countResult = await promise.count(); const total = countResult.total; console.log('总计数', total) const newVehicles = start > 0 ? this.data.vehicles.concat(vehicles) : vehicles; newVehicles.forEach((vehicle, index) => { vehicle.create_time = vehicle.create_time.toString(); }); const hasmore = false; return { newVehicles, total, hasmore, }; } catch (error) { console.log('failed to load data:', error); throw error; } }, /** * 获取初始数据 */ async lowdrelease(start = 0) { const that = this; try { const { newVehicles: vehicles, total: vehiclesTotal, hasmore: hasmore } = await that.loadData(start, 'vehicle'); that.setData({ vehicles: vehicles, hasmore: hasmore, result: vehiclesTotal, }) } catch (error) { console.log('failed to load data:', error); throw error; } }, /** * 点击查询事件 */ async onchaxun(start = 0) { const that = this; that.setData({ isQuerying:true }) try { const { newVehicles: vehicles, total: vehiclesTotal, hasmore: hasmore } = await that.loadData(start, 'vehicle'); that.setData({ startPointStr: '', goalStr: '', vehicles: vehicles, hasmore: hasmore, result: vehiclesTotal, }); } catch (error) { console.log('failed to load data:', error); throw error; } }, /** * 生命周期函数--监听页面显示 */ onShow() { this.lowdrelease(0); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.lowdrelease(0); this.setData({ isQuerying: false }) wx.stopPullDownRefresh(); },
11-12 - 在查询数据时,报错-601002?
如图,当点击查询按钮时,报错,当点击选项卡的时候,获取到对应的数据,当下拉刷新时,没有反应,当点击选项卡时,获取到对应的数据。求指教,这个查询条件的索引要如何弄? [图片][图片] data: { title: ["全部", "我要找车", "我要找人", "我找货车", "我要找货"], hasmore: false, vehicles: '', startPoint: '', goal: '', isQuerying: false, currentIndex: '', vhereRes: '', }, /** * 获取选项卡 */ onItemChengdeEvent(event) { var index = event.detail.index; this.setData({ currentIndex: index }); if (!this.data.isQuerying) { this.lowdrelease(); } else { this.onchaxun() } }, /** * 获取数据库数据 */ async loadData(start = 0, vehicle) { const that = this; const index = that.data.currentIndex; let query = {}; switch (index) { case 0: break; case 1: query = {carpool: '我要找车'}; break; case 2: query = {carpool: '我要找人'}; break; case 3: query = {carpool: '我找货车'}; break; case 4: query = {carpool: '我要找货'}; break; } console.log(query) const vhereRes = { startPoint: db.RegExp({ regexp: that.data.startPoint, options: 'i' }), goal: db.RegExp({ regexp: that.data.goal, options: 'i' }) }; console.log('查询事件起点和终点条件', vhereRes) const quer = that.data.isQuerying ? _.and(vhereRes, query) : query; console.log(quer) const promise = db.collection(vehicle).skip(start).limit(2) .orderBy("redden.reddenStartTime", 'desc') .orderBy("topping.toppingStarttime", "desc") .orderBy("create_time", "desc"); const dataRes = await promise.where(quer).get(); const vehicles = dataRes.data; console.log(vehicles); const countRes = await db.collection(vehicle).where(quer).count(); const total = countRes.total; console.log(total) const newVehicles = start > 0 ? this.data.vehicles.concat(vehicles) : vehicles; newVehicles.forEach((vehicle, index) => { vehicle.create_time = vehicle.create_time.toString(); }); const hasmore = false return {newVehicles,total,hasmore}; }, /** * 获取初始数据 */ async lowdrelease(start = 0) { const that = this; const {newVehicles: vehicles,total: vehiclesTotal,hasmore: hasmore} = await that.loadData(start, 'vehicle'); that.setData({ vehicles: vehicles, hasmore: hasmore, result: vehiclesTotal }) }, /** * 点击查询事件 */ async onchaxun(start = 0) { const that = this; that.setData({ isQuerying: true, }) const {newVehicles: vehicles,total: vehiclesTotal,hasmore: hasmore} = await that.loadData(start, 'vehicle'); that.setData({ startPointStr: '', goalStr: '', vehicles: vehicles, hasmore: hasmore, result: vehiclesTotal }); },
11-11 - 点击查询按钮,获取到的数据不是对应的选项卡数据?
如图,在点击查询按钮之前,选择的是第二个选项卡,当点击查询按钮后,第二个选项卡的内容是第一个选项卡的内容,当点击选项卡的时候,才会获取对应的数据,也就是说,点击查询按钮获取到的是查询数据的初始数据,但是选项卡不是初始值。想要的效果是:点击查询按钮之前,选择的第二个选项卡,那么点击查询按钮后获取到的数据也应该是第二个选项卡的数据 第二问题是:如图2和图3:显示的Number类型,但是报警提示,传入componentes组件的不是Number类型,也就是重新编译代码时,才会有这个警告,其他操作就不会有这个警告出现,这个是什么情况 [图片] [图片][图片]
11-07 - 上拉刷新,加载的数据不是对应选项卡的内容?
如图:当在第五个选项卡的位置,下拉刷新或者点击查询的时候,加载的数据是第一个选项卡的初始数据,而不是第三个选项卡的数据。想要的效果是,要么下拉或者点击查询事件之后,选项卡选中第一个选项卡,要么加载相当应选项卡的数据。 [图片] 代码: /** * 获取选项卡 */ onItemChengdeEvent(event) { var index = event.detail.index; this.setData({ currentTabIndex: index }); if(!this.data.isQuerying){ this.lowdrelease(index); }else{ this.onchaxun(index); } }, /** * 获取数据库数据 */ async lowdrelease(index,start = 0) { const that = this; let query = {}; switch (index) { case 0: break; case 1: query = { carpool: '我要找车' }; break; case 2: query = { carpool: '我要找人' }; break; case 3: query = { carpool: '我找货车' }; break; case 4: query = { carpool: '我要找货' }; break; } console.log('carpool条件',query) let promise = db.collection("wehicle").where(query); if (start > 0) { promise = promise.skip(start); } // 限制每次获取数据数量并排序 promise = promise.limit(5) .orderBy("redden.reddenStartTime", 'desc') .orderBy("topping.toppingStarttime", "desc") .orderBy("create_time", "desc"); const wehiclesRes = await promise.get(); const wehicles = wehiclesRes.data; console.log('获取数据库数据',wehicles) // 统计符合条件的总数而不是所有数据 const countResult = await promise.count(); console.log('总计数',countResult) // 合并当前获取的数据 const newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles; newWehicles.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }); // const hasmore = false; const hasmore = countResult.total > (that.data.wehicles.length + start); that.setData({ wehicles: newWehicles, hasmore: hasmore, result: countResult.total }); }, /** * 点击查询事件 */ async onchaxun(index,start = 0) { const that = this; that.setData({ isQuerying: true, }) const whereRes = { startPoint: db.RegExp({ regexp: that.data.startPoint}), goal: db.RegExp({ regexp: that.data.goal}), } console.log('查询事件起点和终点条件',whereRes) let query = {}; switch (index) { case 0: break; case 1: query = {carpool: '我要找车'}; break; case 2: query = {carpool: '我要找人'}; break; case 3: query = {carpool: '我找货车'}; break; case 4: query = {carpool: '我要找货'}; break; } console.log('查询事件查询条件',query) let promise = db.collection("wehicle").where(_.and(whereRes,query)) if (start > 0) { promise = promise.skip(start) } // 限制每次获取数据数量并排序 promise = promise.limit(2) .orderBy("redden.reddenStartTime", 'desc') .orderBy("topping.toppingStarttime", "desc") .orderBy("create_time", "desc"); const wehiclesRes = await promise.get(); const wehicles = wehiclesRes.data; console.log('查询事件获取数据库数据',wehicles) // 统计符合条件的总数而不是所有数据 const countResult = await promise.count(); console.log('查询事件总计数',countResult) // 合并当前获取的数据 const newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles; newWehicles.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }); // const hasmore = false; const hasmore = countResult.total > (that.data.wehicles.length + start); that.setData({ startPointStr: '', goalStr: '', wehicles: newWehicles, hasmore: hasmore, result:countResult.total, }); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.lowdrelease(0); this.setData({ isQuerying: false, }) wx.stopPullDownRefresh(); },
11-06 - 上拉加载更多失效?
原来的代码是查询了5次才实现的效果,想优化代码,优化之后,遇到两个问题,1、上拉加载失效,加载不出数据。2、第二个问题是,总计数,当上拉加载后,计数被改了,显示的是数据库全部的数量,如图。求大神指点,是什么问题? [图片] [图片] 代码: /** * 获取数据库数据 */ async lowdrelease(index,start = 0) { const that = this; let query = {}; switch(index){ case 0: //查询全部数据 break; case 1: query = {carpool:'我要找人'}; break; case 2: query = {carpool:'我要找车'}; break; case 3: query = {carpool:'我找货车'}; break; case 4: query = {carpool:'我要找货'}; break } let promise = db.collection("wehicle").where(query); if (start > 0) { promise = promise.skip(start); console.log(promise) } promise.limit(5) .orderBy("redden.reddenStartTime",'desc') .orderBy("topping.toppingStarttime","desc") .orderBy("create_time", "desc") .get().then(res => { console.log(res); const wehicles = res.data; promise.count().then(res => { that.setData({ result: res.total }); }); let hasmore = false let newWehicles = start > 0 ? that.data.wehicles.concat(wehicles) : wehicles; newWehicles.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }); that.setData({ wehicles:newWehicles, hasmore:hasmore, }) }) }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { let hasmore = true; if(this.data.wehicles.length == 0){ hasmore = false } this.setData({ hasmore:hasmore }) if(!this.data.isQuerying) { this.lowdrelease(this.data.wehicles.length); }else{ this.onchaxun(this.data.listzeros.length); } },
10-25 - 想实现一个动态样式,让文字颜色改变?
实现效果:当点击变红按钮弹窗,点击确定后,如果数据库有redden这个字段,就让文字变成红色,如果没有就不变。现在写的代码是,文字颜色没有改变,是什么问题? wxml代码: <view class="left" style="color:{{color}};" > <view class="top-group"> <view class="top" wx:if="{{wehicle.istoppinged}}">顶</view> <view class="start-time">出发:{{wehicle.day}} {{wehicle.times}}</view> </view> <view class="distance-group"> <view class="distance">{{wehicle.startPoint}}</view> <view class="distance">→</view> <view class="distance">{{wehicle.goal}}</view> <view class="distance" wx:if="{{wehicle.carpool == '我要找人' || wehicle.carpool == '我要找货'}}"> ({{wehicle.pass}})</view> </view> <view class="car">车型:{{wehicle.car}}</view> <view class="content">备注:{{wehicle.content}}</view> <view class="end-time">发布时间:{{util.timeFormat(wehicle.create_time)}}</view> </view> js代码: // 变红点击确定 async confirmRedden(e) { const that = this; const wehicles = that.data.wehicles; const redindex = that.data.redindex; const wehicle = wehicles[redindex]; const id = wehicle._id; const ReddenStartTime = new Date().getTime(); const ReddenEndTime = that.data.codered; wx.showLoading({ title:"正在变红中..." }) await db.collection("wehicle").doc(id).update({ data:{ redden:{ ReddenStartTime:ReddenStartTime, ReddenEndTime:ReddenEndTime } } }).then(res => { if(!wehicle.redden){ wehicle.redden = (ReddenEndTime) }else{ wehicle.redden.push(ReddenEndTime) } wehicle.isreddened = true; wehicles[redindex] = wehicle; if(wehicle.redden){ wx.hideLoading; wx.showToast({ title:'恭喜!变红成功!' }) setTimeout(function(){ that.setData({ show_red: false, color:'red', // 改变文字颜色为红色 wehicles:wehicles, }) },800) }else{ wx.showToast({ title:"变红失败!请重新变红!" }) that.setData({ color:'', }) } }) },
10-18 - 云函数定时器,出发显示调用成功,返回的是null?
在学习中,想实现一个置顶功能,用云函数定时器,到时间删除字段实现取消置顶功能,调用成功,返回的结果是null,是什么问题? [图片] [图片] 云函数代码: 定时器: { "permissions": { "openapi": [ ] }, "triggers":[ { "name":"mytimer", "type":"timer", "config":"*/30 * * * * * *" } ] } 执行代码: // 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() const _ = db.command // 云函数入口函数 exports.main = async (event, context) => { exports.schedule = { run:async() => { const currentTime = new Date().getTime(); return await db.collection("wehicle").where({timelength:topping.timelength}).watchChanges({ onChange:snapshot => { snapshot.docs.forEach(doc => { if(doc.topping.timelength < currentTime){ await db.collection("wehicle").doc(doc._id).update({ data:{ topping:_.remove() } }) } }) } }) }, } }
10-15 - 定时取消置顶,报错:排序字符不合法?
如图:设定到期时间,到时间让topping字段为空或者删除,取消置顶的功能,在定时项目中,报错:排序字符不合法。求大神指教: [图片] [图片] 插入timelength字段代码: async confirm(e) { const that = this; const code = that.data.code; const timelength = new Date() + code*1000*60*60; const index = that.data.index; const wehicle = that.data.wehicles[index]; const wehicles = that.data.wehicles; wx.showLoading({ title: "正在置顶中..." }) wx.cloud.callFunction({ name: "topping", data: { id: wehicle._id, timelength: timelength, }, success: res => { console.log(res); if(!wehicle.topping){ wehicle.topping = (timelength) }else{ wehicle.topping.push(timelength) } wehicle.istoppinged = true; wehicles[index] = wehicle; if (wehicle.topping) { wx.hideLoading(); wx.showToast({ title: "恭喜!置顶成功!", }) setTimeout(function () { that.setData({ show_input: false, wehicles:wehicles }) }, 800) } else { wx.showToast({ title: "置顶失败,请重新置顶!", }) } } }) }, 定时器代码timer: { "permissions": { "openapi": [ ] }, "triggers":[ { "name":"mytimer", "type":"timer", "config":"*/10 * * * * * *" } ] } 需要执行代码: // 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() const _ = db.command // 云函数入口函数 exports.main = async (event, context) => { const currentTime = new Date().getTime() // 当前时间的时间戳(毫秒) // 查询所有到期(即当前时间已超过timelength)的文档并更新他们,删除topping字段 return await db.collection("wehicle").orderBy('topping.timelength','lt').watchChanges({ onChange:snapshot => { snapshot.docs.forEach(doc => { if(doc.topping.timelength < currentTime){//如果当前时间超过了timelength,则更新文档,删除topping字段 db.collection("wehicle").doc(doc._id).update({data:{topping:[]}})//清空topping字段或设置为空数组(根据你的需求决定) } }) } }) }
10-12