

/**
 *
 * author: water
 * data: 2019.7.8
 * */


// http://localhost:9595/#/?page=0&cId=1&gId=461&company=1&type=0

//  page:页面索引
//  company：公司类型
//  type：引入文件，center1，center2
//  cId：
//  gId：

//公共接口
/*let ips = '61.150.87.163';
let port = ':10100';
let ips2 = '61.150.87.163';*/
// let wsport = ':9326';
// let ip = 'http://' + ips + port;
// let wsip = 'ws://' + ips + wsport;

/*
let ips = 'api.58yunkang.com/';
let port = '';
let ips2 = 'api.58yunkang.com';
*/
// let ips = 'api.58yunkang.com/';
// let port = '';
// let ips2 = 'api.58yunkang.com';
// 测试参数：?type=1&company=1&cId=273861&gId=273861
// http://127.0.0.1:9595/#/?gId=641&cId=1&company=1&type=1&page=0

// let ips = '117.160.144.194:10400/';
// let ips2 = '117.160.144.194:10400';

// let ips = 'gavin0212.imwork.net/';

// let ips = 'api.anquanzhuli.com/';
// let ips2 = 'api.anquanzhuli.com';
// let port = '';
// let wsUrl = 'ws.anquanzhuli.com/';
// let wsport = 'ws';

/**局域网部署 */
let ips = '127.0.0.1/';
let ips2 = '127.0.0.1';
let port = '';
let wsUrl = '127.0.0.1/';
let wsport = 'ws';
/** */
wsUrl="ws.anquanzhuli.com/"
ips="api.xycqzj.com"
ips2 = 'api.xycqzj.com';


let ip = 'http://' + ips + port;
let wsip = 'ws://' + wsUrl + wsport;
import { Message } from 'element-ui';

// message.message('登录成功'+num,'success');
//  success 1 成功   / warning 2 警告  /  info 3 消息   / error 4 错误

function message(title,type) {

  let word = '';

  switch (type){
    case 1 : word = 'success';break;
    case 2 : word = 'warning';break;
    case 3 : word = 'info';break;
    case 4 : word = 'error';break;
  }

  Message.closeAll(); // 手动关闭所有实例

  Message({
    showClose: true,
    message: title,
    type:word,
    duration:"1800",
  });
}

//回到顶部
function exitTop() {
  document.documentElement.scrollTop = "0px"
  document.body.scrollTop = "0px";
}
let wsArr = [];
let wsUrlArr = [];

function websocketFn(obj,callBackFn){

  let lockReconnect = false;//避免重复连接
  let wsUrl = wsip + '?gId='+obj.gId+'&cId='+obj.cId+'&path='+obj.path+'&from='+ips2+port;
  let ws;
  let tt;
  function createWebSocket() {
    try {
      ws = new WebSocket(wsUrl);
      init();
    } catch(e) {
      console.log('catch');
      reconnect(wsUrl);
    }
  }
  function init() {
    ws.onclose = function () {
      console.log('链接关闭');
      ws = null;
      reconnect(wsUrl);
    };
    ws.onerror = function() {
      console.log('发生异常了');
      reconnect(wsUrl);
    };
    // 监听socket连接  是否连接上
    ws.onopen = function () {
      console.log('建立连接onopen');
      //心跳检测重置
      heartCheck.start();
    };
    // 监听socket消息
    ws.onmessage = function (evt) {
      //拿到任何消息都说明当前连接是正常的
      // console.log('接收到消息onmessage', evt.data);
      heartCheck.start();
      let data = evt.data;

      if(data.indexOf('10000') != -1){
        return;
      }
      if(data.indexOf('10001') != -1){
        // 主动推数据
        var mystr = JSON.parse(data.substring(5));
        // console.log(typeof(mystr),mystr)
        callBackFn((mystr));
      }else{
        // 第一次链接
        /*var mystr = JSON.parse(data);
        console.log(typeof(mystr), mystr)
        callBackFn(mystr)*/
      }
    };

  }
  function reconnect(url) {
    // console.log(lockReconnect)
    if(lockReconnect) {
      return;
    };
    // console.log(lockReconnect)

    lockReconnect = true;
    //没连接上会一直重连，设置延迟避免请求过多  4秒后会重新创建一个新的websocket链接
    tt && clearTimeout(tt);
    tt = setTimeout(function () {
      createWebSocket(url);
      lockReconnect = false;
    }, 4000);
  }
  //心跳检测
  var heartCheck = {
    timeout: 55*1000, //此时间不能大于服务器设置的心跳时间
    timeoutObj: null,
    serverTimeoutObj: null,
    start: function(){
      // console.log('start', this.timeoutObj, this.serverTimeoutObj);

      var self = this;
      this.timeoutObj && clearTimeout(this.timeoutObj);
      this.serverTimeoutObj && clearTimeout(this.serverTimeoutObj);
      this.timeoutObj = setTimeout(function(){
        //这里发送一个心跳，后端收到后，返回一个心跳消息，
        console.log(self.timeoutObj, self.serverTimeoutObj, obj.path+'-cmd');
        // console.log('wswsws', ws)
        if(ws){
          ws.send(obj.path+'-cmd');
        }
        self.serverTimeoutObj = setTimeout(function() {
          // console.log(111);
          // console.log(ws);
          ws.close();
          // createWebSocket();
        }, self.timeout);

      }, this.timeout)
    }
  }
  createWebSocket(wsUrl);
}


export default {
  ip,
  message,
  exitTop,
  websocketFn,
}
