修正了 pages 名为拼音的问题
This commit is contained in:
248
pages/invite-fighter/invite-fighter.js
Normal file
248
pages/invite-fighter/invite-fighter.js
Normal file
@@ -0,0 +1,248 @@
|
||||
// pages/yaoqingma/yaoqingma.js
|
||||
// 阿龙电竞 - 邀请码页面 (优化重构版)
|
||||
import request from '../../utils/request.js'
|
||||
|
||||
Page({
|
||||
/**
|
||||
* 页面的初始数据 (字段保持与你渲染层一致)
|
||||
*/
|
||||
data: {
|
||||
yaoqingma: '', // 邀请码
|
||||
isLoading: false, // 是否正在加载
|
||||
copySuccess: false, // 是否复制成功
|
||||
copyTimer: null // 复制成功提示计时器
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad: function (options) {
|
||||
|
||||
// 仅初始化,不自动请求
|
||||
this.chushihuaYaoqingma();
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面显示
|
||||
*/
|
||||
onShow: function () {
|
||||
// 每次显示页面时重置复制状态
|
||||
if (this.data.copySuccess) {
|
||||
this.chongzhiCopyZhuangtai();
|
||||
}
|
||||
},
|
||||
onShow() {
|
||||
// 原有代码...
|
||||
|
||||
// 🆕 注册通知组件
|
||||
this.registerNotificationComponent();
|
||||
this.registerNotificationComponent();
|
||||
},
|
||||
|
||||
// 🆕 新增:注册通知组件
|
||||
registerNotificationComponent() {
|
||||
const app = getApp();
|
||||
const notificationComp = this.selectComponent('#global-notification');
|
||||
|
||||
if (notificationComp && notificationComp.showNotification) {
|
||||
console.log('🏪 商城页面注册通知组件');
|
||||
app.globalData.globalNotification = {
|
||||
show: (data) => notificationComp.showNotification(data),
|
||||
hide: () => notificationComp.hideNotification()
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面卸载
|
||||
*/
|
||||
onUnload: function () {
|
||||
// 清理计时器
|
||||
if (this.data.copyTimer) {
|
||||
clearTimeout(this.data.copyTimer);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 初始化邀请码数据
|
||||
*/
|
||||
chushihuaYaoqingma: function () {
|
||||
|
||||
// 只从缓存读取并显示,不主动请求
|
||||
try {
|
||||
const cachedYaoqingma = wx.getStorageSync('yaoqingma');
|
||||
if (cachedYaoqingma && cachedYaoqingma.trim() !== '') {
|
||||
|
||||
this.setData({ yaoqingma: cachedYaoqingma });
|
||||
} else {
|
||||
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理邀请码相关操作 (主入口函数)
|
||||
* 如果没有邀请码则生成,如果有则复制
|
||||
*/
|
||||
chuliYaoqingma: async function () {
|
||||
// 防止重复点击
|
||||
if (this.data.isLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已经有邀请码,执行复制操作
|
||||
if (this.data.yaoqingma && this.data.yaoqingma.trim() !== '') {
|
||||
this.fuzhiYaoqingma();
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果没有邀请码,向后端请求生成
|
||||
await this.shengchengYaoqingma();
|
||||
},
|
||||
|
||||
/**
|
||||
* 向后端请求生成邀请码
|
||||
*/
|
||||
shengchengYaoqingma: async function () {
|
||||
|
||||
this.setData({ isLoading: true });
|
||||
|
||||
try {
|
||||
// 使用封装的request工具发送POST请求(与你的调用方式一致)
|
||||
const response = await request({
|
||||
url: '/yonghu/yaoqingma',
|
||||
method: 'POST'
|
||||
// 如果需要参数可以在此添加 data: {}
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 处理业务响应
|
||||
if (response.data.code === 200) {
|
||||
const newYaoqingma = response.data.data.yaoqingma;
|
||||
this.chuliShengchengChenggong(newYaoqingma);
|
||||
} else {
|
||||
this.chuliShiwu('生成失败', response.message);
|
||||
}
|
||||
} catch (error) {
|
||||
// 处理网络或系统错误
|
||||
|
||||
this.chuliShiwu('网络错误', error.message || '请检查网络后重试');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理生成成功的逻辑
|
||||
*/
|
||||
chuliShengchengChenggong: function (newYaoqingma) {
|
||||
|
||||
|
||||
// 更新页面数据
|
||||
this.setData({
|
||||
yaoqingma: newYaoqingma,
|
||||
isLoading: false
|
||||
});
|
||||
|
||||
// 保存到缓存
|
||||
try {
|
||||
wx.setStorageSync('yaoqingma', newYaoqingma);
|
||||
|
||||
} catch (error) {
|
||||
|
||||
}
|
||||
|
||||
// 显示成功提示
|
||||
wx.showToast({
|
||||
title: '生成成功!',
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理错误情况
|
||||
*/
|
||||
chuliShiwu: function (title, message) {
|
||||
this.setData({ isLoading: false });
|
||||
|
||||
wx.showToast({
|
||||
title: message || title,
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 复制邀请码到剪贴板
|
||||
*/
|
||||
fuzhiYaoqingma: function () {
|
||||
const yaoqingma = this.data.yaoqingma;
|
||||
|
||||
if (!yaoqingma || yaoqingma.trim() === '') {
|
||||
wx.showToast({
|
||||
title: '暂无邀请码可复制',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
wx.setClipboardData({
|
||||
data: yaoqingma,
|
||||
success: () => {
|
||||
|
||||
this.setData({ copySuccess: true });
|
||||
wx.showToast({
|
||||
title: '已复制',
|
||||
icon: 'success',
|
||||
duration: 1500
|
||||
});
|
||||
// 2秒后重置状态
|
||||
const timer = setTimeout(() => {
|
||||
this.chongzhiCopyZhuangtai();
|
||||
}, 2000);
|
||||
this.setData({ copyTimer: timer });
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('复制失败:', error);
|
||||
wx.showToast({
|
||||
title: '复制失败',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 重置复制状态
|
||||
*/
|
||||
chongzhiCopyZhuangtai: function () {
|
||||
if (this.data.copyTimer) {
|
||||
clearTimeout(this.data.copyTimer);
|
||||
}
|
||||
this.setData({
|
||||
copySuccess: false,
|
||||
copyTimer: null
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 用户点击右上角分享
|
||||
*/
|
||||
onShareAppMessage: function () {
|
||||
if (this.data.yaoqingma) {
|
||||
return {
|
||||
title: '我的阿龙电竞邀请码',
|
||||
path: `/pages/yaoqingma/yaoqingma?yaoqingma=${this.data.yaoqingma}`
|
||||
};
|
||||
}
|
||||
return {
|
||||
title: '阿龙电竞陪玩平台',
|
||||
path: '/pages/index/index'
|
||||
};
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user