App({ onLaunch() { // 初始化音效上下文 this.globalData.audioContexts = { flip: wx.createInnerAudioContext(), // 翻页声 success: wx.createInnerAudioContext(), // 收藏成功 inkDrop: wx.createInnerAudioContext(), // 水滴声 swipe: wx.createInnerAudioContext() // 滑动切换 }; // 预设音效资源(本地文件) // 翻页声 - 纸张摩擦 this.globalData.audioContexts.flip.src = '/assets/audio/flip.mp3'; // 收藏成功 - 清脆铃声 this.globalData.audioContexts.success.src = '/assets/audio/success.mp3'; // 水滴声 - 水墨滴落 this.globalData.audioContexts.inkDrop.src = '/assets/audio/inkDrop.mp3'; // 滑动声 this.globalData.audioContexts.swipe.src = '/assets/audio/swipe.mp3'; // 微信登录获取 openid this.wxLogin(); }, // 微信登录 wxLogin() { wx.login({ success: (res) => { if (res.code) { // 发送 code 到后端换取 openid wx.request({ url: `${this.globalData.apiBaseUrl}/api/auth/login`, method: 'POST', data: { code: res.code }, success: (response) => { if (response.data && response.data.success) { this.globalData.openid = response.data.openid; console.log('登录成功,openid:', response.data.openid); } else { console.error('登录失败:', response.data); } }, fail: (err) => { console.error('登录请求失败:', err); } }); } else { console.error('登录失败:', res.errMsg); } } }); }, // 获取 openid(如果还未获取到则返回 null) getOpenid() { return this.globalData.openid; }, playAudio(type) { const ctx = this.globalData.audioContexts[type]; if (ctx) { ctx.stop(); try { ctx.play(); } catch (err) { console.log('音效播放失败:', err); } } }, globalData: { audioContexts: {}, openid: null, // API 基础地址 - 修改这里即可切换环境 apiBaseUrl: 'http://localhost:8080' // apiBaseUrl: 'https://feast.yidaima.cn/jsu' // 生产环境:'https://api.yourdomain.com' } });