mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 19:03:27 +08:00
uniapp【优化】: 封装授权功能
This commit is contained in:
36
CoreCms.Net.Uni-App/CoreShop/core/utils/handle-authorize.ts
Normal file
36
CoreCms.Net.Uni-App/CoreShop/core/utils/handle-authorize.ts
Normal 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}`]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,4 +27,7 @@ export * from './time-format';
|
|||||||
|
|
||||||
|
|
||||||
/** 处理分享 */
|
/** 处理分享 */
|
||||||
export * from './handle-share';
|
export * from './handle-share';
|
||||||
|
|
||||||
|
/** 处理授权 */
|
||||||
|
export * from './handle-authorize';
|
||||||
@@ -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 {
|
export default {
|
||||||
@@ -197,5 +206,6 @@ export default {
|
|||||||
getSetting,
|
getSetting,
|
||||||
openSetting,
|
openSetting,
|
||||||
authorize,
|
authorize,
|
||||||
showToast
|
showToast,
|
||||||
|
showModal
|
||||||
}
|
}
|
||||||
@@ -70,7 +70,7 @@
|
|||||||
import type { Response, UserInfoType } from '@/core/models';
|
import type { Response, UserInfoType } from '@/core/models';
|
||||||
import { onLoad } from '@dcloudio/uni-app';
|
import { onLoad } from '@dcloudio/uni-app';
|
||||||
import { UserToken } from '@/core/consts';
|
import { UserToken } from '@/core/consts';
|
||||||
import { handleShowToast, handleStaticResources } from '@/core/utils';
|
import { handleShowToast, handleStaticResources, handleAuthorize } from '@/core/utils';
|
||||||
import { ShareEnum } from '@/core/enum';
|
import { ShareEnum } from '@/core/enum';
|
||||||
import { queryUserInfo, queryActivityDetial, queryDistributionStoreInfo, queryAgentInfo, queryGoodsDetail, queryPinTuanGoodsDetail, queryServiceDetail } from '@/core/api';
|
import { queryUserInfo, queryActivityDetial, queryDistributionStoreInfo, queryAgentInfo, queryGoodsDetail, queryPinTuanGoodsDetail, queryServiceDetail } from '@/core/api';
|
||||||
import LPainter from '@/uni_modules/lime-painter/components/l-painter/l-painter.vue';
|
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
|
// #ifdef APP-PLUS || APP-PLUS-NVUE
|
||||||
uni.downloadFile({
|
uni.downloadFile({
|
||||||
url: state.imgSrc,
|
url: state.imgSrc,
|
||||||
@@ -348,20 +348,18 @@
|
|||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
// #ifdef MP
|
// #ifdef MP
|
||||||
uni.authorize({
|
let authorizeRes : boolean = await handleAuthorize({ scope: 'writePhotosAlbum' });
|
||||||
scope: 'scope.writePhotosAlbum',
|
if (authorizeRes) {
|
||||||
success() {
|
uni.saveImageToPhotosAlbum({
|
||||||
uni.saveImageToPhotosAlbum({
|
filePath: state.imgSrc,
|
||||||
filePath: state.imgSrc,
|
success() {
|
||||||
success() {
|
handleShowToast('保存成功', 'success');
|
||||||
handleShowToast('保存成功', 'success');
|
},
|
||||||
},
|
fail() {
|
||||||
fail() {
|
handleShowToast('图片保存失败', 'error');
|
||||||
handleShowToast('图片保存失败', 'error');
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
// #endif
|
// #endif
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user