
import { isFunc, isEmpty } from "./base";

function getUserInfo() {
    return wx.getStorageSync('userInfo') || null;
}

function setUserInfo(user) {
    wx.setStorageSync('userInfo', user)
}

function setaddress(address) {
    wx.setStorageSync('address', address)
}
function getaddress() {
  return wx.getStorageSync('address')
}



function setUserInfoPart(userPart) {
    let user = getUserInfo()
    if (user == null) {
        user = userPart
    } else {
        for(let k in userPart) {
            user[k] = userPart[k]
        }
    }
    setUserInfo(user)
}

/**
 * 获取用户当前选中的店铺
 */
function getCurrStore() {
    return wx.getStorageSync('store') || null
}

/**
 * 获取用户当前选中的桌位
 */
function getCurrTable() {
    return wx.getStorageSync('table') || null
}
function getChooseStore() {
    return wx.getStorageSync('chooseStore') || null
}


function setChooseStore(chooseStore) {
    wx.setStorageSync('chooseStore', chooseStore)
}
function getMobile() {
    const userInfo = getUserInfo()
    if (userInfo != null) {
        return userInfo.mobile || ''
    }
    return ''
}

function getStoreId() {
    const store = getCurrStore()
    if (store != null) {
        return store.id || null;
    }
    return null
}


// 缓存门店取餐方式

function setStoreWay(storeway) {
    wx.setStorageSync('storeway', storeway)
}



function getStoreWay(storeway) {
    return wx.getStorageSync('storeway')||null;
}


function setCurrStore(store) {
    wx.setStorageSync('store', store)
}



function setCurrTable(table) {
    wx.setStorageSync('table', table)
}

//存储最近的经纬度
function setNearLocation(longitude,latitude) {
    wx.setStorageSync('nearLocation', {
        longitude:longitude,
        latitude:latitude
    })
}
//获取最近的经纬度
function getNearLocation() {
    return wx.getStorageSync('nearLocation')||null;
}
function getTableId() {
    const table = getCurrTable()
    if (table != null) {
        return table.tableId || null
    }
    return null
}

function getSessionKey() {
    return wx.getStorageSync('loginInfo').sessionKey
}

function getOpenId() {
    return wx.getStorageSync('loginInfo').openId
}

function getLoginInfo() {
    wx.getStorageSync('loginInfo')||null;
}

/**
 * 授权手机号
 * @param {*} e 
 * @param {*} func 
 */
function getPhoneNumber(e, func) {
    console.log(e)
    let code = e.detail.code;
    //获取手机号
    if (isEmpty(code)) {
        // 兼容老版本
        var encryptedData = e.detail.encryptedData;
        // console.log(encryptedData);
        var iv = e.detail.iv;
        if (!encryptedData || encryptedData.length == 0 || !iv || iv.length == 0) {
          return;
        }
        //获取手机号
        doDecodePhoneOld(encryptedData, iv, function (mobile) {
          func(mobile)
        });
    } else {
        doDecodePhone(code, function (mobile) {
            func(mobile)
        });
    }
}




function checkInvalid(param = {}, callBack) {
    // wx.checkSession({
    //     success() {
    //         if (isFunc(callBack)) {
    //             callBack(param)
    //         }
    //     },
    //     fail() {
    //         login(callBack)
    //     }
    // })
    login(callBack)
}


module.exports = {
    getMobile,
    getPhoneNumber,
    getStoreId,
    getTableId,
    setCurrStore,
    getCurrStore,
    getCurrTable,
    setCurrTable,
    setUserInfo,
    getUserInfo,
    setUserInfoPart,
    getLoginInfo,
    setNearLocation,
    getNearLocation,
    setStoreWay,
    getStoreWay,
    setaddress,
    getaddress,
    setChooseStore,
    getChooseStore,
}