index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {
  2. getMsgDet,
  3. sendMsg,
  4. getNewMsgDet
  5. } from "~/api/message"
  6. import {
  7. createStoreBindings
  8. } from 'mobx-miniprogram-bindings'
  9. import event from '~/mixins/event'
  10. import {
  11. store
  12. } from '~/store/index'
  13. let interval = null
  14. const app = getApp()
  15. Page({
  16. behaviors: [event],
  17. data: {
  18. targetUid: '',
  19. value: '',
  20. list: [],
  21. pageNo: 1,
  22. totalNo: 1,
  23. scrollTop: 0,
  24. triggered: false,
  25. isIos: app.globalData.isIOS,
  26. navBarHeight: app.globalData.navBarHeight,
  27. uid: ''
  28. },
  29. onLoad(options) {
  30. wx.setNavigationBarTitle({
  31. title: options.title,
  32. })
  33. this.setData({
  34. targetUid: options.uid,
  35. uid: wx.getStorageSync('uid')
  36. })
  37. this.storeBindings = createStoreBindings(this, {
  38. store,
  39. fields: {
  40. userInfo: 'userInfo'
  41. },
  42. })
  43. this.storeBindings.updateStoreBindings()
  44. this.getMsgDet().then(() => {
  45. this.setData({
  46. scrollTop: 10000
  47. })
  48. })
  49. this.interval = setInterval(() => {
  50. this.getNewMsgDet()
  51. }, 5000)
  52. },
  53. getMsgDet() {
  54. return new Promise(async (reslove) => {
  55. let pageNo = this.data.pageNo
  56. if (this.data.totalNo < pageNo) {
  57. return this.setData({
  58. triggered: false,
  59. })
  60. }
  61. let data = await getMsgDet({
  62. senderUid: this.data.targetUid,
  63. pageNo,
  64. pageSize: 10
  65. })
  66. let {
  67. list,
  68. totalNo
  69. } = data
  70. this.setData({
  71. list: [...list, ...this.data.list],
  72. totalNo,
  73. pageNo: totalNo >= pageNo ? ++pageNo : pageNo,
  74. triggered: false,
  75. })
  76. reslove()
  77. })
  78. },
  79. async getNewMsgDet() {
  80. let res = await getNewMsgDet({
  81. senderUid: this.data.targetUid,
  82. })
  83. let newList = [...this.data.list, ...res]
  84. this.setData({
  85. list: newList,
  86. })
  87. },
  88. async sendReply() {
  89. if (!this.data.value) {
  90. return
  91. }
  92. await sendMsg({
  93. content: this.data.value,
  94. type: '1',
  95. receiverUid: this.data.targetUid
  96. })
  97. this.setData({
  98. value: '',
  99. scrollTop: this.data.list.length * 1000
  100. })
  101. this.getNewMsgDet()
  102. },
  103. previewImage(e) {
  104. var imageUrl = e.currentTarget.dataset.src;
  105. wx.previewImage({
  106. urls: [imageUrl]
  107. })
  108. },
  109. chooseImage() {
  110. wx.chooseImage({
  111. count: 1, // 可选择的图片数量
  112. sizeType: ['compressed'], // 压缩图片
  113. sourceType: ['album', 'camera'], // 来源:相册或相机
  114. success: (res) => {
  115. this.uploadImage(res.tempFilePaths[0]);
  116. }
  117. })
  118. },
  119. uploadImage(imagePath) {
  120. wx.uploadFile({
  121. url: 'https://reader-api.ai160.com/file/upload',
  122. filePath: imagePath,
  123. name: '朗读录音',
  124. header: {
  125. uid: wx.getStorageSync('uid')
  126. },
  127. success: async (res) => {
  128. await sendMsg({
  129. content: JSON.parse(res.data).data,
  130. type: '2',
  131. receiverUid: this.data.targetUid
  132. })
  133. this.getNewMsgDet()
  134. this.setData({
  135. scrollTop: this.data.list.length * 1000
  136. })
  137. }
  138. })
  139. },
  140. bindKeyInput(e) {
  141. this.setData({
  142. value: e.detail.value
  143. })
  144. },
  145. jumpUserInfo({
  146. currentTarget
  147. }) {
  148. wx.navigateTo({
  149. url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
  150. })
  151. },
  152. onUnload() {
  153. clearInterval(this.interval)
  154. }
  155. })