From 696fb4a627421ecaed4b438373a71a40efb980b2 Mon Sep 17 00:00:00 2001 From: yansongda Date: Fri, 10 Nov 2023 23:30:15 +0800 Subject: [PATCH] =?UTF-8?q?optimized(frontend:submit):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=8F=90=E4=BA=A4=E4=B8=AD=E7=9A=84=20Promise=20?= =?UTF-8?q?=E9=80=BB=E8=BE=91=20(#26)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 ++++ miniprogram/pages/shorturl/index.ts | 20 +++++-------- miniprogram/pages/totp/edit.ts | 46 +++++++++++++---------------- miniprogram/pages/user/edit.ts | 18 ++++------- 4 files changed, 41 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddc02a2..4bc2cc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v1.1.5 + +### optimized + +- optimized(frontend:submit): 优化提交中的 Promise 逻辑(#26) + ## v1.1.4 ### feat diff --git a/miniprogram/pages/shorturl/index.ts b/miniprogram/pages/shorturl/index.ts index 96dc17b..adca972 100644 --- a/miniprogram/pages/shorturl/index.ts +++ b/miniprogram/pages/shorturl/index.ts @@ -14,19 +14,15 @@ Page({ async submit(e: FormSubmit) { await wx.showLoading({ title: '生成中', mask: true }) - const { link } = e.detail.value + try { + const { short } = await api.create(e.detail.value.link) - api - .create(link) - .then(({ short }) => { - this.setData({ short }) - }) - .catch((e) => { - this.setData({ toptipError: e.message }) - }) - .finally(async () => { - await wx.hideLoading() - }) + this.setData({ short }) + } catch (e: unknown) { + this.setData({ toptipError: e instanceof Error ? e.message : '未知异常' }) + } + + await wx.hideLoading() }, async copy() { if (this.data.short == '') { diff --git a/miniprogram/pages/totp/edit.ts b/miniprogram/pages/totp/edit.ts index 287527b..d19ac63 100644 --- a/miniprogram/pages/totp/edit.ts +++ b/miniprogram/pages/totp/edit.ts @@ -26,34 +26,30 @@ Page({ async onShow() { await wx.showLoading({ title: '加载中' }) - api - .detail(this.data.id) - .then(({ id, issuer, username }) => { - this.setData({ id, issuer: issuer ?? '', username: username ?? '' }) - }) - .catch(() => { - this.setData({ dialogShow: true }) - }) - .finally(() => wx.hideLoading()) + try { + const { id, issuer, username } = await api.detail(this.data.id) + + this.setData({ id, issuer: issuer ?? '', username: username ?? '' }) + } catch (e: unknown) { + this.setData({ dialogShow: true }) + } + + await wx.hideLoading() }, async submit(e: FormSubmit) { - await wx.showToast({ title: '更新中', icon: 'loading', mask: true, duration: 3000 }) + await wx.showToast({ title: '更新中', icon: 'loading', mask: true }) + + try { + await api.update({ id: this.data.id, ...e.detail.value } as UpdateRequest) - api - .update({ id: this.data.id, ...e.detail.value } as UpdateRequest) - .then(() => { - wx.showToast({ - title: '修改成功', - icon: 'success', - mask: true, - success: () => { - setTimeout(() => wx.navigateBack(), 1500) - } - }) - }) - .catch((e: unknown) => { - this.setData({ toptipError: e instanceof Error ? e.message : '未知异常' }) - }) + await wx.showToast({ title: '修改成功', icon: 'success', mask: true }) + + setTimeout(() => wx.navigateBack(), 1500) + } catch (e: unknown) { + await wx.hideToast() + + this.setData({ toptipError: e instanceof Error ? e.message : '未知异常' }) + } }, async cancel() { await wx.navigateBack() diff --git a/miniprogram/pages/user/edit.ts b/miniprogram/pages/user/edit.ts index cfcad91..64c4f22 100644 --- a/miniprogram/pages/user/edit.ts +++ b/miniprogram/pages/user/edit.ts @@ -46,28 +46,22 @@ Page({ }) }, async submit(e: FormSubmit) { - await wx.showToast({ title: '更新中', icon: 'loading', mask: true, duration: 3000 }) + await wx.showToast({ title: '更新中', icon: 'loading', mask: true }) try { await api.update(e.detail.value as UpdateRequest) // 同步完成之后更新下全局的用户信息状态 await utils.sync() + + await wx.showToast({ title: '修改成功', icon: 'success', mask: true }) + + setTimeout(() => wx.navigateBack(), 1500) } catch (e: unknown) { - this.setData({ toptipError: e instanceof Error ? e.message : '未知异常' }) await wx.hideToast() - return + this.setData({ toptipError: e instanceof Error ? e.message : '未知异常' }) } - - wx.showToast({ - title: '修改成功', - icon: 'success', - mask: true, - success: () => { - setTimeout(() => wx.navigateBack(), 1000) - } - }) }, async cancel() { await wx.navigateBack()