permission.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. console.log('permissionStatus:' + status);
  98. if (status === 1970168948) {
  99. result = null;
  100. } else if (status === 1735552628) {
  101. result = 1;
  102. } else {
  103. result = 0;
  104. }
  105. plus.ios.deleteObject(avaudiosession);
  106. return result;
  107. }
  108. function calendar() {
  109. var result = null;
  110. var EKEventStore = plus.ios.import('EKEventStore');
  111. var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
  112. if (ekAuthStatus == 3) {
  113. result = 1;
  114. console.log('日历权限已经开启');
  115. } else {
  116. console.log('日历权限没有开启');
  117. }
  118. plus.ios.deleteObject(EKEventStore);
  119. return result;
  120. }
  121. function memo() {
  122. var result = null;
  123. var EKEventStore = plus.ios.import('EKEventStore');
  124. var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
  125. if (ekAuthStatus == 3) {
  126. result = 1;
  127. console.log('备忘录权限已经开启');
  128. } else {
  129. console.log('备忘录权限没有开启');
  130. }
  131. plus.ios.deleteObject(EKEventStore);
  132. return result;
  133. }
  134. function requestIOS(permissionID) {
  135. return new Promise((resolve, reject) => {
  136. switch (permissionID) {
  137. case 'push':
  138. resolve(push());
  139. break;
  140. case 'location':
  141. resolve(location());
  142. break;
  143. case 'record':
  144. resolve(record());
  145. break;
  146. case 'camera':
  147. resolve(camera());
  148. break;
  149. case 'album':
  150. resolve(album());
  151. break;
  152. case 'contact':
  153. resolve(contact());
  154. break;
  155. case 'calendar':
  156. resolve(calendar());
  157. break;
  158. case 'memo':
  159. resolve(memo());
  160. break;
  161. default:
  162. resolve(0);
  163. break;
  164. }
  165. });
  166. }
  167. function requestAndroid(permissionID) {
  168. return new Promise((resolve, reject) => {
  169. plus.android.requestPermissions(
  170. [permissionID],
  171. function (resultObj) {
  172. var result = 0;
  173. for (var i = 0; i < resultObj.granted.length; i++) {
  174. var grantedPermission = resultObj.granted[i];
  175. console.log('已获取的权限:' + grantedPermission);
  176. result = 1;
  177. }
  178. for (var i = 0; i < resultObj.deniedPresent.length; i++) {
  179. var deniedPresentPermission = resultObj.deniedPresent[i];
  180. console.log('拒绝本次申请的权限:' + deniedPresentPermission);
  181. result = 0;
  182. }
  183. for (var i = 0; i < resultObj.deniedAlways.length; i++) {
  184. var deniedAlwaysPermission = resultObj.deniedAlways[i];
  185. console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
  186. result = -1;
  187. }
  188. resolve(result);
  189. },
  190. function (error) {
  191. console.log('result error: ' + error.message);
  192. resolve({
  193. code: error.code,
  194. message: error.message,
  195. });
  196. },
  197. );
  198. });
  199. }
  200. function gotoAppPermissionSetting() {
  201. if (permission.isIOS) {
  202. var UIApplication = plus.ios.import('UIApplication');
  203. var application2 = UIApplication.sharedApplication();
  204. var NSURL2 = plus.ios.import('NSURL');
  205. var setting2 = NSURL2.URLWithString('app-settings:');
  206. application2.openURL(setting2);
  207. plus.ios.deleteObject(setting2);
  208. plus.ios.deleteObject(NSURL2);
  209. plus.ios.deleteObject(application2);
  210. } else {
  211. var Intent = plus.android.importClass('android.content.Intent');
  212. var Settings = plus.android.importClass('android.provider.Settings');
  213. var Uri = plus.android.importClass('android.net.Uri');
  214. var mainActivity = plus.android.runtimeMainActivity();
  215. var intent = new Intent();
  216. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  217. var uri = Uri.fromParts('package', mainActivity.getPackageName(), null);
  218. intent.setData(uri);
  219. mainActivity.startActivity(intent);
  220. }
  221. }
  222. const permission = {
  223. get isIOS() {
  224. return typeof isIOS === 'boolean'
  225. ? isIOS
  226. : (isIOS = uni.getSystemInfoSync().platform === 'ios');
  227. },
  228. requestIOS: requestIOS,
  229. requestAndroid: requestAndroid,
  230. gotoAppSetting: gotoAppPermissionSetting,
  231. };
  232. export default permission;