permission.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // app权限申请处理
  2. const state = {
  3. // 处理应用程序权限请求
  4. CAMERA: false,
  5. WRITE_EXTERNAL_STORAGE: false,
  6. ACCESS_FINE_LOCATION: false,
  7. CALL_PHONE: false,
  8. RECORD_AUDIO: false,
  9. isIos: uni.getSystemInfoSync().platform == 'ios',
  10. mapping: {
  11. 'CAMERA': {
  12. title: '摄像头权限说明',
  13. content: '摄像头权限将用于拍摄照片和视频。这样,您可以在应用程序中记录瞬间、分享内容或进行其他相关操作。',
  14. methods: 'SET_CAMERA'
  15. },
  16. 'WRITE_EXTERNAL_STORAGE': {
  17. title: '存储空间/照片权限申请说明',
  18. content: '便于您使用该功能上传您的照片/图片/视频及用于更换头像、发布商品/分享、下载、与客服沟通等场景中读取和写入相册和文件内容。',
  19. methods: 'SET_WRITE_EXTERNAL_STORAGE'
  20. },
  21. 'ACCESS_FINE_LOCATION': {
  22. title: '地理位置权限申请说明',
  23. content: '易趣聯盟应用程序可以提供基于位置的服务、定位导航、附近搜索等功能。',
  24. methods: 'SET_ACCESS_FINE_LOCATION'
  25. },
  26. 'CALL_PHONE': {
  27. title: '拨打/管理电话权限申请说明',
  28. content: '便于您使用该功能联系买家/客服、业务等场景下使用',
  29. methods: 'SET_CALL_PHONE'
  30. },
  31. 'RECORD_AUDIO': {
  32. title: '录音权限申请说明',
  33. content: '便于发送录音信息给你客服等场景下使用',
  34. methods: 'SET_RECORD_AUDIO'
  35. }
  36. }
  37. }
  38. const mutations = {
  39. // 管理权限告知目的
  40. SET_CAMERA(state, val) {
  41. state.CAMERA = val
  42. },
  43. SET_WRITE_EXTERNAL_STORAGE(state, val) {
  44. state.WRITE_EXTERNAL_STORAGE = val
  45. },
  46. SET_CALL_PHONE(state, val) {
  47. state.CALL_PHONE = val
  48. },
  49. SET_ACCESS_FINE_LOCATION(state, val) {
  50. state.ACCESS_FINE_LOCATION = val
  51. },
  52. SET_RECORD_AUDIO(state, val) {
  53. state.RECORD_AUDIO = val
  54. }
  55. }
  56. const actions = {
  57. // 权限获取
  58. async requestPermissions({
  59. state,
  60. dispatch,
  61. commit
  62. }, permissionID) {
  63. console.log(permissionID,"進入")
  64. try {
  65. let qx = true;
  66. if (!state[permissionID] && !state.isIos) {
  67. qx = false;
  68. }
  69. console.log('android.permission.' + permissionID, '当前手机权限')
  70. return new Promise(async (resolve, reject) => {
  71. // ios不需要这个
  72. if (state.isIos) {
  73. resolve(1)
  74. return
  75. } else {
  76. if (!qx) {
  77. uni.showModal({
  78. title: state.mapping[permissionID].title,
  79. content: state.mapping[permissionID].content,
  80. success: async res => {
  81. if (res.confirm) {
  82. const re = await successQx();
  83. resolve(re);
  84. }
  85. },
  86. });
  87. } else {
  88. const re = await successQx();
  89. resolve(re);
  90. }
  91. }
  92. // Android权限查询
  93. function requestAndroidPermission(permissionID_) {
  94. return new Promise((resolve, reject) => {
  95. console.log("调用")
  96. plus.android.requestPermissions(
  97. [
  98. permissionID_], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  99. function(resultObj) {
  100. console.log("调用1",resultObj)
  101. var result = 0
  102. for (var i = 0; i < resultObj.granted.length; i++) {
  103. // var grantedPermission = resultObj.granted[i];
  104. // console.log('已获取的权限:' + grantedPermission);
  105. result = 1
  106. }
  107. for (var i = 0; i < resultObj.deniedPresent
  108. .length; i++) {
  109. // var deniedPresentPermission = resultObj.deniedPresent[i];
  110. // console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  111. result = 0
  112. }
  113. for (var i = 0; i < resultObj.deniedAlways
  114. .length; i++) {
  115. // var deniedAlwaysPermission = resultObj.deniedAlways[i];
  116. // console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  117. result = -1
  118. }
  119. resolve(result)
  120. },
  121. function(error) {
  122. console.log('申请权限错误:' + error.code + ' = ' + error
  123. .message)
  124. resolve({
  125. code: error.code,
  126. message: error.message
  127. })
  128. }
  129. )
  130. })
  131. }
  132. function successQx() {
  133. return new Promise(async (resolve, reject) => {
  134. const result = await requestAndroidPermission('android.permission.' + permissionID)
  135. if (result === 1) {
  136. // '已获得授权'
  137. commit(state.mapping[permissionID].methods, true)
  138. } else if (result === 0) {
  139. // '未获得授权'
  140. commit(state.mapping[permissionID].methods, false)
  141. } else {
  142. commit(state.mapping[permissionID].methods, true)
  143. uni.showModal({
  144. title: '提示',
  145. content: '操作权限已被拒绝,请手动前往设置',
  146. confirmText: '立即设置',
  147. success: (res) => {
  148. if (res.confirm) {
  149. dispatch('gotoAppPermissionSetting')
  150. }
  151. }
  152. })
  153. }
  154. resolve(result)
  155. })
  156. }
  157. })
  158. } catch (error) {
  159. console.log(error)
  160. reject(error)
  161. }
  162. },
  163. // 提示框
  164. nativeObjView({
  165. state
  166. }, permissionID) {
  167. const systemInfo = uni.getSystemInfoSync()
  168. const statusBarHeight = systemInfo.statusBarHeight
  169. const navigationBarHeight = systemInfo.platform === 'android' ? 48 :
  170. 44 // Set the navigation bar height based on the platform
  171. const totalHeight = statusBarHeight + navigationBarHeight
  172. let view = new plus.nativeObj.View('per-modal', {
  173. top: '0px',
  174. left: '0px',
  175. width: '100%',
  176. backgroundColor: 'rgba(0,0,0,0.5)'
  177. })
  178. view.drawRect({
  179. color: '#fff',
  180. radius: '5px'
  181. }, {
  182. top: totalHeight + 'px',
  183. left: '5%',
  184. width: '90%',
  185. height: '100px'
  186. })
  187. view.drawText(state.mapping[permissionID].title, {
  188. top: totalHeight + 5 + 'px',
  189. left: '8%',
  190. height: '30px'
  191. }, {
  192. align: 'left',
  193. color: '#000'
  194. }, {
  195. onClick: function(e) {
  196. console.log(e)
  197. }
  198. })
  199. view.drawText(state.mapping[permissionID].content, {
  200. top: totalHeight + 35 + 'px',
  201. height: '60px',
  202. left: '8%',
  203. width: '84%'
  204. }, {
  205. whiteSpace: 'normal',
  206. size: '14px',
  207. align: 'left',
  208. color: '#656563'
  209. })
  210. function show() {
  211. view = plus.nativeObj.View.getViewById('per-modal')
  212. if (view != null) {
  213. view.show()
  214. view = null // 展示的时候也得清空,不然影响下次的关闭,不知道为啥
  215. }
  216. }
  217. function close() {
  218. view = plus.nativeObj.View.getViewById('per-modal')
  219. if (view != null) {
  220. view.close()
  221. view = null
  222. }
  223. }
  224. return {
  225. show,
  226. close
  227. }
  228. },
  229. // 跳转到**应用**的权限页面
  230. gotoAppPermissionSetting({
  231. state
  232. }) {
  233. if (state.isIos) {
  234. var UIApplication = plus.ios.import('UIApplication')
  235. var application2 = UIApplication.sharedApplication()
  236. var NSURL2 = plus.ios.import('NSURL')
  237. var setting2 = NSURL2.URLWithString('app-settings:')
  238. application2.openURL(setting2)
  239. plus.ios.deleteObject(setting2)
  240. plus.ios.deleteObject(NSURL2)
  241. plus.ios.deleteObject(application2)
  242. } else {
  243. var Intent = plus.android.importClass('android.content.Intent')
  244. var Settings = plus.android.importClass('android.provider.Settings')
  245. var Uri = plus.android.importClass('android.net.Uri')
  246. var mainActivity = plus.android.runtimeMainActivity()
  247. var intent = new Intent()
  248. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
  249. var uri = Uri.fromParts('package', mainActivity.getPackageName(), null)
  250. intent.setData(uri)
  251. mainActivity.startActivity(intent)
  252. }
  253. }
  254. }
  255. const getters = {}
  256. export default {
  257. state,
  258. getters,
  259. mutations,
  260. actions
  261. }