// 商户中心  请求
// const BaseURL = "http://192.168.0.70:8299";
// const BaseURL = "http://192.168.0.151:8299";
const BaseURL = "http://192.168.0.17:8299";
// const BaseURL = 'https://bwshop.caiquanwang.com.cn/prod-api'
// const BaseURL = "https://wrj.songlanyun.com";
function createRequest (baseURL, authType = 'token') {
  return (url, method = 'GET', data = {}, customHeaders = {}) => new Promise((resolve, reject) => {
    const header = {
      ...customHeaders,
      // 根据不同认证类型添加 headers
      ...(authType === 'token' ? { 'Authorization': 'Bearer ' + uni.getStorageSync('Authorization') } : {}),
    };

    uni.request({
      url: baseURL + url,
      method,
      data,
      header,
      success (res) {
        console.log('请求成功:', res);
        if (res.statusCode >= 200 && res.statusCode < 300) {
          if (res.data.code == 401) {
            uni.$u.toast('请登录商户账号！');
            uni.setStorageSync('Authorization', '');
            setTimeout(() => {
              uni.reLaunch({
                url: '/pages/tabBar/mine',
              });
            }, 1000)
          } else if (res.data.code != 200) {
            uni.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          resolve(res.data);
        } else {
          console.error(`请求失败: ${res.statusCode}`);
          reject(res);
        }
      },
      fail (err) {
        console.error('网络异常:', err);
        reject(err);
      }
    });
  });
}
export const request = createRequest(BaseURL, 'token');