123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- // import { models, db, _ } from '../../utils/cloudbase.js'
- import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
- const app = getApp();
- Page({
- data: {
- categoriesindex: 1,
- categories: [],
- goods: [],
- souimg: '',
- gouwucimg: '',
- pageNumber: 1,
- pageSize: 10,
- hasMore: true, // 是否还有更多数据
- isLoading: false, // 防止多次触发
- types_ids: null, // 当前选中的分类 tag_id
- searchText: '', // 搜索内容
- },
- onShow() {
- if (typeof this.getTabBar === 'function' && this.getTabBar()) {
- this.getTabBar().setSelected(1); // 比如首页就是 0
- }
- // 获取tab数据
- this.getTabdata();
- },
- async onLoad(options) {
- const fileIDs = [
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png',
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/gouwuc_img.png'
- ];
- const fileList = await getTempFileURLs(fileIDs)
- this.setData({
- souimg: fileList[0].tempFileURL,
- gouwucimg: fileList[1].tempFileURL
- })
-
- // // 并发下载多个 fileID
- // Promise.all(
- // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
- // ).then(results => {
- // // 每个 result 对应一个下载结果
- // const tempFilePaths = results.map(r => r.tempFilePath);
- // console.log('全部下载成功:', tempFilePaths);
- // this.setData({
- // souimg: tempFilePaths[0],
- // gouwucimg: tempFilePaths[1]
- // });
- // }).catch(err => {
- // console.error('有文件下载失败:', err);
- // });
- },
- // tab数据
- async getTabdata() {
- const models = await getModels()
- const { data } = await models.tab.list({
- filter: {
- where: {
- position: 4, // 显示位置
- },
- },
- envType: "prod",
- });
-
- // 返回查询到的数据
- const sortedRecords = data.records.sort((a, b) => {
- return a.sort - b.sort; // 升序排列
- });
- this.setData({
- categories: sortedRecords,
- goods: [],
- pageNumber: 1,
- hasMore: true,
- }, () => {
- this.getDatalist()
- })
- },
- onReachBottom() {
- // 上拉触底事件的处理函数
- this.loadMore();
- },
- loadMore() {
- // 加载更多数据的逻辑
- console.log('加载更多');
- // this.getDatalist(this.data.types_ids, true);
- if (this.data.isLoading || !this.data.hasMore) return;
- this.setData({ isLoading: true });
- this.getDatalist(this.data.types_ids, true);
- },
- //模糊搜搜
- onSearchInput(e) {
- const searchText = e.detail.value.trim();
- this.setData({
- searchText,
- goods: [],
- pageNumber: 1,
- hasMore: true
- }, () => {
- this.getDatalist(this.data.types_ids);
- });
- },
- // 点击搜搜
- onSearchConfirm() {
- this.setData({
- goods: [],
- pageNumber: 1,
- hasMore: true
- }, () => {
- this.getDatalist(this.data.types_ids);
- });
- },
- // 列表数据
- async getDatalist(tag_id = null, isLoadMore = false) {
- const models = await getModels()
- const _ = await getCommand()
- const userInfo = wx.getStorageSync('userInfo');
- const { pageNumber, pageSize, searchText } = this.data;
-
- const where = {
- school_id: _.in(userInfo.school_id)
- };
-
- if (tag_id) {
- where.tag_id = tag_id;
- }
-
- if (searchText) {
- where.name = {
- $regex_ci: searchText
- };
- }
-
- const { data } = await models.wx_merchandise.list({
- filter: { where },
- pageSize,
- pageNumber,
- getCount: true,
- envType: "prod",
- });
-
- const collectList = data.records || [];
-
- // 循环查 groupbuy 表
- for (let item of collectList) {
- try {
- const res = await models.wx_groupbuy_groupbuy.list({
- filter: { where: { groupbuy_id: item._id } },
- envType: "prod",
- });
- item.specList = res.data.records || [];
- } catch (err) {
- console.error(`获取 groupbuy 失败,商品ID: ${item._id}`, err);
- item.specList = [];
- }
- }
- // 批量获取商品图片临时 URL
- const fileIDs = collectList.map(item => item.img).filter(f => f)
- let tempFiles = []
- if (fileIDs.length > 0) {
- tempFiles = await getTempFileURLs(fileIDs)
- }
- // 将临时 URL 替换原 img
- const goodsList = collectList.map((item, index) => ({
- ...item,
- img: tempFiles[index]?.tempFileURL || item.img
- }))
- this.setData({
- goods: isLoadMore ? this.data.goods.concat(goodsList) : goodsList,
- pageNumber: pageNumber + 1,
- hasMore: goodsList.length === pageSize,
- isLoading: false
- });
- },
- // 点击tab分类 async
- tabcategories(e) {
- const types = e.currentTarget.dataset.type;
- const tagId = types.sort === 1 ? null : types._id;
-
- this.setData({
- categoriesindex: types.sort,
- types_ids: tagId,
- goods: [],
- pageNumber: 1,
- hasMore: true
- }, () => {
- this.getDatalist(tagId, false); // ✅ 这里也改成直接传 tagId
- });
- },
- navigateToDetail(e) {
- const index = e.currentTarget.dataset.index;
- const item = this.data.goods[index];
- wx.navigateTo({
- url: '/subpackages/productdetails/productdetails?data=' + encodeURIComponent(JSON.stringify(item))
- });
- }
- });
|