import base64
import pickle


def get_carts_from_cookies(request):
    """
    获取cookie中的购物车数据
    :param request: 请求对象
    :return: 购物车字典 or {}
    """
    carts_from_cookies = request.COOKIES.get('carts')
    if carts_from_cookies: # "XdnhjgrbewghjrtebABFGNJEKb="
        # 1、先base64解码
        # b'\x56\x78.....'
        cart_dict1 = base64.b64decode(carts_from_cookies.encode())
        # 2、再pickle解码
        cart_dict2 = pickle.loads(cart_dict1)
        return cart_dict2
    else:
        return {}