收藏
回答

模拟器里有内容,但是预览扫码后手机里为什么没有内容?

// pages/index/index.js
Page({
  data: {
    dinnerOptions: [],
    result: "点击下方按钮开始抽取",
    suggestion: "",
    hasDrawn: false,
    isDrawing: false
  },


  // 生命周期函数--监听页面加载
  onLoad: function() {
    this.initDinnerOptions();
  },
  
  // 生命周期函数--监听页面显示
  onShow: function() {
    // 确保每次页面显示时都有数据
    if (!this.data.dinnerOptions || this.data.dinnerOptions.length === 0) {
      this.initDinnerOptions();
    }
  },
  
  // 初始化晚餐选项
  initDinnerOptions: function() {
    console.log('初始化晚餐选项...');
    
    // 使用异步方式获取存储数据
    wx.getStorage({
      key: 'dinnerOptions',
      success: (res) => {
        console.log('从存储获取的选项:', res.data);
        if (res.data && Array.isArray(res.data) && res.data.length > 0) {
          console.log('使用已保存的选项');
          this.setData({
            dinnerOptions: res.data
          });
        } else {
          this.useDefaultOptions();
        }
      },
      fail: (err) => {
        console.log('获取存储失败:', err);
        this.useDefaultOptions();
      }
    });
  },
  
  // 使用默认选项
  useDefaultOptions: function() {
    const defaultOptions = ["烧烤""火锅""涮串""烤肉"];
    console.log('使用默认选项:', defaultOptions);
    
    this.setData({
      dinnerOptions: defaultOptions
    });
    
    // 异步保存默认选项到本地存储
    wx.setStorage({
      key: 'dinnerOptions',
      data: defaultOptions,
      success: () => {
        console.log('默认选项保存成功');
      },
      fail: (err) => {
        console.error('保存默认选项失败:', err);
      }
    });
  },


  // 保存选项到本地存储
  saveOptions: function(options) {
    wx.setStorage({
      key: 'dinnerOptions',
      data: options,
      success: () => {
        console.log('选项保存成功');
      },
      fail: (err) => {
        console.error('保存选项失败:', err);
        // 显示错误提示
        wx.showToast({
          title: '保存失败,请重试',
          icon: 'none'
        });
      }
    });
  },


  // 添加新选项
  addOption() {
    if (this.data.hasDrawn) {
      wx.showToast({
        title: '已抽奖,无法修改选项',
        icon: 'none'
      });
      return;
    }
    
    wx.showModal({
      title: '添加晚餐选项',
      editable: true,
      placeholderText: '请输入新的晚餐选项',
      success: (res) => {
        if (res.confirm) {
          const newOption = res.content.trim();
          if (!newOption) {
            wx.showToast({
              title: '选项不能为空',
              icon: 'none'
            });
            return;
          }
          
          const options = this.data.dinnerOptions || [];
          if (options.includes(newOption)) {
            wx.showToast({
              title: '该选项已存在',
              icon: 'none'
            });
            return;
          }
          
          const newOptions = options.concat(newOption);
          this.setData({
            dinnerOptions: newOptions
          });
          
          // 保存到本地存储
          this.saveOptions(newOptions);
          
          wx.showToast({
            title: '选项添加成功',
            icon: 'success',
            duration: 1500
          });
        }
      }
    });
  },


  // 删除选项
  deleteOption(e) {
    if (this.data.hasDrawn) {
      wx.showToast({
        title: '已抽奖,无法修改选项',
        icon: 'none'
      });
      return;
    }


    const index = e.currentTarget.dataset.index;
    const options = this.data.dinnerOptions;
    
    if (options.length <= 2) {
      wx.showToast({
        title: '至少保留两个选项',
        icon: 'none'
      });
      return;
    }


    wx.showModal({
      title: '确认删除',
      content: `确定要删除"${options[index]}"吗?`,
      success: (res) => {
        if (res.confirm) {
          options.splice(index, 1);
          this.setData({
            dinnerOptions: options
          });
          
          // 保存到本地存储
          this.saveOptions(options);
          
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        }
      }
    });
  },


  // 随机抽取晚餐
  drawDinner() {
    if (this.data.isDrawing) return;
    
    const options = this.data.dinnerOptions;
    if (options.length < 2) {
      wx.showToast({
        title: '请至少添加两个选项',
        icon: 'none'
      });
      return;
    }


    this.setData({
      isDrawing: true,
      hasDrawn: false,
      result: "正在抽取..."
    });


    let count = 0;
    const maxCount = 20// 转动次数
    const interval = 50// 初始间隔时间(毫秒)


    const roll = () => {
      const randomIndex = Math.floor(Math.random() * options.length);
      const currentOption = options[randomIndex];
      
      this.setData({
        result: currentOption
      });


      if (count < maxCount) {
        count++;
        // 逐渐增加间隔时间,使动画变慢
        const nextInterval = interval + (count * 10);
        setTimeout(roll, nextInterval);
      } else {
        this.setData({
          isDrawing: false,
          hasDrawn: true,
          suggestion: `今晚就吃${currentOption}吧!`
        });
      }
    };


    roll();
  },


  // 重置抽取
  resetDraw() {
    this.setData({
      hasDrawn: false,
      result: "点击下方按钮开始抽取",
      suggestion: ""
    });
    
    // 确保本地存储中的选项是最新的
    this.saveOptions(this.data.dinnerOptions);
  },


  // 抽取按钮按下效果
  handleDrawBtnTouchStart() {
    this.setData({
      ['drawBtnActive']: true
    });
  },


  // 抽取按钮释放效果
  handleDrawBtnTouchEnd() {
    this.setData({
      ['drawBtnActive']: false
    });
  },
  
  // 重置按钮按下效果
  handleResetBtnTouchStart() {
    this.setData({
      ['resetBtnActive']: true
    });
  },


  // 重置按钮释放效果
  handleResetBtnTouchEnd() {
    this.setData({
      ['resetBtnActive']: false
    });
  }
});

这种问题是怎么回事呀?

回答关注问题邀请回答
收藏

1 个回答

  • 社区技术运营专员-Jahozheng
    社区技术运营专员-Jahozheng
    06-14

    你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)

    06-14
    有用
    回复
登录 后发表内容