function getHeader() {
  let token = uni.cache.get("token") || "";
  const shopId = uni.cache.get("shopId") * 1;
  const userInfo = uni.cache.get("userInfo") || {};

  return {
    version: uni.conf.version,
    type: uni.getSystemInfoSync().platform,
    // #ifdef APP-PLUS
    platformType: "APP",
    // #endif
    // #ifdef H5
    platformType: "H5",
    // #endif
    // #ifdef MP-WEIXIN
    platformType: "WX",
    // #endif
    // #ifdef MP-ALIPAY
    platformType: "ALI",
    // #endif
    token,
    id: userInfo.id || "",
    shopId: shopId || "",
    userId: userInfo.id || "",
  };
}

// 上传
export function upload(uri, file, data, showLoading = true, extParams = {}) {
  return new Promise((resolve, reject) => {
    uni.showLoading();

    uni
      .uploadFile(
        Object.assign(
          {
            url: uni.conf.baseUrl + uri,
            formData: data,
            name: "file",
            filePath: file.path || file.url || file,
            header: getHeader(),
          },
          extParams
        )
      )
      .then((httpData) => {
        // uni.upload 返回bodyData 的是 string类型。 需要解析。
        httpData.data = JSON.parse(httpData.data);
        if( httpData.data.code==200){
          resolve( httpData.data.data);
        }
        reject()
      })
      .catch((err) => {
        reject();
        uni.hideLoading();
        infoBox.showErrorToast(`上传失败`);
      });
  });
}

export const uploadFile = (file, data) => {
  return upload("/account/user/common/upload", file, data);
};
