...
```
### 5.2 获取列表数据
```js
const productList = computed(() => {
const productList = cartList[shopId] || [];
return productList
})
```
### 5.3 设置 是否选中 状态
```html
changeCartItemChecked(shopId, item._id)"
>
```
```js
const changeCartItemChecked = (shopId, productId) => {
store.commit('changeCartItemChecked', { shopId, productId })
}
changeCartItemChecked(state, payload) {
const { shopId, productId } = payload;
const product = state.cartList[shopId][productId];
product.check = !product.check;
},
```
## 6 全选及清空购物车的功能实现
### 6.1 清空购物车
```js
const cleanCartProducts = (shopId) => {
store.commit('cleanCartProducts', { shopId })
}
cleanCartProducts(state, payload) {
const { shopId } = payload;
state.cartList[shopId] = {};
},
```
### 6.2 全选
```html
```
1. 设置 是否全选中 状态
```js
const allChecked = computed(() => {
const productList = cartList[shopId];
let result = true
if(productList) {
for (let i in productList) {
const product = productList[i]
if(product.count > 0 && !product.check) {
result = false
}
}
}
return result
})
```
2. 状态改变
```js
const setCartItemsChecked = (shopId) => {
store.commit('setCartItemsChecked', { shopId })
}
setCartItemsChecked(state, payload) {
const { shopId } = payload;
const products = state.cartList[shopId];
if(products) {
for(let key in products) {
const product = products[key];
product.check = true;
}
}
}
```
## 7 商家详情页面代码优化
1. 获取购物车信息逻辑
```js
const useCartEffect = () => {
const total = computed(() => {})
const price = computed(() => {})
const productList = computed(() => {})
const allChecked = computed(() => {})
const changeCartItemChecked = () => {}
const cleanCartProducts = () => {}
const setCartItemsChecked = () => {}
}
```
2. 展示隐藏购物车逻辑
```js
const toggleCartEffect = () => {
const showCart = ref(false);
const handleCartShowChange = () => {}
}
```
## 8 购物车数据结构的变更
### 8.1 store 存放购物车数据
```js
export default createStore({
state: {
cartList: {
// 商铺id: {
// shopName: '沃尔玛',
// productList: {
// productId 商品id: {
// // 商品内容及购物数量
// }
// }
}
},
```
### 8.2 修改相关逻辑
1. 获取商品列表
```js
const productList = cartList[shopId]?.productList;
```
2. 页面展示购物车商品列表
```js
{{ cartList?.[shopId]?.productList?.[item._id]?.count || 0 }}
```
3. 写入 `store` 时的初始化
```js
let shopInfo = state.cartList[shopId] || {
shopName: '',
productList: {}
};
```
### 8.3 获取店铺名称
1. 进入店铺详情 -- 获取列表数据时传递
2. 加入购物车时触发
```js
changeShopName(shopId, shopName);
```
## 9 通过 LocalStorage 实现购物车信息持久存储
```js
const setLocalCartList = (state) => {
const { cartList } = state
const cartListString = JSON.stringify(cartList)
localStorage.cartList = cartListString
}
const getLocalCartList = () => {
return JSON.parse(localStorage.cartList) || {}
}
export default createStore({
state: {
cartList: getLocalCartList()
}
})
```
# 五 核心购物链路开发
> 效果图

## 1 确认订单页面创建及顶部布局
### 1.1 页面创建
1. 跳转
```html
去结算
```
2. 路由
```js
{
path: '/orderConfirmation/:id',
name: 'OrderConfirmation',
component: () => import('../views/orderConfirmation/OrderConfirmation')
},
```
### 1.2 顶部布局
* 渐变
```css
.top {
position: relative;
background-size: 100% 1.59rem;
background-image: linear-gradient(0, rgba(0, 145, 225, 0) 4%, #0091ff 50%);
background-repeat: no-repeat;
}
```
## 2 订单商品列表数据获取与布局

## 3 页面布局及展示逻辑开发
```html
```
```scss
.wrapper {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
background: #eee;
}
.top {
position: relative;
height: 1.96rem;
background-size: 100% 1.59rem;
background-image: linear-gradient(0, rgba(0, 145, 225, 0) 4%, #0091ff 50%);
background-repeat: no-repeat;
}
.products {
margin: .16rem .18rem .1rem .18rem;
&__wrapper {
overflow-y: scroll;
margin: 0 .18rem;
position: absolute;
left: 0;
right: 0;
bottom: .6rem;
top: 2.5rem;
}
}
.order {
position: absolute;
left: 0;
right: 0;
bottom: 0;
display: flex;
height: .49rem;
line-height: .49rem;
background: #fff;
}
```
## 4 确认订单页面组件拆分及弹框布局制作
### 4.1 组件拆分
```html
```
#### 1. `TopArea`
* `handleClick()`
#### 2. `ProductList`
* `const { productList, shopName } = useCommonCartEffect(shopId)`
#### 3. `Order`
* `const { caculations } = useCommonCartEffect(shopId)`
### 4.2 弹框布局制作
* mask + 弹框(取消订单 创建订单)
## 5 购物流程开发完成
* 创建订单/取消订单 -> 清空购物车 + 跳转回首页
## 6 下单流程开发优化
* “去结算” 按钮展示与否
* `productList` 过滤掉 `count==0` 的数据
* 点击 “确认订单” 跳转至 订单页面
## 7 订单列表页布局开发
## 8 订单列表逻辑开发
# 六 真机调试及工程发布流程
## 1 如何进行真机调试
* 局域网同 `ip`
## 2 解决真机调试过程中的展示问题
## 3 实现项目对不同设备的展示适配
* 修改 `public > index.html`
```js
var width = document.documentElement.clientWidth || document.body.clientWidth;
var ratio = width / 375;
var fontSize = 100 * ratio;
document.getElementsByTagName('html')[0].style['font-size'] = fontSize + 'px';
```
## 4 项目上线和课程总结