chatroom.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <chat id="chat" :username="username" ref="chat" chatType="singleChat" @onClickInviteMsg="onClickMsg"></chat>
  3. </template>
  4. <script>
  5. let disp = require("../../hxChatSDK/utils/broadcast");
  6. import chat from "../../chat/components/chat/chat.vue";
  7. var WebIM = require("../../hxChatSDK/utils/WebIM")["default"];
  8. export default {
  9. data() {
  10. return {
  11. username: {
  12. your: ""
  13. }
  14. };
  15. },
  16. components: {
  17. chat
  18. },
  19. props: {},
  20. // options = 系统传入的 url 参数
  21. onLoad(options) {
  22. console.log('options>>>', options)
  23. let username = JSON.parse(options.username);
  24. this.setData({
  25. username: username
  26. });
  27. // 生成的支付宝小程序在onLoad里获取不到,这里放到全局变量下
  28. uni.username = username;
  29. var userNameText = username.your;
  30. const imUserInfo = uni.getStorageSync("imUserInfo_" + username.your);
  31. if (imUserInfo && imUserInfo.nickName) {
  32. userNameText = imUserInfo.nickName;
  33. }
  34. uni.setNavigationBarTitle({
  35. title: userNameText
  36. });
  37. // this.subscribePresence();
  38. },
  39. onUnload(options) {
  40. disp.fire("em.chatroom.leave");
  41. },
  42. onPullDownRefresh: function() {
  43. uni.showNavigationBarLoading();
  44. this.$refs.chat.getMore();
  45. // 停止下拉动作
  46. uni.hideNavigationBarLoading();
  47. uni.stopPullDownRefresh();
  48. },
  49. methods: {
  50. /**
  51. * 需要到环信开通功能才能生效(增值功能)
  52. */
  53. subscribePresence() {
  54. let option = {
  55. usernames: [this.username.your],
  56. expiry: 7 * 24 * 3600 // 单位为秒
  57. }
  58. WebIM.conn.subscribePresence(option).then(res => {
  59. console.log('subscribePresence>>>>>', res)
  60. })
  61. WebIM.conn.addEventHandler('MESSAGES', {
  62. onPresenceStatusChange: function(msg) {
  63. // 这里可以处理订阅用户状态更新后的逻辑。
  64. console.log('状态更新', msg);
  65. },
  66. })
  67. },
  68. onClickMsg(msg) {
  69. msg.action = 'join'
  70. uni.navigateTo({
  71. url: "../emedia/index?srcData=" + JSON.stringify(msg)
  72. });
  73. },
  74. onNavigationBarButtonTap(e) {
  75. uni.navigateTo({
  76. url: `/pages/moreMenu/moreMenu?username=${this.username.your}&type=singleChat`
  77. })
  78. },
  79. }
  80. };
  81. </script>
  82. <style>
  83. @import "./chatroom.css";
  84. </style>