index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.trim()) {
  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.chooseMedia({
  111. count: 1,
  112. mediaType: ['image'],
  113. sourceType: ['album', 'camera'],
  114. sizeType: ['compressed'], // 压缩图片
  115. camera: 'back',
  116. success: (res) => {
  117. this.uploadImage(res.tempFiles[0].tempFilePath);
  118. }
  119. })
  120. },
  121. uploadImage(imagePath) {
  122. wx.uploadFile({
  123. url: 'https://reader-api.ai160.com/file/upload',
  124. filePath: imagePath,
  125. name: '朗读录音',
  126. header: {
  127. uid: wx.getStorageSync('uid')
  128. },
  129. success: async (res) => {
  130. await sendMsg({
  131. content: JSON.parse(res.data).data,
  132. type: '2',
  133. receiverUid: this.data.targetUid
  134. })
  135. this.getNewMsgDet()
  136. this.setData({
  137. scrollTop: this.data.list.length * 1000
  138. })
  139. }
  140. })
  141. },
  142. bindKeyInput(e) {
  143. this.setData({
  144. value: e.detail.value
  145. })
  146. },
  147. jumpUserInfo({
  148. currentTarget
  149. }) {
  150. wx.navigateTo({
  151. url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
  152. })
  153. },
  154. onUnload() {
  155. clearInterval(this.interval)
  156. }
  157. })