index.js 3.3 KB

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