JwChat实现聊天框功能

42 篇文章 8 订阅

实现前言:

       这是一个基于element的聊天框插件,可扩展性还是很强的。

JwChat:官网入口
gitee地址:入口

大佬图片与实现效果:

 

实现步骤:

1、安装:

cnpm i jwchat -S

 2、配置使用:

//element 必须
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

//聊天室-基于element
import Chat from 'jwchat';
Vue.use(Chat)

3、页面调用:

<JwChat-index
      :config="config"
      :taleList="list"
      @enter="bindEnter"
      v-model="inputMsg"
      :showRightBox='true'
      scrollType="scroll"
      :winBarConfig="winBarConfig"
      :quickList="config.quickList"
      @clickTalk="talkEvent"
    >
      <!-- :toolConfig="tool" -->
       <JwChat-rightbox class="rightSlot" :config="rightConfig" @click="rightClick" />
      <!--<JwChat-talk class="rightSlot" :Talelist="talk" :config="quickConfig" @event="bindTalk" />-->
      <template slot="tools">
        <div style="width:20rem;text-align:right;" @click="toolEvent(12)">
          <JwChat-icon type="icon-lishi" title="自定义"/>
        </div>
      </template>
    </JwChat-index>

调用源码:

<template>
  <div class="jwchat__body wrapper">
    <JwChat-index
      :config="config"
      :taleList="list"
      @enter="bindEnter"
      v-model="inputMsg"
      :showRightBox='true'
      scrollType="scroll"
      :winBarConfig="winBarConfig"
      :quickList="config.quickList"
      @clickTalk="talkEvent"
    >
      <!-- :toolConfig="tool" -->
       <JwChat-rightbox class="rightSlot" :config="rightConfig" @click="rightClick" />
      <!--<JwChat-talk class="rightSlot" :Talelist="talk" :config="quickConfig" @event="bindTalk" />-->
      <template slot="tools">
        <div style="width:20rem;text-align:right;" @click="toolEvent(12)">
          <JwChat-icon type="icon-lishi" title="自定义"/>
        </div>
      </template>
    </JwChat-index>
  </div>
</template>

<script>
  const img = 'https://www.baidu.com/img/flexible/logo/pc/result.png'
  const listData = [
    {
      "date": "2020/04/25 21:19:07",
      "text": {
        "text": "<i class='el-icon-document-checked' style='font-size:2rem;'/>",
        "subLink":{
          "text": "a.txt",
          "prop": {
            underline: false
          }
        },

      },
      "mine": false,
      "name": "留恋人间不羡仙",
      "img": "https://img0.baidu.com/it/u=3066115177,3339701526&fm=26&fmt=auto&gp=0.jpg"
    },
    {
      "date": "",
      "text": { "text": "起床不" },
      "mine": false,
      "name": "留恋人间不羡仙",
      "img": "https://img0.baidu.com/it/u=3066115177,3339701526&fm=26&fmt=auto&gp=0.jpg"
    },
    {
      "text": "2020/04/25 21:19:07",
      "type": 'tip'
    },
    {
      "date": "2020/04/25 21:19:07",
      "text": { "text": "<audio data-src='https://www.w3school.com.cn/i/horse.mp3'/>" },
      "mine": false,
      "name": "只盼流星不盼雨",
      "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
    },
    {
      "date": "2020/04/25 21:19:07",
      "text": { "text": "<img data-src='"+img+"'/>" },
      "mine": false,
      "name": "只盼流星不盼雨",
      "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
    },
    {
      "date": "2020/04/16 21:19:07",
      "text": { "text": "<video data-src='https://www.w3school.com.cn/i/movie.mp4' controls='controls' />" },
      "mine": true,
      "name": "JwChat",
      "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
    },{
      "date": "2021/03/02 13:14:21",
      "mine": false,
      "name": "留恋人间不羡仙",
      "img": "https://img0.baidu.com/it/u=3066115177,3339701526&fm=26&fmt=auto&gp=0.jpg",
      "text": {
        system: {
          title: '在接入人工前,智能助手将为您首次应答。',
          subtitle: '猜您想问:',
          content: [
            {
              id: `system1`,
              text: '组件如何使用'
            },
            {
              id: `system2`,
              text: '组件参数在哪里查看'
            },
            {
              id: 'system',
              text: '我可不可把组件用在商业'
            }
          ]
        }
      }
    }
  ]
  function getListArr(size) {
    const listSize = listData.length
    if(!size){
      size = listSize
    }
    let result = []
    for (let i = 0; i < size; i++) {
      const item = listData[Math.random()*listSize>>0]
      item.id = Math.random().toString(16).substr(-6)
      result.push(item)
    }
    return result
  }
  export default {
    components: { },
    data () {
      return {
        inputMsg: '',
        list: [],
        tool: {
          // show: ['file', 'history', 'img', ['文件1', '', '美图']],
          // showEmoji: false,
          callback: this.toolEvent
        },
        rightConfig: {
          listTip: '当前在线',
          // notice: '【公告】这是一款高度自由的聊天组件,基于AVue、Vue、Element-ui开发。点个赞再走吧 ',
          list: [
            {
              name: 'JwChat',
              "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg",
              id:1,
            },
            {
              id:2,
              name: 'JwChat',
              "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
            },
            {
              id:3,
              name: 'JwChat',
              "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
            },
            {
              id:4,
              name: '留恋人间不羡仙',
              "img": "https://img0.baidu.com/it/u=3066115177,3339701526&fm=26&fmt=auto&gp=0.jpg"
            },
            {
              name: '只盼流星不盼雨',
              "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
            }
          ]
        },
        rightConfig2:{
          listTip: '当前在线',
          // notice: '【公告】这是一款高度自由的聊天组件,基于AVue、Vue、Element-ui开发。点个赞再走吧 ',
          list: [
            {
              name: 'JwChat',
              "img": "https://img1.baidu.com/it/u=2109725846,3376113789&fm=26&fmt=auto&gp=0.jpg"
            },
            {
              name: '留恋人间不羡仙',
              "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
            },
            {
              name: '只盼流星不盼雨',
              "img": "https://img1.baidu.com/it/u=2109725846,3376113789&fm=26&fmt=auto&gp=0.jpg"
            }
          ]
        },
        quickConfig: {
          nav: ['快捷回复', '超级回复'],
          showAdd: true,
          maxlength: 200,
          showHeader: true,
          showDeleteBtn: true,
        },
        talk: [
          '快捷回复1',
          '快捷回复2',
          '快捷回复3',
          '快捷回复4',
          '快捷回复5',
          '快捷回复6',
        ],
        config: {
          img: 'https://img1.baidu.com/it/u=2109725846,3376113789&fm=26&fmt=auto&gp=0.jpg',
          name: 'JwChat',
          dept: '最简单、最便捷',
          callback: this.bindCover,
          historyConfig:{
            show: true,
            tip: '加载更多',
            callback: this.bindLoadHistory,
          },
          quickList: [
            {text: '这里是jwchat,您想了解什么问题。', id:3},
            {text: 'jwchat是最好的聊天组件', id:4},
            {text: '谁将烟焚散,散了纵横的牵绊;听弦断,断那三千痴缠。', id:5},
            {text: '长夏逝去。山野间的初秋悄然涉足。', id:6},
            {text: '江南风骨,天水成碧,天教心愿与身违。', id:7},
            {text: '总在不经意的年生。回首彼岸。纵然发现光景绵长。', id:8},
            {text: '外面的烟花奋力的燃着,屋里的人激情的说着情话', id:10},
            {text: '假如你是云,我就是雨,一生相伴,风风雨雨;', id:11},
            {text: '即使泪水在眼中打转,我依旧可以笑的很美,这是你学不来的坚强。', id:12},
            {text: ' 因为不知来生来世会不会遇到你,所以今生今世我会加倍爱你。', id:13},
          ]
        },
        winBarConfig: {
          active: 'win01',
          width: '160px',
          listHeight: '60px',
          list: [ {
            id: 'win00',
            img: 'https://img1.baidu.com/it/u=2109725846,3376113789&fm=26&fmt=auto&gp=0.jpg',
            name: 'JwChat',
            dept: '最简单、最便捷',
            readNum: 1
          },
            {
              id: 'win01',
              img: 'https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg',
              name: '阳光明媚爱万物',
              dept: '沙拉黑油',
              readNum: 12
            },
            {
              id: 'win02',
              img: 'https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg',
              name: '只盼流星不盼雨',
              dept: '最后说的话',
              readNum: 12
            },
            {
              id: 'win03',
              img: 'https://img0.baidu.com/it/u=3066115177,3339701526&fm=26&fmt=auto&gp=0.jpg',
              name: '留恋人间不羡仙',
              dept: '这里可以放万物',
              readNum: 0
            },
            {
              id: 'win04',
              img: 'https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg',
              name: '阳光明媚爱万物',
              dept: '官方客服'
            }],
          callback: this.bindWinBar,

        }
      }
    },
    methods: {
      /**
       * @description: 点击加载更多的回调函数
       * @param {*}
       * @return {*}
       */
      bindLoadHistory () {
        const history = new Array(3).fill().map((i, j) => {
          return {
            "date": "2020/05/20 23:19:07",
            "text": { "text": j + new Date() },
            "mine": false,
            "name": "JwChat",
            "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
          }
        })
        let list = history.concat(this.list)
        this.list = list
        console.log('加载历史', list, history)
      },
      talkEvent (play) {
        console.log(play)
        alert(1)
      },
      bindEnter (str) {
        const msg = this.inputMsg
        console.log(msg, str);
        if (!msg) return;
        const msgObj = {
          "date": "2020/05/20 23:19:07",
          "text": { "text": msg },
          "mine": true,
          "name": "JwChat",
          "img": "https://img1.baidu.com/it/u=31094377,222380373&fm=26&fmt=auto&gp=0.jpg"
        }
        this.list.push(msgObj)
      },
      /**
       * @description:
       * @param {*} type 当前点击的按钮
       * @param {*} plyload 附加文件或者需要处理的数据
       * @return {*}
       */
      toolEvent (type, plyload) {
        console.log('tools', type, plyload)
      },
      bindCover (event) {
        console.log('header', event)
      },
      rightClick (type) {
        console.log('rigth', type)
      },
      bindTalk (play) {
        console.log('talk', play)
        const { key, value } = play
        if(key === 'navIndex')
          this.talk = [1,1,1,1,1,1,1,1].reduce((p)=>{
            p.push('随机修改颜色 #'+Math.random().toString(16).substr(-6))
            return p
          },[])
        if(key === 'select') {
          this.inputMsg = value
          this.bindEnter()
        }
        if(key === 'delIndex') {
          this.talk.splice(value,1)
        }
      },
      bindWinBar (play = {}) {
        const {type, data={}} = play
        console.log(play);
        if(type==='winBar'){
          const { id, dept, name, img } = data
          this.config = {...this.config,  id, dept, name, img}
          this.winBarConfig.active = id
          if(id==='win00'){
            this.list = getListArr()
          } else
            this.list = getListArr(Math.random()*4>>0)
        }
        if(type==='winBtn'){
          const { target: {id } = {} } = data
          const {list} = this.winBarConfig
          this.winBarConfig.list =list.reduce((p,i)=>{
            if(id!=i.id)
              p.push( i )
            return p
          },[])
        }
      }
    },
    mounted () {
      this.list = getListArr()
    }
  }
</script>
<style scoped>
  .rightSlot {
    width: 100%;
    height: 100%;
    overflow: hidden;
    flex-direction: column;
  }
</style>
<style>
  .active {
    color: red;
  }
  .jwchat__body {
    height: 100vh;
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
  }
</style>

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
JWChat源码 1、首先配置jwchat要具备的环境(我开发的环境) tomcat7.0 jdk1.7 openfire3.9.3 2.在网上查找下在jwchat.war文件,将其解压后放到tomcat目录下的webapps下 3.下载xalan的xalan.jar,serializer.jar, xercesImpl.jar, xml-apis.jar放到webapps\jwchat\WEB-INF\lib目录下即可。 4.修改配置文件 config.js 复制代码 1 var SITENAME = "127.0.0.1";//127.0.0.1是你openfire服务器的名称 2 var BACKENDS = 3 [ 4 { 5 name:"Open Relay", 6 description:"HTTP Binding backend that allows connecting to any jabber server", 7 httpbase:"/jwchat/JHB/", 8 type:"binding", 9 default_server: SITENAME 10 } 11 ]; 复制代码 jsjac.js 在302行if(this.has_sasl)前加this.has_sasl=false; web.xml 1 <servlet-mapping> 2 <servlet-name>Jabber HTTP Binding Servlet</servlet-name> 3 <url-pattern>/JHB/</url-pattern> <!--这里的修改主页是对应上面的config.js,如果config.js的httpbase配置是/jwchat/,那这里的就不用修改了 --> 4 </servlet-mapping> (黄色表示要修改的部分) 5.启动tomcat ,访问http://l127.0.0.1:8080/jwchat/ (切记如果你的openfire服务器名称为127.0.0.1,访问jwchat时浏览器输入127.0.0.1,如果输入localhost则会报Authorization failed),我就是因为这个问题烦恼了很长一段时间。 <报错修改> 1.如果报Service unavailable A.可能是openfire没有启动 B.config.js中的var SITENAME = "127.0.0.1";没有与openfire服务器名称相同   2修改openfire服务器名称 A.点击服务器,服务器管理器,编辑属性 修改服务器名称,修改完的服务器名称前有感叹号 B.出现叹号,我们要删除安全证书,点击服务器,服务器设置,服务器证书删掉这里的证书。 C.删除后,在当前页面会出现提示,然后按照提示点击相应的链接,重启服务器,然后登陆,再生成证书 D.然后生成完成后,手动重启openfire服务器,再次登陆发现警告没有了。 3. 在服务器端增加配置 xmpp.httpbind.client.requests.polling = 0 xmpp.httpbind.client.requests.wait = 10
一、apache服务器配置。 我们下载安装的是httpd-2.2.17-win32-x86-no_ssl.msi,安装完成后,我们需要配置一下,由于jwchat是用javacript去和openfire进行通讯的,所以他们之间的通讯是基于http的,但是由于浏览器为了安全性是不允许javascript跨域访问的。我们必须通过别的技术来绕过这限制,所以我们采取apache服务器的重定向功能去突破这个限制。 安装完成后进入到apache的安装目找到conf文件夹下的httpd.conf文件,用记事本打开,把下列几个配置项放开(默认被注释掉了) 1、LoadModule rewrite_module modules/mod_rewrite.so 2、LoadModule proxy_module modules/mod_proxy.so 3、LoadModule proxy_http_module modules/mod_proxy_http.so 然后再在本配置文件的末尾加入如下几行配置 ServerName blzc.com <Directory /var/jwchat> Options +Indexes +MultiViews </Directory> AddDefaultCharset UTF-8 RewriteEngine on ProxyPass /jwchat/http-bind/ http://blzc.com:7070/http-bind/ 在此有必要对proxyPass的参数做些说明 “/jwchat/http-bind/”:jwchat就通过访问http://域名/jwchat/http-bind/地址去和openfire通讯,apache接到请求后就会重新定向到http://blzc.com:7070/http-bind/ “http://blzc.com:7070/http-bind/”:被重新定向的地址,也就是我们的openfire的http访问地址。7070端口是openfire的默认访问端口,当然我们也可以进入openfire进行配置。 二、openfire的配置 安装好openfire后进入web式的管理界面,选择服务器-》服务器管理器-》系统属性 在里边添加两个属性 xmpp.httpbind.client.requests.polling = 0 xmpp.httpbind.client.requests.wait = 10 三、部署jwchat jwchat解压后的文件内容放到apache访问目录/jwchat文件夹下, 然后打开config.js文件,把相应的内容修改为如下的形式 var SITENAME = "blzc.com";//你要访问openfir的域名 name:"Native Binding", description:"Ejabberd's native HTTP Binding backend", httpbase:"/jwchat/http-bind/",//访问openfire的地址 type:"binding", servers_allowed:[SITENAME] 最后再把jwchat文件夹考到apache的htdocs文件夹下,重新启动服务,然后访问http://localhost/jwchat即可。 若出现“authorization failed”,则可能是SSL不支持的原因,需要修改jsjac.js,在if(this.has_sasl) 前面加上this.has_sasl=false;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值