洛阳学员端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
6.9 KiB

  1. // 页面路径:store/index.js
  2. const permissionToast = {
  3. // 初始化状态
  4. state: {
  5. // 处理应用程序权限请求
  6. WRITE_EXTERNAL_STORAGE: false,
  7. ACCESS_FINE_LOCATION: false,
  8. CAMERA: false,
  9. CALL_PHONE: false,
  10. /* #ifdef APP-PLUS */
  11. isIos: plus.os.name == "iOS",
  12. /* #endif */
  13. mapping: {
  14. 'CAMERA': {
  15. title: "洛阳学车对扫开相机/摄像头权限申请说明",
  16. content: "便于您使用该功能拍照,扫码,打卡签到签退等功能",
  17. methods: 'SET_CAMERA'
  18. },
  19. 'WRITE_EXTERNAL_STORAGE': {
  20. title: "洛阳学车对存储空间/照片权限申请说明",
  21. content: "便于您使用该功能上传您的照片/图片/视频及用于更换头像、下载、投诉与咨询等场景中读取和写入相册和文件内容。",
  22. methods: 'SET_WRITE_EXTERNAL_STORAGE'
  23. },
  24. 'ACCESS_FINE_LOCATION': {
  25. title: "洛阳学车对地理位置权限申请说明",
  26. content: "洛阳学车应用程序可以提供基于位置的服务、定位导航、计算距离、扫码场地打卡等功能。",
  27. methods: 'SET_ACCESS_FINE_LOCATION'
  28. },
  29. 'CALL_PHONE': {
  30. title: "洛阳学车拨打/管理电话权限申请说明",
  31. content: "便于您使用该功能联系驾校、教练场景下使用",
  32. methods: 'SET_CALL_PHONE'
  33. }
  34. }
  35. },
  36. mutations: {
  37. // 管理权限告知目的
  38. SET_WRITE_EXTERNAL_STORAGE(state, val) {
  39. state.WRITE_EXTERNAL_STORAGE = val
  40. },
  41. SET_CALL_PHONE(state, val) {
  42. state.CALL_PHONE = val
  43. },
  44. SET_ACCESS_FINE_LOCATION(state, val) {
  45. state.ACCESS_FINE_LOCATION = val
  46. },
  47. SET_CAMERA(state,val) {
  48. state.CAMERA = val
  49. }
  50. },
  51. actions: {
  52. //权限获取
  53. async requestPermissions({state,dispatch,commit}, permissionID) {
  54. console.log('gogogoog来了没??')
  55. try {
  56. if (!state[permissionID] && !state.isIos) {
  57. var viewObj = await dispatch('nativeObjView', permissionID);
  58. viewObj.show();
  59. }
  60. console.log('android.permission.' + permissionID, '当前手机权限');
  61. return new Promise(async (resolve, reject) => {
  62. //苹果不需要这个
  63. if (state.isIos) {
  64. resolve(1);
  65. return
  66. }
  67. // Android权限查询
  68. function requestAndroidPermission(permissionID_) {
  69. return new Promise((resolve, reject) => {
  70. plus.android.requestPermissions(
  71. [permissionID_], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
  72. function(resultObj) {
  73. var result = 0;
  74. for (var i = 0; i < resultObj.granted.length; i++) {
  75. var grantedPermission = resultObj.granted[i];
  76. console.log('已获取的权限:' + grantedPermission);
  77. result = 1
  78. }
  79. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  80. var deniedPresentPermission = resultObj.deniedPresent[i];
  81. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  82. result = 0
  83. }
  84. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  85. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  86. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  87. result = -1
  88. }
  89. resolve(result);
  90. },
  91. function(error) {
  92. console.log('申请权限错误:' + error.code + " = " + error
  93. .message);
  94. resolve({
  95. code: error.code,
  96. message: error.message
  97. });
  98. }
  99. );
  100. });
  101. }
  102. const result = await requestAndroidPermission(
  103. 'android.permission.' + permissionID
  104. );
  105. if (result === 1) {
  106. //'已获得授权'
  107. commit(state.mapping[permissionID].methods, true)
  108. } else if (result === 0) {
  109. //'未获得授权'
  110. commit(state.mapping[permissionID].methods, false)
  111. } else {
  112. commit(state.mapping[permissionID].methods, true)
  113. uni.showModal({
  114. title: '提示',
  115. content: '操作权限已被拒绝,请手动前往设置',
  116. confirmText: "立即设置",
  117. success: (res) => {
  118. if (res.confirm) {
  119. dispatch('gotoAppPermissionSetting')
  120. }
  121. }
  122. })
  123. }
  124. if (viewObj) viewObj.close()
  125. resolve(result);
  126. });
  127. } catch (error) {
  128. console.log(error);
  129. uni.$u.toast('应用定位权限已关闭,请手动打开')
  130. uni.hideLoading()
  131. reject(error);
  132. }
  133. },
  134. //提示框
  135. nativeObjView({state}, permissionID) {
  136. const systemInfo = uni.getSystemInfoSync();
  137. const statusBarHeight = systemInfo.statusBarHeight;
  138. const navigationBarHeight = systemInfo.platform === 'android' ? 48 :
  139. 44; // Set the navigation bar height based on the platform
  140. const totalHeight = statusBarHeight + navigationBarHeight;
  141. let view = new plus.nativeObj.View('per-modal', {
  142. top: '0px',
  143. left: '0px',
  144. width: '100%',
  145. backgroundColor: '#444',
  146. //opacity: .5;
  147. })
  148. view.drawRect({
  149. color: '#fff',
  150. radius: '5px'
  151. }, {
  152. top: totalHeight + 'px',
  153. left: '5%',
  154. width: '90%',
  155. height: "100px",
  156. })
  157. view.drawText(state.mapping[permissionID].title, {
  158. top: totalHeight + 5 + 'px',
  159. left: "8%",
  160. height: "30px"
  161. }, {
  162. align: "left",
  163. color: "#000",
  164. }, {
  165. onClick: function(e) {
  166. console.log(e);
  167. }
  168. })
  169. view.drawText(state.mapping[permissionID].content, {
  170. top: totalHeight + 35 + 'px',
  171. height: "60px",
  172. left: "8%",
  173. width: "84%"
  174. }, {
  175. whiteSpace: 'normal',
  176. size: "14px",
  177. align: "left",
  178. color: "#656563"
  179. })
  180. function show() {
  181. view = plus.nativeObj.View.getViewById('per-modal');
  182. view.show()
  183. view = null //展示的时候也得清空,不然影响下次的关闭,不知道为啥
  184. }
  185. function close() {
  186. view = plus.nativeObj.View.getViewById('per-modal');
  187. view.close();
  188. view = null
  189. }
  190. return {
  191. show,
  192. close
  193. }
  194. },
  195. // 跳转到**应用**的权限页面
  196. gotoAppPermissionSetting({state}) {
  197. if (state.isIos) {
  198. var UIApplication = plus.ios.import("UIApplication");
  199. var application2 = UIApplication.sharedApplication();
  200. var NSURL2 = plus.ios.import("NSURL");
  201. // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
  202. var setting2 = NSURL2.URLWithString("app-settings:");
  203. application2.openURL(setting2);
  204. plus.ios.deleteObject(setting2);
  205. plus.ios.deleteObject(NSURL2);
  206. plus.ios.deleteObject(application2);
  207. } else {
  208. // console.log(plus.device.vendor);
  209. var Intent = plus.android.importClass("android.content.Intent");
  210. var Settings = plus.android.importClass("android.provider.Settings");
  211. var Uri = plus.android.importClass("android.net.Uri");
  212. var mainActivity = plus.android.runtimeMainActivity();
  213. var intent = new Intent();
  214. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  215. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  216. intent.setData(uri);
  217. mainActivity.startActivity(intent);
  218. }
  219. }
  220. }
  221. }
  222. // 导出 store
  223. export default permissionToast