chat.js 11 KB

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