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.
 
 

119 lines
2.6 KiB

Component({
data: {
// 测试数据
items: [{
id: 1,
content: '打游戏',
checked: false,
pushed: false,
}],
// 输入框当前内容
inputedValue: "",
},
methods: {
// 更新待办的完成状态
checkboxChange(e) {
let items = JSON.parse(JSON.stringify(this.data.items));
for (const [index, item] of items.entries()) {
if (item.checked !== e.detail.value.includes(item.id)) {
// setData动态修改数据元素的一种方式
const key = `items[${index}].checked`;
const checked = !item.checked;
console.log(key, " ", checked);
this.setData({
[key]: checked,
});
// 调用云函数,更新数据库
getApp().cloud().callFunction({
name: 'updateCheck',
data: {
id: item.id,
checked: checked,
},
});
break;
}
}
},
// 获取随机id
getUUID(randomLength = 12) {
// 返回36位UUID
return Math.random().toString().substr(2, randomLength) + Date.now().toString(36);
},
// 监听输入框按键
keyInput(e) {
this.setData({
inputedValue: e.detail.value
})
},
// 新建待办事项
inputSubmit() {
// 设置新条目的id
const newID = this.getUUID();
if (this.data.inputedValue === "") {
console.log("输入为空");
return;
}
const newItem = {
id: newID,
content: this.data.inputedValue,
checked: false,
pushed: false,
};
// 将新条目更新到items状态中
let items = JSON.parse(JSON.stringify(this.data.items));
items.unshift(newItem);
this.setData({
items: items,
inputedValue: "",
});
// 将items提交到云数据库中
this.uploadData(newItem);
// 订阅服务
this.subscribe();
// 发送邮件服务
getApp().cloud().callFunction({
name: 'sendEmail',
data: {
content: newItem.content,
}
})
},
// 向用户申请发送推送服务
subscribe() {
// 填写模板id
const templateId = 'qyVdbBkue4HUeuzXIeHo8LH938gMPKJMj2bP9zTnQCo';
wx.requestSubscribeMessage({
tmplIds: [templateId],
success(res) {
console.log("订阅成功!", res);
},
fail(err) {
console.log('订阅失败!', err);
}
})
},
async uploadData(item) {
const db = await getApp().database();
db.collection('todo').add({
data: item,
})
}
},
//生命周期函数
lifetimes: {
// 在组件实例进入页面节点树时执行
attached() {
getApp().cloud().callFunction({
name: 'getList',
complete: res => {
console.log('获取数据成功!');
// console.log('Result: ',res);
this.setData({
items: res.result.fetchResult.data,
})
}
})
}
},
})