message-list.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import HouseApi from './../../apis/house'
  2. import ImApi from './../../apis/im'
  3. import { randomString } from './../../utils/tools'
  4. import { fotmatDate } from './../../utils/date'
  5. import ImSend from '../../utils/imSend'
  6. const app = getApp()
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. house: {},
  13. house_id: '',
  14. customers: [],
  15. cardShowStatus: false,
  16. selectedCustomer: {}
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. this.getHouseDetail(options.house_id)
  23. this.getMessageList()
  24. },
  25. toCustomerList () {
  26. wx.navigateTo({
  27. url: `/pages/customer-list/customer-list?house_id=${this.data.house_id}`,
  28. })
  29. },
  30. getHouseDetail (house_id) {
  31. HouseApi.houseDetail(house_id).then(res => {
  32. res.data.detail_images = JSON.parse(res.data.detail_images)
  33. this.setData({
  34. house: res.data,
  35. house_id: house_id
  36. })
  37. })
  38. },
  39. confirmInvite (e) {
  40. const { index } = e.currentTarget.dataset
  41. this.setData({
  42. cardShowStatus: true,
  43. selectedCustomer: this.data.customers[index]
  44. })
  45. },
  46. submit (e) {
  47. if (e.detail) {
  48. ImSend.sendTextMsg(e.detail, this.data.selectedCustomer.user_id)
  49. }
  50. const room_id = randomString(18)
  51. const { house } = this.data
  52. console.log(this.data.selectedCustomer)
  53. this.postDataToOpen(room_id).then(res => {
  54. // const vr_link = res.data.vrLink
  55. ImSend.sendVrMsg(house, res.data.roomId, this.data.selectedCustomer.id, true)
  56. })
  57. this.setData({
  58. cardShowStatus: false
  59. })
  60. },
  61. sendVrMsg () {
  62. },
  63. postDataToOpen (room_id) {
  64. return HouseApi.postDataToOpen({house_id: this.data.house_id, room_id: '', type: 'customer'})
  65. },
  66. cancle () {
  67. this.setData({
  68. cardShowStatus: false
  69. })
  70. },
  71. getMessageList () {
  72. return ImApi.getContacts().then(res => {
  73. this.customers = this.dealConversations(res.data.friends)
  74. this.setData({
  75. customers: this.customers
  76. })
  77. })
  78. },
  79. dealConversations (conversations) {
  80. return conversations.map(item => {
  81. let content = item.latestMsgContent
  82. try {
  83. let parseContent = JSON.parse(content)
  84. if (parseContent.house_name) {
  85. content = `【云带看】${JSON.parse(content).house_name}`
  86. } else if (parseContent.duration) {
  87. content = '【语音】'
  88. } else if (parseContent.content) {
  89. content = '【图片】'
  90. }
  91. } catch (err) {
  92. }
  93. item.latestMsgContent = content
  94. item.latestMsgTime = fotmatDate(item.latestMsgTime, 'yyyy-MM-dd hh:mm:ss')
  95. return item
  96. }).sort((a,b) => new Date(b.latestMsgTime) - new Date(a.latestMsgTime))
  97. },
  98. sendVrMsg () {
  99. const room_id = randomString(18)
  100. const { house } = this.data
  101. ImSend.sendVrMsg(house, room_id, this.data.selectedCustomer.id, true)
  102. },
  103. sendMsg ({content, msgType}) {
  104. let defaultContent = {
  105. fromId: app.globalData.userinfo.user_id,
  106. fromName: app.globalData.userinfo.phone,
  107. toId: this.data.selectedCustomer.id, //this.properties.agent_user.agency_user_id,
  108. toName: this.data.selectedCustomer.name,
  109. type: 'TYPE_ONE',
  110. msgType,
  111. content: content
  112. }
  113. console.log(getApp().getIMHandler())
  114. return getApp().getIMHandler().newFriendSendMsg({content: defaultContent})
  115. },
  116. bindinput (e) {
  117. const { value } = e.detail
  118. this.keyword = value
  119. },
  120. filterMessage () {
  121. this.setData({
  122. customers: this.customers.filter(item => item.name.indexOf(this.keyword) !== -1)
  123. })
  124. }
  125. })