🎉

コラボフロー 駅すぱあと

2024/11/12に公開
(function () {
  "use strict";

  const fromStation = "パーツID";
  const toStation = "パーツID";
  const result = "パーツID";

  collaboflow.events.on([`request.input.${fromStation}.change`, `request.input.${toStation}.change`], async function (e) {
    const parts = e.parts;

    const from = parts[fromStation].value;
    const to = parts[toStation].value;

    const calculatedValue = await calcTransportationFee(from, to);

    parts[result].value = calculatedValue;
  });
})();

// 交通費系計算の関数
async function calcTransportationFee(fromStation, toStation) {
  const url = new URL("https://api.ekispert.jp");
  url.pathname = "/v1/json/search/course/plain";
  const accessKey = "hA5nfdFZBasSZKkk";
  url.searchParams.set('key', accessKey);
  url.searchParams.set('from', fromStation);
  url.searchParams.set('to', toStation);

  try {
    const response = await fetch(url.toString());
    const data = await response.json();

    const courses = data.ResultSet.Course;
    const prices = courses.map((course) => {
      return Number(course.Price.find(item => item.kind === 'FareSummary').Oneway);
    });

    const minPrice = Math.min(...prices);
    console.log(minPrice);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}

// calcTransportationFee("東京", "大阪");
// 駅名検索
async function searchStationName(keyword) {
  const url = new URL("https://api.ekispert.jp");
  url.pathname = "/v1/json/station/light";
  const accessKey = "アクセスキー";
  url.searchParams.set('key', accessKey);
  url.searchParams.set('name', keyword);

  try {
    const response = await fetch(url.toString());
    const data = await response.json();
    const list = data.ResultSet.Point.map((point) => {
      return point.Station.Name;
    });
    console.log(list);
  } catch (error) {
    console.error('Error fetching data:', error);
  }
}

searchStationName("海老名");

Discussion