123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import { models, db, _ } from '../../utils/cloudbase.js'
- Page({
- data: {
- item: {},
- quantity: 1,
- showimg: '',
- gouwucimg: '',
- gaoliao: 0,
- steppernum: 1,
- },
- onLoad(options) {
- const itemData = decodeURIComponent(options.data);
- const item = JSON.parse(itemData);
- this.setData({
- item: item
- });
- const fileIDs = [
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show.png',
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/gouwuc_img.png'
- ];
-
- // 并发下载多个 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);
- });
- },
- // 跳转购物车
- gotogwc() {
- wx.switchTab({
- url: `/pages/shoppingcart/shoppingcart`
- });
- },
- // 规格选择
- setspecs(e) {
- const index = e.currentTarget.dataset.item;
- this.setData({
- gaoliao: index
- })
- },
- // 购买数量
- onChange(e) {
- const num = e.detail
- this.setData({
- steppernum:num
- })
- },
-
- // 加入购物车
- async addToCart() {
- // 加入购物车逻辑
- console.log('加入购物车', this.data.item, this.data.item._id);
- const { data } = await models.shopping_cart.get({
- filter: {
- where: {
- 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: {
- 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
- });
- }
- }
- },
-
- buyNow() {
- // 立即购买逻辑
- console.log('立即购买');
- }
- });
|