const {
  Toast
} = require('./util');

const serverName = 'https://www.4dmuseum.cn/2.0'; // 正式
// const serverName = 'http://192.168.0.10:8081'; // 本地

//const imgServer = 'https://www.4dmuseum.cn/'
const imgServer = ''

const urls = {
  //用户API
  getLoginSessionKey: '/wx/api/user/getLoginSessionKey',
  // checkPaySessionKey:'/wx/api/user/checkLoginSessionKey',
  checkLoginSessionKey: '/wx/api/user/getBrowsedExhibitions',

  logout: '/wx/api/user/logout',
  isCollect: '/wx/api/exhibition/like',
  collectExhibitions: "/wx/api/user/getExhibitions",
  getOrders: '/wx/api/user/getOrders',

  //轮播图
  getBannerList: '/wx/api/banner/list',
  getBannerDetail:'/wx/api/banner/detail',

  //展会
  getExhibitionList: "/wx/api/exhibition/listByType",
  getExhibitionListOffline: "/wx/api/exhibition/offline",
  getExhibitionDetail: "/wx/api/exhibition/detail",
  getExhibitionSearch: "/wx/api/exhibition/search",
  getKeywordCode: "/wx/api/dataDictionary/keywordCode",

  //展馆
  getPavilionDetail: "/wx/api/pavilion/detail",
  getPavilionSearch: "/wx/api/pavilion/search",

  //同城
  getDomesticCity: '/wx/api/exhibition/domesticCity',
  getInternationalCity: '/wx/api/exhibition/internationalCity',
  getNearByList: '/wx/api/exhibition/nearByList',
  //支付
  orderPay: '/wx/api/order/pay',
  getPayParams: '/wx/api/order/getPayParams',
  getProduct: '/wx/api/exhibition/getProduct',

  //看过
  getExhibitionsByIds: '/wx/api/exhibition/getExhibitionsByIds',

  //评论
  getComments: '/wx/api/user/getComments',

  //保存浏览
  saveBrowsedExhibitions: '/wx/api/user/saveBrowsedExhibitions',

  //获取浏览记录id
  getBrowsedExhibitions: 'wx/api/user/getBrowsedExhibitions',
  //
  getCommentslist: '/wx/api/comment/list',

  //评论
  writtenComments: '/wx/api/comment/written',

  //点赞评论
  commentLike: '/wx/api/comment/like',

  //未读
  longPolling: '/wx/api/user/longPolling',

  //搜索的热门推荐
  recommend: '/wx/api/exhibition/recommend',

  //线下展讯热门推荐
  offlineRecommend: '/wx/api/exhibition/offlineRecommend'
};
// 上传路径
const uploadUrls = {};
const requestFns = {};

Object.keys(urls).forEach(function(key) {
  // console.log(token)
  requestFns[key] = function(
    data = {},
    method = "",
    success = () => {},
    fail = () => {},
    complete = () => {}
  ) {
    console.log(`request ${urls[key]}`);
    let commonData = {
      loginSessionKey: wx.getStorageSync('token') || ""
    };


    const url = `${serverName}${urls[key]}`;
    return wx.request({
      method: method || 'get',
      url,
      data: {
        ...data,
        ...commonData
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 0) {
          // console.log(`request ${urls[key]}, success:`, res);
          success(res);

        } else if (res.data.code == 102) {
          // 未登录
          // Toast.showToast('warn', '请登录后进行下一步操作');
          wx.showModal({
            title: '提示',
            content: '登录才能进行以上操作,确定登录吗？',
            confirmColor: '#e83828',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/login_page/index',
                  success: function (res) { },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })


          // Toast.showToast('tip', "登录才能进行以上操作", success => {
            
            
          // })
          fail(res);
        } else if (res.data.code == 11) {
          Toast.showToast('warn', '提交信息不完整');
          fail(res);
        } else if (res.data.code == 14) {
          Toast.showToast('warn', '该展会异常，请联系管理员');
          fail(res);
        } else if (res.data.code == 15) {
          Toast.showToast('warn', '该展会不需要支付');
          fail(res);
        } else if (res.data.code == 16) {
          Toast.showToast('warn', '金额有误');
          fail(res);
        } else if (res.data.code == 101) {
          Toast.showToast('warn', '网络超时，请重新登录');
          fail(res);
        } else if (res.data.code == 103) {
          Toast.showToast('warn', '微信统一下单异常');
          fail(res);
        } else if (res.data.code == 104) {
          Toast.showToast('warn', '获取参数失败，请重新下单');
          fail(res);
        } else if (res.data.code == 106) {
          Toast.showToast('warn', '您已下单，请前往【我的-我的订单】完成支付');
          fail(res);
        } else if (res.data.code == 105) {
          Toast.showToast('warn', '您已经发表过评论，不能重复评论');
          fail(res);
        } else if (res.data.code == 107) {
          Toast.showToast('warn', '账号异常，请重新登录');
          fail(res);
        } else if (res.data.code == -1) {
          Toast.showToast('warn', '请求发送失败');
          fail(res);
        } else {
          Toast.showToast('warn', res.data.msg || '网络超时，请检查网络');
          fail(res);
        }
      },
      fail: function(res) {
        fail(res);
      },
      complete: function() {
        complete()
      }
    });
  };
});


module.exports.request = requestFns;
module.exports.serverName = serverName;
module.exports.imgServer = imgServer;
