parent-message-send.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {
  2. createTextMessage,
  3. createImageMessage,
  4. createVideoMessage,
  5. createFileMessage,
  6. createVoiceMessage
  7. } from '@/utils/parent-message-factory'
  8. function normalizeUserMeta(userMeta = {}) {
  9. return {
  10. direction: userMeta.direction || 'right',
  11. department: userMeta.department || '家长',
  12. name: userMeta.name || '我'
  13. }
  14. }
  15. export function buildTextOutgoingMessage(text, userMeta = {}) {
  16. const content = (text || '').trim()
  17. if (!content) return null
  18. return createTextMessage({
  19. ...normalizeUserMeta(userMeta),
  20. content,
  21. showReceiptToggle: true
  22. })
  23. }
  24. export async function pickImageOutgoingMessages(userMeta = {}) {
  25. return new Promise((resolve, reject) => {
  26. uni.chooseImage({
  27. count: 9,
  28. sizeType: ['compressed'],
  29. sourceType: ['album'],
  30. success: (res) => {
  31. const messages = (res.tempFilePaths || []).map((imageUrl) =>
  32. createImageMessage({ ...normalizeUserMeta(userMeta), imageUrl })
  33. )
  34. resolve(messages)
  35. },
  36. fail: reject
  37. })
  38. })
  39. }
  40. export async function shootImageOutgoingMessage(userMeta = {}) {
  41. return new Promise((resolve, reject) => {
  42. uni.chooseImage({
  43. count: 1,
  44. sizeType: ['compressed'],
  45. sourceType: ['camera'],
  46. success: (res) => {
  47. const first = (res.tempFilePaths || [])[0]
  48. resolve(first ? createImageMessage({ ...normalizeUserMeta(userMeta), imageUrl: first }) : null)
  49. },
  50. fail: reject
  51. })
  52. })
  53. }
  54. export async function pickVideoOutgoingMessage(userMeta = {}) {
  55. return new Promise((resolve, reject) => {
  56. uni.chooseVideo({
  57. sourceType: ['camera', 'album'],
  58. compressed: true,
  59. maxDuration: 60,
  60. success: (res) => {
  61. const videoUrl = res.tempFilePath || ''
  62. if (!videoUrl) return resolve(null)
  63. const duration = Number.isFinite(res.duration)
  64. ? `${String(Math.floor(res.duration / 60)).padStart(2, '0')}:${String(Math.floor(res.duration % 60)).padStart(2, '0')}`
  65. : ''
  66. resolve(
  67. createVideoMessage({
  68. ...normalizeUserMeta(userMeta),
  69. videoUrl,
  70. coverUrl: res.thumbTempFilePath || '/static/logo.png',
  71. duration
  72. })
  73. )
  74. },
  75. fail: reject
  76. })
  77. })
  78. }
  79. export async function pickFileOutgoingMessage(userMeta = {}) {
  80. if (!uni.chooseMessageFile) {
  81. return null
  82. }
  83. return new Promise((resolve, reject) => {
  84. uni.chooseMessageFile({
  85. count: 1,
  86. type: 'file',
  87. success: (res) => {
  88. const file = (res.tempFiles || [])[0]
  89. if (!file) return resolve(null)
  90. resolve(
  91. createFileMessage({
  92. ...normalizeUserMeta(userMeta),
  93. fileName: file.name || '未命名文件',
  94. fileUrl: file.path || ''
  95. })
  96. )
  97. },
  98. fail: reject
  99. })
  100. })
  101. }
  102. export function buildVoiceOutgoingMessage({
  103. durationSeconds = 0,
  104. audioUrl = '',
  105. voiceText = ''
  106. } = {}, userMeta = {}) {
  107. const safeDuration = Math.max(1, Math.floor(durationSeconds || 0))
  108. return createVoiceMessage({
  109. ...normalizeUserMeta(userMeta),
  110. duration: String(safeDuration),
  111. audioUrl,
  112. voicePreview: '语音消息',
  113. voiceText
  114. })
  115. }