// index.js // const db=wx.cloud.database() // import { models, db, getTempFileURLs } from '../../utils/cloudbase.js' import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js' const app = getApp(); Page({ data: { carousellist: [], homepage_8: '', homepage_9: '', souimg: '', }, onShow() { if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setSelected(0); // 首页索引是0 } // 获取轮播图数据 this.getBanner(); }, async onLoad() { // // 声明新的 cloud 实例 // var c1 = new wx.cloud.Cloud({ // // 资源方 小程序A的 AppID // resourceAppid: 'wx1ee76fb4846f8901', // // 资源方 小程序A的 的云开发环境ID // resourceEnv: 'honghgaier-5guiffgcf17a2eea', // }) // await c1.init() // 获取图片 const fileIDs = [ 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/homepage/homepage_8.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/homepage/homepage_9.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png', ]; const fileList = await getTempFileURLs(fileIDs) this.setData({ homepage_8: fileList[0].tempFileURL, homepage_9: fileList[1].tempFileURL, souimg: fileList[2].tempFileURL, }) // c1.getTempFileURL({ // fileList: fileIDs, // success: res => { // console.log('getTempFileURL结果', res.fileList); // this.setData({ // homepage_8: res.fileList[0].tempFileURL, // homepage_9: res.fileList[1].tempFileURL, // souimg: res.fileList[2].tempFileURL, // }); // }, // fail: err => { // console.error('getTempFileURL失败', err); // } // }); }, // 轮播数据 async getBanner() { const db = await getDB() const models = await getModels() const _ = await getCommand() const { data } = await models.banner.list({ filter: { where: { position: 0 } }, // envType: pre 体验环境, prod 正式环境 envType: "prod", }); let recordsdata = data.records || [] // 假设 banner 数据里有 fileID 或 cloudPath 字段 const fileIDs = recordsdata.map(item => item.url) // 或 item.fileID const tempFiles = await getTempFileURLs(fileIDs) // 给 records 添加可直接渲染的 url recordsdata = recordsdata.map((item, index) => ({ ...item, url: tempFiles[index].tempFileURL })) // 返回查询到的数据 this.setData({ carousellist: recordsdata }) }, // 页面跳转 二级页面 selectGroupOption(e) { const isLoggedIn = app.checkLoginStatus(); // 检查是否登录 if (!isLoggedIn) return; // 未登录已跳转,不继续 const option = e.currentTarget.dataset.option; let url = ''; switch (option) { case 'dahome': url = '/subpackages/dahome/dahome'; break; case 'imghome': url = '/subpackages/imghome/imghome'; break; default: return; } wx.navigateTo({ url: url }); }, // 搜索框跳转 goSearchPage() { wx.navigateTo({ url: '/subpackages/dahome/dahome?autofocus=1' }); }, // 上传轮播图 存储轮播图图片地址 和展示位置 后续需要存一个上传的图片名字 // async getData() { // try { // // 上传文件到云存储 // const uploadRes = await wx.cloud.uploadFile({ // cloudPath: "imgs/banner/carousel_3.jpg", // 上传至云端的路径 // filePath: "image/imgs/carousel_3.jpg" // 小程序临时文件路径 // }); // // 获取上传文件的 fileID // const fileID = uploadRes.fileID; // console.log("文件 ID:", fileID); // // 将文件路径存入 banner 表 // const { data } = await models.banner.create({ // data: { // remark: "", // 备注 // position: 4, // 展示位置 // describe: "", // 描述 // url: fileID, // 路径 // }, // envType: "prod", // 正式环境 // }); // // 返回创建的数据 id // console.log("创建的数据:", data); // } catch (error) { // console.error("操作失败:", error); // wx.showToast({ // title: '操作失败,请稍后再试', // icon: 'none', // }); // } // } // async // async getData() { // wx.cloud // .uploadFile({ // cloudPath: "imgs/banner/carousel_1.jpg", // 上传至云端的路径 // filePath: "image/imgs/carousel_1.jpg" // 小程序临时文件路径,需结合小程序相关 API 获取 // }) // .then((res) => { // // 返回文件 ID // const fileID = res.fileID; // console.log("文件 ID:", fileID); // // 下载文件 // wx.cloud.downloadFile({ // fileID: fileID, // }) // .then((res) => { // const tempFilePath = res.tempFilePath; // console.log("临时文件路径:", tempFilePath); // // 获取文件大小 // wx.getFileInfo({ // filePath: tempFilePath, // }) // .then((info) => { // console.log("文件大小:", info.size, "字节"); // const fileSizeInBytes = info.size; // 文件大小(字节) // const fileSizeInMB = fileSizeInBytes / (1024 * 1024); // 转换为 MB // console.log("文件大小:", fileSizeInMB.toFixed(2), "MB"); // }) // .catch((err) => { // console.error("获取文件大小失败:", err); // }); // }) // .catch((err) => { // console.error("文件下载失败:", err); // }); // }); // const { data } = await models.login.list({ // filter: { // where: {} // }, // pageSize: 10, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效 // pageNumber: 1, // 第几页 // getCount: true, // 开启用来获取总数 // // envType: pre 体验环境, prod 正式环境 // envType: "prod", // }); // // 返回查询到的数据列表 records 和 总数 total // console.log(data); // const { data } = await models.file_manage.get({ // filter: { // where: { // $and: [ // { // _id: { // $eq: 'BT7HKJ5Z2Y', // 推荐传入_id数据标识进行操作 // }, // }, // ] // } // }, // // envType: pre 体验环境, prod 正式环境 // envType: "prod", // }); // // 返回创建的数据 id // console.log(data); // { id: "7d8ff72c665eb6c30243b6313aa8539e"} // db.collection("homelist").get().then(res => { // console.log(res); // }).catch(err => { // console.log(err); // }) // }, // 退款 // async tuikuan() { // wx.cloud.callFunction({ // // 云函数名称 // name: 'wxpayFunctions', // data: { // // 调用云函数中的申请退款方法 // type: 'wxpay_refund', // // 业务其他参数... // // 推荐用 transaction_id,如果没有就用 out_trade_no // // transaction_id: this.data.transactionId, // // out_trade_no: this.data.outTradeNo, // transaction_id: '4200002828202509037486438814', // 微信支付订单号 // out_refund_no: 'refund_' + Date.now(), // amount: { // refund: 5, // 退款金额 // total: 5, // 原来订单金额 // currency: 'CNY', //退款币种 // // from: { // // account: '', // 出资账户类型 // // amount: '', // 出资金额 // // } // } // }, // success: (res) => { // console.log('申请退款结果: ', res); // }, // }); // }, })