import Taro from "@tarojs/taro";

export default function getAuthorize(scope) {
  return new Promise((resolve, reject) => {
    Taro.getSetting({
      success(res) {
        if (!res.authSetting[scope]) {
          Taro.authorize({
            scope,
            success: resolve,
            fail: (err) => {
              console.error(err);
              reject(err);
            },
          });
        } else {
          resolve();
        }
      },
      fail(err) {
        console.error(err);
        reject(err);
      },
    });
  });
}

export const getLocation = async (options) => {
  // try {
  return await new Promise((resolve, reject) => {
    wx.getLocation({
      ...(options || {}),
      success(res) {
        resolve(res);
      },
      fail(err) {
        console.error(err);
        reject(err);
      },
    });
  });
  // } catch (err_1) {
  //   console.error(err_1);
  //   reject(err_1);
  // }
};
const key = "HTPBZ-HHJA7-XD2XD-PRS37-H3HVJ-U5BAA";
export const h5GetLocation = () => {
  return new Promise((resolve, reject) => {
    const url = `${MAPHOST}/ws/location/v1/ip?key=${key}`;
    const res = Taro.request({
      url: url,
      method: "GET",
      dataType: "json",
      header: {
        "content-type": "application/json",
      },
    });
    resolve(res);
    reject(res);
    // .then((res) => {
    //   resolve(res.result);
    // })
    // .catch((error) => {
    //   reject(error);
    //   console.error("请求失败", error);
    // });
  });
};
