绿满眶商城微信小程序-uniapp
peng
2025-10-13 3a2524692711cf35fc142881b2837368ae2c3d25
pages/product/m-buy/goods.vue
@@ -604,6 +604,10 @@
         this.formValues.templateId = this.consumizetemplateInfo.id;
         this.formValues.templateName = this.consumizetemplateInfo.name;
         
         // 查找对应的模板字段以获取contentType
         const templateItem = this.consumizetemplateInfo.templateConstomizeTitles.find(item => item.id === fieldId);
         const contentType = templateItem ? templateItem.contentType : '';
         // 查找是否已存在该字段
         const existingIndex = this.formValues.templateForm.findIndex(item => item.id === fieldId);
         
@@ -612,6 +616,7 @@
            this.$set(this.formValues.templateForm, existingIndex, {
               id: fieldId,
               templateTitle: templateTitle,
               contentType: contentType, // 添加contentType
               value: value
            });
         } else {
@@ -619,6 +624,7 @@
            this.formValues.templateForm.push({
               id: fieldId,
               templateTitle: templateTitle,
               contentType: contentType, // 添加contentType
               value: value
            });
         }
@@ -736,6 +742,14 @@
       * 添加到购物车或购买
       */
      addToCartOrBuy(val) {
         // 检查商品是否需要模板并且模板数据是否完整
         if (this.consumizetemplateInfo && this.consumizetemplateInfo.id) {
            const isValid = this.validateTemplateData();
            if (!isValid) {
               return;
            }
         }
         console.log(JSON.stringify(this.formValues))
         if (!this.selectSkuList) {
            // 使用setTimeout延迟显示提示,避免影响弹窗
@@ -795,6 +809,56 @@
            });
         }
      },
      /**
       * 验证模板数据是否完整
       */
      validateTemplateData() {
         // 检查模板图片是否已选择(如果有模板图片)
         if (this.consumizetemplateInfo.templateImgs &&
            this.consumizetemplateInfo.templateImgs.length > 0 &&
            this.selectedImageObjects.length === 0) {
            setTimeout(() => {
               uni.showToast({
                  title: '请选择模板图片',
                  icon: 'none'
               });
            }, 100);
            return false;
         }
         // 检查动态表单项是否已填写
         if (this.consumizetemplateInfo.templateConstomizeTitles) {
            for (const item of this.consumizetemplateInfo.templateConstomizeTitles) {
               const formItem = this.formValues.templateForm.find(formItem => formItem.id === item.id);
               // 如果表单项不存在或者值为空
               if (!formItem || !formItem.value || formItem.value.trim() === '') {
                  setTimeout(() => {
                     uni.showToast({
                        title: `请填写${item.templateTitle}`,
                        icon: 'none'
                     });
                  }, 100);
                  return false;
               }
               // 特别检查图片类型的表单项是否有上传
               if (item.contentType === 'IMAGE' &&
                  (!this.imagePreviewUrls[item.id] || this.imagePreviewUrls[item.id].trim() === '')) {
                  setTimeout(() => {
                     uni.showToast({
                        title: `请上传${item.templateTitle}`,
                        icon: 'none'
                     });
                  }, 100);
                  return false;
               }
            }
         }
         return true;
      },
      formatSku(list) {
         // 格式化数据
         let arr = [{}];