我正在开发使用Spring启动2和6角,而前提条件之一是允许用户将产品添加到购物车中,即使他们没有通过认证的购物车网站。为了达到这个目标,我的增值产品保存到用户的浏览器作为JSON对象的本地存储,你可以从下面的图片看。
当用户登录时,该数据是合并到其存储验证的用户车数据,与其他他的数据的数据库表。
管理Web存储我使用的库NGX-webstorage:https://www.npmjs.com/package/ngx-webstorage
我写了一个打字稿类来管理存储,这一点我在下面提供:
@Injectable()
export class LocalStorageHelper {
constructor(private localStorageService: LocalStorageService) { }
/**
* Creates an empty cart if no one already exists and returns it.
*/
private createCart(): Cart {
const cart = new Cart(1, 0, []);
this.localStorageService.store('cart', this.toJSONString(cart));
return cart;
}
/**
* Clears the cart (i.e., when the user logs in successfully).
*/
public clearCart(): void {
this.localStorageService.clear('cart');
}
public saveCart(cart: Cart): Cart {
this.localStorageService.store('cart', this.toJSONString(cart));
return cart;
}
/**
* Converts the local storage cart content into a Cart object for further processing.
*/
public getCart(): Cart {
let cart: Cart = this.toJSONObject(this.localStorageService.retrieve('cart'));
if (cart === null) {
cart = this.createCart();
}
return cart;
}
/**
* Adds a product to the cart, by first, it checks whether that product already exists.
*
* @param product the product being added.
*/
public addToCart(product: Product): Cart {
const cart = this.getCart();
const item = cart.items.find(i => i.product.id === product.id);
if (item === undefined) {
cart.items.push(new CartItem(1, product.price, product));
} else {
item.quantity += 1;
item.totalAmount += product.price;
}
cart.totalAmount += product.price;
return this.saveCart(cart);
}
/**
* Drops an item from the cart.
*
* @param productId id of the product being dropped from the cart.
*/
public dropFromCart(productId: number): Cart {
const cart = this.getCart();
const item = cart.items.find(i => i.product.id === productId);
if (item !== undefined) {
cart.totalAmount -= item.totalAmount;
cart.items.splice(cart.items.indexOf(item, 0), 1);
}
return this.saveCart(cart);
}
/**
* Updates the quantity of a product by one with the given one.
*
* @param productId id of the product whose quantity is gonna be updated.
* @param productQty quantity of the product being updated (should be greater than 0).
*/
public updateItemQuantity(productId: number, productQty: number): Cart {
if (productQty < 1) {
return;
}
const cart = this.getCart();
const item = cart.items.find(i => i.product.id === productId);
if (item !== undefined) {
const deltaAmount = CartUtil.calculateDeltaAmount(item, productQty);
item.quantity = productQty;
item.totalAmount = productQty * item.product.price;
cart.totalAmount += deltaAmount;
}
return this.saveCart(cart);
}
/**
* Converts a string to a JSON.
*
* @param value the string being converted.
*/
private toJSONObject(value: string): any {
return JSON.parse(value);
}
/**
* Converts a JSON object to a string.
*
* @param value the JSOn object being converted.
*/
private toJSONString(value: any): string {
return JSON.stringify(value);
}
}
我不喜欢这样的解决方案是,将这些数据以明文暴露,而其他网站在线提供的哈希,即
所以我怀疑是关于用什么格式存储的内容车到本地存储。它是罚款我是怎么做的,现在,还是有这样做的更好的办法?
我不认为你应该在本地存储在用户的机器上车的内容。它可以很容易不同步时,服务器上的产品的变化(如价格被更新)。
我建议你在服务器上创建一个匿名车,只存储车的标识符。我相信这是你指的哈希提其他的解决方案时。
具有存储在服务器上的车会给你比国家同步作为国内其它一些其他的好处,例如。你将能够跟踪车遗弃。