Files
JianSu-Naming/miniprogram/app.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2026-04-16 11:25:29 +08:00
App({
onLaunch() {
// 初始化音效上下文
this.globalData.audioContexts = {
flip: wx.createInnerAudioContext(), // 翻页声
success: wx.createInnerAudioContext(), // 收藏成功
inkDrop: wx.createInnerAudioContext(), // 水滴声
swipe: wx.createInnerAudioContext() // 滑动切换
2026-04-16 11:25:29 +08:00
};
// 预设音效资源(本地文件)
// 翻页声 - 纸张摩擦
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;
2026-04-16 11:25:29 +08:00
},
playAudio(type) {
const ctx = this.globalData.audioContexts[type];
if (ctx) {
ctx.stop();
try {
ctx.play();
} catch (err) {
console.log('音效播放失败:', err);
}
2026-04-16 11:25:29 +08:00
}
},
globalData: {
audioContexts: {},
openid: null,
// API 基础地址 - 修改这里即可切换环境
apiBaseUrl: 'http://localhost:8080'
// apiBaseUrl: 'https://feast.yidaima.cn/jsu'
// 生产环境:'https://api.yourdomain.com'
2026-04-16 11:25:29 +08:00
}
});