1.1.5 • Published 4 years ago

fonlineconsultation v1.1.5

Weekly downloads
52
License
-
Repository
-
Last release
4 years ago
FChat使用手册:
目录

Table of Contents generated with DocToc


简介

简易版的仿微信版移动端h5聊天插件,使用简单,兼容音频,高度定制化,ios/android兼容。


安装
npm i fonlineconsultation

使用方法

main.js

import '../node_modules/fonlineconsultation/packages/styles/index.less';
import fonlineconsultation from 'fonlineconsultation';
Vue.use(fonlineconsultation);
//音频开启,参照下面音频章节
import '../node_modules/fonlineconsultation/packages/recorder.js';

x.vue

<FonlineConsultation></FonlineConsultation>

数据结构
  {
         type:消息类型(message / image / video / audio),
         content:消息内容,
         pos:消息位置(left/right),
         ? topText: //头像上方的文字显示(可选)
         ? duration:"" //音频时间 仅音频存在
  }

可配置参数

必要方法

方法名作用
send获取普通消息回调,用来发送
sendImg获取图片/视频(可开关)信息回调,用来发送
sendVoice获取音频消息回调,用来发送

如何使用?

<FonlineConsultation
    @send="sendMsg"
    @sendImg="sendImg"
    @sendVoice="senVoice"
></FonlineConsultation>

方法的定义

/*
*@ send
*/
sendMsg(sendObject){
    //sendObject 为消息对象
}
/*
*@ sendImg
*
*/
sendImg(pro){
   pro.then((sendObject)=>{
       //sendObject为图片消息对象
   })
}
/*
*@ sendVoice
*
*/
sendVoice(sendObject){
    //sendObject为消息对象
}

使用样例

/*
* sendMessage为服务器api
*/  
methods: {
    // 模拟一个复读机
    sendImg(cb) {
      cb.then((sendObj) => {
        this.$FChat.loading();
        sendMessage(sendObj).then((res) => {
          if (res.data) { 
            this.$FChat.cancelLoading();
            // 我方发送
            this.$FChat.sendMessage(sendObj);
            // 对方发送
            let { content, pos, type } = res.data;
            this.$FChat.sendMessage({
              content: content,
              type: type,
              pos: "left",
            });
          }
        });
      }).catch((err) => {
        alert(err);
      });
    },
    // 普通文本消息
    sendMsg(msgObject) {
      this.$FChat.loading();
      sendMessage(msgObject).then((res) => {
        if (res.data) { 
          this.$FChat.cancelLoading();
          // 己方发送
          this.$FChat.sendMessage(msgObject);
          // 服务器返回 - 格式拼接
          let { content, pos, type } = res.data;
          this.$FChat.sendMessage({
            content: content,
            type: type,
            pos: "left"
          });
        }
      });
    },
    //音频
    senVoice(msgObject) {
      sendMessage(msgObject).then((res) => {
        if (res.data) { 
          this.$FChat.cancelLoading();
          // 己方发送
          this.$FChat.sendMessage(msgObject);
          // 服务器返回 - 格式拼接
          let { content, pos, type, duration } = res.data;
          this.$FChat.sendMessage({
            content: content,
            type: type,
            pos: "left",
            duration: duration
          });
        }
      });
    },
  }

属性参数

参数名功能示例说明类型
usernameheader头部的联系人:username="'admin'"String
contentHeight聊天区域高度:contentHeight="200"不建议修改,如果有header头可以修改FChat类的ToastTop,兼容性较好,而且请确认插件位于页面最低,防止出现输入法弹出高度问题Number
contentBg聊天区域的背景颜色:contentBg="'#123456'"String
preventId控制音频长按时禁止选中的范围(防止长按选中导致失去焦点,详情见注意):preventId="'allContent'"插件通过document.getElementById()获取String
leftIcon左侧的图标(音频):leftIcon="'iconFont icon-xxx'"左侧图标(音频/键盘)切换时的音频图标String
leftIconChange左侧图标(键盘)同上左侧图标(音频/键盘)切换时的键盘图标String
leftIconSize左侧图标的大小,两个相同默认大小23Number
leftIconColor左侧图标的颜色十六进制/rgb颜色默认为#000String
rightIconLeft右侧图标的最左侧图标(图片)String
rightIconRight右侧图标的最左侧图标(发送)String
rightIconSize右侧图标的大小,两个相同默认大小20Number
rightIconColor右侧图标的颜色十六进制/rgb颜色String
popBg聊天气泡颜色十六进制/rgb颜色默认#6AEA9EString
popSize聊天气泡字号默认15Number
popRightAva聊天气泡右侧头像图片链接String
popLeftAva聊天气泡左侧头像图片链接String

插槽

(如需自定义请重写方法)

名称作用
leftIcon左侧图标插槽(带有切换音频/文本方法)
centerText中间的输入框插槽(原框带仿微信文本换行功能,替换后如需要请自行实现)
rightIcon右侧图标插槽(带有选择图片,发送文本方法包含发送后动态宽高的计算)
loading图片loading(id为messageLoading,绑定着消息发送的loading方法)
header整个header的插槽,注意要设置id为onlineHeader方便计算内容高度
hLeftheader左侧功能
hCenterheader中间整体
hRightheader右侧功能

单独气泡重写插槽

插槽名称说明
message文字消息插槽
image图片消息插槽
audio音频消息插槽
video视频消息插槽
topText气泡上方文字(需数据结构中存在topText)

该条消息的数据均可通过

//message
<template v-slot:message="message">
	{{message}}
</template>
//image
<template v-slot:image="image">
	{{image}}
</template>
//audio
<template v-slot:audio="audio">
	{{audio}}
</template>
//video
<template v-slot:voice="voice">
	{{voice}}
</template>
<template v-slot:topText ="item">
    {{item}}
</template>

来获取当前信息所对应的对象。

注:如果文本消息出现类似网址,则会渲染成a标签:

content:"我是一个网址<a>http://www.baidu.com</a>"

整体聊天气泡重写插槽

插槽名为:popList

你可以通过如下方式获取到消息列表:

this.$FChat.getMessage()
//或者使用插槽的返回值,也可以拿到消息列表
<FonlineConsultation  @send="sendMsg" @sendImg="sendImg" @sendVoice="senVoice">
    <template v-slot:popList ="List">
        {{List}}
    </template>
</FonlineConsultation>    

音频开启方法

为了多端兼容使用Recorder.js插件,关于Vue使用Recorder.js的demo可参照样例

直接引入到main.js,可通过this.$Recorder调用相关api

import './node_modules/fonlineconsultation/packages/recorder.js';

在需要的.vue中调用此方法并键入微信授权/下载素材的url

mounted(){
   this.$FChat.openVoice("https://fepic.natapp4.cc/api/getSign", true);
}

FChart类

可修改属性 - 通过setConfig修改

  • maxSize - 图片最大大小(默认为2M)
  • onlyImg - 是否只允许发图片(默认为图片+视频都可以发送)
  • ToastTop - 如果该插件在你的项目中并非全屏显示,则需要传入该插件距顶部所差高度,用于响应式计算,请务必将插件置于页面最底,防止出现输入法弹出后的高度计算问题
//比如项目上有个header,高度为100,则这样做即可
this.$FChat.setConfig('ToastTop',100);

方法一览(*为常用)

方法作用参数
openVoice*修改微信地址和授权url - 服务器地址(微信授权+下载素材)isPromise - 是否自动进行音频授权
setConfig*设置基本属性key - 要修改的属性名value - 修改为config - 对象形式(和上面二选一)
getLoading获取当前loading状态null
loading*显示全屏loadingtime - 显示时长loadingId - 自定义loading的id(display切换显示隐藏),没有可不传texDisabled - 文本框在loading状态下是否disabled,默认false
cancelLoading*取消loading状态loadingId - 自定义loading的id(display切换显示隐藏),没有可不传
scrollMessage*滚动到消息框最底部null
delMessage清空消息框(input)null
setMessageList*渲染返回的消息列表(格式要为指定格式)arr - 消息列表handleFuc - 可自定该如何处理(默认为直接渲染)
getMessage*获取消息列表null
addMessage获取普通消息对象msg - 内容pos - 方位(left / right)
addImage获取图片对象pos - 方位
sendMessage*发送图片/文字消息,渲染FchatObj - 即消息对象
playVoice*开始录音,返回promisenull
pauseVoice*停止录音,返回promise,包含blob和durationnull
sendVoice*发送音频消息,渲染content - blob对象/音乐网址等auto可以读取的pos - 方位duration - 音频持续时间
playVoice*播放音频src - 音频链接/blob等audio支持的格式duration - 持续时间
createToast*创建提示框(顶部)msg - 提示消息timer - 持续时间

Demo

Vue样例


注意

由于h5本身自带的长按复制功能,按钮长按后会出现选中文本导致失效,所以在长按事件中加入了禁止长按复制功能(松开后自动取消),但仅仅限制了该插件的范围,可通过传入preventId属性来增大限制范围防止误选中,主要使用user-select等属性进行控制。

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

11.0.7

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago