permission.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <template>
  2. <cwg-page-wrapper class="permission-page">
  3. <view class="content">
  4. <view class="permission-list">
  5. <view v-for="item in permissionList" :key="item.key" class="permission-item"
  6. @click="onPermissionClick(item)">
  7. <view class="item-left">
  8. <view class="item-info">
  9. <view class="item-title">{{ item.title }}</view>
  10. <view class="item-desc">{{ item.desc }}
  11. <text class="item-description" v-if="item.description"
  12. @click.stop="goDetail(item)">《{{ item.description }}》</text>
  13. </view>
  14. </view>
  15. </view>
  16. <view class="item-right">
  17. <view :class="['status', item.status]">
  18. {{ item.status }}
  19. </view>
  20. <cwg-icon color="#817f7f" icon="chevron-right" />
  21. </view>
  22. </view>
  23. </view>
  24. <view class="tips">
  25. <text class="tips-text">
  26. {{ t('mine.p14') }}
  27. </text>
  28. </view>
  29. </view>
  30. </cwg-page-wrapper>
  31. </template>
  32. <script setup lang="ts">
  33. import { ref, onMounted } from 'vue'
  34. import { permission } from '@/js_sdk/wa-permission/permission.js'
  35. import { onLoad } from '@dcloudio/uni-app'
  36. import useRouter from "@/hooks/useRouter";
  37. import { useI18n } from "vue-i18n";
  38. const { t } = useI18n();
  39. const router = useRouter();
  40. // #ifdef APP-PLUS
  41. let isIos = false
  42. try {
  43. isIos = (plus.os.name === "iOS")
  44. } catch (e) {
  45. console.error('检测平台失败:', e)
  46. }
  47. // #endif
  48. /* =======================
  49. * 权限列表配置(核心)
  50. * ======================= */
  51. const permissionList = ref([
  52. // {
  53. // key: 'location',
  54. // title: t('mine.p1'),
  55. // desc: t('mine.p2'),
  56. // icon: 'location',
  57. // iconBg: 'location',
  58. // iosPermission: 'location',
  59. // androidPermission: 'android.permission.ACCESS_FINE_LOCATION',
  60. // status: t('mine.p13'),
  61. // type: 4,
  62. // description: t('mine.p3')
  63. // },
  64. {
  65. key: 'camera',
  66. title: t('mine.p4'),
  67. desc: t('mine.p5'),
  68. icon: 'camera',
  69. iconBg: 'camera',
  70. iosPermission: 'camera',
  71. androidPermission: 'android.permission.CAMERA',
  72. status: t('mine.p13'),
  73. type: 2,
  74. description: t('mine.p6')
  75. },
  76. // {
  77. // key: 'microphone',
  78. // title: '麦克风',
  79. // desc: '录制音频',
  80. // icon: 'sound',
  81. // iconBg: 'microphone',
  82. // iosPermission: 'record',
  83. // androidPermission: 'android.permission.RECORD_AUDIO',
  84. // status: t('mine.p13')
  85. // },
  86. {
  87. key: 'album',
  88. title: t('mine.p7'),
  89. desc: t('mine.p8'),
  90. icon: 'image',
  91. iconBg: 'album',
  92. iosPermission: 'photoLibrary',
  93. androidPermission: 'android.permission.READ_EXTERNAL_STORAGE',
  94. status: t('mine.p13'),
  95. type: 3,
  96. description: t('mine.p9')
  97. },
  98. {
  99. key: 'clipboard',
  100. title: t('mine.p10'),
  101. desc: t('mine.p11'),
  102. icon: 'list',
  103. iconBg: 'clipboard',
  104. iosPermission: null,
  105. androidPermission: null,
  106. status: t('mine.p12'),
  107. }
  108. ])
  109. const goDetail = (item: any) => {
  110. let type = item.type;
  111. // console.log(item, 111111);
  112. router.push({
  113. path: "/pages/mine/privacy",
  114. query: {
  115. type
  116. },
  117. });
  118. }
  119. /* =======================
  120. * 点击入口 - 检查权限并引导授权
  121. * ======================= */
  122. const onPermissionClick = async (item: any) => {
  123. // 粘贴板无需授权,直接提示
  124. if (!item.iosPermission && !item.androidPermission) {
  125. uni.showToast({
  126. title: '该权限无需授权',
  127. icon: 'success'
  128. })
  129. return
  130. }
  131. // #ifdef APP-PLUS
  132. permission.gotoAppPermissionSetting()
  133. // #endif
  134. // #ifndef APP-PLUS
  135. uni.showToast({
  136. title: '当前平台无需授权',
  137. icon: 'success'
  138. })
  139. // #endif
  140. }
  141. /* =======================
  142. * 初始化检查权限 - 遍历所有权限进行检查
  143. * ======================= */
  144. const checkAllPermissions = async () => {
  145. // #ifdef APP-PLUS
  146. try {
  147. for (const item of permissionList.value) {
  148. // 粘贴板无需检查
  149. if (!item.iosPermission && !item.androidPermission) {
  150. item.status = t('mine.p12')
  151. continue
  152. }
  153. if (isIos) {
  154. // iOS 权限检查
  155. const hasPermission = permission.judgeIosPermission(item.iosPermission)
  156. item.status = hasPermission ? t('mine.p12') : t('mine.p13')
  157. // console.log(`${item.title} iOS 初始状态:`, item.status)
  158. } else {
  159. // Android 权限请求
  160. const result = await permission.checkAndroidPermission(item.androidPermission)
  161. // console.log(`${item.title} Android 权限结果:`, result)
  162. if (result === 1) {
  163. item.status = t('mine.p12')
  164. } else if (result === -1) {
  165. item.status = t('mine.p13')
  166. } else {
  167. item.status = t('mine.p13')
  168. }
  169. }
  170. }
  171. } catch (error) {
  172. console.error('检查权限失败:', error)
  173. }
  174. // #endif
  175. // #ifndef APP-PLUS
  176. // 非 APP 平台全部标记为已授权
  177. permissionList.value.forEach(item => {
  178. item.status = t('mine.p12')
  179. })
  180. // #endif
  181. }
  182. onMounted(() => {
  183. checkAllPermissions()
  184. })
  185. </script>
  186. <style lang="scss" scoped>
  187. @import "@/uni.scss";
  188. .permission-page {
  189. min-height: 100vh;
  190. background-color: #f5f5f5;
  191. padding: px2rpx(12) !important;
  192. .content {
  193. .permission-list {
  194. background-color: #fff;
  195. border-radius: px2rpx(16);
  196. overflow: hidden;
  197. .permission-item {
  198. display: flex;
  199. align-items: center;
  200. justify-content: space-between;
  201. padding: px2rpx(32) px2rpx(12);
  202. border-bottom: 1px solid #f0f0f0;
  203. &:last-child {
  204. border-bottom: none;
  205. }
  206. &:active {
  207. background-color: #f8f8f8;
  208. }
  209. .item-left {
  210. display: flex;
  211. align-items: center;
  212. flex: 1;
  213. .icon-wrapper {
  214. width: px2rpx(44);
  215. height: px2rpx(44);
  216. border-radius: px2rpx(8);
  217. display: flex;
  218. align-items: center;
  219. justify-content: center;
  220. margin-right: px2rpx(24);
  221. &.location {
  222. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  223. }
  224. &.camera {
  225. background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  226. }
  227. &.microphone {
  228. background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  229. }
  230. &.album {
  231. background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
  232. }
  233. &.clipboard {
  234. background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
  235. }
  236. }
  237. .item-info {
  238. flex: 1;
  239. .item-title {
  240. font-size: px2rpx(18);
  241. font-weight: 500;
  242. color: #333;
  243. display: block;
  244. margin-bottom: px2rpx(8);
  245. }
  246. .item-desc {
  247. font-size: px2rpx(16);
  248. color: #999;
  249. display: block;
  250. }
  251. .item-description {
  252. font-size: px2rpx(14);
  253. color: var(--main-yellow);
  254. }
  255. }
  256. }
  257. .item-right {
  258. display: flex;
  259. align-items: center;
  260. gap: px2rpx(4);
  261. .status {
  262. font-size: px2rpx(16);
  263. padding: px2rpx(8) px2rpx(12);
  264. border-radius: px2rpx(4);
  265. &.未授权 {
  266. color: #999;
  267. background-color: #f5f5f5;
  268. }
  269. &.已授权 {
  270. color: #52c41a;
  271. background-color: #f6ffed;
  272. }
  273. &.已拒绝 {
  274. color: #ff4d4f;
  275. background-color: #fff1f0;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. .tips {
  282. margin-top: px2rpx(24);
  283. padding: px2rpx(24);
  284. background-color: #fffbe6;
  285. border-radius: px2rpx(16);
  286. border: 1px solid #ffe58f;
  287. .tips-text {
  288. font-size: px2rpx(12);
  289. color: #d48806;
  290. line-height: 1.6;
  291. }
  292. }
  293. }
  294. }
  295. </style>