SsInputBox.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <view ref="targetDiv" id="target_input_box" class="input-box" :class="{ spread: globalStore.spread }">
  3. <view class="input-container hover-scale">
  4. <textarea
  5. v-if="globalStore.spread"
  6. v-model="message"
  7. class="input-container-input"
  8. placeholder="请输入你想咨询的问题..."
  9. :maxlength="maxLength"
  10. @input="handleInputFilter"
  11. @compositionstart="handleCompositionStart"
  12. @compositionend="handleCompositionEnd"
  13. @linechange="linechange"
  14. />
  15. <input
  16. v-else
  17. v-model="message"
  18. class="input-container-input"
  19. placeholder="请输入你想咨询的问题..."
  20. :maxlength="maxLength"
  21. @input="handleInputFilter"
  22. @compositionstart="handleCompositionStart"
  23. @compositionend="handleCompositionEnd"
  24. />
  25. <view class="input-buttons">
  26. <view class="input-buttons-limit">{{ message.length }}/200</view>
  27. <view class="input-buttons-right">
  28. <ss-recording @set-message="quickSend"></ss-recording>
  29. <view class="buttons-separate"></view>
  30. <button class="send-button" @tap="clickSend">
  31. <view v-if="globalStore.chatLoading" class="send-button-stop"></view>
  32. <uni-icons v-else type="arrow-up" size="24" color="#ffffff"></uni-icons>
  33. </button>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import { useGlobalStore } from '@/stores/global';
  41. import SsRecording from './SsRecording.vue';
  42. import { RequestApi } from '@/api/requestApi';
  43. import { MessageApi } from '../api/message';
  44. export default {
  45. components: {
  46. SsRecording
  47. },
  48. data() {
  49. return {
  50. globalStore: useGlobalStore(),
  51. message: '',
  52. conversationId: '', // 消息ID,
  53. isComposing: false, // 中文输入法状态标识
  54. maxLength: 200,
  55. // 过滤规则配置
  56. filterRules: {
  57. regExp: /[^a-zA-Z0-9\u4e00-\u9fa5\,\,\.\。\!\!\%]/, // 允许:中文/英文/数字
  58. forbiddenWords: ['微信', 'vx'] // 黑名单关键词
  59. }
  60. };
  61. },
  62. computed: {
  63. /**
  64. * 发送消息标识
  65. */
  66. sendMessageFlag() {
  67. const msg = this.message.trim();
  68. return msg == '' || this.globalStore.chatLoading;
  69. }
  70. },
  71. mounted() {
  72. this.globalStore.setInputLine(3);
  73. },
  74. methods: {
  75. // 处理中文输入法状态
  76. handleCompositionStart() {
  77. this.isComposing = true;
  78. },
  79. handleCompositionEnd(e) {
  80. this.isComposing = false;
  81. this.handleInputFilter(e); // 输入法结束后触发过滤
  82. }, // 核心过滤方法
  83. handleInputFilter(e) {
  84. if (this.isComposing) return; // 输入法中间状态不处理
  85. let value = e.detail.value;
  86. // 步骤1:基础字符过滤
  87. // value = value.replace(this.filterRules.regExp, '');
  88. // 步骤2:长度截断
  89. if (value.length > this.maxLength) {
  90. value = value.slice(0, this.maxLength);
  91. uni.showToast({
  92. title: `最多输入${this.maxLength}个字符`,
  93. icon: 'none'
  94. });
  95. }
  96. // 步骤3:同步数据(解决iOS双向绑定延迟问题)
  97. this.message = value;
  98. },
  99. clickSend() {
  100. if (this.globalStore.chatLoading) {
  101. MessageApi.stop();
  102. this.globalStore.setChatLoading(false);
  103. setTimeout(() => {
  104. MessageApi.stop();
  105. }, 500);
  106. } else {
  107. this.sendMessage();
  108. }
  109. },
  110. sendMessage() {
  111. const msg = this.message.trim();
  112. if (this.sendMessageFlag) {
  113. return;
  114. }
  115. if (msg == '') {
  116. uni.showToast({
  117. title: '请输入你想咨询的问题',
  118. icon: 'none'
  119. });
  120. return;
  121. }
  122. this.$emit('newMessage');
  123. // 停止消息
  124. MessageApi.stop();
  125. // 设置会话主体
  126. this.globalStore.setSpread(true);
  127. this.globalStore.setMenuSwitch(false);
  128. // 设置为消息回复中
  129. this.globalStore.setChatLoading(true);
  130. // 发送消息内容
  131. this.globalStore.pushChatList({
  132. type: 'send_message',
  133. content: msg
  134. });
  135. this.$emit('changeChat');
  136. // 清除对话内容
  137. this.message = '';
  138. // 初始化单次对话内容
  139. this.globalStore.pushChatList({
  140. chatId: '',
  141. source: [],
  142. avatar: this.getStaticImageUrl('/images/robot-avatar.png'),
  143. type: 'text',
  144. title: msg,
  145. content: ''
  146. });
  147. MessageApi.sendMessage(msg, this.conversationId, (result) => {
  148. switch (result.type) {
  149. case 'id':
  150. this.conversationId = result.value;
  151. this.globalStore.replyChatList('', result.id);
  152. break;
  153. case 'text':
  154. this.globalStore.replyChatList(result.value);
  155. break;
  156. case 'source':
  157. case 'messageCompleted':
  158. this.globalStore.replyChatListSource(result.value);
  159. break;
  160. default:
  161. MessageApi.stop();
  162. this.globalStore.setChatLoading(false);
  163. }
  164. this.$emit('changeChat');
  165. });
  166. },
  167. quickSend(msg) {
  168. this.message = msg;
  169. this.sendMessage();
  170. },
  171. editMessage(msg) {
  172. this.message = msg;
  173. },
  174. loadService(categoryId, title, avatar) {
  175. if (this.globalStore.chatLoading) {
  176. return;
  177. }
  178. this.globalStore.setChatLoading(true);
  179. this.globalStore.setSpread(true);
  180. this.$emit('newMessage');
  181. let replyContent = {
  182. type: 'category',
  183. source: [],
  184. avatar: this.getImageUrl(avatar),
  185. title: title,
  186. content: categoryId
  187. };
  188. let [last] = this.globalStore.chatList.slice(-1);
  189. if (last) {
  190. if (typeof last.type === 'category') {
  191. if (last.content != categoryId) {
  192. this.globalStore.pushChatList(replyContent);
  193. }
  194. } else {
  195. this.globalStore.pushChatList(replyContent);
  196. }
  197. } else {
  198. this.globalStore.pushChatList(replyContent);
  199. }
  200. this.$nextTick(() => {
  201. this.globalStore.setMenuSwitch(false);
  202. this.globalStore.setChatLoading(false);
  203. });
  204. }
  205. }
  206. };
  207. </script>
  208. <style scoped lang="scss">
  209. @keyframes rotation {
  210. 0% {
  211. transform: translate(-50%, -50%) rotate(0deg);
  212. }
  213. 100% {
  214. transform: translate(-50%, -50%) rotate(360deg);
  215. }
  216. }
  217. .input-box {
  218. padding-bottom: 20rpx;
  219. .input-container {
  220. margin: 0 auto;
  221. display: flex;
  222. align-items: center;
  223. justify-content: space-between;
  224. padding: 18rpx 24rpx;
  225. border-radius: 24rpx;
  226. position: relative;
  227. overflow: hidden;
  228. box-shadow: 0 8rpx 14rpx 0 rgba(0, 0, 0, 0.08);
  229. &::before,
  230. &:before {
  231. content: '';
  232. position: absolute;
  233. width: 110%;
  234. padding-top: 110%;
  235. top: 50%;
  236. left: 50%;
  237. z-index: -2;
  238. transform: translate(-50%, -50%);
  239. background: linear-gradient(45deg, #ffa37c 30%, #ff7bb0 40%, #ad8afe 60%, #73b9ff 70%);
  240. animation: rotation 10s infinite linear;
  241. }
  242. &::after,
  243. &:after {
  244. content: '';
  245. position: absolute;
  246. top: 2px;
  247. left: 2px;
  248. bottom: 2px;
  249. right: 2px;
  250. z-index: -1;
  251. border-radius: 22rpx;
  252. background: linear-gradient(#f6faff, #e8f1fe);
  253. }
  254. .input-container-input {
  255. width: calc(100% - 192rpx);
  256. border: 0;
  257. background-color: transparent;
  258. font-size: 32rpx;
  259. max-height: 100rpx;
  260. &::-webkit-input-placeholder {
  261. /* Chrome/Safari/Opera */
  262. color: #aeaeb0;
  263. }
  264. &::-moz-placeholder {
  265. /* Firefox */
  266. color: #aeaeb0;
  267. }
  268. &:-ms-input-placeholder {
  269. /* IE 10+ */
  270. color: #aeaeb0;
  271. }
  272. &:-moz-placeholder {
  273. /* Firefox 18- */
  274. color: #aeaeb0;
  275. }
  276. }
  277. .input-buttons {
  278. display: flex;
  279. align-items: center;
  280. font-size: 38rpx;
  281. .input-buttons-limit {
  282. font-size: 28rpx;
  283. color: #7e8aa2;
  284. display: none;
  285. //transform: translateY(32rpx);
  286. }
  287. .input-buttons-right {
  288. display: flex;
  289. align-items: center;
  290. .buttons-separate {
  291. background-color: #d5d6d8;
  292. height: 32rpx;
  293. width: 1px;
  294. margin: 0 26rpx;
  295. }
  296. .send-button {
  297. background-color: #2942d4;
  298. border-radius: 32rpx;
  299. width: 66rpx;
  300. height: 66rpx;
  301. display: flex;
  302. justify-content: center;
  303. align-items: center;
  304. padding: 0;
  305. .send-button-stop {
  306. display: inline-block;
  307. width: 32rpx;
  308. height: 32rpx;
  309. background-color: #ffffff;
  310. border-radius: 6rpx;
  311. }
  312. }
  313. }
  314. }
  315. }
  316. &.position-1 {
  317. // position: absolute;
  318. // z-index: 200;
  319. // width: calc(100% - 64rpx);
  320. }
  321. &.bottom {
  322. bottom: 0 !important;
  323. }
  324. &.spread {
  325. padding-bottom: 20rpx;
  326. .input-container {
  327. display: block;
  328. padding: 24rpx 32rpx;
  329. .input-container-input {
  330. width: 100%;
  331. }
  332. .input-buttons {
  333. .input-buttons-limit {
  334. display: block;
  335. }
  336. justify-content: space-between;
  337. }
  338. }
  339. }
  340. }
  341. </style>