index.js 4.0 KB

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