Skip to content

Commit

Permalink
optimized(frontend:submit): 优化提交中的 Promise 逻辑 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda authored Nov 10, 2023
1 parent c876d32 commit 696fb4a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 49 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v1.1.5

### optimized

- optimized(frontend:submit): 优化提交中的 Promise 逻辑(#26)

## v1.1.4

### feat
Expand Down
20 changes: 8 additions & 12 deletions miniprogram/pages/shorturl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ Page({
async submit(e: FormSubmit<FormData>) {
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 == '') {
Expand Down
46 changes: 21 additions & 25 deletions miniprogram/pages/totp/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormData>) {
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()
Expand Down
18 changes: 6 additions & 12 deletions miniprogram/pages/user/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,22 @@ Page({
})
},
async submit(e: FormSubmit<FormData>) {
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()
Expand Down

0 comments on commit 696fb4a

Please sign in to comment.