清空购物车作业怎么写
发布网友
发布时间:2023-03-17 17:00
我来回答
共1个回答
热心网友
时间:2023-11-04 11:40
前端:
cart.vue
async delAll() {
// 统计购物车列表长度
let len = this.list.length;
let result = await deleteAllCart();
if (result.status === 1) {
this.list.splice(0, len);
}
}
interface.js
export const deleteAllCart = (data) => post(url + "api/cart/deleteAll", data);
后端:
controller层:
/**
* 清空用户购物车
* @return \think\response\Json
*/
public function deleteAll() {
$res = (new CartBusiness())->deleteAllReids($this->userId);
if ($res === FALSE) {
return Show::error();
}
return Show::success($res);
}
business层:
/**
* 清空指定用户id的购物车
* @param int $userId 用户id
* @return bool
*/
public function deleteAllReids($userId) {
// 先获取购物车里所有的商品
try {
$get = Cache::hGetAll(Key::userCart($userId));
} catch (\Exception $e) {
Log::record("deleteAllReids".$e->getMessage());
return FALSE;
}
if ($get) {
$res = Cache::del(Key::userCart($userId));
}
return $res