网络编程 发布日期:2025/11/1 浏览次数:1
taro 实现购物车逻辑
效果
taro是什么?
Taro 是一套遵循 React 语法规范的 多端开发 解决方案。
需要安装taro ui
$ npm install taro-ui
taro官方文档
taroUI 官方文档
cart/index.jsx页面代码
import Taro, { Component } from '@tarojs/taro'
import { View, Checkbox, CheckboxGroup } from '@tarojs/components'
//用到了taro的三个组件
//想了解可以去查看taro的官方文档
import './index.scss'
import { AtButton, AtInputNumber, AtCard } from 'taro-ui'
import { request, toast } from '../../utils/index'
class Index extends Component {
constructor(props) {
super(props)
this.state = {
message: '', //购物车为空时显示的信息
cartdata: [], //购物车的数据列表
isactive: false, //全选按钮是否选中
check:false, //单个商品购物车是否被选中
totalnum:0, //总数量
totalprice:0, //总价格
activedata:[] //复选框选中的数据列表
}
}
componentDidShow () {
//获取购物车数据
try {
const token = Taro.getStorageSync('token') //这两个数据是我在登录页面,登录时添加到本地的token和用户id
const userid = Taro.getStorageSync('userid')
if (token) { //如果登录了
const usrename = Taro.getStorageSync('username') //同样登录时添加到本地的用户名
Taro.setNavigationBarTitle({ //改变导航栏的标题
title: usrename + '---购物车'
})
request({ //这里的request是封装后的方法
url: '/cart', //接口
data: { //需要传递的数据
token,
userid
}
}).then(res => {
console.log(res.data)
const { code } = res.data
if (code === '10119') { //后端返回的值 ,判断状态
toast({ title: '登录已经过期,请从新登录' })
Taro.navigateTo({ //跳转到登录页
url: '/pages/login/index'
})
} else if (code === '10012') {
this.setState({
message: '购物车空空如也'
})
} else {
//因为taro是基于react的,在react中,状态不能直接改变,要用this.setState
this.setState({ //登录成功,购物车有数据时,将购物车的列表数据添加到购物车数据中
cartdata: res.data.data
})
}
})
} else { //如果没登录
toast({ title: '请登录' })
Taro.navigateTo({ //跳转到登录页面
url: '/pages/login/index'
})
}
} catch (e) {
}
}
componentDidUpdate(){
//计算总数量,总价格
let num=0;
let price=0;
if(this.state.activedata.length!=0){ //如果选中的数组长度不为0时,就是有商品被选中了
this.state.activedata.map((item)=>{ //map遍历数组
num+= +item.num //将数量相加 + 号为一元运算符,将字符串类型转换为数值类型
price+=item.num*item.price //求价格
})
this.setState({ //设置值
totalnum:num,
totalprice:price
})
}else{ //如果没有商品被选中
this.setState({
totalnum:0,
totalprice:0
})
}
}
render() {
return ( //结构开始
<View>{
this.state.message.length === 0 "htmlcode">
cart/index.scss页面代码
@import "~taro-ui/dist/style/components/card.scss";
@import "~taro-ui/dist/style/components/button.scss";
@import "~taro-ui/dist/style/components/loading.scss";
@import "~taro-ui/dist/style/components/icon.scss";
@import "~taro-ui/dist/style/components/input-number.scss";
utils/index.js代码
const publicurl ='',//接口就不放上去了,因为也不是我的,这里就放接口前的公共网址
import Taro from '@tarojs/taro'
export function request(options){
const {url,data,method}=options
wx.showLoading({ //显示loading框
title: '加载中',
})
return new Promise((resolve,reject)=>{
Taro.request({ //数据请求 与小程序类似
url: publicurl+url,
data:data || {},
method:method || 'GET',
success(res){
//成功
resolve(res)
},
fail(err){
//失败
reject(err)
},
complete(){
// complete 接口调用结束的回调函数
wx.hideLoading(); //隐藏loading框
}
})
})
}
export function toast(options){
const {title,icon, duration}=options
Taro.showToast({
title,
icon: icon || 'none',
duration:duration || 1000
})
}
总结