utils.js 2.65 KB
import api from "@/api/search";
import {
  buildCode
} from "@/assets/js/buildcodes";

const splitUrlOrigin = (url) => {
  if (url.includes("://")) {
    let temp = url.split("://")[1].split("/"),
      newUrl = "";
    temp.forEach((e, i) => {
      if (i != 0) {
        newUrl += "/" + e;
      }
    })
    return newUrl;
  } else {
    return url;
  }
};

export const splitUrlOriginG = splitUrlOrigin;

export const buildImgurl = (url) => {
  let isHttps = window.config.https,
    origin = window.location.origin;
  if (isHttps) {
    url.replace('http', 'https');
    url.replace('20080', '20070')
  }
  if (!origin.includes("localhost")) {
    url = origin + splitUrlOrigin(url);
  }
  return url;
};

export const matchDetailImg = (picUnidArr) => {
  return new Promise((resolve, reject) => {
    api.detailImgBatch({
        pic_unids: picUnidArr,
      })
      .then((res) => {
        let imgUnidEnum = {};
        if (res.length > 0) {
          res.forEach((item) => {
            imgUnidEnum[item.pic_unid] = buildImgurl(item.pic_url);
          });
        }
        resolve({
          imgUnidEnum: imgUnidEnum
        });
      });
  });
};

export const getDataList = (params, type = "") => {
  let httpRequest = null;
  if (type == "event") {
    httpRequest = api.eventTableList;
  } else {
    httpRequest = api.tableList;
  }
  let origin = window.location.origin,
    isLocalhost = false;
  if (origin.includes("localhost")) {
    isLocalhost = true;
  }
  return new Promise(async (resolve, reject) => {
    const res = await httpRequest(params);
    let formatterData = [],
      picUnid_Arr = [];
    res.list_data.forEach((item, index) => {
      if (item.pics[0].pic_unid) {
        picUnid_Arr.push(item.pics[0].pic_unid);
      }
    });
    let imgUnidEnum = {};
    if (picUnid_Arr.length > 0) {
      const imgRes = await matchDetailImg(picUnid_Arr);
      console.log('imgRes', imgRes);
      imgUnidEnum = imgRes.imgUnidEnum;
    }
    console.log('imgUnidEnum', imgUnidEnum)
    res.list_data.forEach((item, index) => {
      const pic_unid = item.pics ? item.pics[0].pic_unid : null;
      if (pic_unid) {
        item.pics[0].src_url = imgUnidEnum[pic_unid] || "";
      } else {
        item.pics[0].src_url = "";
      }
      const videoSrc_url = item.video ? item.video[0].src_url : null;
      if (videoSrc_url) {
        if (!isLocalhost) {
          item.video[0].src_url = origin + splitUrlOrigin(videoSrc_url);
        }
      }
      formatterData.push(buildCode.init(item));
    });
    console.log('formatterData', formatterData)
    resolve({
      total_num: res.total_num,
      formatterData: formatterData,
      list_data: res.list_data
    })
  });
}