123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- // import { models, db, _ } from '../../utils/cloudbase.js'
- import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
- Page({
- data: {
- item: {},
- quantity: 1,
- showimg: '',
- gouwucimg: '',
- gaoliao: 0,
- steppernum: 1,
- outTradeNo: '',
- transactionId: ''
- },
- async onLoad(options) {
- const itemData = decodeURIComponent(options.data);
- const item = JSON.parse(itemData);
- console.log(item, 'itemitemitemitemitem');
- // 处理 detail_images 云路径 => 临时 URL
- if (item.specList && item.specList.length > 0) {
- for (let i = 0; i < item.specList.length; i++) {
- const images = item.specList[i].detail_images || [];
- if (images.length > 0) {
- const tempFiles = await getTempFileURLs(images);
- // 替换原数组为 tempFileURL
- item.specList[i].detail_images = tempFiles.map(f => f.tempFileURL);
- }
- }
- }
- this.setData({
- item: item
- }, () => {
- this.getbrowse()
- });
- const fileIDs = [
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/show.png',
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/gouwuc_img.png'
- ];
- const fileList = await getTempFileURLs(fileIDs)
- this.setData({
- showimg: 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({
- // showimg: tempFilePaths[0],
- // gouwucimg: tempFilePaths[1]
- // });
- // }).catch(err => {
- // console.error('有文件下载失败:', err);
- // });
- },
- // 新增浏览量
- async getbrowse() {
- const models = await getModels()
- const { data } = await models.wx_merchandise.update({
- data: {
- browse: this.data.item.browse + 1, // 浏览数
- },
- filter: {
- where: {
- $and: [
- {
- _id: {
- $eq: this.data.item._id, // 推荐传入_id数据标识进行操作
- },
- },
- ]
- }
- },
- // envType: pre 体验环境, prod 正式环境
- envType: "prod",
- });
-
- // 返回更新成功的条数
- console.log(data, 'datadata');
- this.setData({
- 'item.browse': this.data.item.browse + 1
- })
- },
- previewImage(e) {
- const index = e.currentTarget.dataset.index;
- const images = this.data.item.detail_images;
- wx.previewImage({
- current: images[index], // 当前预览的图片
- urls: images // 所有可预览的图片列表
- });
- },
- // 跳转购物车
- gotogwc() {
- wx.switchTab({
- url: `/pages/shoppingcart/shoppingcart`
- });
- },
- // 点击切换规格
- selectSpec(e) {
- const specIndex = e.currentTarget.dataset.specindex;
- this.setData({
- gaoliao: specIndex
- })
- },
- // 购买数量
- onChange(e) {
- const num = e.detail
- this.setData({
- steppernum:num
- })
- },
-
- // 加入购物车
- async addToCart() {
- // 加入购物车逻辑
- console.log('加入购物车', this.data.item, this.data.item._id);
- const userInfo = wx.getStorageSync('userInfo');
- const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID
- const models = await getModels()
- const { data } = await models.shopping_cart.get({
- filter: {
- where: {
- user_id: { $eq: userId },
- merchandise_id: { $eq: this.data.item._id }, // 推荐传入_id数据标识进行操作
- specs_index: { $eq: this.data.gaoliao },
- }
- },
- envType: "prod",
- });
-
- // 返回查询到的数据
- console.log(data);
- const datalist = data || {}
- if (!datalist || Object.keys(datalist).length === 0) {
- console.log('购物车中没有该商品,准备新增');
- // 执行插入操作
- const { data } = await models.shopping_cart.create({
- data: {
- user_id: userId,
- merchandise_id: this.data.item._id,
- num: this.data.steppernum, // 商品数量
- specs_index: this.data.gaoliao, // 规格下标
- },
- envType: "prod",
- });
-
- // 判断是否有 id 返回
- if (data && (data.id || data.Id)) {
- console.log('加入购物车成功:', data);
- wx.showToast({
- title: '加入购物车成功',
- icon: 'success',
- duration: 1500
- });
- } else {
- // 数据结构异常,视为失败
- console.error('加入购物车失败:无返回 id', data);
- wx.showToast({
- title: '加入失败',
- icon: 'error',
- duration: 1500
- });
- }
- } else {
- console.log('购物车已有该商品,准备更新数量');
- // 执行更新操作
- const { data } = await models.shopping_cart.update({
- data: {
- num: datalist.num + 1, // 商品数量
- },
- filter: {
- where: {
- $and: [
- {
- _id: {
- $eq: datalist._id, // 推荐传入_id数据标识进行操作
- },
- },
- ]
- }
- },
- envType: "prod",
- });
-
- // 返回更新成功的条数
- console.log(data);
- // 判断是否有 id 返回
- if (data.count > 0) {
- console.log('加入购物车成功:', data);
- wx.showToast({
- title: '加入购物车成功',
- icon: 'success',
- duration: 1500
- });
- } else {
- // 数据结构异常,视为失败
- console.error('加入购物车失败:无返回 id', data);
- wx.showToast({
- title: '加入失败',
- icon: 'error',
- duration: 1500
- });
- }
- }
- },
- async buyNow() {
- // 将数据存入本地缓存
-
- const selectedItems = [
- {
- ...this.data.item, // 复制原始 item 对象
- specs_index: this.data.gaoliao, // 新增字段
- num: this.data.steppernum
- }
- ];
- console.log(selectedItems,'selectedItems');
- wx.setStorageSync('checkoutItems', selectedItems);
- wx.navigateTo({
- url: '/subpackages/submitorder/submitorder'
- });
- },
- });
|