permission.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /// null = 未请求,1 = 已允许,0 = 拒绝|受限, 2 = 系统未开启
  2. var isIOS
  3. function album() {
  4. var result = 0;
  5. var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
  6. var authStatus = PHPhotoLibrary.authorizationStatus();
  7. if (authStatus === 0) {
  8. result = null;
  9. } else if (authStatus == 3) {
  10. result = 1;
  11. } else {
  12. result = 0;
  13. }
  14. plus.ios.deleteObject(PHPhotoLibrary);
  15. return result;
  16. }
  17. function camera() {
  18. var result = 0;
  19. var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
  20. var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
  21. if (authStatus === 0) {
  22. result = null;
  23. } else if (authStatus == 3) {
  24. result = 1;
  25. } else {
  26. result = 0;
  27. }
  28. plus.ios.deleteObject(AVCaptureDevice);
  29. return result;
  30. }
  31. function location() {
  32. var result = 0;
  33. var cllocationManger = plus.ios.import("CLLocationManager");
  34. var enable = cllocationManger.locationServicesEnabled();
  35. var status = cllocationManger.authorizationStatus();
  36. if (!enable) {
  37. result = 2;
  38. } else if (status === 0) {
  39. result = null;
  40. } else if (status === 3 || status === 4) {
  41. result = 1;
  42. } else {
  43. result = 0;
  44. }
  45. plus.ios.deleteObject(cllocationManger);
  46. return result;
  47. }
  48. function push() {
  49. var result = 0;
  50. var UIApplication = plus.ios.import("UIApplication");
  51. var app = UIApplication.sharedApplication();
  52. var enabledTypes = 0;
  53. if (app.currentUserNotificationSettings) {
  54. var settings = app.currentUserNotificationSettings();
  55. enabledTypes = settings.plusGetAttribute("types");
  56. if (enabledTypes == 0) {
  57. result = 0;
  58. console.log("推送权限没有开启");
  59. } else {
  60. result = 1;
  61. console.log("已经开启推送功能!")
  62. }
  63. plus.ios.deleteObject(settings);
  64. } else {
  65. enabledTypes = app.enabledRemoteNotificationTypes();
  66. if (enabledTypes == 0) {
  67. result = 3;
  68. console.log("推送权限没有开启!");
  69. } else {
  70. result = 4;
  71. console.log("已经开启推送功能!")
  72. }
  73. }
  74. plus.ios.deleteObject(app);
  75. plus.ios.deleteObject(UIApplication);
  76. return result;
  77. }
  78. function contact() {
  79. var result = 0;
  80. var CNContactStore = plus.ios.import("CNContactStore");
  81. var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
  82. if (cnAuthStatus === 0) {
  83. result = null;
  84. } else if (cnAuthStatus == 3) {
  85. result = 1;
  86. } else {
  87. result = 0;
  88. }
  89. plus.ios.deleteObject(CNContactStore);
  90. return result;
  91. }
  92. function record() {
  93. var result = null;
  94. var avaudiosession = plus.ios.import("AVAudioSession");
  95. var avaudio = avaudiosession.sharedInstance();
  96. var status = avaudio.recordPermission();
  97. if (status === 1970168948) {
  98. result = null;
  99. } else if (status === 1735552628) {
  100. result = 1;
  101. } else {
  102. result = 0;
  103. }
  104. plus.ios.deleteObject(avaudiosession);
  105. return result;
  106. }
  107. function calendar() {
  108. var result = null;
  109. var EKEventStore = plus.ios.import("EKEventStore");
  110. var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
  111. if (ekAuthStatus == 3) {
  112. result = 1;
  113. console.log("日历权限已经开启");
  114. } else {
  115. console.log("日历权限没有开启");
  116. }
  117. plus.ios.deleteObject(EKEventStore);
  118. return result;
  119. }
  120. function memo() {
  121. var result = null;
  122. var EKEventStore = plus.ios.import("EKEventStore");
  123. var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
  124. if (ekAuthStatus == 3) {
  125. result = 1;
  126. console.log("备忘录权限已经开启");
  127. } else {
  128. console.log("备忘录权限没有开启");
  129. }
  130. plus.ios.deleteObject(EKEventStore);
  131. return result;
  132. }
  133. function requestIOS(permissionID) {
  134. return new Promise((resolve, reject) => {
  135. switch (permissionID) {
  136. case "push":
  137. resolve(push());
  138. break;
  139. case "location":
  140. resolve(location());
  141. break;
  142. case "record":
  143. resolve(record());
  144. break;
  145. case "camera":
  146. resolve(camera());
  147. break;
  148. case "album":
  149. resolve(album());
  150. break;
  151. case "contact":
  152. resolve(contact());
  153. break;
  154. case "calendar":
  155. resolve(calendar());
  156. break;
  157. case "memo":
  158. resolve(memo());
  159. break;
  160. default:
  161. resolve(0);
  162. break;
  163. }
  164. });
  165. }
  166. function requestAndroid(permissionID) {
  167. return new Promise((resolve, reject) => {
  168. plus.android.requestPermissions(
  169. [permissionID],
  170. function(resultObj) {
  171. var result = 0;
  172. for (var i = 0; i < resultObj.granted.length; i++) {
  173. var grantedPermission = resultObj.granted[i];
  174. console.log('已获取的权限:' + grantedPermission);
  175. result = 1
  176. }
  177. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  178. var deniedPresentPermission = resultObj.deniedPresent[i];
  179. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  180. result = 0
  181. }
  182. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  183. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  184. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  185. result = -1
  186. }
  187. resolve(result);
  188. },
  189. function(error) {
  190. console.log('result error: ' + error.message)
  191. resolve({
  192. code: error.code,
  193. message: error.message
  194. });
  195. }
  196. );
  197. });
  198. }
  199. function gotoAppPermissionSetting() {
  200. if (permission.isIOS) {
  201. var UIApplication = plus.ios.import("UIApplication");
  202. var application2 = UIApplication.sharedApplication();
  203. var NSURL2 = plus.ios.import("NSURL");
  204. var setting2 = NSURL2.URLWithString("app-settings:");
  205. application2.openURL(setting2);
  206. plus.ios.deleteObject(setting2);
  207. plus.ios.deleteObject(NSURL2);
  208. plus.ios.deleteObject(application2);
  209. } else {
  210. var Intent = plus.android.importClass("android.content.Intent");
  211. var Settings = plus.android.importClass("android.provider.Settings");
  212. var Uri = plus.android.importClass("android.net.Uri");
  213. var mainActivity = plus.android.runtimeMainActivity();
  214. var intent = new Intent();
  215. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  216. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  217. intent.setData(uri);
  218. mainActivity.startActivity(intent);
  219. }
  220. }
  221. const permission = {
  222. get isIOS(){
  223. return typeof isIOS === 'boolean' ? isIOS : (isIOS = uni.getSystemInfoSync().platform === 'ios')
  224. },
  225. requestIOS: requestIOS,
  226. requestAndroid: requestAndroid,
  227. gotoAppSetting: gotoAppPermissionSetting
  228. }
  229. module.exports = permission