计算每一天销售额
实现思路
- 获取seven_day_sales数据表数据
- 获取当前日期记录
无记录,新增当前日期销售记录
有记录,将当前订单总价与该日期销售额相加
// config/Date_anaysis.js
const db = wx.cloud.database()
const _ = db.command
const seven = db.collection('seven_day_sales')
class analysis {
constructor() {}
async sameday (time, sales_value) {
try {
let query = await seven.where({time}).get()
if (query.data.length === 0) {
await seven.add({data: {time, sales_value}})
} else {
let total_amount = Number(query.data[0].sales_value) + sales_value
let final_data = parseFloat((a).toFixed(10))
await seven.doc(query.data[0]._id).update({data: {sales_value:total_amount}})
}
} catch (e) {
throw '错误'
}
}
}
export {analysis}
小程序端调用订阅消息接口
订阅消息接口:wx.requestSubscribeMessage(Object object)
// pages/home-page-page.vue
// 省略部分代码
methods: {
placean_order () {
wx.requestSubscribeMessage({
tmplIds: [this.tmplIds],
success: (res) => {
this.sub_database()
},
fail: (res) => {
this.sub_database()
}
})
},
}
使用 GoEasy
Websocket消息发送和接收 - GoEasy PubSub
小程序端
// main.js
// 省略部分代码
import GoEasy from 'goeasy'
Vue.prototype.goeasy = GoEasy.getInstance({
host:"hangzhou.goeasy.io", //若是新加坡区域:singapore.goeasy.io
appkey:"您的common key",
modules:['pubsub']//根据需要,传入‘pubsub’或'im’,或数组方式同时传入
})
// App.vue
// 省略部分代码
onLaunch: function() {
//建立连接
this.goeasy.connect({
id:"", //pubsub选填,im必填
data:{}, //必须是一个对象,pubsub选填,im必填,用于上下线提醒和查询在线用户列表时,扩展更多的属性
onSuccess: function () { //连接成功
console.log("连接成功") //连接成功
},
onFailed: function (error) { //连接失败
console.log("连接失败");
},
onProgress:function(attempts) { //连接或自动重连中
console.log("连接或自动重连中");
}
});
}
后台
// src/main.js
// 省略部分代码
import GoEasy from 'goeasy'
// 即时通讯:订单提醒
Vue.prototype.goeasy = GoEasy.getInstance({
host: "hangzhou.goeasy.io", //若是新加坡区域:singapore.goeasy.io
appkey: "您的common key",
modules: ['pubsub']//根据需要,传入‘pubsub’或'im’,或数组方式同时传入
});
// src/App.vue
created() {
// 订单数存入本地缓存
const order_num = localStorage.getItem("order_num")
if(!order_num || order_num < 1){
localStorage.setItem('order_num',0)
}
//1.建立连接
this.goeasy.connect({
id:"001", //pubsub选填,im必填
data:{"avatar":"/www/xxx.png","nickname":"Neo"}, //必须是一个对象,pubsub选填,im必填,用于上下线提醒和查询在线用户列表时,扩展更多的属性
onSuccess: ()=>{ //连接成功
console.log("连接成功") //连接成功
this.sub_message()
},
onFailed: (error)=>{ //连接失败
console.log("连接失败");
},
onProgress:(attempts)=>{ //连接或自动重连中
console.log("连接或自动重连中");
}
});
},
methods:{
// 2.接收订阅消息
sub_message(){
var pubsub = this.goeasy.pubsub;
pubsub.subscribe({
channel: "my_channel",//替换为您自己的channel
onMessage: (message)=> {
const order_num_a = localStorage.getItem("order_num")
localStorage.setItem('order_num',Number(order_num_a) + 1)
const order_num_b = localStorage.getItem("order_num")
// console.log(order_num_b)
this.$store.commit('order_remind',Number(order_num_b))
// console.log(message);
},
onSuccess: ()=> {
console.log("Channel订阅成功。");
},
onFailed: (error)=> {
console.log("Channel订阅失败, 错误编码:" + error.code + " 错误信息:" + error.content)
}
});
}
}
即时通讯功能新订单提醒
// pages/home-page/page.vue
// 省略部分代码
methods: {
push_message () {
var pubsub = this.goeasy.pubsub
pubsub.publish({
channel: "my_channel",//替换为您自己的channel
message: "小程序端发送",//替换为您想要发送的消息内容
onSuccess:function(){
console.log("消息发布成功。");
},
onFailed: function (error) {
console.log("消息发送失败,错误编码:"+error.code+" 错误信息:"+error.content);
}
});
},
}