fun.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /** 将15寸和32寸共同用到的 函数内容,封装成一个js文件 */
  2. import {
  3. nextTick,
  4. reactive,
  5. watch
  6. } from 'vue';
  7. import {
  8. getDataList
  9. } from '../api/ChatApp';
  10. import ttsUtils from './tts/ttsUtils';
  11. // 发送消息
  12. const handleSend = async (msgList) => {
  13. msgList.isClick = !msgList.isClick
  14. const data = {
  15. AppId: '0c8ff06b-91d9-43f7-b61e-ca5da1db15d4',
  16. AId: 'TK1234',
  17. content: ''
  18. }
  19. if (msgList.chatMsg != '') {
  20. msgList.isloading[msgList.len] = true
  21. msgList.len++
  22. let obj = {
  23. otherContent: '',
  24. userContent: '',
  25. image: ""
  26. }
  27. data.content = msgList.chatMsg
  28. obj.userContent = msgList.chatMsg
  29. msgList.chatList.push(obj)
  30. obj.otherContent = await GetNewsList(msgList, data)
  31. console.log(obj.otherContent)
  32. // await sleep(16000);
  33. getChatContent(msgList, obj.otherContent, msgList.len)
  34. ttsUtils.toSpeak(obj.otherContent)
  35. }
  36. msgList.otherContent = ''
  37. }
  38. // 调用数据接口
  39. const GetNewsList = async (msgList, data) => {
  40. msgList.chatMsg = ''
  41. var res = await getDataList(data)
  42. console.log(res)
  43. return res
  44. }
  45. // 逐字显示内容
  46. const getChatContent = ((msgList, text, index) => {
  47. if (text == undefined) return
  48. msgList.timer = setInterval(() => {
  49. msgList.textCount++;
  50. if (msgList.textCount == text.length + 1) {
  51. msgList.otherChatList[index - 1] = text;
  52. clearInterval(msgList.timer);
  53. } else {
  54. // 取字符串子串
  55. let nowStr = text.substring(0, msgList.textCount);
  56. msgList.otherChatList[index - 1] = nowStr;
  57. }
  58. }, 50);
  59. console.log(msgList.timer)
  60. msgList.textCount = 0
  61. msgList.isloading[index - 1] = false
  62. msgList.isClick = !msgList.isClick
  63. })
  64. // 滚动条回到最底部
  65. const scrollToBottom = ((msgList) => {
  66. nextTick(() => {
  67. const query = uni.createSelectorQuery();
  68. query.select("#content-box").boundingClientRect()
  69. query.select("#content-overflow").boundingClientRect()
  70. query.exec(res => {
  71. const scrollViewHeight = res[0].height
  72. const scrollContentHeight = res[1].height
  73. if (scrollViewHeight < scrollContentHeight) {
  74. const scrollTop = scrollContentHeight - scrollViewHeight
  75. msgList.scrollTop = scrollTop
  76. }
  77. })
  78. })
  79. })
  80. // 背景颜色变换
  81. const styleChange = ((styleData) => {
  82. styleData.value = !styleData.value
  83. if (styleData.value == true) {
  84. styleData.color = "rgba(0,0,0,0.8)"
  85. styleData.fontColor = "#fff"
  86. styleData.btnColor = "#ccc"
  87. styleData.contentColor = "rgba(0,0,0,0.5)"
  88. styleData.labelColor = "rgb(105,105,105)"
  89. styleData.borderColor = "rgb(105,105,105)"
  90. } else {
  91. styleData.color = "#f5f5f5"
  92. styleData.fontColor = "black"
  93. styleData.btnColor = "rgb(30,144,255)"
  94. styleData.contentColor = "#fff"
  95. styleData.labelColor = "#fff"
  96. styleData.borderColor = "rgb(30,144,255)"
  97. }
  98. })
  99. //快捷键输入
  100. const shortcuts = ((msgList, item) => {
  101. msgList.chatMsg = item
  102. handleSend(msgList)
  103. })
  104. export {
  105. handleSend,
  106. styleChange,
  107. scrollToBottom,
  108. shortcuts
  109. }