App.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <div id="app">
  3. <transition name='fade' mode="out-in">
  4. <router-view/>
  5. </transition>
  6. </div>
  7. </template>
  8. <script>
  9. import Vue from 'vue';
  10. export default {
  11. name: 'App',
  12. data(){
  13. return{
  14. reconnectData:null,
  15. lockReconnect:false, //避免重复连接,因为onerror之后会立即触发 onclose
  16. timeout:5000, //10s一次心跳检测
  17. timeoutObj:null,
  18. serverTimeoutObj:null,
  19. apiToken:'',
  20. linkfailure:true,
  21. Messagenum:0,
  22. kfuser_info:{}
  23. }
  24. },
  25. methods:{
  26. /**************************************/
  27. //初始化weosocket
  28. initWebSocket(apiToken) {
  29. // 书写接口信息
  30. // http://stadmin.bocai108.com/
  31. const wsuri = "ws://ky.281570.com:9101?apiToken=" + apiToken;//www.service.com 线上
  32. // const wsuri = "ws://192.168.2.187:9101?apiToken=" + apiToken;//www.service.com //192.168.2.186 本地
  33. // 创建websocket实例
  34. Vue.prototype.$websocket = new WebSocket(wsuri);
  35. this.$websocket.onopen = this.websocketonopen; //连接成功
  36. this.$websocket.onmessage = this.websocketonmessage; //接收消息
  37. this.$websocket.onerror = this.websocketonerror;//链接错误提示
  38. this.$websocket.onclose = this.socket_close;//链接断开提示
  39. },
  40. // 得到保存好的当前客服信息
  41. getCustomerUserInfo()
  42. {
  43. if(typeof this.$store.getters.get_user_info != 'string'){
  44. this.kfuser_info = this.$store.getters.get_user_info;
  45. }else if(this.$store.getters.get_user_info!= ""){
  46. this.kfuser_info = JSON.parse(this.$store.getters.get_user_info);
  47. }
  48. return this.kfuser_info;
  49. },
  50. /**************************************/
  51. //连接成功
  52. websocketonopen() {
  53. console.log('已经链接');
  54. let user_info = '';
  55. // console.log(this.$store.getters.get_user_info != "");
  56. user_info=this.getCustomerUserInfo();
  57. if(user_info){
  58. this.$websocket.send(JSON.stringify({
  59. type: 'init',
  60. data: {
  61. uid: 'KF' + user_info.id,
  62. group: user_info.group_id,
  63. token: user_info.token,
  64. name: user_info.user_name,
  65. avatar: user_info.user_avatar,
  66. }
  67. }));
  68. }
  69. this.$store.dispatch("SET_SOCKET_OPEN",true);//列表下标
  70. this.heatBeat();
  71. },
  72. /*******************************************/
  73. //连接关闭触发
  74. socket_close(e){
  75. console.log('断开连接',e);
  76. //this.init()
  77. this.reconnect();
  78. },
  79. getVisitorList(){
  80. this.kfuser_info=this.getCustomerUserInfo();
  81. this.$websocket.send(JSON.stringify({
  82. type: 'kfusersmaps',
  83. data: {
  84. kfuid: this.kfuser_info.id
  85. }
  86. }));
  87. },
  88. /******************************************/
  89. //数据接收
  90. websocketonmessage(e){
  91. this.heatBeat();//收到消息会刷新心跳检测,如果一直收到消息,就推迟心跳发送
  92. const redata = JSON.parse(e.data);//接收数据源
  93. if(!redata) {
  94. return
  95. }
  96. //
  97. if(redata.message_type == "ping"){
  98. this.$websocket.send('{"type":"pong"}')
  99. return false;
  100. }
  101. if(redata.type == "pong") return false;
  102. if(redata.type != "pong" || redata.message_type != "ping"){
  103. if(redata.message_type === 'connct') {
  104. this.$store.dispatch("SET_SESSSION_USER", redata.data.user_info);
  105. }
  106. }
  107. this.getVisitorList();
  108. console.log(redata);
  109. let getters = this.$store.getters;
  110. console.log({'getters':this.$store})
  111. //获取vuex数据
  112. let session = getters.get_session;//会话列表
  113. let offline = getters.get_offline;//离线列表
  114. let sessionType = getters.get_type;//选择状态(会话/离线)
  115. let dataIndex = getters.get_num;//列表下标
  116. let current_session = getters.get_current;//当前会话
  117. let session_name = getters.get_session_name;//当前用户名
  118. this.$store.dispatch("SET_SESSION_MESSAGE",JSON.parse(e.data));
  119. // 用户接入数据
  120. if(redata.message_type == "connect"){
  121. this.$message({
  122. message: '访客已进入对话',
  123. type: 'success'
  124. });
  125. let _this = this;
  126. let arr =[
  127. redata.data,
  128. offline,
  129. session,
  130. sessionType,
  131. dataIndex,
  132. current_session,
  133. session_name,
  134. ]
  135. _this.$public.visitorsConnect(arr,function(data){
  136. //离线匹配列表访客链接回调
  137. _this.$store.dispatch("SET_OFFLINE",data);
  138. },function(data,session,offline,type,num,dataList,name){
  139. //将用户添加到会话列表中
  140. // console.log('接入用户昵称',name);
  141. if(name){
  142. data.user_info.name = name;
  143. _this.$store.dispatch("SET_SESSION_NAME",name);//当前会话对象名
  144. }
  145. session.push(data.user_info);
  146. //获取接入的用户信息
  147. //_this.get_user_info(list);
  148. //获取接入的用户信息写入vuex
  149. _this.$store.dispatch("SET_SESSION",session);//会话列表
  150. _this.$store.dispatch("SET_OFFLINE",offline);//离线列表
  151. _this.$store.dispatch("SET_TYPE",type);//选择类型(会话/离线)
  152. _this.$store.dispatch("SET_NUM",num);//列表下标
  153. _this.$store.dispatch("SET_CURRENT",dataList);//当前会话数据
  154. })
  155. }
  156. //登录验证失败跳转登录页面
  157. if(redata.message_type == 'checkfalse'){
  158. // this.$router.push({
  159. // path:'/login',
  160. // query: { pid:escape("这就是一个编码没有什么用啊") }
  161. // })
  162. }
  163. //用户离线后会话窗口切换
  164. if(redata.message_type == "userClose"){
  165. let _this =this;
  166. let arr= [
  167. redata.data,
  168. session,
  169. offline,
  170. sessionType,
  171. dataIndex,
  172. session_name,
  173. current_session,
  174. ]
  175. _this.$public.userOffline(arr,function(session,offline,type,index,name,list,userInfo){
  176. _this.$store.dispatch("SET_CURRENT",list);//当前会话数据
  177. _this.$store.dispatch("SET_SESSION_NAME",name);//当前会话对象名
  178. _this.$store.dispatch("SET_NUM",index);
  179. _this.$store.dispatch("SET_TYPE",type);
  180. _this.$store.dispatch("SET_SESSION",session);
  181. _this.$store.dispatch("SET_OFFLINE",offline);
  182. // _this.get_user_info(userInfo);
  183. })
  184. }
  185. //用户会话结束窗口切换
  186. if(redata.message_type == "delUser"){
  187. this.$message({
  188. message: '访客已退出会话',
  189. type: 'warning'
  190. });
  191. let _this =this;
  192. let arr= [
  193. redata.data,
  194. session,
  195. offline,
  196. sessionType,
  197. dataIndex,
  198. session_name,
  199. current_session,
  200. ]
  201. _this.$public.sessionEnd(arr,function(session,offline,type,index,name,list,userInfo){
  202. console.log('session',session);
  203. console.log('offline',offline);
  204. _this.$store.dispatch("SET_CURRENT",list);//当前会话数据
  205. _this.$store.dispatch("SET_SESSION_NAME",name);//当前会话对象名
  206. _this.$store.dispatch("SET_NUM",index);
  207. _this.$store.dispatch("SET_TYPE",type);
  208. _this.$store.dispatch("SET_SESSION",session);
  209. _this.$store.dispatch("SET_OFFLINE",offline);
  210. // _this.get_user_info(userInfo);
  211. })
  212. }
  213. //接收用户消息数据
  214. if(redata.message_type == "chatMessage"){
  215. this.Messagenum = this.$store.getters.get_megnum;
  216. this.Messagenum = ++this.Messagenum;
  217. this.$store.dispatch("SET_MEGNNUM", this.Messagenum);
  218. this.receiveAudio();
  219. let _this =this;
  220. this.$public.receivesMessage(redata.data,session,this.$frce,function(data,chatList,index){
  221. //判断是否是当前对话信息
  222. if(dataIndex == index){
  223. _this.$store.dispatch("SET_CURRENT",chatList.data);//当前会话数据
  224. }else{
  225. let num = Number.isInteger(chatList.num) ? chatList.num : 0;
  226. // session[index].num = num+1;
  227. _this.$set(session[index],'num',num+1)
  228. }
  229. //更新会话时间
  230. session[index].intime =data.time;
  231. //更新会话列表中最新回复消息
  232. if(data.content.text){
  233. // _this.$set(chatList,'text',data.content.text)
  234. session[index].text =data.content.text;
  235. }
  236. _this.$store.dispatch("SET_SESSION",session);
  237. })
  238. }
  239. // history
  240. //reLoginErr
  241. if(redata.message_type == "reLoginErr"){
  242. // console.log('haha');
  243. this.init()
  244. }
  245. },
  246. /**************************************/
  247. //连接断开,失败
  248. websocketonerror(e) {
  249. console.log('失败',e);
  250. if(this.linkfailure){
  251. this.init()
  252. }
  253. },
  254. /******************************************/
  255. //断开链接数据初始化
  256. init(){
  257. this.$store.dispatch("SET_STATEVALUE",'在线');
  258. this.$store.dispatch("SET_SESSION",[]);
  259. this.$store.dispatch("SET_USER",'');
  260. this.$store.dispatch("SET_OFFLINE",[]);
  261. this.$store.dispatch("SET_SESSION",[]);//会话列表
  262. this.$store.dispatch("SET_OFFLINE",[]);//离线列表
  263. this.$store.dispatch("SET_TYPE",1);//选择类型(会话/离线)
  264. this.$store.dispatch("SET_NUM",0);//列表下标
  265. this.$store.dispatch("SET_CURRENT",[]);//当前会话数据
  266. this.$store.dispatch("SET_SESSION_NAME",'');//当前会话对象名
  267. this.$store.dispatch("SET_SESSION_MESSAGE",{});
  268. this.$store.dispatch("SET_NAVSTATE",'TheCurrentSession');
  269. this.$store.dispatch("SET_IS_INIT",false);
  270. this.$token = '';
  271. this.$router.push('/login')
  272. this.linkfailure = true;
  273. //this.reconnect();
  274. },
  275. /*******************************************/
  276. // 接收消息音频提示
  277. receiveAudio() {
  278. let receive = new Audio()
  279. receive.src = "../static/audio/receive.wav";
  280. receive.play();
  281. },
  282. /***************************************/
  283. //socket重连
  284. reconnect(){
  285. if(this.lockReconnect){ //这里很关键,因为连接失败之后之后会相继触发 连接关闭,不然会连接上两个 WebSocket
  286. return
  287. }
  288. this.lockReconnect = true;
  289. this.reconnectData && clearTimeout(this.reconnectData);
  290. this.reconnectData = setTimeout(()=>{
  291. this.initWebSocket(this.apiToken);
  292. this.lockReconnect = false;
  293. },3000)
  294. },
  295. /*************************************/
  296. //心跳检测
  297. heatBeat(){
  298. this.timeoutObj && clearTimeout(this.timeoutObj);
  299. this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
  300. this.timeoutObj = setTimeout(()=>{
  301. // console.log('发送',{type:'ping'});
  302. this.$websocket.send(JSON.stringify({type:'ping'})) //根据后台要求发送
  303. this.serverTimeoutObj = setTimeout(()=> {
  304. this.$websocket.close(); //如果4秒之后我们没有收到 后台返回的心跳检测数据 断开socket,断开后会启动重连机制
  305. }, 4000);
  306. }, this.timeout)
  307. },
  308. /******************************/
  309. },
  310. mounted() {
  311. Vue.prototype.$socket_open = false;
  312. this.apiToken = this.$md5('customer-service'+window.location.origin)
  313. this.initWebSocket(this.apiToken);
  314. // 获取系统时间
  315. //let t = new Date().getTime();
  316. //this.get('api'+this.$ports.TIME+'?t='+t).then(res => {
  317. // this.$http.get(this.$ports.TIME+'?t='+t).then(res => {
  318. // if (res.data.code == 1) {
  319. // let time = res.data.data.time.split(' ');
  320. // time[0] =(new Date( time[0].replace(/-/g,'/')).getTime()) /1000;
  321. // sessionStorage.setItem("time",JSON.stringify(time));
  322. // sessionStorage.setItem("logo",res.data.data.logo);
  323. // }
  324. // });
  325. },
  326. destroyed() {
  327. this.lockReconnect = true;
  328. this.$websocket.close();
  329. // this.websock.close() //离开路由之后断开websocket连接
  330. clearTimeout(this.reconnectData); //离开清除 timeout
  331. clearTimeout(this.timeoutObj); //离开清除 timeout
  332. clearTimeout(this.serverTimeoutObj); //离开清除 timeout
  333. localStorage.removeItem('user');
  334. // console.log(123)
  335. }
  336. }
  337. </script>
  338. <style>
  339. .fade-enter-active, .fade-leave-active {
  340. transition: opacity .5s;
  341. }
  342. .fade-enter, .fade-leave-to {
  343. opacity: 0;
  344. }
  345. </style>