From 8cd5493eec242eb9a9f3826bb8c477a7be957791 Mon Sep 17 00:00:00 2001 From: qiang Date: Mon, 7 Dec 2020 18:14:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(App):=20=E4=BF=AE=E5=A4=8D=E9=83=A8?= =?UTF-8?q?=E5=88=86=20API=20=E7=BC=BA=E5=A4=B1=E8=AF=A6=E7=BB=86=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/api/plugin/get-provider.js | 4 +- .../app-plus/service/api/plugin/oauth.js | 16 +- .../app-plus/service/api/plugin/payment.js | 20 +-- .../app-plus/service/api/plugin/share.js | 157 +++++++----------- src/platforms/app-plus/service/api/util.js | 4 +- 5 files changed, 82 insertions(+), 119 deletions(-) diff --git a/src/platforms/app-plus/service/api/plugin/get-provider.js b/src/platforms/app-plus/service/api/plugin/get-provider.js index 0bdb45905..9a7d0fca1 100644 --- a/src/platforms/app-plus/service/api/plugin/get-provider.js +++ b/src/platforms/app-plus/service/api/plugin/get-provider.js @@ -58,7 +58,7 @@ export function getProvider ({ providers[service]((err, provider) => { if (err) { invoke(callbackId, { - errMsg: 'getProvider:fail:' + err.message + errMsg: 'getProvider:fail ' + err.message }) } else { invoke(callbackId, { @@ -70,7 +70,7 @@ export function getProvider ({ }) } else { invoke(callbackId, { - errMsg: 'getProvider:fail:服务[' + service + ']不支持' + errMsg: 'getProvider:fail 服务[' + service + ']不支持' }) } } diff --git a/src/platforms/app-plus/service/api/plugin/oauth.js b/src/platforms/app-plus/service/api/plugin/oauth.js index d3244ebf6..1c31d8388 100644 --- a/src/platforms/app-plus/service/api/plugin/oauth.js +++ b/src/platforms/app-plus/service/api/plugin/oauth.js @@ -20,6 +20,8 @@ function getService (provider) { */ export function login (params, callbackId) { const provider = params.provider || 'weixin' + const errorCallback = warpPlusErrorCallback(callbackId, 'login') + getService(provider).then(service => { function login () { service.login(res => { @@ -29,12 +31,7 @@ export function login (params, callbackId) { authResult: authResult, errMsg: 'login:ok' }) - }, err => { - invoke(callbackId, { - code: err.code, - errMsg: 'login:fail:' + err.message - }) - }, provider === 'apple' ? { scope: 'email' } : params.univerifyStyle || {}) + }, errorCallback, provider === 'apple' ? { scope: 'email' } : params.univerifyStyle || {}) } // 先注销再登录 // apple登录logout之后无法重新触发获取email,fullname;一键登录无logout @@ -43,12 +40,7 @@ export function login (params, callbackId) { } else { service.logout(login, login) } - }).catch(err => { - invoke(callbackId, { - code: err.code || '', - errMsg: 'login:fail:' + err.message - }) - }) + }).catch(errorCallback) } export function getUserInfo (params, callbackId) { diff --git a/src/platforms/app-plus/service/api/plugin/payment.js b/src/platforms/app-plus/service/api/plugin/payment.js index 525d15d75..ec17f0610 100644 --- a/src/platforms/app-plus/service/api/plugin/payment.js +++ b/src/platforms/app-plus/service/api/plugin/payment.js @@ -2,29 +2,27 @@ import { invoke } from '../../bridge' +import { + warpPlusErrorCallback +} from '../util' + export function requestPayment (params, callbackId) { const provider = params.provider + const errorCallback = warpPlusErrorCallback(callbackId, 'requestPayment') + plus.payment.getChannels(services => { const service = services.find(({ id }) => id === provider) if (!service) { invoke(callbackId, { - errMsg: 'requestPayment:fail:支付服务[' + provider + ']不存在' + errMsg: 'requestPayment:fail 支付服务[' + provider + ']不存在' }) } else { plus.payment.request(service, params.orderInfo, res => { res.errMsg = 'requestPayment:ok' invoke(callbackId, res) - }, err => { - invoke(callbackId, { - errMsg: 'requestPayment:fail:' + err.message - }) - }) + }, errorCallback) } - }, err => { - invoke(callbackId, { - errMsg: 'requestPayment:fail:' + err.message - }) - }) + }, errorCallback) } diff --git a/src/platforms/app-plus/service/api/plugin/share.js b/src/platforms/app-plus/service/api/plugin/share.js index 200528b0b..e2c209472 100644 --- a/src/platforms/app-plus/service/api/plugin/share.js +++ b/src/platforms/app-plus/service/api/plugin/share.js @@ -3,7 +3,8 @@ import { } from '../constants' import { - getRealPath + getRealPath, + warpPlusErrorCallback } from '../util' import { @@ -82,19 +83,13 @@ const parseParams = (args, callbackId, method) => { } const sendShareMsg = function (service, params, callbackId, method = 'share') { - service.send( - params, - () => { - invoke(callbackId, { - errMsg: method + ':ok' - }) - }, - err => { - invoke(callbackId, { - errMsg: method + ':fail:' + err.message - }) - } - ) + const errorCallback = warpPlusErrorCallback(callbackId, method) + + service.send(params, () => { + invoke(callbackId, { + errMsg: method + ':ok' + }) + }, errorCallback) } export function shareAppMessageDirectly ({ @@ -117,37 +112,24 @@ export function shareAppMessageDirectly ({ 'shareAppMessageDirectly' ) } + const errorCallback = warpPlusErrorCallback(callbackId, 'shareAppMessageDirectly') + if (useDefaultSnapshot) { const pages = getCurrentPages() const webview = plus.webview.getWebviewById(pages[pages.length - 1].__wxWebviewId__ + '') if (webview) { const bitmap = new plus.nativeObj.Bitmap() - webview.draw( - bitmap, - () => { - const fileName = TEMP_PATH + '/share/snapshot.jpg' - bitmap.save( - fileName, { - overwrite: true, - format: 'jpg' - }, - () => { - imageUrl = fileName - goShare() - }, - err => { - invoke(callbackId, { - errMsg: 'shareAppMessageDirectly:fail:' + err.message - }) - } - ) - }, - err => { - invoke(callbackId, { - errMsg: 'shareAppMessageDirectly:fail:' + err.message - }) - } - ) + webview.draw(bitmap, () => { + const fileName = TEMP_PATH + '/share/snapshot.jpg' + bitmap.save( + fileName, { + overwrite: true, + format: 'jpg' + }, () => { + imageUrl = fileName + goShare() + }, errorCallback) + }, errorCallback) } else { goShare() } @@ -158,73 +140,62 @@ export function shareAppMessageDirectly ({ export function share (params, callbackId, method = 'share') { params = parseParams(params, callbackId, method) + const errorCallback = warpPlusErrorCallback(callbackId, method) + if (typeof params === 'string') { return invoke(callbackId, { - errMsg: method + ':fail:' + params + errMsg: method + ':fail ' + params }) } const provider = params.provider - plus.share.getServices( - services => { - const service = services.find(({ - id - }) => id === provider) - if (!service) { - invoke(callbackId, { - errMsg: method + ':fail:分享服务[' + provider + ']不存在' - }) - } else { - if (service.authenticated) { - sendShareMsg(service, params, callbackId) - } else { - service.authorize( - () => sendShareMsg(service, params, callbackId), - err => { - invoke(callbackId, { - errMsg: method + ':fail:' + err.message - }) - } - ) - } - } - }, - err => { + plus.share.getServices(services => { + const service = services.find(({ + id + }) => id === provider) + if (!service) { invoke(callbackId, { - errMsg: method + ':fail:' + err.message + errMsg: method + ':fail 分享服务[' + provider + ']不存在' }) + } else { + if (service.authenticated) { + sendShareMsg(service, params, callbackId) + } else { + service.authorize( + () => sendShareMsg(service, params, callbackId), + errorCallback + ) + } } - ) + }, errorCallback) } -export function shareWithSystem (params, callbackId, method = 'shareWithSystem') { - let { - type, - imageUrl, - summary: content, - href +export function shareWithSystem (params, callbackId, method = 'shareWithSystem') { + let { + type, + imageUrl, + summary: content, + href } = params type = type || 'text' - const allowedTypes = ['text', 'image'] - if (allowedTypes.indexOf(type) < 0) { - invoke(callbackId, { - errMsg: method + ':fail:分享参数 type 不正确' - }) - } - if (typeof imageUrl === 'string' && imageUrl) { - imageUrl = getRealPath(imageUrl) + const allowedTypes = ['text', 'image'] + const errorCallback = warpPlusErrorCallback(callbackId, method) + + if (allowedTypes.indexOf(type) < 0) { + invoke(callbackId, { + errMsg: method + ':fail 分享参数 type 不正确' + }) + } + if (typeof imageUrl === 'string' && imageUrl) { + imageUrl = getRealPath(imageUrl) } - plus.share.sendWithSystem({ - type, - pictures: imageUrl && [imageUrl], - content, - href + plus.share.sendWithSystem({ + type, + pictures: imageUrl && [imageUrl], + content, + href }, function (res) { invoke(callbackId, { errMsg: method + ':ok' }) - }, function (err) { - invoke(callbackId, { - errMsg: method + ':fail:' + err.message - }) - }) -} + }, errorCallback) +} diff --git a/src/platforms/app-plus/service/api/util.js b/src/platforms/app-plus/service/api/util.js index f52c6d20c..0d5a35f0b 100644 --- a/src/platforms/app-plus/service/api/util.js +++ b/src/platforms/app-plus/service/api/util.js @@ -190,9 +190,11 @@ export function warpPlusSuccessCallback (callbackId, neme) { export function warpPlusErrorCallback (callbackId, neme, errMsg) { return function errorCallback (error) { error = error || {} + const code = error.code || 0 invoke(callbackId, { errMsg: `${neme}:fail ${error.message || errMsg || ''}`, - errCode: error.code || 0 + errCode: code, + code }) } } -- GitLab