From 747741c073d235e7e73c581c52715d9e52429942 Mon Sep 17 00:00:00 2001 From: 15093570141 <141405260+17521612761@users.noreply.github.com> Date: Fri, 25 Oct 2024 09:51:47 +0800 Subject: [PATCH] =?UTF-8?q?uniapp=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91:=20?= =?UTF-8?q?=E5=B0=81=E8=A3=85=E6=8E=88=E6=9D=83=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CoreShop/core/utils/handle-authorize.ts | 36 +++++++++++++++++++ .../CoreShop/core/utils/index.ts | 5 ++- .../CoreShop/core/utils/uni-promise.ts | 12 ++++++- .../pages/share/sharePoster/sharePoster.vue | 30 ++++++++-------- 4 files changed, 65 insertions(+), 18 deletions(-) create mode 100644 CoreCms.Net.Uni-App/CoreShop/core/utils/handle-authorize.ts diff --git a/CoreCms.Net.Uni-App/CoreShop/core/utils/handle-authorize.ts b/CoreCms.Net.Uni-App/CoreShop/core/utils/handle-authorize.ts new file mode 100644 index 00000000..408e1a12 --- /dev/null +++ b/CoreCms.Net.Uni-App/CoreShop/core/utils/handle-authorize.ts @@ -0,0 +1,36 @@ +import { authorize, showModal, openSetting } from './uni-promise'; + +interface AuthorizationParams { + scope : string; + modalTitle ?: string; + modalContent ?: string; + modalConfirmText ?: string; +} + +export const handleAuthorize = async ({ scope, modalTitle = "温馨提示", modalContent = "如果您拒绝授权,将无法正常使用此功能", modalConfirmText = "去授权" } : AuthorizationParams) : Promise => { + try { + /** 唤起授权 */ + const authorizeRes = await authorize({ + scope: `scope.${scope}`, + }) + /** 接受授权 */ + if (authorizeRes.errMsg === 'authorize:ok') { + return true; + } + } catch (res : any) { + /** 没有授权弹框提示-因为后续操作需要点击才能调用 */ + const modalRes = await showModal({ + title: modalTitle, + content: modalContent, + confirmText: modalConfirmText, + }) + + if (!modalRes.confirm) { + return false; + } + + /** 打开授权设置面板 */ + const openSetRes = await openSetting(); + return openSetRes.authSetting[`scope.${scope}`] + } +} \ No newline at end of file diff --git a/CoreCms.Net.Uni-App/CoreShop/core/utils/index.ts b/CoreCms.Net.Uni-App/CoreShop/core/utils/index.ts index 4d2aaa48..3c3de92e 100644 --- a/CoreCms.Net.Uni-App/CoreShop/core/utils/index.ts +++ b/CoreCms.Net.Uni-App/CoreShop/core/utils/index.ts @@ -27,4 +27,7 @@ export * from './time-format'; /** 处理分享 */ -export * from './handle-share'; \ No newline at end of file +export * from './handle-share'; + +/** 处理授权 */ +export * from './handle-authorize'; \ No newline at end of file diff --git a/CoreCms.Net.Uni-App/CoreShop/core/utils/uni-promise.ts b/CoreCms.Net.Uni-App/CoreShop/core/utils/uni-promise.ts index 223d9b57..1adb56b9 100644 --- a/CoreCms.Net.Uni-App/CoreShop/core/utils/uni-promise.ts +++ b/CoreCms.Net.Uni-App/CoreShop/core/utils/uni-promise.ts @@ -178,6 +178,15 @@ export const showToast = (options : UniNamespace.ShowToastOptions) : Promise => { + return new Promise((success, fail) => { + uni.showModal({ + ...options, + success, + fail + }); + }) +} export default { @@ -197,5 +206,6 @@ export default { getSetting, openSetting, authorize, - showToast + showToast, + showModal } \ No newline at end of file diff --git a/CoreCms.Net.Uni-App/CoreShop/pages/share/sharePoster/sharePoster.vue b/CoreCms.Net.Uni-App/CoreShop/pages/share/sharePoster/sharePoster.vue index 010f800c..419309ed 100644 --- a/CoreCms.Net.Uni-App/CoreShop/pages/share/sharePoster/sharePoster.vue +++ b/CoreCms.Net.Uni-App/CoreShop/pages/share/sharePoster/sharePoster.vue @@ -70,7 +70,7 @@ import type { Response, UserInfoType } from '@/core/models'; import { onLoad } from '@dcloudio/uni-app'; import { UserToken } from '@/core/consts'; - import { handleShowToast, handleStaticResources } from '@/core/utils'; + import { handleShowToast, handleStaticResources, handleAuthorize } from '@/core/utils'; import { ShareEnum } from '@/core/enum'; import { queryUserInfo, queryActivityDetial, queryDistributionStoreInfo, queryAgentInfo, queryGoodsDetail, queryPinTuanGoodsDetail, queryServiceDetail } from '@/core/api'; import LPainter from '@/uni_modules/lime-painter/components/l-painter/l-painter.vue'; @@ -326,7 +326,7 @@ } /** 保存到本地 */ - const handlesavePoster = () => { + const handlesavePoster = async () => { // #ifdef APP-PLUS || APP-PLUS-NVUE uni.downloadFile({ url: state.imgSrc, @@ -348,20 +348,18 @@ // #endif // #ifdef MP - uni.authorize({ - scope: 'scope.writePhotosAlbum', - success() { - uni.saveImageToPhotosAlbum({ - filePath: state.imgSrc, - success() { - handleShowToast('保存成功', 'success'); - }, - fail() { - handleShowToast('图片保存失败', 'error'); - } - }); - } - }) + let authorizeRes : boolean = await handleAuthorize({ scope: 'writePhotosAlbum' }); + if (authorizeRes) { + uni.saveImageToPhotosAlbum({ + filePath: state.imgSrc, + success() { + handleShowToast('保存成功', 'success'); + }, + fail() { + handleShowToast('图片保存失败', 'error'); + } + }); + } // #endif }