From d667ec264bfcf07ab44d4117e658222329d62f88 Mon Sep 17 00:00:00 2001 From: ws Date: Mon, 27 Apr 2026 19:24:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E9=80=BB=E8=BE=91=E4=BD=BF=E7=94=A8=20async/await?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 50 ++++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/app.js b/app.js index 9c0bfea..0d7a29a 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,6 @@ // app.js const { BASE_URL, API } = require('./utils/config') +const { request } = require('./utils/api') App({ onLaunch() { @@ -24,35 +25,28 @@ App({ }) }, - loginWithCode(code) { - wx.request({ - url: BASE_URL + API.LOGIN, - method: 'GET', - data: { code }, - success: (res) => { - const result = res.data - if (result && result.code === 0 && result.data) { - const userInfo = { - openid: result.data.openid, - sessionKey: result.data.session_key, - unionid: result.data.unionid || '' - } - this.globalData.userInfo = userInfo - this.globalData.isLoggedIn = true - wx.setStorageSync('userInfo', userInfo) - if (this.loginReadyCallback) { - this.loginReadyCallback(userInfo) - } - } else { - console.error('后端登录失败', result) - this.handleLoginFail() - } - }, - fail: (err) => { - console.error('请求后端登录接口失败', err) - this.handleLoginFail() + async loginWithCode(code) { + try { + const data = await request({ + url: BASE_URL + API.LOGIN, + method: 'GET', + data: { code } + }) + const userInfo = { + openid: data.openid, + sessionKey: data.session_key, + unionid: data.unionid || '' } - }) + this.globalData.userInfo = userInfo + this.globalData.isLoggedIn = true + wx.setStorageSync('userInfo', userInfo) + if (this.loginReadyCallback) { + this.loginReadyCallback(userInfo) + } + } catch (err) { + console.error('后端登录失败', err) + this.handleLoginFail() + } }, handleLoginFail() {