diff --git a/miniprogram/pages/plan-detail/plan-detail.js b/miniprogram/pages/plan-detail/plan-detail.js index fb4fab1..49fcbec 100644 --- a/miniprogram/pages/plan-detail/plan-detail.js +++ b/miniprogram/pages/plan-detail/plan-detail.js @@ -8,18 +8,30 @@ Page({ id: '', plan: null, exercises: [], - loading: true + loading: true, + error: false, + errorMsg: '' }, onLoad(query) { const id = query.id || ''; - this.setData({ statusBarHeight: app.globalData.statusBarHeight || 20, id }); + const h = (app && app.globalData && app.globalData.statusBarHeight) || 20; + this.setData({ statusBarHeight: h, id }); this.load(); }, load() { - this.setData({ loading: true }); - planSvc.get(this.data.id).then((plan) => { + const id = this.data.id; + if (!id) { + this.setData({ loading: false, error: true, errorMsg: '缺少计划标识,无法打开' }); + return; + } + this.setData({ loading: true, error: false }); + planSvc.get(id).then((plan) => { + if (!plan || !plan.id) { + this.setData({ loading: false, error: true, errorMsg: '未找到该计划' }); + return; + } const raw = plan && plan.updatedAt; const updatedAtText = typeof raw === 'string' ? raw.slice(0, 10) @@ -27,13 +39,22 @@ Page({ this.setData({ plan: { ...plan, updatedAtText, icon: iconName(plan.emoji) }, exercises: (plan && plan.exercises) || [], - loading: false + loading: false, + error: false }); - }).catch(() => { - this.setData({ loading: false }); + }).catch((err) => { + const status = err && err.statusCode; + const msg = status === 404 + ? '未找到该计划' + : (status === 401 ? '登录已失效,请重新登录' : '加载失败,请检查网络后重试'); + this.setData({ loading: false, error: true, errorMsg: msg }); }); }, + onRetry() { + this.load(); + }, + onRemoveExercise(e) { const exerciseId = e.currentTarget.dataset.id; planSvc.removeExercise(this.data.id, exerciseId).then(() => { diff --git a/miniprogram/pages/plan-detail/plan-detail.wxml b/miniprogram/pages/plan-detail/plan-detail.wxml index 9e40023..b77c862 100644 --- a/miniprogram/pages/plan-detail/plan-detail.wxml +++ b/miniprogram/pages/plan-detail/plan-detail.wxml @@ -35,5 +35,11 @@ - {{ loading ? '加载中…' : '未找到该计划' }} + 加载中… + + + {{errorMsg || '加载失败'}} + 重试 + + 未找到该计划