index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.getNewMsgDet()
  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. chooseImage() {
  104. wx.chooseImage({
  105. count: 1, // 可选择的图片数量
  106. sizeType: ['compressed'], // 压缩图片
  107. sourceType: ['album', 'camera'], // 来源:相册或相机
  108. success: (res) => {
  109. this.uploadImage(res.tempFilePaths[0]);
  110. }
  111. })
  112. },
  113. uploadImage(imagePath) {
  114. wx.uploadFile({
  115. url: 'https://reader-api.ai160.com/file/upload',
  116. filePath: imagePath,
  117. name: '朗读录音',
  118. header: {
  119. uid: wx.getStorageSync('uid')
  120. },
  121. success: async (res) => {
  122. await sendMsg({
  123. content: JSON.parse(res.data).data,
  124. type: '2',
  125. receiverUid: this.data.targetUid
  126. })
  127. this.getNewMsgDet()
  128. this.setData({
  129. scrollTop: this.data.list.length * 1000
  130. })
  131. }
  132. })
  133. },
  134. bindKeyInput(e) {
  135. console.log(e);
  136. this.setData({
  137. value: e.detail.value
  138. })
  139. },
  140. jumpUserInfo({
  141. currentTarget
  142. }) {
  143. wx.navigateTo({
  144. url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
  145. })
  146. },
  147. onUnload() {
  148. clearInterval(this.interval)
  149. }
  150. })