index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. export * from './api/index'
  2. import util from '../../../utils/util'
  3. import api from '../../../config/api'
  4. export const authorizeRecord = async () => {
  5. let isAuth = await new Promise((r, j) => {
  6. wx.authorize({
  7. scope: 'scope.record',
  8. success: () => r(true),
  9. fail: () => r(false)
  10. })
  11. })
  12. if (isAuth) return true
  13. let res = await new Promise(r => {
  14. wx.showModal({
  15. title: '提示',
  16. content: '您未授权录音,说话功能将无法使用',
  17. showCancel: true,
  18. confirmText: "授权",
  19. confirmColor: "#52a2d8",
  20. success: res => r(res),
  21. fail: () => r(false)
  22. })
  23. })
  24. if (!res || res.cancel) return;
  25. isAuth = await new Promise((r) => {
  26. wx.openSetting({
  27. success: res => r(res.authSetting['scope.record']),
  28. fail: () => r(false)
  29. })
  30. })
  31. return isAuth
  32. }
  33. const agetUserInfo = async () => {
  34. const res = await util.request(api.UserInfo)
  35. if (res.errno === 401) {
  36. return {
  37. userId: UNLOGIN,
  38. avatar: ''
  39. }
  40. } else {
  41. const data = res.data
  42. data.region = data.city ? data.city.split(',') : []
  43. data.birthday = data.birthday || '1990-01-01'
  44. return data
  45. }
  46. }
  47. export const getUserInfo = async () => {
  48. let userInfo = wx.getStorageSync('userInfo');
  49. let token = wx.getStorageSync('token');
  50. if (userInfo && userInfo.userId && token) {
  51. let info = await agetUserInfo()
  52. return {
  53. ...info,
  54. ...userInfo,
  55. avatarUrl: info.avatar
  56. };
  57. } else {
  58. return {
  59. userId: UNLOGIN,
  60. avatar: ''
  61. }
  62. }
  63. }
  64. export const voiceFactory = (params) => {
  65. let ring = false
  66. const start = () => {
  67. if (!ring) {
  68. ring = true
  69. // getApp().globalData.roomId = 'KKV4_RID_' + params.roomId.toString()
  70. getApp().globalData.roomId = '266' + params.roomId.toString()
  71. getApp().setVoiceProps({
  72. ...params.user,
  73. sdkAppID: params.sdkAppId.toString(),
  74. sig: params.sig,
  75. // sdkAppID: '1400431163',
  76. // sig: "eJyrVgrxCdYrSy1SslIy0jNQ0gHzM1NS80oy0zLBwiYmUNHilOzEgoLMFCUrQxMDAxNjQ0MzY4hMSWZuKlDUzMzEyMzMwNIcIppaUZBZBBS3MAMqhxqRmQ400cnEqyAkKDjLvbg4wjPP1CLA3dI5wzEzpKgoq7jUIjHEN9M7KsDM1C8lx9NWqRYAq4gvLA__",
  77. noMute: false,
  78. action: 'startCall'
  79. })
  80. }
  81. }
  82. const changeMute = (noMute) => {
  83. getApp().setVoiceProps({
  84. noMute
  85. })
  86. }
  87. const stop = () => {
  88. console.error('stop', ring)
  89. if (ring) {
  90. ring = false
  91. getApp().setVoiceProps({
  92. noMute: true,
  93. action: 'stopCall'
  94. })
  95. }
  96. }
  97. return { start, stop, ring, changeMute }
  98. }