Skip to content

Commit

Permalink
fix: 修复每日记账提醒云函数执行失败的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
ztkuaikuai committed Jun 9, 2024
1 parent eb30ed1 commit 61431fc
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ module.exports = {
// 定时任务触发函数
// 每日下午7点半执行,向订阅用户发送每日记账提醒
_timing: async function () {
mjDailyNoti.dailyNotification()
await mjDailyNoti.dailyNotification()
},
async test() {
await mjDailyNoti.dailyNotification()
},
async dailyNotification(){
const tmplId = 'n2kSsJNErg1EWpRrKqTDz2yZvyqC-LH7pLmudAsWNDE'
Expand Down Expand Up @@ -67,9 +70,13 @@ module.exports = {
// 获取昨日支出、收入
const yesterdayRes = await dbJQL.collection("mj-user-bills").where(`user_id == "${userInfo.uid}" && dateToString(add(new Date(0),bill_date),"%Y-%m-%d","+0800") == "${yesterdayDate}"`).groupBy('bill_type').groupField('sum(bill_amount) as bill_amount_total').orderBy('bill_type asc').get()
console.log('yesterdayRes: ',yesterdayRes);
const yesterExpenseTemp = yesterdayRes.data.filter(item => item.bill_type === 0)[0]?.bill_amount_total / 100 || '0'
const yesterTransferBalanceTemp = yesterdayRes.data.filter(item => item.bill_type === 2)[0]?.bill_amount_total / 100 || '0'
const yesterIncomeTemp = yesterdayRes.data.filter(item => item.bill_type === 1)[0]?.bill_amount_total / 100 || '0'
// 线上云函数不支持可选链操作符 es2020
const yesterExpenseTempObj = yesterdayRes.data.filter(item => item.bill_type === 0)[0]
const yesterExpenseTemp = yesterExpenseTempObj && yesterExpenseTempObj.bill_amount_total ? (yesterExpenseTempObj.bill_amount_total / 100) : '0'
const yesterTransferBalanceTempObj = yesterdayRes.data.filter(item => item.bill_type === 2)[0]
const yesterTransferBalanceTemp = yesterTransferBalanceTempObj && yesterTransferBalanceTempObj.bill_amount_total ? (yesterTransferBalanceTempObj.bill_amount_total / 100) : '0'
const yesterIncomeTempObj = yesterdayRes.data.filter(item => item.bill_type === 1)[0]
const yesterIncomeTemp = yesterIncomeTempObj && yesterIncomeTempObj.bill_amount_total ? (yesterIncomeTempObj.bill_amount_total / 100) : '0'
// 昨日支出和收入
const yesterdayExpense = Number(yesterExpenseTemp) + Number(yesterTransferBalanceTemp)
const yesterdayIncome = Number(yesterIncomeTemp)
Expand All @@ -78,8 +85,10 @@ module.exports = {
// 筛选条件 bill_date 日期格式化成 2023-09 的字段,按照账单类型进行分组,并计算每个分组的总价
const res = await dbJQL.collection("mj-user-bills").where(`user_id == "${userInfo.uid}" && dateToString(add(new Date(0),bill_date),"%Y-%m","+0800") == "${formattedDate}"`).groupBy('bill_type').groupField('sum(bill_amount) as bill_amount_total').orderBy('bill_type asc').get()
console.log(res)
const monthlyExpenseTemp = res.data.filter(item => item.bill_type === 0)[0]?.bill_amount_total / 100 || '0'
const transferBalanceTemp = res.data.filter(item => item.bill_type === 2)[0]?.bill_amount_total / 100 || '0'
const monthlyExpenseTempObj = res.data.filter(item => item.bill_type === 0)[0]
const monthlyExpenseTemp = monthlyExpenseTempObj && monthlyExpenseTempObj.bill_amount_total ? (monthlyExpenseTempObj.bill_amount_total / 100) : '0'
const transferBalanceTempObj = res.data.filter(item => item.bill_type === 2)[0]
const transferBalanceTemp = transferBalanceTempObj && transferBalanceTempObj.bill_amount_total ? (transferBalanceTempObj.bill_amount_total / 100) : '0'
// 月支出 = 月支出 + 转账的手续费
const monthlyExpense = Number(monthlyExpenseTemp) + Number(transferBalanceTemp)

Expand All @@ -92,7 +101,7 @@ module.exports = {
lang: "zh_CN",
data: {
"thing3": {
"value": "记得记记记账啊啊啊!"
"value": "记得记账啊啊啊啊啊啊啊!"
},
"amount6": {
"value": `${yesterdayExpense.toFixed(2)}元`
Expand Down

0 comments on commit 61431fc

Please sign in to comment.