index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <div class="action-container">
  3. <div class="action-list">
  4. <el-popover
  5. v-for="(item, index) in getOptions"
  6. :key="index"
  7. placement="top"
  8. trigger="hover"
  9. :content="item.content"
  10. popper-class="custom-popper"
  11. :close-delay="10"
  12. >
  13. <el-upload
  14. v-if="item.type == 'image'"
  15. action="#"
  16. accept=".jpg,.png,.svg,.jpeg,.gif,.bmp"
  17. :file-list="imageList"
  18. :http-request="uploadImage"
  19. :show-file-list="false"
  20. :multiple="true"
  21. :before-upload="beforeUploadImage"
  22. slot="reference"
  23. >
  24. <span class="action-img image"></span>
  25. </el-upload>
  26. <el-upload
  27. v-else-if="item.type == 'attach'"
  28. action="#"
  29. accept=".pdf,.word,.docx,.ppt,.pptx"
  30. :file-list="attachList"
  31. :http-request="uploadFiles"
  32. :show-file-list="false"
  33. :multiple="true"
  34. :before-upload="beforeUploadAttach"
  35. slot="reference"
  36. >
  37. <span class="action-img attach"></span>
  38. </el-upload>
  39. <span v-else-if="isRobot === 2" class="action-img" :class="item.type" slot="reference" @click="onClick(item.type)"></span>
  40. </el-popover>
  41. </div>
  42. <div v-if="isLink">
  43. <slot></slot>
  44. <div class="turn-btn" @click.stop="onTurn">转人工</div>
  45. <slot></slot>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. import { Popover, Upload } from 'element-ui'
  51. import { fileUpload } from '@/api/modules/'
  52. export default {
  53. name: 'action-container',
  54. components: {
  55. [Popover.name]: Popover,
  56. [Upload.name]: Upload
  57. },
  58. props: {
  59. /**
  60. * 支持的配置['keyboard', 'sound', 'image', 'attach', 'rate']
  61. * 键盘:keyboard
  62. * 语音:sound
  63. * 图片:image
  64. * 附件:attach
  65. * 评分:rate
  66. */
  67. options: {
  68. type: Array,
  69. default: () => {
  70. return ['image']
  71. }
  72. },
  73. /**
  74. * 右侧人工跳转
  75. */
  76. isLink: {
  77. type: Boolean,
  78. default: false
  79. },
  80. isRobot: {
  81. type: Number,
  82. default: 0
  83. }
  84. },
  85. data () {
  86. return {
  87. list: this.options,
  88. imageList: [],
  89. attachList: []
  90. }
  91. },
  92. watch: {
  93. options (val) {
  94. this.list = val
  95. }
  96. },
  97. computed: {
  98. getOptions () {
  99. const list = this.list
  100. const arr = list.map(v => {
  101. const temp = {}
  102. switch (v) {
  103. case 'keyboard':
  104. temp.type = 'keyboard'
  105. temp.content = '键盘'
  106. break
  107. case 'sound':
  108. temp.type = 'sound'
  109. temp.content = '语音'
  110. break
  111. case 'image':
  112. temp.type = 'image'
  113. temp.content = '图片'
  114. break
  115. case 'attach':
  116. temp.type = 'attach'
  117. temp.content = '上传附件'
  118. break
  119. case 'rate':
  120. temp.type = 'rate'
  121. temp.content = '评价客服'
  122. break
  123. }
  124. return temp
  125. })
  126. return arr
  127. }
  128. },
  129. methods: {
  130. onClick (data) {
  131. this.$emit('action', data)
  132. },
  133. onTurn () {
  134. this.$emit('turn')
  135. },
  136. async uploadImage (file) {
  137. // this.$emit('upload-image', file.file)
  138. const params = new FormData()
  139. params.append('file', file.file)
  140. const { data } = await fileUpload(params)
  141. if (data) {
  142. this.imageList.push(data)
  143. this.$emit('upload-image', data)
  144. }
  145. },
  146. async uploadFiles (file) {
  147. const params = new FormData()
  148. params.append('file', file.file)
  149. const { data } = await fileUpload(params)
  150. if (data) {
  151. this.attachList.push(data)
  152. this.$emit('upload-attach', data)
  153. }
  154. },
  155. beforeUploadImage (file) {
  156. const size = file.size / 1024 / 1024
  157. const fileSuffix = file.name.substring(file.name.lastIndexOf('.') + 1)
  158. const whiteList = ['jpg', 'jpeg', 'png', 'svg', 'gif', 'bmp']
  159. if (size > 10) {
  160. this.$toast('上传失败,文件大小不能超过10M')
  161. return false
  162. }
  163. if (whiteList.indexOf(fileSuffix) === -1) {
  164. this.$toast('上传失败,只能选择图片格式')
  165. return false
  166. }
  167. },
  168. beforeUploadAttach (file) {
  169. const size = file.size / 1024 / 1024
  170. const fileSuffix = file.name.substring(file.name.lastIndexOf('.') + 1)
  171. const whiteList = ['doc', 'docx', 'pdf', 'ppt', 'pptx']
  172. if (size > 10) {
  173. this.$toast('上传失败,文件大小不能超过10M')
  174. return false
  175. }
  176. if (whiteList.indexOf(fileSuffix) === -1) {
  177. this.$toast('上传失败,只能选择PDF、WORD、PPT格式')
  178. return false
  179. }
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss">
  185. .custom-popper{
  186. min-width: 0;
  187. padding: 8px 16px;
  188. }
  189. </style>
  190. <style lang="scss" scoped>
  191. .action-container{
  192. width: 100%;
  193. height: 48px;
  194. display: flex;
  195. justify-content: space-between;
  196. padding: 8px 0 16px;
  197. box-sizing: border-box;
  198. .action-list{
  199. display: flex;
  200. align-items: center;
  201. }
  202. &.white{
  203. background: #fff;
  204. }
  205. .action-img{
  206. display: inline-block;
  207. width: 24px;
  208. height: 24px;
  209. margin-right: 16px;
  210. background-repeat: no-repeat;
  211. background-position: center;
  212. background-size: contain;
  213. cursor: pointer;
  214. &.keyboard{
  215. background-image: url('~@/assets/image/keyboard.png');
  216. }
  217. &.sound{
  218. background-image: url('~@/assets/image/sound.png');
  219. }
  220. &.image{
  221. background-image: url('~@/assets/image/image.png');
  222. }
  223. &.attach{
  224. background-image: url('~@/assets/image/attach.png');
  225. }
  226. &.rate{
  227. background-image: url('~@/assets/image/rate.png');
  228. }
  229. }
  230. .turn-btn{
  231. padding: 4px 16px 4px 36px;
  232. border: 1px solid $color_main;
  233. border-radius: 4px;
  234. color: $color_main;
  235. font-size: 12px;
  236. background: url('~@/assets/image/hand.png') no-repeat 16px center;
  237. background-size: 18px 18px;
  238. cursor: pointer;
  239. }
  240. }
  241. </style>