Im.js 6.17 KB
// // pages/crowdfunding/components/Im/Im.js
// import {
//   shopmallApi
// } from '~/pages/crowdfunding/api/index'
// import USERAPI from '~/api/usersApi/index'
// import IM from './tim.js'
// let timObj = null
// const msgMaxNum = 100

// Component({
//   /**
//    * 组件的属性列表
//    */
//   properties: {
//     liveDetail: {
//       type: Object,
//       value: () => {
//         return {
//           imGroupId: '',
//           imSdkAppId: ''
//         }
//       }
//     },
//     detail: {
//       type: Object,
//       value: () => { }
//     },
//     isAction: {
//       type: Boolean,
//       value: false
//     }
//   },

//   /**
//    * 组件的初始数据
//    */
//   data: {
//     userInfo: {},
//     isFocus: false,
//     notifyTxt: '直播间内严禁出现违法违规、低俗、涉黄、吸烟酗酒等内容,直播期间请勿相信私下交易,诱导购买等信息,以防人身或财产损失。请大家注意财产安全,谨防网络诈骗,文明观看直播。',
//     messageContent: '',
//     messageList: []
//   },

//   lifetimes: {
//     created() {
//       this.getInfo().then(() => {
//         this.getUserSig()
//       })
//       wx.$eventbus.on('updateImNick', () => {
//         this.updateProfile()
//       })
//     },
//   },

//   /**
//    * 组件的方法列表
//    */
//   methods: {
//     // fixInputBeCovered({ target }) {
//     //   setTimeout(function () {
//     //     target.scrollIntoView(true)
//     //   }, 100)
//     // },
//     // handleInputFocus() {
//     //   clearTimeout(this.timer)
//     //   this.timer = setTimeout(() => {
//     //     this.isFocus = !this.isFocus
//     //   }, 300)
//     // },
//     imOnShow() {
//       this.getUserSig()
//     },
//     getInfo() {
//       return new Promise(async resolve => {
//         let { openId } = await USERAPI.getWXUserInfo()
//         const { nickName } = wx.$storage.get('user_profile') || wx.$storage.get('srcm_shopmall_user_info') || {}
//         this.setData({
//           userInfo: { openId: openId, nickName: nickName }
//         })
//         resolve()
//       })
//     },
//     h5Login() {
//       this.triggerEvent('h5Login')
//     },
//     destroyIm() {
//       if (timObj) {
//         timObj.destroy().then(() => {
//           timObj = null
//         })
//       }
//     },
//     async getUserSig() {
//       if(timObj) {
//         return true
//       }
//       let merchantId = wx.$storage.get('srcm_shopmall_merchant_id') || '',
//         systemInfo = wx.$storage.get('system_info')
//       this.setData({
//         systemInfo
//       })
//       // sourceShopId-来源门店;visitSource-访问来源;bizType-业务类型(0-众筹);bizCode-业务标识(众筹-活动ID)
//       let params = {
//         userId: this.data.userInfo.openId,
//         sourceShopId: merchantId || '',
//         visitSource: '小程序-众筹',
//         bizType: 0,
//         bizCode: this.data.detail.id
//       }
//       shopmallApi.getLiveSig(params).then(res => {
//         let { data } = res
//         this.userSig = data.userSig
//         this.setData({
//           userSig: data.userSig
//         })
//         this.initTim()
//       })
//     },
//     initTim() {
//       const _this = this
//       const imOptions = {
//         userID: this.data.userInfo.openId,
//         userSig: this.data.userSig,
//         groupID: this.data.liveDetail.imGroupId,
//         SDKAppID: this.data.liveDetail.imSdkAppId,
//         onMessageReceived: data => this.onMessageReceived(this, data),
//         onNetStateChange: this.onNetStateChange
//       }
//       timObj = new IM(imOptions)
//       timObj.join(imOptions).then(res => {
//         _this.updateProfile()
//       })
//     },
//     updateProfile() {
//       if(!this.data.userInfo?.nickName) {
//         const { nickName } = wx.$storage.get('user_profile') || wx.$storage.get('srcm_shopmall_user_info') || {}
//         this.setData({
//           ['userInfo.nickName']: nickName
//         })
//       }
//       timObj && timObj.updateMyProfile({ nick: this.data.userInfo.nickName || ('游客' + this.data.userInfo.openId.slice(-3)) })
//     },
//     // 收到消息触发
//     onMessageReceived(_this, data) {
//       // console.log('收到消息:', data)
//       let msgLen = _this.data.messageList.length
//       let dataLen = data.length
//       if (msgLen + dataLen > msgMaxNum) {
//         _this.setData({
//           messageList: _this.data.messageList.slice(msgLen - dataLen)
//         })
//       }
//       data.forEach(item => {
//         // 根据消息类型判断,执行不同操作
//         if (item.type == 'TIMTextElem') {
//           // 群成员发送消息
//           // let eleData = JSON.parse()
//           let eleData = {
//             userName: item.nick,
//             content: item.payload.text
//           }
//           // 获取群聊消息,消息上屏
//           _this.updateMessageList(eleData, _this)
//         }
//       })
//     },
//     // 网络异常处理
//     onNetStateChange(notify) {
//       // wx.$api.showToast(notify)
//     },
//     // 更新数据push进消息列表
//     updateMessageList(data) {
//       const list = [...this.data.messageList, data]
//       this.setData({
//         messageList: list
//       }, () => {
//         this.scrollToBottom()
//       })
//     },
//     // changeChatHeight(height = '230px') {
//     //   var chatBoxDom = this.$el.querySelector('.chatBox-middle')
//     //   chatBoxDom.style.height = height
//     //   this.scrollToBottom()
//     // },
//     // handleFocus() {
//     //   this.changeChatHeight('116px')
//     // },
//     //发送聊天
//     sendTextMessage(e) {
//       clearTimeout(this.time)
//       this.time = setTimeout(() => {
//         //里面写你的操作
//         let { value } = e.detail
//         if (value === '' || value.trim().length === 0) {
//           return
//         }
//         timObj.send(value)
//         this.setData({
//           messageContent: ''
//         })
//       }, 200)

//     },
//     scrollToBottom() {
//       this.setData({
//         toView: `item${this.data.messageList.length - 1}`
//       })
//     }
//   }
// })