@slan-health/taro-vueuse
Version:
taro vue版本hooks
80 lines (79 loc) • 2.26 kB
JavaScript
import { getSetting, openSetting, authorize } from "@tarojs/taro";
import useModal from "../useModal/index.js";
function useDeviceAuth() {
const [showModalAsync] = useModal();
const requestDeviceAuth = ({
scope,
modalTitle = "尚未进行授权,功能将无法使用",
confirmText = "去授权"
}) => {
if (!scope) {
throw new Error("please set scope property");
}
return new Promise((resolve, reject) => {
getSetting({
success(res) {
if (!res.authSetting[scope] && res.authSetting[scope] == false) {
showModalAsync({
content: modalTitle,
confirmText
}).then((res2) => {
if (res2.confirm) {
openSetting({
success: (openRes) => {
if (!openRes.authSetting[scope]) {
authorize({
scope,
success() {
resolve({
...res2,
authSetting: {
...res2.authSetting,
[scope]: true
}
});
},
fail(err) {
reject(err);
}
});
} else {
resolve(res2);
}
}
});
} else {
reject({
errMsg: "取消授权"
});
}
});
} else if (!res.authSetting[scope]) {
authorize({
scope,
success() {
resolve({
...res,
authSetting: {
...res.authSetting,
[scope]: true
}
});
},
fail(err) {
reject(err);
}
});
} else {
resolve(res);
}
}
});
});
};
return [requestDeviceAuth];
}
export {
useDeviceAuth as default
};
//# sourceMappingURL=index.js.map