permission.js 8.0 KB

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