chat.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // comps/chat/chat.js
  2. let msgType = require("../../utils/msgtype");
  3. let msgStorage = require("../../utils/msgstorage");
  4. let WebIM = require("../../utils/WebIM")["default"];
  5. let disp = require("../../utils/broadcast");
  6. let emediaState = require("../../utils/emediaState");
  7. Component({
  8. /**
  9. * 组件的属性列表
  10. */
  11. properties: {
  12. username: {
  13. type: Object,
  14. value: {},
  15. },
  16. chatType: {
  17. type: String,
  18. value: msgType.chatType.SINGLE_CHAT,
  19. },
  20. },
  21. /**
  22. * 组件的初始数据
  23. */
  24. data: {
  25. __comps__: {
  26. msglist: null,
  27. inputbar: null,
  28. audio: null,
  29. },
  30. pubUrl: '',
  31. subUrl: '',
  32. showEmedia: false,
  33. showmultiEmedia: false,
  34. showSingleEmedia: false,
  35. showEmediaInvite: false,
  36. action: null,
  37. emediaAction: null,
  38. multiEmediaVisible: 'block',
  39. inputbarVisible: 'block',
  40. confrId: '',
  41. groupId: '',
  42. singleEmediaType: 1,
  43. },
  44. /**
  45. * 组件的方法列表
  46. */
  47. methods: {
  48. toggleRecordModal() {
  49. this.data.__comps__.audio.toggleRecordModal();
  50. },
  51. normalScroll() {
  52. this.data.__comps__.msglist.normalScroll();
  53. this.data.__comps__.inputbar.cancelEmoji();
  54. },
  55. shortScroll() {
  56. this.data.__comps__.msglist.shortScroll();
  57. },
  58. saveSendMsg(evt) {
  59. msgStorage.saveMsg(evt.detail.msg, evt.detail.type);
  60. // this.data.__comps__.inputbar.cancelEmoji();
  61. },
  62. getMore() {
  63. this.selectComponent('#chat-msglist').getHistoryMsg()
  64. },
  65. callVideo(callType, type) {
  66. if (emediaState.callStatus > 0) {
  67. console.log('正在通话中')
  68. }
  69. const value = '邀请您进行视频通话'
  70. const callId = wx.WebIM.conn.getUniqueId().toString();
  71. const channelName = emediaState.confr.channelName || Math.uuid(8)
  72. let username = this.data.username.your;
  73. if (callType === 'contact') {
  74. this.sendInviteMsg([username], channelName, callId, type)
  75. emediaState.setConf({
  76. channel: channelName,
  77. token: '',
  78. type: type,
  79. callId: callId,
  80. callerDevId: wx.WebIM.conn.context.jid.clientResource,
  81. calleeDevId: null,
  82. confrName: '',
  83. callerIMName: this.data.username.myName,
  84. calleeIMName: username
  85. })
  86. } else if (selectTab === 'group') {
  87. this.props.showInviteModal()
  88. this.props.setGid(selectItem)
  89. // this.props.updateConfrInfo(selectItem, false, false)
  90. }
  91. const inviteStatus = 1
  92. emediaState.callStatus = inviteStatus
  93. wx.WebIM.rtc.timer = setTimeout(() => {
  94. if (callType === 'contact') {
  95. emediaState.cancelCall(username)
  96. wx.WebIM.client && wx.WebIM.client.leave()
  97. emediaState.hangup()
  98. this.onHangup()
  99. } else {
  100. // 多人不做超时
  101. }
  102. }, 30000)
  103. },
  104. // 点击发起视频的回调
  105. onMakeVideoCall(evt) {
  106. if (evt.detail == "group") {
  107. this.setData({
  108. showEmediaInvite: true,
  109. inputbarVisible: 'none',
  110. action: 'create'
  111. //showEmedia: true
  112. })
  113. } else {
  114. this.callVideo('contact', 1)
  115. this.setData({
  116. showSingleEmedia: true,
  117. inputbarVisible: 'none'
  118. })
  119. }
  120. },
  121. // 群组发起视频邀请的回调
  122. // onStartConfr(data) {
  123. // console.log('发起邀请的回调', data.detail)
  124. // const channel = emediaState.confr.channel || Math.uuid(8)
  125. // let callId = emediaState.confr.callId || WebIM.conn.getUniqueId().toString();
  126. // let username = this.data.username.your;
  127. // getApp().globalData.channel = channel
  128. // emediaState.setConf('callId', callId)
  129. // // if(data.detail.action == 'invite'){
  130. // this.sendInviteMsg(data.detail.confrMember, channel, callId, 2)
  131. // // }
  132. // if (!emediaState.confr.channel) {
  133. // emediaState.setConf({
  134. // channel: channel,
  135. // token: '',
  136. // type: 2,
  137. // callId: callId,
  138. // callerDevId: wx.WebIM.conn.context.jid.clientResource,
  139. // calleeDevId: null,
  140. // confrName: '',
  141. // callerIMName: wx.WebIM.conn.context.userId,
  142. // calleeIMName: username
  143. // })
  144. // }
  145. // this.setData({
  146. // showEmediaInvite: false,
  147. // showmultiEmedia: true,
  148. // multiEmediaVisible: 'block',
  149. // inputbarVisible: 'none',
  150. // confrMember: data.detail.confrMember,
  151. // emediaAction: {
  152. // action: 'create'
  153. // }
  154. // })
  155. // },
  156. // joinConf(data) {
  157. // console.log('data ---', data)
  158. // console.log('emediaState', emediaState)
  159. // if (emediaState.confr.type == 1 || emediaState.confr.type == 0) {
  160. // this.setData({
  161. // showSingleEmedia: true,
  162. // inputbarVisible: 'none',
  163. // emediaAction: {
  164. // action: 'join'
  165. // }
  166. // })
  167. // } else {
  168. // this.setData({
  169. // showEmediaInvite: false,
  170. // showmultiEmedia: true,
  171. // multiEmediaVisible: 'block',
  172. // inputbarVisible: 'none',
  173. // confrMember: 'confrMember',
  174. // emediaAction: {
  175. // action: 'join'
  176. // }
  177. // })
  178. // }
  179. // },
  180. onGoBack() {
  181. this.setData({
  182. showEmediaInvite: false,
  183. showmultiEmedia: true,
  184. multiEmediaVisible: 'block',
  185. inputbarVisible: 'none',
  186. confrMember: []
  187. })
  188. },
  189. onInviteMember(e) {
  190. let username = this.data.username;
  191. if (!this.data.username.groupId) {
  192. username.groupId = e.detail
  193. }
  194. this.setData({
  195. action: 'invite',
  196. showEmediaInvite: true,
  197. inputbarVisible: 'none',
  198. //showmultiEmedia: false
  199. multiEmediaVisible: 'none',
  200. username
  201. })
  202. },
  203. onMakeAudioCall() {
  204. this.callVideo('contact', 0)
  205. this.setData({
  206. showSingleEmedia: true,
  207. inputbarVisible: 'none',
  208. singleEmediaType: 0,
  209. emediaAction: {
  210. action: 'create'
  211. }
  212. })
  213. },
  214. onCreateConfrSuccess(data) {
  215. console.log('创建会议回调', data)
  216. this.setData({
  217. confrId: data.detail.confrId
  218. })
  219. const channel = Math.uuid(8)
  220. getApp().globalData.channel = channel
  221. getApp().globalData.confrId = data.detail.confrId
  222. this.sendInviteMsg(this.data.confrMember, channel, data)
  223. },
  224. sendInviteMsg(members, channel, callId, type) {
  225. console.log("%c members", "background: green")
  226. console.log(members)
  227. members && members.forEach((value) => {
  228. let id = wx.WebIM.conn.getUniqueId();
  229. let msg = new wx.WebIM.message('txt', id);
  230. msg.set({
  231. msg: wx.WebIM.conn.context.userId + ' invite you to video call',
  232. from: wx.WebIM.conn.context.userId,
  233. to: value,
  234. chatType: 'singleChat',
  235. ext: {
  236. action: 'invite',
  237. channelName: channel,
  238. type: type, //0为1v1音频,1为1v1视频,2为多人通话
  239. callerDevId: wx.WebIM.conn.context.jid.clientResource, // 主叫方设备Id
  240. callId: callId, // 随机uuid,每次呼叫都不同,代表一次呼叫
  241. ts: Date.now(),
  242. msgType: 'rtcCallWithAgora'
  243. },
  244. success(id, serverMsgId) {
  245. console.log('发送邀请消息成功 to: ' + value)
  246. },
  247. fail(id, serverMsgId) {
  248. console.log('发送邀请消息失败了')
  249. }
  250. });
  251. console.log('发送邀请')
  252. wx.WebIM.conn.send(msg.body);
  253. })
  254. },
  255. onClickInviteMsg(data) {
  256. // old fun
  257. return
  258. },
  259. onHangup() {
  260. this.setData({
  261. showEmediaInvite: false,
  262. showmultiEmedia: false,
  263. showSingleEmedia: false,
  264. inputbarVisible: 'block'
  265. })
  266. getApp().globalData.confrId = ''
  267. },
  268. onRender() {
  269. wx.pageScrollTo({
  270. scrollTop: 5000,
  271. duration: 300,
  272. success: function () { console.log('滚动成功') },
  273. fail: function () { console.log('滚动失败') }
  274. })
  275. }
  276. },
  277. // lifetimes
  278. created() { },
  279. attached() { },
  280. ready() {
  281. console.log('this data >> ', this.data)
  282. this.data.__comps__.inputbar = this.selectComponent("#chat-inputbar");
  283. this.data.__comps__.msglist = this.selectComponent("#chat-msglist");
  284. this.data.__comps__.audio = this.selectComponent("#chat-suit-audio");
  285. disp.on('em.message.fullscreenchange', (detail) => {
  286. if (detail.fullscreen) {
  287. this.setData({
  288. inputbarVisible: 'none'
  289. })
  290. } else {
  291. this.setData({
  292. inputbarVisible: 'block'
  293. })
  294. }
  295. })
  296. },
  297. moved() { },
  298. detached() { },
  299. })