util.ts 830 B

123456789101112131415161718192021222324252627282930313233
  1. export const authorizeRecord = async () => {
  2. let isAuth = await new Promise((r) => {
  3. wx.authorize({
  4. scope: 'scope.record',
  5. success: () => r(true),
  6. fail: () => r(false)
  7. })
  8. })
  9. if (isAuth) return true
  10. let res = await new Promise<WechatMiniprogram.ShowModalSuccessCallbackResult | boolean>(r => {
  11. wx.showModal({
  12. title: '提示',
  13. content: '您未授权录音,说话功能将无法使用',
  14. showCancel: true,
  15. confirmText: "授权",
  16. confirmColor: "#52a2d8",
  17. success: res => r(res),
  18. fail: () => r(false)
  19. })
  20. })
  21. if (!res || (typeof res === 'object' && res.cancel)) return;
  22. isAuth = await new Promise((r) => {
  23. wx.openSetting({
  24. success: res => r(res.authSetting['scope.record']),
  25. fail: () => r(false)
  26. })
  27. })
  28. return isAuth
  29. }