import { getPlatformEnv } from "@/utils";

const originOpenLocation = uni.openLocation;
// 唤起地图
uni.openLocation = (config) => {
  // #ifdef MP-WEIXIN || MP-ALIPAY
  originOpenLocation(config);
  // #endif
  // #ifdef H5
  const env = getPlatformEnv();
  let mapUrl = "";
  if (env.bIsDtDreamApp) {
    mapUrl = `zwfw://openThirdMap?longitude=${config.longitude}&latitude=${config.latitude}&locationName=${config.address}`;
    ZWJSBridge.openLink({
      url: mapUrl,
    })
      .then((result) => {
        console.log(result);
      })
      .catch((error) => {
        console.log(error);
      });
  } else if (env.bIsWxMini) {
    console.log("微信禁止外链...无法拉起外部导航");
    uni.showToast({
      title: '微信环境无法导航',
    })
    /* uni.getLocation({
      success(res) {
        mapUrl = `https://apis.map.qq.com/uri/v1/routeplan?type=drive
        &from=我的位置&fromcoord=${res.latitude},${res.longitude}
        &to=${config.address}&tocoord=${config.latitude},${config.longitude}
        &policy=1&referer=ZA2BZ-CIVCD-VZZ4M-PQD2X-V6YBJ-NBFXT`;
        window.open(mapUrl, "_blank");
      },
    }); */
  } else {
    uni.getLocation({
      success(res) {
        mapUrl = `https://uri.amap.com/navigation?from=${res.longitude},${res.latitude},你的位置&to=${config.longitude},${config.latitude},${config.address}&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0`;
        console.log("地图导航地址：", mapUrl);
        window.open(mapUrl, "_blank");
      },
    });
  }
  // #endif
};

//扫码
const originScanCode = uni.openLocation;
uni.scanCode = (config) => {
  // #ifdef MP-WEIXIN || MP-ALIPAY
  originScanCode(config);
  // #endif
  // #ifdef H5
  ZWJSBridge.scan({
    type: "qrCode",
  })
    .then((data) => {
      console.log('浙里办微信扫码成功')
      config.success({ result: data.text });
    })
    .catch((error) => {
      console.log(error);
    });
  // #endif
};

// uni.makePhoneCall在安卓浙里办有问题，故重写
const originPhoneCall=uni.makePhoneCall;
uni.makePhoneCall=(config)=>{
  ZWJSBridge.phoneCall({
    corpId:config.phoneNumber
  }).then((result) => {
     config.success&&config.success()
  }).catch((error) => {
    config.fail&&config.fail()
  });
}

// 由于微信浙里办环境地图请求还是在走wx.request环境，重写wx.request
/* const originRequest=uni.request
wx.request=(config)=>{
  console.error('确定进来了吗')
  originRequest(config)
} */