From 8e508d6934f0fd47a9fd0c417e79025ce86bdc3a Mon Sep 17 00:00:00 2001 From: lik Date: Wed, 11 Nov 2026 15:50:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(apply):=20=E5=BE=85=E5=AE=A1=E6=A0=B8?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E8=BD=AE=E8=AF=A2=E5=AE=A1=E6=A0=B8=E7=8A=B6?= =?UTF-8?q?=E6=80=81=EF=BC=8C=E9=80=9A=E8=BF=87=E5=90=8E=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=BF=9B=E5=85=A5=E9=A6=96=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/apply/index.js | 63 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/pages/apply/index.js b/pages/apply/index.js index b2aaf38..822a58e 100644 --- a/pages/apply/index.js +++ b/pages/apply/index.js @@ -13,6 +13,8 @@ const SEX_OPTIONS = [ ] const SEX_LABELS = SEX_OPTIONS.map((item) => item.label) const MOBILE_REGEX = /^1[3-9]\d{9}$/ +// 待审核期间的轮询间隔:后台审核通过后本页自动刷新角色并跳转首页 +const PENDING_POLL_INTERVAL = 10 * 1000 Page({ data: { @@ -41,6 +43,55 @@ Page({ onShow() { this.refresh() + this.startPolling() + }, + + onHide() { + this.stopPolling() + }, + + onUnload() { + this.stopPolling() + }, + + startPolling() { + this.stopPolling() + this._pollTimer = setInterval(() => this.pollApproval(), PENDING_POLL_INTERVAL) + }, + + stopPolling() { + if (this._pollTimer) { + clearInterval(this._pollTimer) + this._pollTimer = null + } + }, + + /** + * 待审核轮询:后台审核通过后员工档案生效,角色解析为 attendant, + * 这里直接跳首页,无需用户手动重进 + */ + pollApproval() { + // 只在待审核/已通过待同步时轮询;加载、提交、修改资料期间跳过 + if (this.data.loading || this.data.submitting || this.data.editing) return + if (this.data.view !== 'pending' && this.data.view !== 'approved') return + if (this._polling) return + this._polling = true + + role + .ensureRoleSafe(true) + .then((detail) => { + if (detail.ok && detail.role !== 'none') { + this.stopPolling() + wx.switchTab({ url: '/pages/home/index' }) + return + } + // 角色未变(或解析失败):静默刷新申请单,驳回等状态变化也能及时反映 + return this.loadApplication(true) + }) + .catch(() => {}) + .then(() => { + this._polling = false + }) }, /** @@ -55,6 +106,7 @@ Page({ .ensureRoleSafe(true) .then((detail) => { if (detail.ok && detail.role !== 'none') { + this.stopPolling() wx.switchTab({ url: '/pages/home/index' }) return } @@ -63,7 +115,10 @@ Page({ .catch(() => this.loadApplication()) }, - loadApplication() { + /** + * @param {boolean} [silent] 轮询静默刷新:不弹错误提示,不切换 loading + */ + loadApplication(silent) { const app = getApp() const user = app && app.globalData ? app.globalData.user : null const accountName = user && user.profile && user.profile.name ? user.profile.name : '微信用户' @@ -74,7 +129,9 @@ Page({ .then((res) => { if (res.code !== 0) { // 未登录(新用户走到这里时还没有账号)不弹错误,直接展示登录入口 - if (res.code !== 401) { + if (silent) { + console.info('[apply] 轮询刷新未拿到申请单:', res.code, res.msg) + } else if (res.code !== 401) { wx.showToast({ title: res.msg || '加载失败', icon: 'none' }) } else { console.info('[apply] 未登录,展示登录入口') @@ -138,7 +195,7 @@ Page({ }) .catch(() => { this.setData({ loading: false, accountName, loggedIn }) - wx.showToast({ title: '网络错误,请重试', icon: 'none' }) + if (!silent) wx.showToast({ title: '网络错误,请重试', icon: 'none' }) }) },