/* Vuex的cart模块的state */
state: () => {
return {
//{pro_id, pro_name, pro_main_images, pro_price, count: 1//已选择件数, selected: true, isEffective: true, pro_stock}
list: []
}
},
/* Vuex的cart模块的actions */
//合并本地购物车
async mergeLocalCart(ctx) {
const userId = ctx.rootState.user.user.u_id;
// console.log("Store-Cart-userid",userId)
// 判断是否登录
if (userId) {
// 调用合并API接口函数进行购物合并
const cartList = ctx.getters.validList.map(({ pro_id, pro_price, selected, count }) => {
return { pro_id, pro_price, selected, count }
})
const res = await mergeCart(userId, cartList)
// console.log("Api-Cart-合并购物车mergeLocalCart-res", res);
// 合并成功将本地购物车删除
ctx.commit('setCartList', [])
} else {
console.log("用户还未登录");
}
}
/* api的购物车请求*/
/**
* axios的POST请求:合并本地购物车
* @param {Array
export const mergeCart = (userId, cartList) => {
//cartList 是 { pro_id, pro_price, selected, count }对象数组
return request('/cart/merge', 'post', {userId,cartList})
}
/*登陆页面login-form.vue的主要代码*/
//登陆成功后合并购物车操作
store.dispatch('cart/mergeLocalCart').then(()=>{
// 2. 提示
Message({ type: "success", text: "登录成功" });
// 3. 跳转
router.push(route.query.redirectUrl || "/");
})
//cartList:[{ pro_id: 1, pro_price: 22, count: 1, selected: true }];
/**
* 查询某一用户的购物车信息
*/
static queryByUserid(cart_user_id) {
return new Promise((resolve, reject) => {
let sql = `SELECT * FROM pin_cart WHERE cart_user_id = ${cart_user_id};`;
this.query(sql).then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
/**
* 修改购物车
*/
static updateById(cart_id,good) {
return new Promise((resolve, reject) => {
let sql = `UPDATE pinke.pin_cart SET cart_product_id = ${good.pro_id} , cart_old_price = ${good.pro_price} , cart_count = ${good.count} , cart_selected = ${good.selected} ,cart_is_effective = '1' WHERE cart_id = ${cart_id}; `;
this.query(sql).then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
/**
* 新增购物车商品
*/
static add(user_id,good) {
return new Promise((resolve, reject) => {
let sql = `INSERT INTO pin_cart (cart_user_id, cart_product_id, cart_old_price, cart_count, cart_selected, cart_is_effective) VALUES (${user_id}, ${good.pro_id}, ${good.pro_price}, ${good.count}, ${good.selected}, '1');`;
this.query(sql).then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
let result = await this.queryByUserid(u_id);
result.findIndex
,如果已存在则更新await this.updateById(result[index].cart_id,good);
,不存在则新增await this.add(u_id, good);
result = await this.queryByUserid(u_id);
res.json({ list: result })
module.exports = class user_dao extends require('../dao/cart_dao') {
/* 合并购物车 */
static async mergeCart(req, res) {
//获取客户端本地购物车信息
let u_id = req.body.userId;
let cartList = req.body.cartList;//cartList:[{ pro_id: 1, pro_price: 22, count: 1, selected: true }]
// console.log("mergeCart__________________req.body", req.body);
//先获取用户的所有购物车信息
let result = await this.queryByUserid(u_id);
//循环本地购物车,如果购物车内不存在则新增,若存在则修改
cartList.forEach(async good => {
//本地的商品是否在result中存在
let index = result.findIndex((currVal) => {
return currVal.cart_product_id === good.pro_id
})
// console.log("mergeCart__________________找到的index",index);
if (index===-1){
//新增
await this.add(u_id, good);
// console.log("mergeCart__________________新增");
}else{
//修改
await this.updateById(result[index].cart_id,good);
// console.log("mergeCart__________________修改");
}
});
result = await this.queryByUserid(u_id);
res.json({ list: result })
}
}
const cart = require('../controller/cart')
/* POST 合并本地购物车. */
router.post('/merge', function (req, res) {
cart.mergeCart(req, res);
});
这是笔者的想法实现,如果有更好的想法希望大家留言给出意见,感谢