yk-authpup.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <template>
  2. <view v-if="showPopup&&popupShow" class="uni-popup" :style="{top:isNativeHead?'':StatusBar}" @click="close(true)">
  3. <view :class="[type, ani, animation ? 'ani' : '']" class="uni-custom uni-popup__wrapper">
  4. <view class="uni-popup__wrapper-box">
  5. <view class="title">{{authList[permissionID].title}}</view>
  6. <view class="content">{{authList[permissionID].content}}</view>
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. name: 'YkAuthpup',
  14. props: {
  15. // 开启动画
  16. animation: {
  17. type: Boolean,
  18. default: true
  19. },
  20. type: {
  21. type: String,
  22. default: 'top'
  23. },
  24. show: {
  25. type: Boolean,
  26. default: true
  27. },
  28. //是否是原生头部
  29. isNativeHead:{
  30. type: Boolean,
  31. default: false
  32. },
  33. permissionID: {
  34. type: [String, Number],
  35. default: ''
  36. }
  37. },
  38. data() {
  39. return {
  40. ani: '',
  41. showPopup: false,
  42. popupShow: true,
  43. StatusBar:'',
  44. refuseNum:'',//拒绝次数,
  45. authList: {
  46. 'WRITE_EXTERNAL_STORAGE': {
  47. title: "先见平台对存储空间/照片权限申请说明",
  48. content: "便于您使用该功能上传您的照片/图片/视频及用于更换头像、意见反馈、保存相册、分享、下载与律师或客服沟通等场景中读取和写入相册和文件内容。"
  49. },
  50. 'ACCESS_FINE_LOCATION': {
  51. title: "先见平台对地理位置权限申请说明",
  52. content: "便于应用程序可以提供基于位置的服务、定位导航、附近搜索等功能。"
  53. },
  54. 'CAMERA':{
  55. title: "先见平台对相机/摄像头权限申请说明",
  56. content: "便于您使用该功能拍照上传您的照片/视频及用于更换头像、意见反馈、保存相册、发布动态、下载与律师或客服沟通等场景中使用"
  57. },
  58. 'RECORD_AUDIO':{
  59. title: "先见平台对麦克风权限申请说明",
  60. content: "便于您使用该功能进行录音、语音通话、发布语音、与律师或客服语音沟通等场景中使用"
  61. },
  62. 'CALL_PHONE': {
  63. title: "先见平台对拨打/管理电话权限申请说明",
  64. content: "便于您使用该功能联系律师或客服、业务经理与联系等场景下使用"
  65. }
  66. }
  67. }
  68. },
  69. created() {
  70. // #ifdef APP-PLUS
  71. this.getSystemInfo();
  72. // #endif
  73. },
  74. methods: {
  75. //获取状态栏高度
  76. getSystemInfo() {
  77. let _this = this;
  78. uni.getSystemInfo({
  79. success: function(e) {
  80. _this.StatusBar = e.statusBarHeight + 'px'; //用于自定义头部时,给手机状态栏留出位置,可通过isNativeHead这个参数控制
  81. }
  82. })
  83. },
  84. open() {
  85. this.requestPermissions(this.permissionID);
  86. },
  87. close(type) {
  88. this.ani = '';
  89. this.showPopup = false;
  90. },
  91. //权限检测
  92. requestPermissions(permissionID) {
  93. let _this = this;
  94. // #ifdef APP-PLUS
  95. //判断安卓与ios设备
  96. if (plus.os.name == 'Android') {
  97. let _permissionID = 'android.permission.' + permissionID;
  98. plus.android.checkPermission(_permissionID,
  99. granted => {
  100. if (granted.checkResult == -1) {
  101. //还未授权当前查询的权限,打开权限申请目的自定义弹框
  102. _this.showPopup = true;
  103. _this.popupShow = true
  104. _this.$nextTick(() => {
  105. setTimeout(() => {
  106. _this.ani = 'uni-' + _this.type
  107. },30)
  108. })
  109. }
  110. },
  111. error => {
  112. console.log(error.message);
  113. }
  114. );
  115. plus.android.requestPermissions([_permissionID],
  116. (e) => {
  117. //关闭权限申请目的自定义弹框
  118. _this.popupShow = true
  119. console.log(e,'kkkkkkkk')
  120. if (e.granted.length > 0) {
  121. //当前查询权限已授权,此时可以通知页面执行接下来的操作
  122. _this.popupShow = false
  123. _this.$emit('changeAuth');
  124. }
  125. if (e.deniedAlways.length > 0) {
  126. _this.showPopup = true
  127. _this.$nextTick(() => {
  128. setTimeout(() => {
  129. _this.ani = 'uni-' + _this.type
  130. },30)
  131. })
  132. //当前查询权限已被永久禁用,此时需要引导用户跳转手机系统设置去开启
  133. uni.showModal({
  134. title: '温馨提示',
  135. content: '还没有该权限,立即去设置开启?',
  136. cancelText: "取消",
  137. confirmText: "去设置",
  138. showCancel: true,
  139. confirmColor: '#000',
  140. cancelColor: '#666',
  141. success: (res) => {
  142. if (res.confirm) {
  143. _this.goSetting();
  144. }
  145. },
  146. complete() {
  147. _this.popupShow = false
  148. }
  149. })
  150. }
  151. })
  152. } else {
  153. //IOS不需要添加自定义弹框来描述权限目的,因为在配置文件的隐私信息访问的许可描述里可添加
  154. //正常可以直接调用uni的API调起权限询问弹框使用各种权限,下面的判断使用场景主要是在IOS禁用某权限后,这个可以判断有无权限,进而引导用户跳转设置开启,仅列出了位置、相册、通讯录、相机、录音等权限,其他IOS权限可具体参考 https://ext.dcloud.net.cn/plugin?id=15787
  155. let result = 0;
  156. if (permissionID == 'ACCESS_FINE_LOCATION') {
  157. //IOS检测位置权限
  158. let cLLocationManager = plus.ios.importClass("CLLocationManager"),
  159. authStatus = cLLocationManager.authorizationStatus(),
  160. enable = cLLocationManager.locationServicesEnabled();
  161. if (enable && authStatus != 2) {
  162. result = 1;
  163. } else {
  164. result = 0;
  165. }
  166. plus.ios.deleteObject(cLLocationManager);
  167. } else if (permissionID == 'WRITE_EXTERNAL_STORAGE') {
  168. //IOS检测相册权限
  169. let PHPhotoLibrary = plus.ios.importClass("PHPhotoLibrary"),
  170. authStatus = PHPhotoLibrary.authorizationStatus();
  171. if (authStatus === 3) {
  172. result = 1;
  173. } else {
  174. result = 0;
  175. }
  176. plus.ios.deleteObject(PHPhotoLibrary);
  177. } else if (permissionID == 'CAMERA') {
  178. //IOS检测相机/摄像头权限
  179. let avCaptureDevice = plus.ios.importClass("AVCaptureDevice"),
  180. authStatus = avCaptureDevice.authorizationStatusForMediaType("vide");
  181. if (authStatus === 3) {
  182. result = 1;
  183. } else {
  184. result = 0;
  185. }
  186. plus.ios.deleteObject(avCaptureDevice);
  187. } else if (permissionID == 'CALL_PHONE') {
  188. //IOS检测通讯录权限
  189. let contactStore = plus.ios.importClass("CNContactStore"),
  190. authStatus = contactStore.authorizationStatusForEntityType(0);
  191. if (authStatus === 3) {
  192. result = 1;
  193. } else {
  194. result = 0;
  195. }
  196. plus.ios.deleteObject(contactStore);
  197. }else if(permissionID == 'RECORD_AUDIO'){
  198. //IOS检测麦克风权限
  199. let aVAudioSession = plus.ios.importClass("AVAudioSession"),
  200. aVAudio = aVAudioSession.sharedInstance(),
  201. authStatus = aVAudio.recordPermission();
  202. if ([1684369017, 1970168948].includes(authStatus)) {
  203. result = 0;
  204. } else {
  205. result = 1;
  206. }
  207. plus.ios.deleteObject(aVAudioSession);
  208. }
  209. if (result) {
  210. //当前查询权限已授权,此时可以通知页面执行接下来的操作
  211. that.$emit('changeAuth')
  212. } else {
  213. //当前查询的权限已禁用,引导用户跳转手机系统设置去开启
  214. uni.showModal({
  215. title: '温馨提示',
  216. content: '还没有该权限,立即去设置开启?',
  217. cancelText: "取消",
  218. confirmText: "去设置",
  219. showCancel: true,
  220. confirmColor: '#000',
  221. cancelColor: '#666',
  222. success: (res) => {
  223. if (res.confirm) {
  224. _this.goSetting();
  225. }
  226. }
  227. })
  228. }
  229. }
  230. // #endif
  231. },
  232. //跳转手机系统设置
  233. goSetting() {
  234. if (plus.os.name == "iOS") {
  235. var UIApplication = plus.ios.import("UIApplication");
  236. var application2 = UIApplication.sharedApplication();
  237. var NSURL2 = plus.ios.import("NSURL");
  238. var setting2 = NSURL2.URLWithString("app-settings:");
  239. application2.openURL(setting2);
  240. plus.ios.deleteObject(setting2);
  241. plus.ios.deleteObject(NSURL2);
  242. plus.ios.deleteObject(application2);
  243. } else {
  244. var Intent = plus.android.importClass("android.content.Intent");
  245. var Settings = plus.android.importClass("android.provider.Settings");
  246. var Uri = plus.android.importClass("android.net.Uri");
  247. var mainActivity = plus.android.runtimeMainActivity();
  248. var intent = new Intent();
  249. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  250. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  251. intent.setData(uri);
  252. mainActivity.startActivity(intent);
  253. }
  254. }
  255. }
  256. }
  257. </script>
  258. <style lang="scss">
  259. .uni-popup {
  260. position: fixed;
  261. top: 0;
  262. bottom: 0;
  263. left: 0;
  264. right: 0;
  265. z-index: 999999;
  266. overflow: hidden;
  267. &__wrapper {
  268. position: absolute;
  269. z-index: 999;
  270. /* #ifndef APP-NVUE */
  271. box-sizing: border-box;
  272. /* #endif */
  273. &.ani {
  274. /* #ifndef APP-NVUE */
  275. transition: all 0.3s;
  276. /* #endif */
  277. }
  278. &.top {
  279. top: 0;
  280. width:705rpx;
  281. /* #ifdef APP-NVUE */
  282. left:22.5rpx;
  283. /* #endif */
  284. /* #ifndef APP-NVUE */
  285. left:0;
  286. transform: translateY(-705rpx);
  287. /* #endif */
  288. }
  289. &-box {
  290. position: relative;
  291. /* #ifndef APP-NVUE */
  292. box-sizing: border-box;
  293. /* #endif */
  294. }
  295. &.uni-custom {
  296. & .uni-popup__wrapper-box {
  297. width: 705rpx;
  298. /* #ifndef APP-NVUE */
  299. margin: 0 22.5rpx;
  300. /* #endif */
  301. padding: 30upx;
  302. background: #fff;
  303. border: solid 2rpx #ddd;
  304. /* #ifndef APP-NVUE */
  305. box-sizing: border-box;
  306. /* #endif */
  307. border-radius: 16rpx;
  308. .title{
  309. font-size: 32rpx;
  310. font-weight: bold;
  311. }
  312. .content{
  313. margin-top: 16rpx;
  314. line-height: 1.6;
  315. }
  316. }
  317. &.top{
  318. & .uni-popup__wrapper-box {
  319. width: 705rpx;
  320. }
  321. }
  322. }
  323. &.uni-top{
  324. transform: translateY(0);
  325. }
  326. }
  327. }
  328. </style>