// H5
// 微信小程序: KIP
// 微信小程序: CRM
/*
* 1、如果是小程序的话，接入 socket
* 2、如果是h5，获取用户的 openid
* 3、初始化 socket
* */

import { createAxiosByinterceptors } from "@/api/request";
import Stomp from '@/lib/stompjs';
import loginMinix from '@/mixins/login';
import { Decrypt, Encrypt } from '@/utils/crypto';
import { getIsMin, getIsWxh5, getUrlParams } from '@/utils/index.js';
import SockJS from '@/utils/sockjs';
import uni from '@/utils/uniHooks';
import { getAppIdByGroupIdAndMallId } from '@/utils/index.js';


export function initEnv() {
  const href = window.location.href;
  console.log('当前页面的url地址  ', href);
  if (/dev-|8080/.test(href)) {
    // window.env = 'qa';
    // window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
    // window.api = 'qaApi';
    window.env = 'dev';
    window.profileApi = 'https://dev-gateway-kip.kerryonvip.com/api';
    window.api = 'devApi';
    return;
  }
  if (/qa-/.test(href)) {
    window.env = 'qa';
    window.api = 'qaApi';
    window.profileApi = 'https://qa-apim.kerryplus.com/c/api';
    return;
  }
  window.env = 'prod';
  window.profileApi = 'https://apim.kerryplus.com/c/api';
  window.api = 'api';
}
export function requestInit() {
  window.requestms = createAxiosByinterceptors({
    baseURL: `${window.profileApi}/temporary-parking-service`,
  });
}

// websocket 链接
export function  getUrl() {
  // 如果 kerry+ 这边的访问环境是 sl 或者 lt，需要把 wss 指向 qa 环境。
  const href = `${window.location.href}`;
  if (/dev-|8080/.test(href)) {
    return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  }
  if (/qa-/.test(href)) {
    return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  }
  // return 'https://qa-crm-kpl.kerryprops.com.cn/xcrm-api';
  return `https://crm.kerryplus.com/xcrm-api`;
}

export function  createWS() {
  return new Promise((resolve, reject) => {
    try {
      const socket = new SockJS(
        `${this.getUrl()}/hafengWebsocket?token=${window.token}`
      );
      window.stompClient = Stomp.over(socket);
      window.stompClient.debug = null;
      this.windowSendInit();
      window.stompClient.connect({}, (frame) => {
        // 请求 projectId
        window.toWXSendMsg({
          type: 'getProjectId',
          options: {},
        });
        window.subscribe('projectId', (options) => {
          resolve(options);
        });
      });
    } catch (err) {
      reject(err);
    }
  });
}


export function  clientInit() {

}
