封装接口detail.js
export function AddCart(id,type,num){
return myRequest({
url:`/cart`,
method:'POST',
data:{
shop_id:id,
skus_type:type,
num:num
},
header:{
token:true
}
})
}
引入detail.js
import {
getDetail,AddCart
} from '../../utils/detail.js'
对添加购物车按钮设置点击事件addCart
addCart() {
// this.detail请求的页面数据
// this.detail.id请求到的id值
// this.detail.goodsSkus[this.checkgoodsSkusIndex].id
// 判断他是否为单规格,如果是单规格那么就将单规格中的id值传给sku_type,如果为多规格那么就将多规格中的id值传给sku_tpye
let shop_id = this.detail.sku_type === 0 ? this.detail.id : this.detail.goodsSkus[this.checkgoodsSkusIndex]
.id
// 这个skus_type就是请求数据中的单规格与多规格
let skus_type = this.detail.sku_type
// num商品数量
let num = this.detail.num
console.log(shop_id);
AddCart(shop_id,skus_type,num).then(res=>{
console.log(res);
uni.$emit('updateCart')
uni.showToast({
title: '添加成功!正在进入购物车页面.....',
icon:'none'
});
uni.switchTab({
url:'../cart/cart'
})
})
},
在request.js中进行判断拦截
if (options.header.token == false) {
uni.showToast({
title: '请先登录',
icon: 'none'
});
return setTimeout(() => {
uni.navigateTo({
url: '/pages/login/login',
});
}, 1000)
}
对用户登陆进行权限管理