/**
  * config
  * @global variable
  */
import axios from "axios";
import { Toast } from "vant";

const devMode = false; //一键切换开发环境
//后端路径
const router = {
  WxShare: {
    sign: "WxShare/sign"
  },
  uploader: {
    upload: "file/sigleImgUpload",
    safeUpload: "file/sigleImgUploadSafe",
    download: "file/imgDownload?subFilePath="
  },
  systemMsg: {
    findAllOnCAPP: "systemMsg/findAllOnCAPP", //系统消息列表查询
    findDetailOnCAPP: "systemMsg/findDetailOnCAPP", //系统消息详情
  },
  UserMessageRecord: {
    businessFindPage: "UserMessageRecord/businessFindPage", //业务消息列表查询
  },
  RepairType: {
    linkageQuery: "RepairType/linkageQuery" //维保类型
  },
  RepairManage: {
    cPayment: "RepairManage/cPayment", //维保支付
    cLaunchRepair: "RepairManage/cLaunchRepair",
    getDetails: "RepairManage/getDetails",
    cFindpage: "RepairManage/cFindpage",
    cGetDetails: "RepairManage/cGetDetails",
    cRevokeRepair: "RepairManage/cRevokeRepair", //撤销维保
    cEvaluate: "RepairManage/cEvaluate", //维保评价
    cAgainMake: "RepairManage/cAgainMake" //重新预约时间
  },
  wellCustomer: {
    insert: "wellCustomer/cInsert",
    cmyReservationPage: "wellCustomer/cmyReservationPage",
    cselectDetail: "wellCustomer/cselectDetail",
  },
  system: {
    login: "system/login",
    wxPhoneLogin: "system/wxPhoneLogin",
    wxPoneLoginCallBack: "system/wxPoneLoginCallBack",
    wxPoneLoginInfo: "system/wxPoneLoginInfo",
    wxLogin: "system/wxLogin",
    wxLoginInfo: "system/wxLoginInfo",
    logout: "system/logout"  //退出登录
  },
  ComplaintSuggest: {
    addComplaint: "ComplaintSuggest/addComplaint"
  },
  FsAbnormal: {
    addComplaint: "FsAbnormal/addComplaint"
  },
  customer: {
    updatePasswordSecond: "customer/updatePasswordSecond",
    updatePasswordSms: "customer/updatePasswordSms",
    smsverification: "customer/smsverification", //注册获取手机验证码
    register: "customer/register", //用户注册
    bindHouseFirst: "customer/bindHouseFirst", //绑定房源--获取房源信息（实名认证）
    bindHouseSecond: "customer/bindHouseSecond", //绑定房源--确定绑定
    myHouseInfo: "customer/myHouseInfo", //查看租户详细情况
    outBindHouse: "customer/outBindHouse",
    //myHouseInfo: 'customer/myHouseInfo', //查询租户的同住人

    updatePhone: "customer/updatePhone", //修改手机号码
    updatePassword: "customer/updatePassword", //修改密码
    updateUser: "customer/updateUser", //修改用户昵称/头像/实名认证
    updatePasswordthird:"customer/updatePasswordthird" //确认修改密码
  },
  cAPPHouse: {
    applyLiveCancel: "cAPPHouse/applyLiveCancel",
    checkLiveWith: "cAPPHouse/checkLiveWith",
    regions: "cAPPHouse/regions",
    buildingNums: "cAPPHouse/buildingNums",
    units: "cAPPHouse/units",
    floors: "cAPPHouse/floors",
    houses: "cAPPHouse/houses",
    commitBindHouse: "cAPPHouse/commitBindHouse",
    myStewards: "cAPPHouse/myStewards", // 服务团队
    applyLiveFirst: "cAPPHouse/applyLiveFirst",
    applyLiveFirstInfo: "cAPPHouse/applyLiveFirstInfo",
    applyLiveCommit: "cAPPHouse/applyLiveCommit",
    applyLiveList: "cAPPHouse/applyLiveList",
    applyLiveDetail: "cAPPHouse/applyLiveDetail",
    buildingNumsNotRent: "cAPPHouse/buildingNumsNotRent",
    unitsNotRent: "cAPPHouse/unitsNotRent",
    housesNotRent: "cAPPHouse/housesNotRent", 
    chooseHouseCommit: "cAPPHouse/chooseHouseCommit",
    deleteLiveWith: "cAPPHouse/deleteLiveWith",
    newapplyLiveList:"cAPPHouse/newapplyLiveList", //申请列表新接口
    newapplyLiveDetail:"cAPPHouse/newapplyLiveDetail", //申请详情新接口
    newchooseHouseCommit:"cAPPHouse/newchooseHouseCommit", //在线选房新接口
    getPayUrl:"cAPPHouse/getPayUrl",// 根据ID获取支付链接
  },
  Region: {
    findMoreRegions : "Region/findMoreRegions", // 热门项目列表/查看更多
    findDetialOnCApp : "Region/findDetialOnCApp", // 项目详情
    selectListRegion : "Region/selectListRegion" //户型列表项目下拉
  },
  houseLayout: {
    findAllOnCApp : "houseLayout/findAllOnCApp", //热门户型列表/查看更多
    finddetailOnCApp : "houseLayout/finddetailOnCApp", // 户型详情
    listSelect : "houseLayout/listSelect", // 户型列表下拉户型
    findAllModelOnCApp: "houseLayout/findAllModelOnCApp", //查询所有样板间
    findModelDetail: "houseLayout/findModelDetail" //样板间详情
  },
  CommonProblem: {
    cfindpage :"CommonProblem/cfindpage", //常见问题查询列表
    cgetDetails:"CommonProblem/cgetDetails",//常见问题详情
  },
  collectionRecord: {
    addCollection : "collectionRecord/addCollection", //用户收藏项目、户型
    findUserCollection : "collectionRecord/findUserCollection", //获取用户的收藏
    cancelCollection : "collectionRecord/cancelCollection"  // 用户取消收藏项目、户型
  },
  area:{
    findAllCounty : "area/findAllCounty"  //获取户型列表页下拉地区
  },
  search:{
    listSelect : "search/listSelect" // 简洁搜索结果
  },
  activity:{
    payAppyCostOnCAPP: "activity/payAppyCostOnCAPP", //活动支付
    findAllActivity : "activity/findAllActivity", // 社区活动查看更多
    findAllONCApp : "activity/findAllONCApp", //社区活动
    findActivityDetail : "activity/findActivityDetail",  //社区活动详情
    findApplyDetail: "activity/findApplyDetail", //查看活动报名详情
    addApply: "activity/addApply", //活动报名
    cancelApply: "activity/cancelApply", //取消报名
    deleteApply: "activity/deleteApply", //删除报名
    findUserActicity: "activity/findUserActicity" //查询用户参与的活动列表
  },
  content:{
    choiceness : "content/choiceness", //精选活动列表
    detail : "content/detail" // 精选活动详情
  },
  coupon: {
    findCouponDetails : "coupon/findCouponDetails", //查看优惠券详情
    userGetCoupon : "coupon/userGetCoupon", // 领取优惠券
    findUsersCoupons : "coupon/findUsersCoupons", //查看我的优惠券
    verifyCouponOnCapp: "coupon/verifyCouponOnCapp", //验证优惠券是否可用
  },
  dictionary: {
    selectByPCode: "dictionary/selectByPCode",
  },
  device:{
    remoteOpen: "remoteOpen", //远程门锁
  },
  parameterSetting:{
    selectServiceTel: "parameterSetting/selectServiceTel",//查询客户电话
  },
  houseLive:{
    findUserCurrentBillOnCAPP:"houseLive/findUserCurrentBillOnCAPP", //查询用户当前账单
    selectAllBillsOnCAPP:"houseLive/selectAllBillsOnCAPP", //查询用户所有账单
    findBillDetailOnCAPP:"houseLive/findBillDetailOnCAPP", //查询账单详情
    livePayBillOnCAPP:"houseLive/livePayBillOnCAPP", //我的账单缴费
    payBillOnCAPPOnZero:"houseLive/payBillOnCAPPOnZero" //账单支付 支付金额为0的情况
  },
  WEGMeter: {
    findUserHouseMeterDetail:"WEGMeter/findUserHouseMeterDetail", //当前水电表余量读数
    findMeterReding:"WEGMeter/findMeterReding",//刷新水，电表余量
    findHistoryDetailOnCAPP:"WEGMeter/findHistoryDetailOnCAPP",//缴费历史
    payOnCAPP:"WEGMeter/payOnCAPP",//水电费缴费
  },
  scanningPay:{ // 定金缴费
    selHouseOrder:"scanningPay/selHouseOrder",
    newCcbOrderByWx:"scanningPay/newCcbOrderByWx",// 新账单缴费 龙支付
  },
  wxpay:{
    getPayInfo:"wxpay/getPayInfo" // 微信
  },
  houseSailWebsite:{
    listRegion:"houseSailWebsite/listRegion" //查询意向客户项目
  }
};
function interceptorsMethod(store, context) {
  //全局Ajax监控
  axios.interceptors.response.use((response) => {
    response.Toast = Toast;
    // 
    if( response.data.code === 20007) {

      //Toast.fail(response.data.msg);
      // store.dispatch('loginModule/wxLogin').then(res => {
      //   if(res) {
      //      location.href = res.data.data
      //   }else {
      //      context.$router.push({path: "/login"})
      //      // next({
      //      //   path: '/login'
      //      // })
      //   }
      // })
      store.commit("loginModule/CLEAR_LOGIN_INFO");
      // setTimeout(()=> {
      //   context.$router.push({path: "/login"})
      // }, 1000)
      return;
    }
    if( response.data.code !== 10000) {
      //
      //context.$message.success(response.data.message) //你没有相关账单！如有疑问请联系赛领团队
      if(response.data.msg.indexOf("你没有相关账单") > -1||response.data.msg.indexOf("或者已经主动退出") > -1 || response.data.msg.indexOf("你没有承租过任何房源") > -1 || response.data.msg.indexOf("未绑定房源") > -1) {
        return response;
      }
      Toast.fail(response.data.msg || "服务器异常");
      return;
    }
    // 
    return response;
  }, (error) => {
    //
    setTimeout(() => {
      Toast.fail("服务器异常");
    }, 400);
    
    Promise.resolve(error.response);
  });

  axios.interceptors.request.use((request) => {
    return request;
  }, (error) => {
    //store.dispatch('displayErrorLoad');
    
    Promise.reject(error);
  });
}

function requestMethod(method, url, data = null) {
  if (!method) {
    console.error("API function call requires method argument");
    return;
  }

  if (!url) {
    console.error("API function call requires url argument");
    return;
  }
  const [path, subPath] = url.match(/\w+/g);

  if(devMode) {
    method = "get";
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        axios({
          method,
          url,
          data,
          timeout: 50000
        }).then((res) => {
          resolve(res);
        });
      }, 100);
    });
  }
  if(method === "get" || method === "delete") {
    return axios[method](url, {
      params: data,
      timeout: 10000,
      withCredentials: true  //跨域需要
    });
  }
  return axios({
    method,
    url,
    data,
    timeout: 10000,
    withCredentials: true
  });
}

// let pathURI = "http://47.93.117.57/sail-web"
let pathURI = "http://c.sailapartment.com";
let apiUrl = "/";
//let pathURI = "http://192.168.32.130:8081/sail-web"
if(location.host.indexOf("192.168.31.202") > -1 || location.host.indexOf("120.79.43.11") > -1 || location.host.indexOf("gyf") > -1 || location.host.indexOf("localhost") > -1) {
  pathURI = "http://192.168.31.202:8080/sail-web";
  apiUrl = "/sail-web/";     
  //pathURI = "http://192.168.32.130:8081/sail-web"         
}
export default  {
  //serverHost:serverHost,
  //serverURI: '',
  apiUrl:apiUrl,
  serverURI: pathURI,
  getURL: (url) => {
    url = url.replace(/^\//, "");
    const [path, subPath] = url.match(/\w+/g);
    //
    if(devMode) {
      var webroot = "/testData/";
      let devPath = url.replace(/\//g, "");
      return webroot + devPath + ".json";
    }
    //return '/assessment-web/' + router[path][subPath]
    return apiUrl + router[path][subPath];
    //return 'http://192.168.31.202:8080/sail-web/' + router[path][subPath]
    //return 'http://192.168.32.130:8081/sail-web/' + router[path][subPath]
  },
  request:requestMethod,
  interceptorsMethod: interceptorsMethod
};