App.vue 11 KB

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