From 6f3f68985ab6f219d3b78940e2fa8635865b9f58 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Tue, 14 Feb 2023 11:50:43 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20vue2=20=E6=A8=A1=E6=9D=BF=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20Promisify=20=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/default-ts/main.ts | 25 +++++++++++++++++++++++++ template/default/main.js | 27 ++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/template/default-ts/main.ts b/template/default-ts/main.ts index 5267e7a..84bf957 100755 --- a/template/default-ts/main.ts +++ b/template/default-ts/main.ts @@ -3,5 +3,30 @@ import App from './App.vue' Vue.config.productionTip = false +function isPromise(obj: any) { + return ( + !!obj && + (typeof obj === "object" || typeof obj === "function") && + typeof obj.then === "function" + ); +} + +uni.addInterceptor({ + returnValue(res: any) { + if (!isPromise(res)) { + return res; + } + return new Promise((resolve, reject) => { + res.then((res: [any, any]) => { + if (res[0]) { + reject(res[0]); + } else { + resolve(res[1]); + } + }); + }); + }, +}); + const app = new (typeof App === 'function' ? App : Vue.extend(Object.assign({ mpType: 'app' }, App))) app.$mount(); diff --git a/template/default/main.js b/template/default/main.js index 21fcbeb..a1e6c63 100755 --- a/template/default/main.js +++ b/template/default/main.js @@ -5,7 +5,32 @@ Vue.config.productionTip = false App.mpType = 'app' +function isPromise(obj) { + return ( + !!obj && + (typeof obj === "object" || typeof obj === "function") && + typeof obj.then === "function" + ); +} + +uni.addInterceptor({ + returnValue(res) { + if (!isPromise(res)) { + return res; + } + return new Promise((resolve, reject) => { + res.then((res) => { + if (res[0]) { + reject(res[0]); + } else { + resolve(res[1]); + } + }); + }); + }, +}); + const app = new Vue({ ...App }) -app.$mount() +app.$mount()