Files
edu_mp/pages/cloud-test/cloud-test.js
panw 8fc4630bb5 refactor(app): 移除分类和详情模块,新增云开发测试页
- 从项目中删除分类页及相关代码和样式文件
- 从项目中删除详情页及相关代码和样式文件
- 首页简化为欢迎界面并添加跳转云开发测试页入口
- 更新自定义底部栏移除分类入口
- 更新app.js初始化云开发环境
- 修改app.json移除分类页面配置
- 调整首页样式和交互,提高简洁度
- 配置云函数目录路径cloudfunctionRoot为cloudfunctions/
2025-12-05 15:58:08 +08:00

180 lines
4.7 KiB
JavaScript

// pages/cloud-test/cloud-test.js
Page({
data: {
logs: [],
testData: null,
loading: false
},
onLoad() {
this.addLog('页面加载完成,云开发测试就绪');
},
// 添加日志
addLog(message, type = 'info') {
const time = new Date().toLocaleTimeString();
const logs = [...this.data.logs, { time, message, type }];
this.setData({ logs });
},
// 清空日志
onClearLogs() {
this.setData({ logs: [] });
},
// 测试云函数
async onTestFunction() {
this.addLog('开始调用云函数 test...');
this.setData({ loading: true });
try {
const res = await wx.cloud.callFunction({
name: 'test',
data: {
action: 'test',
timestamp: Date.now()
}
});
this.addLog('云函数调用成功', 'success');
this.addLog(`返回结果: ${JSON.stringify(res.result)}`, 'success');
console.log('云函数返回:', res);
} catch (err) {
this.addLog(`云函数调用失败: ${err.message}`, 'error');
console.error('云函数错误:', err);
}
this.setData({ loading: false });
},
// 测试数据库 - 添加数据
async onTestDbAdd() {
this.addLog('开始测试数据库添加...');
this.setData({ loading: true });
try {
const db = wx.cloud.database();
const res = await db.collection('test_collection').add({
data: {
content: '测试数据',
createTime: db.serverDate(),
count: Math.floor(Math.random() * 100)
}
});
this.addLog('数据添加成功', 'success');
this.addLog(`文档ID: ${res._id}`, 'success');
this.setData({ testData: { _id: res._id } });
} catch (err) {
this.addLog(`数据库添加失败: ${err.message}`, 'error');
console.error('数据库错误:', err);
}
this.setData({ loading: false });
},
// 测试数据库 - 查询数据
async onTestDbQuery() {
this.addLog('开始测试数据库查询...');
this.setData({ loading: true });
try {
const db = wx.cloud.database();
const res = await db.collection('test_collection')
.orderBy('createTime', 'desc')
.limit(5)
.get();
this.addLog('数据查询成功', 'success');
this.addLog(`查询到 ${res.data.length} 条数据`, 'success');
res.data.forEach((item, index) => {
this.addLog(`[${index + 1}] ${item.content} - count: ${item.count}`, 'info');
});
} catch (err) {
this.addLog(`数据库查询失败: ${err.message}`, 'error');
console.error('数据库错误:', err);
}
this.setData({ loading: false });
},
// 测试数据库 - 删除数据
async onTestDbDelete() {
if (!this.data.testData?._id) {
this.addLog('请先添加测试数据', 'error');
return;
}
this.addLog('开始测试数据库删除...');
this.setData({ loading: true });
try {
const db = wx.cloud.database();
await db.collection('test_collection').doc(this.data.testData._id).remove();
this.addLog('数据删除成功', 'success');
this.setData({ testData: null });
} catch (err) {
this.addLog(`数据库删除失败: ${err.message}`, 'error');
console.error('数据库错误:', err);
}
this.setData({ loading: false });
},
// 测试云存储 - 上传图片
async onTestStorageUpload() {
this.addLog('开始测试云存储上传...');
try {
// 选择图片
const chooseRes = await wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera']
});
const filePath = chooseRes.tempFiles[0].tempFilePath;
const cloudPath = `test/${Date.now()}-${Math.random().toString(36).substr(2)}.png`;
this.setData({ loading: true });
this.addLog('正在上传图片...');
const uploadRes = await wx.cloud.uploadFile({
cloudPath,
filePath
});
this.addLog('图片上传成功', 'success');
this.addLog(`文件ID: ${uploadRes.fileID}`, 'success');
// 获取临时链接
const urlRes = await wx.cloud.getTempFileURL({
fileList: [uploadRes.fileID]
});
if (urlRes.fileList[0].tempFileURL) {
this.addLog(`临时链接: ${urlRes.fileList[0].tempFileURL.substr(0, 50)}...`, 'success');
}
} catch (err) {
if (err.errMsg?.includes('cancel')) {
this.addLog('用户取消选择', 'info');
} else {
this.addLog(`上传失败: ${err.message || err.errMsg}`, 'error');
}
console.error('存储错误:', err);
}
this.setData({ loading: false });
},
// 返回首页
onBack() {
wx.switchTab({
url: '/pages/index/index'
});
}
});