inputArea.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <template>
  2. <div class="box-ft">
  3. <transition name="msgbox-fade">
  4. <Emoji v-show="emojiShow" @addEmoji="addEmoji"></Emoji>
  5. </transition>
  6. <chat-at
  7. ref="chatAt"
  8. v-if="atShow"
  9. @atperson="atPerson"
  10. :curInd="atInd"
  11. :filterList="filterMembers"
  12. ></chat-at>
  13. <div class="toolbar">
  14. <i class="look-icon" @click.stop="emojiShow = !emojiShow"></i>
  15. <div class="file-icon">
  16. <input type="file" ref="inputFile" name="res" @change="handleFile">
  17. </div>
  18. <!-- <i class="icon-packet" @click="$packetGet"></i> -->
  19. </div>
  20. <div class="send-content">
  21. <form class="input-wrap" @submit="handleSend">
  22. <textarea
  23. @keydown.up.prevent="handleUp"
  24. @keydown.down.prevent="handleDown"
  25. cols="1"
  26. ref="chatInput"
  27. rows="1"
  28. @keydown.enter="handleKeyDown"
  29. placeholder
  30. v-focus
  31. v-model="inputMsg"
  32. @blur="handleBlur"
  33. />
  34. </form>
  35. </div>
  36. <div class="send-action">Enter发送,Ctrl + Enter 换行
  37. <el-button @click="handleSend">发送</el-button>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. import Emoji from '@/components/emoji'
  43. import chatAt from '@/components/chatAt'
  44. import { Message } from 'element-ui'
  45. import { mapState, mapMutations, mapActions } from 'vuex'
  46. import { chatAtMixin } from '@/mixins'
  47. import ImageMin from '@/util/imageMin.js'
  48. // import editArea from './editArea'
  49. export default {
  50. name: 'inputArea',
  51. mixins: [chatAtMixin],
  52. components: {
  53. Emoji,
  54. chatAt
  55. },
  56. computed: {
  57. ...mapState(['group', 'userId']),
  58. ...mapState({
  59. chatInputFocus: state => state.group.chatInputFocus,
  60. blockList: state => state.group.blockList
  61. }),
  62. filterMembers () {
  63. let val = this.inputMsg.replace('@', '')
  64. let members = this.group.members
  65. let resArr = []
  66. for (let k in members) {
  67. if (k == this.userId) continue
  68. if (val.indexOf(members[k].user_name) === -1) {
  69. resArr.push(members[k])
  70. }
  71. }
  72. return resArr
  73. },
  74. atShow () {
  75. return this.inputMsg.match(/@$/) && this.filterMembers.length
  76. }
  77. },
  78. data () {
  79. return {
  80. emojiShow: false,
  81. inputMsg: '',
  82. atInd: 0
  83. }
  84. },
  85. mounted () {
  86. document.addEventListener('paste', this.initPaste)
  87. document.addEventListener('drop', this.initDrop)
  88. document.addEventListener('dragover', this.initDragOver)
  89. document.body.addEventListener('click', () => {
  90. this.emojiShow = false
  91. })
  92. },
  93. beforeDestroy () {
  94. document.removeEventListener('paste', this.initPaste)
  95. document.removeEventListener('drop', this.initDrop)
  96. document.removeEventListener('dragover', this.initDragOver)
  97. },
  98. methods: {
  99. ...mapMutations(['updateChatInputFocus', 'addChatItem']),
  100. ...mapActions(['doSendMsg', 'doSendFile']),
  101. addEmoji (val) {
  102. this.inputMsg += val
  103. this.emojiShow = false
  104. this.$refs.chatInput.focus()
  105. },
  106. handleKeyDown (event) {
  107. if (this.atShow) {
  108. event.preventDefault()
  109. let item = this.filterMembers[this.atInd]
  110. this.atPerson(item.user_name)
  111. return
  112. }
  113. if (event.altKey || event.ctrlKey || event.metaKey) {
  114. // 单纯换行
  115. this.inputMsg = this.inputMsg + '\n'
  116. } else {
  117. event.returnValue = false
  118. this.handleSend(event)
  119. }
  120. return false
  121. },
  122. /**
  123. * @des 处理消息发送
  124. */
  125. handleSend (e) {
  126. // 判断是否被禁言
  127. if (this.blockList.some(id => id == this.userId)) {
  128. Message({
  129. message: '您已被禁言',
  130. type: 'error'
  131. })
  132. return
  133. }
  134. let text = this.inputMsg.trim()
  135. if (text.length === 0) {
  136. Message({
  137. message: '聊天内容不能为空',
  138. type: 'warning'
  139. })
  140. return
  141. }
  142. let opt = {
  143. type: 0,
  144. msg: text
  145. }
  146. // 用户不是第一次发言
  147. if (this.group.members[this.userId]) {
  148. let createTime = Date.now()
  149. this.addChatItem({
  150. from: this.userId,
  151. content: text,
  152. hash: `${createTime}`,
  153. timestamp: createTime,
  154. createTime,
  155. msg_type: '0',
  156. loading: true
  157. })
  158. opt.createTime = createTime
  159. }
  160. this.doSendMsg(opt)
  161. // 滚到底部
  162. this.$nextTick(function () {
  163. this.inputMsg = ''
  164. this.$emit('toBottom')
  165. })
  166. e.preventDefault()
  167. return false
  168. },
  169. /**
  170. * 文件预处理
  171. * @return {Object} data 预处理文件信息
  172. * @param {Number} data.type
  173. * @param {File} data.res
  174. */
  175. async preHandleFile (file) {
  176. let type = file.type
  177. let size = file.size
  178. if (type.match('video')) {
  179. return size > 3 * 1024 * 1024
  180. ? Promise.reject(new Error(file))
  181. : Promise.resolve({
  182. type: 2,
  183. res: file
  184. })
  185. } else if (type.match('audio')) {
  186. return size > 2 * 1024 * 1024
  187. ? Promise.reject(new Error(file))
  188. : Promise.resolve({
  189. type: 3,
  190. res: file
  191. })
  192. } else if (type.match('image')) {
  193. let image = await new ImageMin({
  194. file: file,
  195. maxSize: 1024 * 1024
  196. })
  197. return {
  198. type: 1,
  199. preview: image.base64,
  200. res: image.res
  201. }
  202. }
  203. },
  204. /**
  205. * @des 处理文件发送
  206. */
  207. async handleFile (e) {
  208. let inputfile
  209. if (e.constructor === File) {
  210. inputfile = e
  211. } else {
  212. inputfile = e.target.files[0]
  213. }
  214. try {
  215. let fileInfo = await this.preHandleFile(inputfile)
  216. let opt = { res: fileInfo.res }
  217. if (this.group.members[this.userId]) {
  218. let createTime = Date.now()
  219. this.addChatItem({
  220. content: fileInfo.preview || '',
  221. from: this.userId,
  222. hash: `${createTime}`,
  223. msg_type: fileInfo.type,
  224. timestamp: createTime,
  225. res: fileInfo.res,
  226. loading: true,
  227. createTime
  228. })
  229. opt.createTime = createTime
  230. }
  231. this.doSendFile(opt)
  232. setTimeout(() => {
  233. this.$refs.inputFile.value = null
  234. this.$emit('toBottom')
  235. }, 100)
  236. } catch (error) {
  237. Message({
  238. message: '上传文件大小限制:音频2M以内,视频3M以内',
  239. type: 'warning'
  240. })
  241. }
  242. },
  243. handleBlur () {
  244. this.updateChatInputFocus(false)
  245. },
  246. initDrop (e) {
  247. e.preventDefault()
  248. let files = Array.from(e.dataTransfer.files)
  249. files.forEach(file => this.handleFile(file))
  250. },
  251. initDragOver (e) {
  252. e.preventDefault()
  253. },
  254. initPaste (event) {
  255. var items = (event.clipboardData || window.clipboardData).items
  256. if (items && items.length) {
  257. Array.from(items).forEach(item => {
  258. let file = item.getAsFile()
  259. if (file) {
  260. this.handleFile(file)
  261. }
  262. })
  263. }
  264. }
  265. }
  266. }
  267. </script>
  268. <style lang="scss" scoped>
  269. .box-ft {
  270. height: 180px;
  271. border-top: 1px solid #d6d6d6;
  272. padding-right: 16px;
  273. position: relative;
  274. }
  275. .toolbar {
  276. padding: 10px 20px;
  277. font-size: 0;
  278. i {
  279. color: #333333;
  280. font-size: 18px;
  281. margin-right: 15px;
  282. vertical-align: middle;
  283. cursor: pointer;
  284. }
  285. }
  286. .send-action {
  287. text-align: right;
  288. }
  289. .look-icon {
  290. background: url("../../assets/icon-face.png") no-repeat;
  291. background-size: 100%;
  292. width: 20px;
  293. height: 20px;
  294. display: inline-block;
  295. vertical-align: middle;
  296. cursor: pointer;
  297. }
  298. .file-icon {
  299. background: url("../../assets/icon-file.png") no-repeat;
  300. background-size: 100%;
  301. width: 19px;
  302. height: 18px;
  303. display: inline-block;
  304. vertical-align: middle;
  305. position: relative;
  306. input[type="file"] {
  307. cursor: pointer;
  308. opacity: 0;
  309. position: absolute;
  310. top: 0;
  311. left: 0;
  312. z-index: 1;
  313. width: 100%;
  314. height: 100%;
  315. }
  316. }
  317. .emoji-list {
  318. position: absolute;
  319. border-radius: 4px;
  320. border: 1px solid #d6d6d6;
  321. left: 10px;
  322. height: 226px;
  323. top: -236px;
  324. overflow: auto;
  325. background-color: #ffffff;
  326. width: 232px;
  327. }
  328. .send-action {
  329. color: #bababa;
  330. font-size: 12px;
  331. button {
  332. margin-left: 10px;
  333. }
  334. }
  335. .input-wrap {
  336. textarea {
  337. display: block;
  338. background-color: #eee;
  339. width: 100%;
  340. box-sizing: border-box;
  341. resize: none;
  342. height: 90px;
  343. line-height: 1.4;
  344. padding-left: 20px;
  345. outline: none;
  346. border: 0;
  347. font-size: 14px;
  348. margin-bottom: 10px;
  349. }
  350. }
  351. .icon-packet{
  352. display: inline-block;
  353. margin-left: 20px;
  354. vertical-align: middle;
  355. background: url('../../assets/icon-packet.png') center center no-repeat;
  356. background-size: 100%;
  357. width: 21px;
  358. height: 21px;
  359. }
  360. </style>