App.vue 11 KB

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