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' }) }) },