permission.js 8.0 KB

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