zbDetails.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. const tools = require('../../utils/tools.js');
  2. const getusersig = require('../../utils/getUserSig.js');
  3. const TIM = require('../../utils/tim-wx.js');
  4. const COS = require('../../utils/cos-wx-v5.js');
  5. Page({
  6. data: {
  7. rtcConfig: {
  8. sdkAppID: 1400404379, // 必要参数 开通实时音视频服务创建应用后分配的 sdkAppID
  9. userID: '', // 必要参数 用户 ID 可以由您的帐号系统指定
  10. userSig: '', // 必要参数 身份签名,相当于登录密码的作用
  11. template: '1v1', // 必要参数 组件模版,支持的值 1v1 grid custom ,注意:不支持动态修改, iOS 不支持 pusher 动态渲染
  12. },
  13. roomIdandcourseId: "",
  14. currentGroup: null,
  15. chatContent: [],
  16. inputValue: '',
  17. second: 5,
  18. showmodal: false,
  19. bottom: "", //滑到底状态 防止弹出消息不跳到最后一条
  20. talkStatus: true, //初始值为true
  21. onSdkReady: function (event) {
  22. let _this = this;
  23. console.log(event, "im准备好了")
  24. let promise = _this.data.tim.getGroupList();
  25. console.log(promise, "我获取到promise的值了")
  26. promise.then((imResponse) => {
  27. console.log(imResponse.data.groupList, "我是群组列表"); // 群组列表
  28. if (imResponse.data.groupList.length == 0) {
  29. let promise = _this.data.tim.joinGroup({
  30. groupID: _this.data.roomIdandcourseId,
  31. type: TIM.TYPES.GRP_MEETING
  32. });
  33. promise.then((imResponse) => {
  34. switch (imResponse.data.status) {
  35. case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL:
  36. console.log(_this.data.currentGroup, "等待管理员同意")
  37. break;
  38. case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
  39. _this.setData({
  40. currentGroup: imResponse.data.group
  41. })
  42. _this.getjyList(imResponse.data.group)
  43. console.log(_this.data.currentGroup, "加群成功") // 加入的群组资料
  44. break;
  45. case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
  46. console.log(imResponse, "已经在群中")
  47. break;
  48. default:
  49. break;
  50. }
  51. }).catch(function (imError) {
  52. console.warn('申请加群失败的相关信息', imError); // 申请加群失败的相关信息
  53. });
  54. } else {
  55. for (let i = 0; i < imResponse.data.groupList.length; i++) {
  56. if (imResponse.data.groupList[i].groupID == _this.data.roomIdandcourseId) {
  57. // console.log(imResponse.data.groupList[i])
  58. // let pushArr = this.data.currentGroup.push(imResponse.data.groupList[i])
  59. // console.log(pushArr, "我是push完的东西")
  60. _this.setData({
  61. currentGroup: imResponse.data.groupList[i]
  62. })
  63. _this.getjyList(imResponse.data.groupList[i])
  64. }
  65. if (_this.data.currentGroup == null) {
  66. let promise = _this.data.tim.joinGroup({
  67. groupID: _this.data.roomIdandcourseId,
  68. type: TIM.TYPES.GRP_MEETING
  69. });
  70. promise.then((imResponse) => {
  71. switch (imResponse.data.status) {
  72. case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL:
  73. break; // 等待管理员同意
  74. case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
  75. console.log(imResponse.data.group, "加群成功"); // 加入的群组资料
  76. break;
  77. case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
  78. break;
  79. default:
  80. break;
  81. }
  82. _this.setData({
  83. currentGroup: imResponse.data.group
  84. })
  85. _this.getjyList(imResponse.data.group)
  86. console.log(_this.data.currentGroup, "0000000000000000")
  87. }).catch(function (imError) {
  88. console.warn('申请加群失败的相关信息', imError); // 申请加群失败的相关信息
  89. });
  90. }
  91. }
  92. }
  93. }).catch(function (imError) {
  94. console.log(imError, "获取群组列表失败的相关信息"); // 获取群组列表失败的相关信息
  95. });
  96. //修改成员名字
  97. let promise1 = _this.data.tim.updateMyProfile({
  98. nick: _this.data.miniName
  99. });
  100. promise1.then(function (imResponse) {
  101. console.log(imResponse.data, "更新资料成功"); // 群组列表
  102. }).catch(function (imError) {
  103. console.log(imError, "更新资料失败"); // 获取群组列表失败的相关信息
  104. });
  105. },
  106. onSdkRecived: function (event) {
  107. let _this = this;
  108. console.log(event.data[0].payload.data,"我收到消息了")
  109. if (event.data[0].conversationType == "GROUP" && event.data[0].to == _this.data.currentGroup.groupID) {
  110. console.log("我找到这个会话了", _this.data.chatContent)
  111. if (_this.data.chatContent.length > 0) { // 表示不是第一次填数据,可以使用push
  112. console.log("表示不是第一次填数据,可以使用push", _this.data.chatContent)
  113. const chatContent = _this.data.chatContent
  114. chatContent.push(event.data[0])
  115. _this.setData({
  116. chatContent: chatContent,
  117. bottom: 'scrollBottom'
  118. })
  119. } else { // 表示第一次填数据
  120. console.log("表示第一次填数据", _this.data.chatContent)
  121. _this.setData({
  122. chatContent: event.data,
  123. bottom: 'scrollBottom'
  124. })
  125. }
  126. }
  127. if (event.data[0].payload.data == "stopLive") {
  128. wx.showModal({
  129. content: "当前直播结束了!",
  130. showCancel: false,
  131. success(res) {
  132. if (res.confirm) {
  133. wx.navigateBack({
  134. delta: 1
  135. })
  136. }
  137. }
  138. })
  139. }
  140. if (event.data[0].payload.data == "putLocalStream") {
  141. _this.trtcRoomContext.publishLocalVideo();
  142. _this.trtcRoomContext.publishLocalAudio();
  143. }
  144. if (event.data[0].payload.data == "stopPutLocalStream") {
  145. _this.trtcRoomContext.unpublishLocalVideo();
  146. _this.trtcRoomContext.unpublishLocalAudio();
  147. }
  148. if (event.data[0].payload.data == "readyPutLocalStream") {
  149. _this.setData({
  150. showmodal: true
  151. })
  152. _this.countdown()
  153. }
  154. if (event.data[0].payload.data == "stopTalk") {
  155. _this.setData({
  156. talkStatus: false
  157. })
  158. wx.showToast({
  159. title: '您被老师禁言啦', //提示文字
  160. duration: 3000, //显示时长
  161. mask: true, //是否显示透明蒙层,防止触摸穿透,默认:false
  162. icon: 'none' //图标,支持"success"、"loading"
  163. })
  164. }
  165. if (event.data[0].payload.data == "keepTalk") {
  166. _this.setData({
  167. talkStatus: true
  168. })
  169. wx.showToast({
  170. title: '您的禁言被解除了,可以正常发言啦', //提示文字
  171. duration: 3000, //显示时长
  172. mask: true, //是否显示透明蒙层,防止触摸穿透,默认:false
  173. icon: 'none' //图标,支持"success"、"loading"
  174. })
  175. }
  176. // if (event.data[0].payload.data == "stopTalkList") {
  177. // console.log(event.data[0].payload, "1111111111")
  178. // if (event.data[0].payload.description == "false") {
  179. // _this.setData({
  180. // talkStatus: false
  181. // })
  182. // wx.showToast({
  183. // title: '您被老师禁言啦', //提示文字
  184. // duration: 3000, //显示时长
  185. // mask: true, //是否显示透明蒙层,防止触摸穿透,默认:false
  186. // icon: 'none' //图标,支持"success"、"loading"
  187. // })
  188. // }
  189. // }
  190. }
  191. },
  192. answer() {
  193. this.setData({
  194. showmodal: false,
  195. second: 5
  196. });
  197. clearTimeout(this.data.timer);
  198. let _this = this;
  199. let message = _this.data.tim.createCustomMessage({
  200. to: "teacher-" + _this.data.currentGroup.groupID,
  201. conversationType: TIM.TYPES.CONV_C2C,
  202. payload: {
  203. data: 'yesStudentPutHand'
  204. }
  205. });
  206. let promise = _this.data.tim.sendMessage(message);
  207. promise.then(function (imResponse) {
  208. console.log(imResponse, "我给老师发消息说我同意啦");
  209. }).catch(function (imError) {
  210. console.warn('我给老师发消息说我同意失败啦', imError);
  211. });
  212. },
  213. refuse() {
  214. this.setData({
  215. showmodal: false,
  216. second: 5
  217. });
  218. clearTimeout(this.data.timer);
  219. let _this = this;
  220. let message = _this.data.tim.createCustomMessage({
  221. to: "teacher-" + _this.data.currentGroup.groupID,
  222. conversationType: TIM.TYPES.CONV_C2C,
  223. payload: {
  224. data: 'notStudentPutHand'
  225. }
  226. });
  227. let promise = _this.data.tim.sendMessage(message);
  228. promise.then(function (imResponse) {
  229. console.log(imResponse, "我给老师发消息说我不同意");
  230. }).catch(function (imError) {
  231. console.warn('我给老师发消息说我不同意失败啦', imError);
  232. });
  233. },
  234. countdown() {
  235. var second = this.data.second
  236. if (second == 0) {
  237. this.refuse();
  238. return;
  239. }
  240. this.setData({
  241. timer: setTimeout(() => {
  242. this.setData({
  243. second: second - 1
  244. });
  245. this.countdown();
  246. }, 1000)
  247. })
  248. },
  249. // 举手
  250. handsup() {
  251. console.log(this.data.currentGroup, "0000000000")
  252. if (this.data.talkStatus) {
  253. let message = this.data.tim.createCustomMessage({
  254. to: "teacher-" + this.data.currentGroup.groupID,
  255. conversationType: TIM.TYPES.CONV_C2C,
  256. payload: {
  257. data: 'studentPutHand'
  258. }
  259. });
  260. let promise = this.data.tim.sendMessage(message);
  261. promise.then(function (imResponse) {
  262. console.log(imResponse);
  263. wx.showToast({
  264. title: '举手成功',
  265. icon: 'success',
  266. duration: 2000,
  267. mask: true
  268. })
  269. }).catch(function (imError) {
  270. console.warn('sendMessage error:', imError);
  271. });
  272. } else {
  273. wx.showToast({
  274. title: '您被老师禁言啦', //提示文字
  275. duration: 3000, //显示时长
  276. mask: true, //是否显示透明蒙层,防止触摸穿透,默认:false
  277. icon: 'none' //图标,支持"success"、"loading"
  278. })
  279. }
  280. },
  281. //发送禁言列表消息
  282. getjyList(e) {
  283. let message = this.data.tim.createCustomMessage({
  284. to: "teacher-" + e.groupID,
  285. conversationType: TIM.TYPES.CONV_C2C,
  286. payload: {
  287. data: 'getStopTalkList'
  288. }
  289. });
  290. let promise = this.data.tim.sendMessage(message);
  291. promise.then(function (imResponse) {
  292. console.log("成功", imResponse);
  293. }).catch(function (imError) {
  294. console.warn('sendMessage error:', imError);
  295. });
  296. },
  297. // 发消息
  298. sendMessage() {
  299. if (this.data.talkStatus) {
  300. if (this.data.inputValue == null || this.data.inputValue == "") {
  301. wx.showModal({
  302. content: "不可以发送空消息哦!",
  303. showCancel: false
  304. })
  305. return false
  306. }
  307. console.log(this.data.currentGroup)
  308. let message = this.data.tim.createTextMessage({
  309. to: this.data.currentGroup.groupID,
  310. conversationType: TIM.TYPES.CONV_GROUP,
  311. priority: TIM.TYPES.MSG_PRIORITY_NORMAL,
  312. payload: {
  313. text: this.data.inputValue
  314. }
  315. });
  316. let promise = this.data.tim.sendMessage(message);
  317. promise.then((imResponse) => {
  318. console.log(imResponse, "发送成功");
  319. if (this.data.chatContent.length > 0) { // 表示不是第一次填数据,可以使用push
  320. console.log("表示不是第一次填数据,可以使用push", this.data.chatContent)
  321. const chatContent = this.data.chatContent
  322. chatContent.push({
  323. nick: this.data.miniName,
  324. payload: {
  325. text: this.data.inputValue
  326. }
  327. })
  328. this.setData({
  329. chatContent: chatContent,
  330. bottom: 'scrollBottom'
  331. })
  332. } else { // 表示第一次填数据
  333. console.log("表示第一次填数据", this.data.chatContent)
  334. this.setData({
  335. chatContent: [{
  336. nick: this.data.miniName,
  337. payload: {
  338. text: this.data.inputValue
  339. }
  340. }],
  341. bottom: 'scrollBottom'
  342. })
  343. }
  344. this.setData({
  345. inputValue: ''
  346. })
  347. }).catch(function (imError) {
  348. console.warn('发送失败', imError);
  349. });
  350. } else {
  351. wx.showToast({
  352. title: '您被老师禁言啦', //提示文字
  353. duration: 3000, //显示时长
  354. mask: true, //是否显示透明蒙层,防止触摸穿透,默认:false
  355. icon: 'none' //图标,支持"success"、"loading"
  356. })
  357. }
  358. },
  359. bindKeyInput: function (e) {
  360. this.setData({
  361. inputValue: e.detail.value
  362. })
  363. },
  364. // 滑动聊天室
  365. scrollToTop() {
  366. this.setAction({
  367. scrollTop: 0
  368. })
  369. },
  370. async onTim() {
  371. this.data.tim.on(TIM.EVENT.SDK_READY, this.data.onSdkReady, this);
  372. this.data.tim.on(TIM.EVENT.MESSAGE_RECEIVED, this.data.onSdkRecived, this);
  373. const sig = await getusersig.genTestUserSig(this.data.rtcConfig.userID, this.data.rtcConfig.sdkAppID, "f1ca85809aed35d82d8bb76612787e38dada5d0a6f233662777b42bdb7eb880b")
  374. let promise = this.data.tim.login({
  375. userID: this.data.rtcConfig.userID,
  376. userSig: sig.userSig
  377. });
  378. promise.then((imResponse) => {
  379. console.log(imResponse.data, "登录成功"); // 登录成功
  380. if (imResponse.data.repeatLogin === true) {
  381. console.log(imResponse.data.errorInfo);
  382. }
  383. }).catch(function (imError) {
  384. console.warn('登录失败的相关信息', imError); // 登录失败的相关信息
  385. });
  386. },
  387. offTim() {
  388. this.data.tim.off(TIM.EVENT.SDK_READY, this.data.onSdkReady);
  389. this.data.tim.off(TIM.EVENT.MESSAGE_RECEIVED, this.data.onSdkRecived);
  390. console.log(this.data.roomIdandcourseId, "我是退出的群组")
  391. this.data.tim.quitGroup(this.data.roomIdandcourseId);
  392. let promise = this.data.tim.logout();
  393. promise.then(function (imResponse) {
  394. console.log(imResponse.data, " 登出成功");
  395. }).catch(function (imError) {
  396. console.warn(' 登出失败:', imError);
  397. });
  398. },
  399. bindTRTCRoomEvent: function () {
  400. console.log(this.data.roomIdandcourseId, typeof (this.data.roomIdandcourseId), "我开始监听了。。。。。")
  401. this.trtcRoomContext.enterRoom({
  402. roomID: Number(this.data.roomIdandcourseId) //课程id
  403. // userID: this.data.rtcConfig.userID
  404. })
  405. const TRTC_EVENT = this.trtcRoomContext.EVENT
  406. this.trtcRoomContext.on(TRTC_EVENT.LOCAL_JOIN, (event) => {
  407. console.log('我加入房间了', event)
  408. })
  409. this.trtcRoomContext.on(TRTC_EVENT.LOCAL_LEAVE, (event) => {
  410. console.log('我离开房间了', event)
  411. })
  412. this.trtcRoomContext.on(TRTC_EVENT.ERROR, (event) => {
  413. console.log('进入房间出错了', event)
  414. })
  415. // 远端用户进入房间
  416. this.trtcRoomContext.on(TRTC_EVENT.REMOTE_USER_JOIN, (event) => {
  417. console.log('远端用户进入房间')
  418. })
  419. // 远端用户退出
  420. this.trtcRoomContext.on(TRTC_EVENT.REMOTE_USER_LEAVE, (event) => {
  421. console.log('远端用户退出')
  422. })
  423. this.trtcRoomContext.on(TRTC_EVENT.REMOTE_AUDIO_ADD, (event) => {
  424. console.log(event, "lalalallalalalalal")
  425. this.trtcRoomContext.subscribeRemoteAudio({
  426. userID: event.data.userID
  427. }).then(() => {
  428. console.log("音频订阅成功")
  429. })
  430. })
  431. // 远端用户取消推送音频
  432. this.trtcRoomContext.on(TRTC_EVENT.REMOTE_AUDIO_REMOVE, (event) => {
  433. this.trtcRoomContext.unsubscribeRemoteAudio({
  434. userID: event.data.userID
  435. }).then(() => {
  436. console.log("音频取消订阅")
  437. })
  438. })
  439. this.trtcRoomContext.on(TRTC_EVENT.REMOTE_VIDEO_ADD, (event) => {
  440. this.trtcRoomContext.subscribeRemoteVideo({
  441. userID: event.data.userID,
  442. streamType: event.data.streamType
  443. }).then(() => {
  444. console.log("远端视频订阅成功")
  445. })
  446. })
  447. this.trtcRoomContext.on(TRTC_EVENT.REMOTE_VIDEO_REMOVE, (event) => {
  448. this.trtcRoomContext.unsubscribeRemoteVideo({
  449. userID: event.data.userID,
  450. streamType: event.data.streamType
  451. }).then(() => {
  452. console.log("远端视频取消订阅")
  453. })
  454. })
  455. },
  456. onLoad(options) {
  457. console.log(options, "99999");
  458. this.setData({
  459. roomIdandcourseId: options.id
  460. })
  461. },
  462. async onShow() {
  463. const sessionKey = await tools.checkSessionAndLogin();
  464. const information = await tools.request('/wx/student/selStudentSessionKeyEcho', {
  465. sessionKey
  466. }, 'POST');
  467. this.setData({
  468. miniName: information.data.data.miniName,
  469. ['rtcConfig.userID']: information.data.data.id.toString()
  470. })
  471. const sig = await getusersig.genTestUserSig(this.data.rtcConfig.userID, this.data.rtcConfig.sdkAppID, "f1ca85809aed35d82d8bb76612787e38dada5d0a6f233662777b42bdb7eb880b")
  472. this.setData({
  473. ['rtcConfig.userSig']: sig.userSig
  474. })
  475. wx.setKeepScreenOn({
  476. keepScreenOn: true,
  477. })
  478. this.trtcRoomContext = this.selectComponent('#trtcroom')
  479. this.bindTRTCRoomEvent()
  480. let option = {
  481. SDKAppID: 1400404379 // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID
  482. };
  483. this.setData({
  484. tim: TIM.create(option)
  485. })
  486. this.data.tim.setLogLevel(0); // 普通级别,日志量较多,接入时建议使用
  487. this.data.tim.registerPlugin({
  488. 'cos-wx-sdk': COS
  489. });
  490. this.onTim();
  491. },
  492. onHide() {
  493. console.log("我onhide了")
  494. this.trtcRoomContext.unpublishLocalVideo();
  495. this.trtcRoomContext.unpublishLocalAudio();
  496. this.offTim();
  497. },
  498. onUnload() {
  499. console.log("onUnload")
  500. this.trtcRoomContext.unpublishLocalVideo();
  501. this.trtcRoomContext.unpublishLocalAudio();
  502. this.offTim();
  503. }
  504. })