index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // index.js
  2. // const db=wx.cloud.database()
  3. // import { models, db, getTempFileURLs } from '../../utils/cloudbase.js'
  4. import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
  5. const app = getApp();
  6. Page({
  7. data: {
  8. carousellist: [],
  9. homepage_8: '',
  10. homepage_9: '',
  11. souimg: '',
  12. },
  13. onShow() {
  14. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  15. this.getTabBar().setSelected(0); // 首页索引是0
  16. }
  17. // 获取轮播图数据
  18. this.getBanner();
  19. },
  20. async onLoad() {
  21. // // 声明新的 cloud 实例
  22. // var c1 = new wx.cloud.Cloud({
  23. // // 资源方 小程序A的 AppID
  24. // resourceAppid: 'wx1ee76fb4846f8901',
  25. // // 资源方 小程序A的 的云开发环境ID
  26. // resourceEnv: 'honghgaier-5guiffgcf17a2eea',
  27. // })
  28. // await c1.init()
  29. // 获取图片
  30. const fileIDs = [
  31. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/homepage/homepage_8.png',
  32. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/homepage/homepage_9.png',
  33. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png',
  34. ];
  35. const fileList = await getTempFileURLs(fileIDs)
  36. this.setData({
  37. homepage_8: fileList[0].tempFileURL,
  38. homepage_9: fileList[1].tempFileURL,
  39. souimg: fileList[2].tempFileURL,
  40. })
  41. // c1.getTempFileURL({
  42. // fileList: fileIDs,
  43. // success: res => {
  44. // console.log('getTempFileURL结果', res.fileList);
  45. // this.setData({
  46. // homepage_8: res.fileList[0].tempFileURL,
  47. // homepage_9: res.fileList[1].tempFileURL,
  48. // souimg: res.fileList[2].tempFileURL,
  49. // });
  50. // },
  51. // fail: err => {
  52. // console.error('getTempFileURL失败', err);
  53. // }
  54. // });
  55. },
  56. // 轮播数据
  57. async getBanner() {
  58. const db = await getDB()
  59. const models = await getModels()
  60. const _ = await getCommand()
  61. const { data } = await models.banner.list({
  62. filter: {
  63. where: {
  64. position: 0
  65. }
  66. },
  67. // envType: pre 体验环境, prod 正式环境
  68. envType: "prod",
  69. });
  70. let recordsdata = data.records || []
  71. // 假设 banner 数据里有 fileID 或 cloudPath 字段
  72. const fileIDs = recordsdata.map(item => item.url) // 或 item.fileID
  73. const tempFiles = await getTempFileURLs(fileIDs)
  74. // 给 records 添加可直接渲染的 url
  75. recordsdata = recordsdata.map((item, index) => ({
  76. ...item,
  77. url: tempFiles[index].tempFileURL
  78. }))
  79. // 返回查询到的数据
  80. this.setData({
  81. carousellist: recordsdata
  82. })
  83. },
  84. // 页面跳转 二级页面
  85. selectGroupOption(e) {
  86. const isLoggedIn = app.checkLoginStatus(); // 检查是否登录
  87. if (!isLoggedIn) return; // 未登录已跳转,不继续
  88. const option = e.currentTarget.dataset.option;
  89. let url = '';
  90. switch (option) {
  91. case 'dahome':
  92. url = '/subpackages/dahome/dahome';
  93. break;
  94. case 'imghome':
  95. url = '/subpackages/imghome/imghome';
  96. break;
  97. default:
  98. return;
  99. }
  100. wx.navigateTo({
  101. url: url
  102. });
  103. },
  104. // 搜索框跳转
  105. goSearchPage() {
  106. wx.navigateTo({
  107. url: '/subpackages/dahome/dahome?autofocus=1'
  108. });
  109. },
  110. onShareAppMessage() {
  111. return {
  112. title: '红孩儿智慧教育',
  113. path: '/pages/index/index', // 分享落地页
  114. // imageUrl: // 可以自定义封面图
  115. }
  116. },
  117. // 分享到朋友圈
  118. onShareTimeline() {
  119. return {
  120. title: '红孩儿智慧教育',
  121. query: 'from=timeline'
  122. }
  123. },
  124. // 上传轮播图 存储轮播图图片地址 和展示位置 后续需要存一个上传的图片名字
  125. // async getData() {
  126. // try {
  127. // // 上传文件到云存储
  128. // const uploadRes = await wx.cloud.uploadFile({
  129. // cloudPath: "imgs/banner/carousel_3.jpg", // 上传至云端的路径
  130. // filePath: "image/imgs/carousel_3.jpg" // 小程序临时文件路径
  131. // });
  132. // // 获取上传文件的 fileID
  133. // const fileID = uploadRes.fileID;
  134. // console.log("文件 ID:", fileID);
  135. // // 将文件路径存入 banner 表
  136. // const { data } = await models.banner.create({
  137. // data: {
  138. // remark: "", // 备注
  139. // position: 4, // 展示位置
  140. // describe: "", // 描述
  141. // url: fileID, // 路径
  142. // },
  143. // envType: "prod", // 正式环境
  144. // });
  145. // // 返回创建的数据 id
  146. // console.log("创建的数据:", data);
  147. // } catch (error) {
  148. // console.error("操作失败:", error);
  149. // wx.showToast({
  150. // title: '操作失败,请稍后再试',
  151. // icon: 'none',
  152. // });
  153. // }
  154. // }
  155. // async
  156. // async getData() {
  157. // wx.cloud
  158. // .uploadFile({
  159. // cloudPath: "imgs/banner/carousel_1.jpg", // 上传至云端的路径
  160. // filePath: "image/imgs/carousel_1.jpg" // 小程序临时文件路径,需结合小程序相关 API 获取
  161. // })
  162. // .then((res) => {
  163. // // 返回文件 ID
  164. // const fileID = res.fileID;
  165. // console.log("文件 ID:", fileID);
  166. // // 下载文件
  167. // wx.cloud.downloadFile({
  168. // fileID: fileID,
  169. // })
  170. // .then((res) => {
  171. // const tempFilePath = res.tempFilePath;
  172. // console.log("临时文件路径:", tempFilePath);
  173. // // 获取文件大小
  174. // wx.getFileInfo({
  175. // filePath: tempFilePath,
  176. // })
  177. // .then((info) => {
  178. // console.log("文件大小:", info.size, "字节");
  179. // const fileSizeInBytes = info.size; // 文件大小(字节)
  180. // const fileSizeInMB = fileSizeInBytes / (1024 * 1024); // 转换为 MB
  181. // console.log("文件大小:", fileSizeInMB.toFixed(2), "MB");
  182. // })
  183. // .catch((err) => {
  184. // console.error("获取文件大小失败:", err);
  185. // });
  186. // })
  187. // .catch((err) => {
  188. // console.error("文件下载失败:", err);
  189. // });
  190. // });
  191. // const { data } = await models.login.list({
  192. // filter: {
  193. // where: {}
  194. // },
  195. // pageSize: 10, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
  196. // pageNumber: 1, // 第几页
  197. // getCount: true, // 开启用来获取总数
  198. // // envType: pre 体验环境, prod 正式环境
  199. // envType: "prod",
  200. // });
  201. // // 返回查询到的数据列表 records 和 总数 total
  202. // console.log(data);
  203. // const { data } = await models.file_manage.get({
  204. // filter: {
  205. // where: {
  206. // $and: [
  207. // {
  208. // _id: {
  209. // $eq: 'BT7HKJ5Z2Y', // 推荐传入_id数据标识进行操作
  210. // },
  211. // },
  212. // ]
  213. // }
  214. // },
  215. // // envType: pre 体验环境, prod 正式环境
  216. // envType: "prod",
  217. // });
  218. // // 返回创建的数据 id
  219. // console.log(data);
  220. // { id: "7d8ff72c665eb6c30243b6313aa8539e"}
  221. // db.collection("homelist").get().then(res => {
  222. // console.log(res);
  223. // }).catch(err => {
  224. // console.log(err);
  225. // })
  226. // },
  227. // 退款
  228. // async tuikuan() {
  229. // wx.cloud.callFunction({
  230. // // 云函数名称
  231. // name: 'wxpayFunctions',
  232. // data: {
  233. // // 调用云函数中的申请退款方法
  234. // type: 'wxpay_refund',
  235. // // 业务其他参数...
  236. // // 推荐用 transaction_id,如果没有就用 out_trade_no
  237. // // transaction_id: this.data.transactionId,
  238. // // out_trade_no: this.data.outTradeNo,
  239. // transaction_id: '4200002828202509037486438814', // 微信支付订单号
  240. // out_refund_no: 'refund_' + Date.now(),
  241. // amount: {
  242. // refund: 5, // 退款金额
  243. // total: 5, // 原来订单金额
  244. // currency: 'CNY', //退款币种
  245. // // from: {
  246. // // account: '', // 出资账户类型
  247. // // amount: '', // 出资金额
  248. // // }
  249. // }
  250. // },
  251. // success: (res) => {
  252. // console.log('申请退款结果: ', res);
  253. // },
  254. // });
  255. // },
  256. })