import Vue from "vue"
let prototype = Vue.prototype;

export default {
    /**
     * 添加到购物车
     */
    toCart(id) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.cart.toCart",
                data: {
                    id: id,
                }
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.msg);
                }
                return reject(response.msg);
            }).catch(e => {
                return reject(e);
            });
        });
    },
    /**
     * 增加/减少数量
     */
    change(id, type) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.cart." + (type == 'sub' ? 'sub' : 'add'),
                data: {
                    id: id,
                }
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.msg);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 删除
     */
    delete(id) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.cart.delete",
                data: {
                    id: id,
                }
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.msg);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 列表
     */
    list() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.cart.list",
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    }
}
