permission.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. type passcode =
  2. | 'scope.addPhoneCalendar'
  3. | 'scope.addPhoneContact'
  4. | 'scope.address'
  5. | 'scope.bluetooth'
  6. | 'scope.camera'
  7. | 'scope.invoice'
  8. | 'scope.invoiceTitle'
  9. | 'scope.record'
  10. | 'scope.userInfo'
  11. | 'scope.userLocation'
  12. | 'scope.werun'
  13. | 'scope.writePhotosAlbum'
  14. export const getPermission = ({ code, name }: { code: passcode; name: string }) => {
  15. return new Promise<void>((resolve, reject) => {
  16. wx.getSetting({
  17. success: (res) => {
  18. if (res.authSetting[code] === false) {
  19. wx.showModal({
  20. title: `获取${name}失败`,
  21. content: `获取${name}失败,请在【右上角】-小程序【设置】项中,将【${name}】开启。`,
  22. confirmText: '去设置',
  23. confirmColor: '#FA550F',
  24. cancelColor: '取消',
  25. success(res) {
  26. if (res.confirm) {
  27. wx.openSetting({
  28. success(settinRes) {
  29. if (settinRes.authSetting[code] === true) {
  30. resolve()
  31. } else {
  32. console.warn('用户未打开权限', name, code)
  33. reject()
  34. }
  35. },
  36. })
  37. } else {
  38. reject()
  39. }
  40. },
  41. fail() {
  42. reject()
  43. },
  44. })
  45. } else {
  46. resolve()
  47. }
  48. },
  49. fail() {
  50. reject()
  51. },
  52. })
  53. })
  54. }