chatInput.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <view class="chat-input-container" :class="{ 'parent-container-focused': isFocus }">
  3. <view class="x-f input-container" :class="{
  4. 'input-container-focused': isFocus,
  5. 'input-container-normal': !isFocus
  6. }">
  7. <input id="txgMsg" ref="inputRef" :placeholder="placeholderText" v-model="inputValue" placeholder-style="color:#e7e7e7;"
  8. placeholder-class="placeholder-style" class=" input-native input-field"
  9. :class="{ 'input-focused': isFocus }" @focus="handleFocus" @blur="handleBlur" cursor-spacing="100"
  10. :adjust-position="false" />
  11. <view class="send-btn" @touchend.prevent="handleSend" @click="handleSend">发送</view>
  12. </view>
  13. <view class="icon-container" :class="{ 'icon-hidden': isFocus }">
  14. <view class="icon-bg" @click="handleMoreClick">
  15. <image src="/static/images/live/more-icon.png" class="icon" />
  16. </view>
  17. <view class="icon-bg" @tap="handleOpenCart">
  18. <image src="/static/images/live/shopping.png" class="icon" />
  19. </view>
  20. <!-- <view class="icon-bg " @tap="handleShowGift">
  21. <image src="/static/images/live/gift.png" class="icon" />
  22. </view> -->
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. name: 'ChatInput',
  29. props: {
  30. placeholderText: {
  31. type: String,
  32. default: '说点什么...'
  33. },
  34. value: {
  35. type: String,
  36. default: ''
  37. },
  38. focused: {
  39. type: Boolean,
  40. default: false
  41. }
  42. },
  43. data() {
  44. return {
  45. isFocus: false,
  46. inputValue: '',
  47. blurTimer: null
  48. };
  49. },
  50. watch: {
  51. focused: {
  52. immediate: true,
  53. handler(newVal) {
  54. this.isFocus = newVal;
  55. }
  56. },
  57. inputValue(newVal) {
  58. this.$emit('input', newVal);
  59. },
  60. value: {
  61. immediate: true,
  62. handler(newVal) {
  63. this.inputValue = newVal;
  64. }
  65. }
  66. },
  67. beforeDestroy() {
  68. if (this.blurTimer) {
  69. clearTimeout(this.blurTimer);
  70. }
  71. },
  72. methods: {
  73. handleMoreClick() {
  74. this.$emit('show-more');
  75. },
  76. handleFocus() {
  77. this.isFocus = true;
  78. this.$emit('focus', this.inputValue);
  79. },
  80. handleBlur() {
  81. // 延迟执行 blur,让点击事件先执行
  82. this.blurTimer = setTimeout(() => {
  83. this.isFocus = false;
  84. this.$emit('blur', this.inputValue);
  85. }, 150);
  86. },
  87. handleSend() {
  88. // 清除 blur 定时器,防止 blur 事件触发
  89. if (this.blurTimer) {
  90. clearTimeout(this.blurTimer);
  91. this.blurTimer = null;
  92. }
  93. if (this.inputValue.trim()) {
  94. this.$emit('send', this.inputValue);
  95. this.inputValue = '';
  96. } else {
  97. uni.showToast({
  98. title: '不能发送空消息',
  99. icon: 'none'
  100. });
  101. }
  102. // 发送后手动让输入框失去焦点,收回键盘
  103. this.$nextTick(() => {
  104. this.isFocus = false;
  105. this.$emit('blur', this.inputValue);
  106. // 使用 uni-app 的 blur 方法收回键盘
  107. uni.hideKeyboard();
  108. });
  109. },
  110. handleOpenCart() {
  111. this.$emit('open-cart');
  112. },
  113. handleShowGift() {
  114. this.$emit('show-gift');
  115. },
  116. clearInput() {
  117. this.inputValue = '';
  118. },
  119. setInputValue(value) {
  120. this.inputValue = value;
  121. },
  122. getInputValue() {
  123. return this.inputValue;
  124. }
  125. }
  126. };
  127. </script>
  128. <style scoped lang="scss">
  129. .chat-input-container {
  130. display: flex;
  131. justify-content: space-between;
  132. align-items: center;
  133. padding: 24rpx;
  134. width: 100%;
  135. box-sizing: border-box;
  136. transition: all 0.3s ease;
  137. &.parent-container-focused {
  138. justify-content: flex-start;
  139. }
  140. }
  141. .input-container {
  142. background: rgba(0, 0, 0, 0.3);
  143. height: 90rpx;
  144. box-sizing: border-box;
  145. border-radius: 36rpx;
  146. display: flex;
  147. align-items: center;
  148. transition: all 0.3s ease;
  149. overflow: hidden;
  150. &.input-container-normal {
  151. padding: 10rpx 14rpx 10rpx 32rpx;
  152. flex: 1;
  153. justify-content: space-between;
  154. }
  155. &.input-container-focused {
  156. padding: 10rpx 10rpx 10rpx 20rpx;
  157. width: 100%;
  158. flex: 1 0 auto;
  159. justify-content: flex-start;
  160. gap: 20rpx;
  161. }
  162. }
  163. .input-field {
  164. border: none;
  165. font-size: 32rpx;
  166. color: #ffffff;
  167. background: transparent;
  168. flex: 1;
  169. min-width: 0;
  170. transition: all 0.3s ease;
  171. margin-left: 20rpx;
  172. &.input-focused {
  173. flex: 1;
  174. min-width: 0;
  175. }
  176. }
  177. .send-btn {
  178. background-color: #fafafa;
  179. border-radius: 28rpx;
  180. padding: 12rpx 16rpx;
  181. color: #181818;
  182. font-weight: 500;
  183. min-width: 80rpx;
  184. text-align: center;
  185. flex-shrink: 0;
  186. white-space: nowrap;
  187. cursor: pointer;
  188. transition: all 0.2s ease;
  189. &:active {
  190. opacity: 0.8;
  191. transform: scale(0.98);
  192. }
  193. }
  194. .icon-container {
  195. display: flex;
  196. align-items: center;
  197. justify-content: space-between;
  198. flex-shrink: 0;
  199. transition: all 0.3s ease;
  200. margin-left: 20rpx;
  201. &.icon-hidden {
  202. opacity: 0;
  203. visibility: hidden;
  204. width: 0;
  205. margin-left: 0;
  206. flex: 0;
  207. }
  208. }
  209. .icon-bg {
  210. background: rgba(0, 0, 0, 0.3);
  211. border-radius: 50%;
  212. width: 88rpx;
  213. height: 88rpx;
  214. display: flex;
  215. justify-content: center;
  216. align-items: center;
  217. transition: transform 0.2s ease;
  218. //margin-left: 20rpx;
  219. cursor: pointer;
  220. &:active {
  221. transform: scale(0.95);
  222. }
  223. &+& {
  224. margin-left: 20rpx;
  225. }
  226. .icon {
  227. width: 68rpx;
  228. height: 68rpx;
  229. }
  230. }
  231. </style>