index.js 2.9 KB

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