123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- // app权限申请处理
- const state = {
- // 处理应用程序权限请求
- CAMERA: false,
- WRITE_EXTERNAL_STORAGE: false,
- ACCESS_FINE_LOCATION: false,
- CALL_PHONE: false,
- RECORD_AUDIO: false,
- isIos: uni.getSystemInfoSync().platform == 'ios',
- mapping: {
- 'CAMERA': {
- title: '摄像头权限说明',
- content: '摄像头权限将用于拍摄照片和视频。这样,您可以在应用程序中记录瞬间、分享内容或进行其他相关操作。',
- methods: 'SET_CAMERA'
- },
- 'WRITE_EXTERNAL_STORAGE': {
- title: '存储空间/照片权限申请说明',
- content: '便于您使用该功能上传您的照片/图片/视频及用于更换头像、发布商品/分享、下载、与客服沟通等场景中读取和写入相册和文件内容。',
- methods: 'SET_WRITE_EXTERNAL_STORAGE'
- },
- 'ACCESS_FINE_LOCATION': {
- title: '地理位置权限申请说明',
- content: '易趣聯盟应用程序可以提供基于位置的服务、定位导航、附近搜索等功能。',
- methods: 'SET_ACCESS_FINE_LOCATION'
- },
- 'CALL_PHONE': {
- title: '拨打/管理电话权限申请说明',
- content: '便于您使用该功能联系买家/客服、业务等场景下使用',
- methods: 'SET_CALL_PHONE'
- },
- 'RECORD_AUDIO': {
- title: '录音权限申请说明',
- content: '便于发送录音信息给你客服等场景下使用',
- methods: 'SET_RECORD_AUDIO'
- }
- }
- }
- const mutations = {
- // 管理权限告知目的
- SET_CAMERA(state, val) {
- state.CAMERA = val
- },
- SET_WRITE_EXTERNAL_STORAGE(state, val) {
- state.WRITE_EXTERNAL_STORAGE = val
- },
- SET_CALL_PHONE(state, val) {
- state.CALL_PHONE = val
- },
- SET_ACCESS_FINE_LOCATION(state, val) {
- state.ACCESS_FINE_LOCATION = val
- },
- SET_RECORD_AUDIO(state, val) {
- state.RECORD_AUDIO = val
- }
- }
- const actions = {
- // 权限获取
- async requestPermissions({
- state,
- dispatch,
- commit
- }, permissionID) {
- console.log(permissionID,"進入")
- try {
- let qx = true;
- if (!state[permissionID] && !state.isIos) {
- qx = false;
- }
- console.log('android.permission.' + permissionID, '当前手机权限')
- return new Promise(async (resolve, reject) => {
- // ios不需要这个
- if (state.isIos) {
- resolve(1)
- return
- } else {
- if (!qx) {
- uni.showModal({
- title: state.mapping[permissionID].title,
- content: state.mapping[permissionID].content,
- success: async res => {
- if (res.confirm) {
- const re = await successQx();
- resolve(re);
- }
- },
- });
- } else {
- const re = await successQx();
- resolve(re);
- }
- }
- // Android权限查询
- function requestAndroidPermission(permissionID_) {
- return new Promise((resolve, reject) => {
- console.log("调用")
- plus.android.requestPermissions(
- [
- permissionID_], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
- function(resultObj) {
- console.log("调用1",resultObj)
- var result = 0
- for (var i = 0; i < resultObj.granted.length; i++) {
- // var grantedPermission = resultObj.granted[i];
- // console.log('已获取的权限:' + grantedPermission);
- result = 1
- }
- for (var i = 0; i < resultObj.deniedPresent
- .length; i++) {
- // var deniedPresentPermission = resultObj.deniedPresent[i];
- // console.log('拒绝本次申请的权限:' + deniedPresentPermission);
- result = 0
- }
- for (var i = 0; i < resultObj.deniedAlways
- .length; i++) {
- // var deniedAlwaysPermission = resultObj.deniedAlways[i];
- // console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
- result = -1
- }
- resolve(result)
- },
- function(error) {
- console.log('申请权限错误:' + error.code + ' = ' + error
- .message)
- resolve({
- code: error.code,
- message: error.message
- })
- }
- )
- })
- }
- function successQx() {
- return new Promise(async (resolve, reject) => {
- const result = await requestAndroidPermission('android.permission.' + permissionID)
- if (result === 1) {
- // '已获得授权'
- commit(state.mapping[permissionID].methods, true)
- } else if (result === 0) {
- // '未获得授权'
- commit(state.mapping[permissionID].methods, false)
- } else {
- commit(state.mapping[permissionID].methods, true)
- uni.showModal({
- title: '提示',
- content: '操作权限已被拒绝,请手动前往设置',
- confirmText: '立即设置',
- success: (res) => {
- if (res.confirm) {
- dispatch('gotoAppPermissionSetting')
- }
- }
- })
- }
- resolve(result)
- })
- }
- })
- } catch (error) {
- console.log(error)
- reject(error)
- }
- },
- // 提示框
- nativeObjView({
- state
- }, permissionID) {
- const systemInfo = uni.getSystemInfoSync()
- const statusBarHeight = systemInfo.statusBarHeight
- const navigationBarHeight = systemInfo.platform === 'android' ? 48 :
- 44 // Set the navigation bar height based on the platform
- const totalHeight = statusBarHeight + navigationBarHeight
- let view = new plus.nativeObj.View('per-modal', {
- top: '0px',
- left: '0px',
- width: '100%',
- backgroundColor: 'rgba(0,0,0,0.5)'
- })
- view.drawRect({
- color: '#fff',
- radius: '5px'
- }, {
- top: totalHeight + 'px',
- left: '5%',
- width: '90%',
- height: '100px'
- })
- view.drawText(state.mapping[permissionID].title, {
- top: totalHeight + 5 + 'px',
- left: '8%',
- height: '30px'
- }, {
- align: 'left',
- color: '#000'
- }, {
- onClick: function(e) {
- console.log(e)
- }
- })
- view.drawText(state.mapping[permissionID].content, {
- top: totalHeight + 35 + 'px',
- height: '60px',
- left: '8%',
- width: '84%'
- }, {
- whiteSpace: 'normal',
- size: '14px',
- align: 'left',
- color: '#656563'
- })
- function show() {
- view = plus.nativeObj.View.getViewById('per-modal')
- if (view != null) {
- view.show()
- view = null // 展示的时候也得清空,不然影响下次的关闭,不知道为啥
- }
- }
- function close() {
- view = plus.nativeObj.View.getViewById('per-modal')
- if (view != null) {
- view.close()
- view = null
- }
- }
- return {
- show,
- close
- }
- },
- // 跳转到**应用**的权限页面
- gotoAppPermissionSetting({
- state
- }) {
- if (state.isIos) {
- var UIApplication = plus.ios.import('UIApplication')
- var application2 = UIApplication.sharedApplication()
- var NSURL2 = plus.ios.import('NSURL')
- var setting2 = NSURL2.URLWithString('app-settings:')
- application2.openURL(setting2)
- plus.ios.deleteObject(setting2)
- plus.ios.deleteObject(NSURL2)
- plus.ios.deleteObject(application2)
- } else {
- var Intent = plus.android.importClass('android.content.Intent')
- var Settings = plus.android.importClass('android.provider.Settings')
- var Uri = plus.android.importClass('android.net.Uri')
- var mainActivity = plus.android.runtimeMainActivity()
- var intent = new Intent()
- intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
- var uri = Uri.fromParts('package', mainActivity.getPackageName(), null)
- intent.setData(uri)
- mainActivity.startActivity(intent)
- }
- }
- }
- const getters = {}
- export default {
- state,
- getters,
- mutations,
- actions
- }
|