| | |
| | | }); |
| | | }) |
| | | return promise; |
| | | } |
| | | } |
| | | |
| | | const LOCATION_NOTICE_STORAGE_KEY = 'location_permission_notice_shown' |
| | | const IMAGE_NOTICE_STORAGE_KEY = 'image_permission_notice_shown' |
| | | |
| | | const DEFAULT_LOCATION_NOTICE = { |
| | | title: '获取位置信息', |
| | | content: '为了给您分配附近的订单,并计算接送货的精准距离与导航路线,本应用需要获取您的位置信息。', |
| | | confirmText: '已知晓' |
| | | } |
| | | |
| | | const DEFAULT_IMAGE_NOTICE = { |
| | | title: '获取相机权限', |
| | | content: '为了支持您进行司机身份认证,以及方便您在取送件时拍照留存凭证,本应用需要获取您的相机与相册权限。', |
| | | confirmText: '已知晓' |
| | | } |
| | | |
| | | export function ensureLocationPermissionNotice(modalOptions = {}) { |
| | | const hasShown = uni.getStorageSync(LOCATION_NOTICE_STORAGE_KEY) |
| | | if (hasShown) { |
| | | return Promise.resolve(true) |
| | | } |
| | | |
| | | const options = Object.assign({}, DEFAULT_LOCATION_NOTICE, modalOptions) |
| | | |
| | | return new Promise((resolve) => { |
| | | uni.showModal({ |
| | | title: options.title, |
| | | content: options.content, |
| | | confirmText: options.confirmText, |
| | | showCancel: false, |
| | | confirmColor: '#106efa', |
| | | success: (res) => { |
| | | if (res.confirm) { |
| | | uni.setStorageSync(LOCATION_NOTICE_STORAGE_KEY, true) |
| | | resolve(true) |
| | | return |
| | | } |
| | | resolve(false) |
| | | }, |
| | | fail: () => { |
| | | resolve(false) |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | let hasEmittedLocationPermissionGranted = false |
| | | |
| | | export function getLocationWithNotice(locationOptions = {}, modalOptions = {}) { |
| | | return ensureLocationPermissionNotice(modalOptions).then((confirmed) => { |
| | | if (!confirmed) { |
| | | return Promise.reject({ errMsg: 'location permission notice canceled' }) |
| | | } |
| | | |
| | | return new Promise((resolve, reject) => { |
| | | uni.getLocation(Object.assign({}, locationOptions, { |
| | | success: (res) => { |
| | | if (!hasEmittedLocationPermissionGranted) { |
| | | hasEmittedLocationPermissionGranted = true |
| | | uni.$emit('locationPermissionGranted', res) |
| | | } |
| | | if (typeof locationOptions.success === 'function') { |
| | | locationOptions.success(res) |
| | | } |
| | | resolve(res) |
| | | }, |
| | | fail: (err) => { |
| | | if (typeof locationOptions.fail === 'function') { |
| | | locationOptions.fail(err) |
| | | } |
| | | reject(err) |
| | | } |
| | | })) |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | export function ensureImagePermissionNotice(modalOptions = {}) { |
| | | const hasShown = uni.getStorageSync(IMAGE_NOTICE_STORAGE_KEY) |
| | | if (hasShown) { |
| | | return Promise.resolve(true) |
| | | } |
| | | |
| | | const options = Object.assign({}, DEFAULT_IMAGE_NOTICE, modalOptions) |
| | | |
| | | return new Promise((resolve) => { |
| | | uni.showModal({ |
| | | title: options.title, |
| | | content: options.content, |
| | | confirmText: options.confirmText, |
| | | showCancel: false, |
| | | success: (res) => { |
| | | if (res.confirm) { |
| | | uni.setStorageSync(IMAGE_NOTICE_STORAGE_KEY, true) |
| | | resolve(true) |
| | | return |
| | | } |
| | | resolve(false) |
| | | }, |
| | | fail: () => { |
| | | resolve(false) |
| | | } |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | export function chooseImageWithNotice(imageOptions = {}, modalOptions = {}) { |
| | | return ensureImagePermissionNotice(modalOptions).then((confirmed) => { |
| | | if (!confirmed) { |
| | | return Promise.reject({ errMsg: 'image permission notice canceled' }) |
| | | } |
| | | |
| | | return new Promise((resolve, reject) => { |
| | | uni.chooseImage(Object.assign({}, imageOptions, { |
| | | success: (res) => { |
| | | if (typeof imageOptions.success === 'function') { |
| | | imageOptions.success(res) |
| | | } |
| | | resolve(res) |
| | | }, |
| | | fail: (err) => { |
| | | console.log('chooseImage fail', err) |
| | | const errMsg = (err && err.errMsg) || '' |
| | | const errCode = (err && err.errCode) || '' |
| | | if (errMsg && errMsg.includes('chooseImage:fail No Permission')) { |
| | | // const sourceType = imageOptions.sourceType || ['album', 'camera'] |
| | | |
| | | let toastTitle = '' |
| | | |
| | | // 拍照权限 |
| | | // 拍照权限 |
| | | if (errCode === 11) { |
| | | toastTitle = '相机权限已关闭,请前往手机【设置】中手动开启' |
| | | } |
| | | |
| | | // 相册权限 |
| | | if (errCode === 12) { |
| | | toastTitle = '相册权限已关闭,请前往手机【设置】中手动开启' |
| | | } |
| | | |
| | | // if (sourceType.length === 1) { |
| | | // if (sourceType[0] === 'album') { |
| | | // toastTitle = '相册权限已关闭,请前往手机【设置】中手动开启' |
| | | // } else if (sourceType[0] === 'camera') { |
| | | // toastTitle = '相机权限已关闭,请前往手机【设置】中手动开启' |
| | | // } |
| | | // } else { |
| | | // if (errMsg.includes('camera') || errMsg.includes('Camera')) { |
| | | // toastTitle = '相机权限已关闭,请前往手机【设置】中手动开启' |
| | | // } else if (errMsg.includes('album') || errMsg.includes('Album') || errMsg.includes('photo') || errMsg.includes('Photo')) { |
| | | // toastTitle = '相册权限已关闭,请前往手机【设置】中手动开启' |
| | | // } |
| | | // } |
| | | |
| | | uni.showToast({ |
| | | title: toastTitle, |
| | | icon: 'none', |
| | | duration: 3000 |
| | | }) |
| | | } |
| | | if (typeof imageOptions.fail === 'function') { |
| | | imageOptions.fail(err) |
| | | } |
| | | reject(err) |
| | | } |
| | | })) |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | export function checkLocationPermission() { |
| | | return new Promise((resolve) => { |
| | | if (typeof uni.getAppAuthorizeSetting === 'function') { |
| | | try { |
| | | const setting = uni.getAppAuthorizeSetting() |
| | | const locationAuthorized = setting.locationAuthorized |
| | | resolve(locationAuthorized === 'authorized' || locationAuthorized === 'authorizedAlways') |
| | | return |
| | | } catch (e) { |
| | | console.log('getAppAuthorizeSetting fail', e) |
| | | } |
| | | } |
| | | |
| | | uni.getSetting({ |
| | | success: (res) => { |
| | | const authSetting = res.authSetting || {} |
| | | resolve(!!authSetting['scope.userLocation']) |
| | | }, |
| | | fail: () => { |
| | | resolve(false) |
| | | } |
| | | }) |
| | | }) |
| | | } |