chatroom.vue 2.4 KB

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