import store from '../store/index'

// 获取请求头
const getRequestHeader = (extraHeader = {}) => {
    const mixid = store.state.mixid || wx.getStorageSync('mixid');
    
    // 共用请求头
    const BASE_HEADER = {
        "Shop-Mixid": mixid,
        "Authorization": wx.getStorageSync('sessionid') || "",
        "Offline-Shop-Code": wx.getStorageSync('offlineShopCode') || "",
        "Area-Id": wx.getStorageSync('location') ? JSON.parse(wx.getStorageSync('location')).id : '',
        "Region-id": wx.getStorageSync('location') ? JSON.parse(wx.getStorageSync('location')).systemRegionId : '',
    }

    // 判断是否存在导购员 id
    const setGuideEmployeeId = wx.getStorageSync(mixid + 'setGuideEmployeeId');
    if (setGuideEmployeeId) {
        BASE_HEADER['Guide-Employee-Id'] = setGuideEmployeeId;
    }
    
    return Object.assign(BASE_HEADER, extraHeader);
}

export async function requestGET(url, options) {

    return new Promise((resolve, reject) => {
        wx.request({
            url: url,
            data: options,
            method: "GET",
            header: getRequestHeader(),
            success: function (res) {
                resolve(res)
            },
            fail: function (res) {
                if (res.data && res.data.message && res.data.message == "API rate limit exceeded") {
                    wx.showToast({
                        title: '您好,当前访问人数过多,请稍后再试试',
                        icon: 'none',
                    });
                }
                reject(res)
            }
        })
    })
}

export async function requestPOST(url, options) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: url,
            data: options,
            method: "POST",
            header: getRequestHeader({
                openId: wx.getStorageSync("openid") || ''
            }),
            success: function (res) {
                resolve(res)
            },
            fail: function (res) {
                if (res.data && res.data.message && res.data.message == "API rate limit exceeded") {
                    wx.showToast({
                        title: '您好,当前访问人数过多,请稍后再试试',
                        icon: 'none',
                    });
                }
                reject(res)
            }
        })
    })
}

export function requestPOST1(url, options) {
    return new Promise((resolve, reject) => {
        wx.request({
            url: url,
            data: options,
            method: "POST",
            header: getRequestHeader({
                "Content-Type": "application/x-www-form-urlencoded"
            }),
            success: function (res) {
                resolve(res)
            },
            fail: function (res) {
                reject(res)
            }
        })
    })
}

// function getHeader() {
//     let sessionid = wx.getStorageSync('sessionid');
//     let header = {
//         "Content-Type": "application/x-www-form-urlencoded",
//         "Shop-Mixid": shopMixid,
//         Authorization: sessionid
//     };
//     // if (!sessionid) {
//     //     delete header.Authorization
//     // }
//     return header
// }