uniapp【优化】: 封装授权功能

This commit is contained in:
15093570141
2024-10-25 09:51:47 +08:00
parent e18b149e8a
commit 747741c073
4 changed files with 65 additions and 18 deletions

View File

@@ -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<boolean> => {
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}`]
}
}

View File

@@ -27,4 +27,7 @@ export * from './time-format';
/** 处理分享 */
export * from './handle-share';
export * from './handle-share';
/** 处理授权 */
export * from './handle-authorize';

View File

@@ -178,6 +178,15 @@ export const showToast = (options : UniNamespace.ShowToastOptions) : Promise<any
})
}
export const showModal = (options : UniNamespace.ShowModalOptions) : Promise<any> => {
return new Promise((success, fail) => {
uni.showModal({
...options,
success,
fail
});
})
}
export default {
@@ -197,5 +206,6 @@ export default {
getSetting,
openSetting,
authorize,
showToast
showToast,
showModal
}

View File

@@ -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
}
</script>