import http from './http.js'


let limit = {}
let debounce = {}

const config = {
    // 示例
    // wxLogin : {url : '/api/wxLogin', method : 'POST',
    // auth : false, showLoading : true, loadingTitle : '加载中...',
    // limit : 1000
    // },

    getConfig: {url: '/index/getSysText', method: 'POST', limit: 500},


    // 微信登录接口
    wxLogin: {
        url: '/login/login',
        method: 'POST',
        limit: 500,
        showLoading: true,
    },
    // 修改个人信息接口
    updateInfo: {
        url: '/info/updateInfo',
        method: 'POST',
        auth: true,
        limit: 500,
        showLoading: true,
    },
    //隐私政策
    getPrivacyPolicy: {
        url: '/login/getPrivacyPolicy',
        method: 'GET',
    },
    //用户协议
    getUserAgreement: {
        url: '/login/getUserAgreement',
        method: 'GET',
    },

    // ========================用户登录注册===============================
    //用户登录
    loginUser: {
        url: '/alUser/login',
        method: 'POST',
    },
    //用户注册
    registerUser: {
        url: '/alUser/regUesr',
        method: 'POST',
    },
    // 发送短信接口
    sendSms: {
        url: '/alUser/sendSms',
        method: 'POST',
    },
    // 选择身份
    roleOption: {
        url: '/alUser/role',
        method: 'POST',
		limit : 1000,
    },
    // 忘记密码
    newPassword: {
        url: '/alUser/newPassword',
        method: 'POST',
		limit : 1000,
    },

    // ========================产品报价===============================
    // 清关申请
    addCustoms: {
        url: '/product/addCustoms',
        method: 'POST',
		limit : 1000,
    },
    // 产品报价
    addProduct: {
        url: '/product/addProduct',
        method: 'POST',
    },
    // 下订单
    addProductOrder: {
        url: '/product/addProductOrder',
        method: 'POST',
    },
    // 确认-取消订单
    updateOrder: {
        url: '/product/updateOrder',
        method: 'POST',
    },
    // 我的挂单列表
    getMyProductlist: {
        url: '/product/myProductlist',
        method: 'GET',
    },
    // 撤单
    noShow: {
        url: '/product/noShow',
        method: 'GET',
    },
    // 现货/期货列表
    productList: {
        url: '/product/productList',
        method: 'GET',
    },
    // 交易平台挂单列表
    productlist: {
        url: '/product/productlist',
        method: 'GET',
    },
    // ========================用户地址===============================
    // 用户地址表-添加
    addAddress: {
        url: '/address/add',
        method: 'POST',
    },
    // 用户地址表-编辑
    editAddress: {
        url: '/address/edit',
        method: 'POST',
    },
    // 用户地址表-删除
    deleteAddress: {
        url: '/address/edit',
        method: 'POST',
    },
    // 用户地址表-分页列表查询
    addressList: {
        url: '/address/list',
        method: 'GET',
    },
    // ========================首页等展示接口===============================
    // 帮助与反馈
    addSuggest: {
        url: '/index/addSuggest',
        method: 'POST',
    },
    // 铝价接口
    getAlPrice: {
        url: '/index/alprice',
        method: 'POST',
    },
    // 我的头像昵称，平台客户电话等信息
    getImagePhoneOther: {
        url: '/index/index',
        method: 'POST',
        limit: 500,
    },
    // 我的订单列表
    myOrderlist: {
        url: '/product/myOrderlist',
        method: 'GET',
        limit: 500,
    },
    // 采购商根据订单id查询订单信息
    getMyOrderInfo: {
        url: '/product/myOrderlist',
        method: 'GET',
        limit: 500,
    },
    // 供应商根据挂单id查询挂单详情的接口
    getProductInfo: {
        url: '/product/myOrderlist',
        method: 'GET',
        limit: 500,
    },
    // 获取banner列表
    bannerList: {
        url: '/index/bannerList',
        method: 'POST',
        limit: 500,
    },



}


export function api(key, data, callback, loadingTitle) {
    let req = config[key]

    if (!req) {
        console.error('无效key' + key);
        return
    }

    if (typeof callback == 'string') {
        loadingTitle = callback
    }

    if (typeof data == 'function') {
        callback = data
        data = {}
    }

    // 接口限流
    if (req.limit) {
        let storageKey = req.url
        let storage = limit[storageKey]
        if (storage && new Date().getTime() - storage < req.limit) {
            return
        }
        limit[storageKey] = new Date().getTime()
    }

    //必须登录
    if (req.auth) {
        if (!uni.getStorageSync('token')) {
            uni.navigateTo({
                url: '/pages_order/auth/loginAndRegisterAndForgetPassword'
            })
            console.error('需要登录')
            return
        }
    }

    // 接口防抖
    if (req.debounce) {

        let storageKey = req.url
        let storage = debounce[storageKey]

        if (storage) {
            clearTimeout(storage)
        }

        debounce[storageKey] = setTimeout(() => {

            clearTimeout(storage)

            delete debounce[storageKey]

            http.http(req.url, data, callback, req.method,
                loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
        }, req.debounce)

        return
    }

    http.http(req.url, data, callback, req.method,
        loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
}


export default api