diff --git a/pages/invite-manager/invite-manager.js b/pages/invite-manager/invite-manager.js index b9ecaaf..0d9bac9 100644 --- a/pages/invite-manager/invite-manager.js +++ b/pages/invite-manager/invite-manager.js @@ -29,12 +29,29 @@ Page({ this.setData({ posterBgUrl: bgUrl }); this.loadFromCache(); this.loadUserInfo(); + this.autoFetchAndShowPoster(); }, onShow() { this.registerNotificationComponent(); }, + autoFetchAndShowPoster() { + if (this.data.qrcodeUrl) { + this.autoGeneratePoster(); + return; + } + this.refreshQRCode({ silent: true, autoPoster: true }); + }, + + autoGeneratePoster() { + if (!this.data.qrcodeUrl || this.data.showPosterCanvas || this._autoPosterRunning) return; + this._autoPosterRunning = true; + this.generateMyPoster({ silent: true }).finally(() => { + this._autoPosterRunning = false; + }); + }, + registerNotificationComponent() { const notificationComp = this.selectComponent('#global-notification'); if (notificationComp && notificationComp.showNotification) { @@ -73,7 +90,13 @@ Page({ this.setData({ avatarUrl, uid, nickName }); }, - refreshQRCode() { + refreshQRCode(opts = {}) { + const silent = !!opts.silent; + const autoPoster = !!opts.autoPoster; + if (this.data.qrcodeUrl && silent) { + if (autoPoster) this.autoGeneratePoster(); + return; + } if (this.data.isLoading) return; this.setData({ isLoading: true }); request({ url: '/peizhi/zuzhanghb', method: 'POST' }) @@ -90,14 +113,15 @@ Page({ wx.setStorageSync(inviteKey, yaoqingma); this.setData({ yaoqingma }); } - wx.showToast({ title: '获取成功', icon: 'success' }); - } else { + if (!silent) wx.showToast({ title: '获取成功', icon: 'success' }); + if (autoPoster && this.data.qrcodeUrl) this.autoGeneratePoster(); + } else if (!silent) { wx.showToast({ title: res.data.msg || res.data.message || '获取失败', icon: 'none' }); } }) .catch((err) => { console.error('获取组长海报失败', err); - wx.showToast({ title: '网络错误', icon: 'none' }); + if (!silent) wx.showToast({ title: '网络错误', icon: 'none' }); }) .finally(() => this.setData({ isLoading: false })); }, diff --git a/pages/poster/poster.js b/pages/poster/poster.js index 0e02d9f..756e5ce 100644 --- a/pages/poster/poster.js +++ b/pages/poster/poster.js @@ -26,12 +26,31 @@ Page({ this.setData({ posterBgUrl: bgUrl }); this.loadQRCodeFromCache(); this.loadUserInfo(); + // 进入页自动获取:有缓存不请求;没有则代替点击「获取二维码」,成功后再自动生成海报 + this.autoFetchAndShowPoster(); }, onShow() { this.registerNotificationComponent(); }, + /** 有二维码则直接生成海报;没有则先拉二维码,成功后再生成 */ + autoFetchAndShowPoster() { + if (this.data.qrcodeUrl) { + this.autoGeneratePoster(); + return; + } + this.refreshQRCode({ silent: true, autoPoster: true }); + }, + + autoGeneratePoster() { + if (!this.data.qrcodeUrl || this.data.showPosterCanvas || this._autoPosterRunning) return; + this._autoPosterRunning = true; + this.generateMyPoster({ silent: true }).finally(() => { + this._autoPosterRunning = false; + }); + }, + registerNotificationComponent() { const notificationComp = this.selectComponent('#global-notification'); if (notificationComp && notificationComp.showNotification) { @@ -69,7 +88,14 @@ Page({ this.setData({ avatarUrl, uid, nickName }); }, - refreshQRCode() { + refreshQRCode(opts = {}) { + const silent = !!opts.silent; + const autoPoster = !!opts.autoPoster; + // 已有二维码:不再重复获取 + if (this.data.qrcodeUrl && silent) { + if (autoPoster) this.autoGeneratePoster(); + return; + } if (this.data.isLoading) return; this.setData({ isLoading: true }); request({ @@ -77,21 +103,23 @@ Page({ method: 'POST', }).then((res) => { if (res.data && res.data.code === 0) { - const relativeUrl = res.data.data.url; + const relativeUrl = res.data.data && res.data.data.url; if (relativeUrl) { const key = this._qrCacheKey || posterQrCacheKey('guanshi'); wx.setStorageSync(key, relativeUrl); this.setData({ qrcodeUrl: getFullImageUrl(relativeUrl, app) }); - wx.showToast({ title: '获取成功', icon: 'success' }); - } else { + if (!silent) wx.showToast({ title: '获取成功', icon: 'success' }); + if (autoPoster) this.autoGeneratePoster(); + } else if (!silent) { wx.showToast({ title: '返回数据异常', icon: 'none' }); } - } else { + } else if (!silent) { wx.showToast({ title: res.data.msg || res.data.message || '获取失败', icon: 'none' }); } + // silent 失败:不展示、不打扰 }).catch((err) => { console.error('获取海报失败', err); - wx.showToast({ title: '网络错误', icon: 'none' }); + if (!silent) wx.showToast({ title: '网络错误', icon: 'none' }); }).finally(() => { this.setData({ isLoading: false }); }); @@ -120,24 +148,25 @@ Page({ }); }, - generateMyPoster() { + generateMyPoster(opts = {}) { + const silent = !!opts.silent; if (!this.data.qrcodeUrl) { - wx.showToast({ title: '请先生成二维码', icon: 'none' }); - return; + if (!silent) wx.showToast({ title: '请先生成二维码', icon: 'none' }); + return Promise.resolve(); } - wx.showLoading({ title: '生成海报中...', mask: true }); + if (!silent) wx.showLoading({ title: '生成海报中...', mask: true }); const bgUrl = this.data.posterBgUrl; const urls = [bgUrl, this.data.qrcodeUrl, this.data.avatarUrl]; - Promise.all(urls.map((url) => this.loadImage(url))) + return Promise.all(urls.map((url) => this.loadImage(url))) .then((images) => { - wx.hideLoading(); + if (!silent) wx.hideLoading(); this.setData({ showPosterCanvas: true }, () => { setTimeout(() => this.drawPoster(images[0], images[1], images[2]), 200); }); }) .catch((err) => { - wx.hideLoading(); - wx.showToast({ title: '图片加载失败', icon: 'none' }); + if (!silent) wx.hideLoading(); + if (!silent) wx.showToast({ title: '图片加载失败', icon: 'none' }); console.error('图片加载失败', err); }); },