获取一级分类下所有商品、商品详情、登录操作、购物车添加、购物车获取

一.获取一级分类下所有商品

1.页面跳转 传参

index.vue

	// 2.一级分类 切换选项卡
    changeIndex(index,fid){
        this.activeIndex = index

        uni.navigateTo({
            url:"../product/product?fid="+fid
        })
    },

2.获取数据

参数:

参数名说明
fid一级分类id,必填
page请求页码,必填
size分页偏移量,必填

接口:api.js

// 7.获取一级分类下所有商品-分页
const _getCateGoodPage= (data)=>{
	let option={
		url:"/api/getcategoodPage",
		data
	}
	 return http(option)
}

export default {
    _getFirstCate,_getBanner,_getSeckill,_getIndexGoods,_search,_getCates,
    _getCateGoodPage
}

请求代码: product.vue

<template>
	<view>
		<view class="search">
			<icon type="search" color="white" size="10"></icon>
			<input type="text" value="" placeholder="搜索商品" placeholder-class="placeholder"/>
		</view>
		<view class="list" v-if="goods.length > 0">
			<view class="row" v-for="(item,index) in goods" :key="item.id">
				<image :src="baseUrl+item.img" mode="widthFix"></image>
				<view class="info">
					<text>{{item.goodsname}}</text>	
					<text>¥{{item.price}}</text>	
					<text>3565评论</text>	
				</view>
			</view>
		</view>
		<view class="tishi" v-else>
			没有数据亲!
		</view>
	</view>
</template>

<script>
	
	
export default {
	data(){
		return {
			totalPage:1,
			goods:[],
			baseUrl:"",
			page:1,
			size:4
		}
	},
	onShow(){
		this.baseUrl = this.$config.baseUrl
		// console.log(this);
		this.getCateGood()
		
	},
	methods:{
        //1.请求数据,更新视图
		async getCateGood(){
			let res = await this.$api._getCateGoodPage({fid:this.$mp.query.fid,page:this.page,size:this.size})
			//console.log(res);
			// 总页码
			this.totalPage = res.data.list[0]
			
			
			// 数据   null == []
			res.data.list[1] = res.data.list[1] || []
			// 老拼接新数据
			this.goods = this.goods.concat(res.data.list[1] ) 
		}
	},
	// 2.上拉触底 加载下一页数据
	onReachBottom(){
        //页码 +1
		this.page++;
        // 请求的页码 > 总页码 return
		if(this.page >this.totalPage){
			uni.showToast({
				title:"数据已经到底了",
				icon:"none"
			})
			return
		}
		
		this.getCateGood()
		
	}
}
</script>

二.商品详情

0.页面跳转

//10.跳转到商品详情页
			toDetailPage(id){
				uni.navigateTo({
					url:"../details/details?id="+id
				})
			}

1.获取商品数据

**接口 **api.js

// 8获取一个商品信息
const _getGoodsInfo = (data)=>{
	let option={
		url:"/api/getgoodsinfo",
		data
	}
	return http(option)
}

export default {
	_getFirstCate,_getBanner,_getSeckill,_getIndexGoods,_search,_getCates,_getCateGoodPage
	,_getGoodsInfo
}

代码案例 detail.vue

export default {
		data() {
			return {
				info:{},
				baseUrl:"",
				num:1
			}
		},
		methods: {
            //获取商品详情数据 
			async getGoodInfo(){
				let res = await this.$api._getGoodsInfo({id:this.$mp.query.id})
					//console.log(res);
                	//处理商品属性的格式  字符串-->数组
				res.data.list[0].specsattr = res.data.list[0].specsattr.split(",")
                	//更新视图
				this.info = res.data.list[0]
				
			},
            //数量加加
			add(){
				this.num++
				if(this.num >= this.info.number ){
					this.num = this.info.number
				}
			},
            //数量减减
			dec(){
				this.num--
				if(this.num <= 1){
					this.num =1
				}
			}
			
		},
		onShow(){
			this.getGoodInfo()
			this.baseUrl = this.$config.baseUrl
		}

三.登录操作

1.获取手机验证码

参数

参数名说明
phone手机号

接口:api.js

// 9.获取验证码
const _getSms = (data)=>{
	let option={
		url:"/api/sms",
		data
	}
	return http(option)
}

export default {
	_getFirstCate,_getBanner,_getSeckill,_getIndexGoods,_search,_getCates,_getCateGoodPage
	,_getGoodsInfo,_getSms
}

代码案例 send.vue

async getSms(){
				let phone = this.phone
				let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
				// console.log(reg.test(phone));
				
				// 验证手机格式
				if(!reg.test(phone)){
					uni.showToast({
						title:"手机号格式错误",
						icon:"none"
					})
					return
				}
				
				
				let res = await this.$api._getSms({phone})
				console.log(res);
				
			}

2.执行登录

参数

参数名说明
phone手机号

接口

// 9.登录
const _login = (data)=>{
	let option={
		url:"/api/wxlogin",
		data
	}
	return http(option)
}


export default {
	_getFirstCate,_getBanner,_getSeckill,_getIndexGoods,_search,_getCates,_getCateGoodPage
	,_getGoodsInfo,_getSms,_login
}

代码案例

export default {
		data() {
			return {
				phone:"",
				msg:"获取手机验证码",
				time:null,
				isSend:false,
				code:""
			}
			
		},
		methods: {
			async getSms(){
				// 3.2 处理重复点击 ,避免重新触发定时器
				if(this.isSend){ return};
				
				let phone = this.phone
				let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
				// console.log(reg.test(phone));
				
				// 1.验证手机格式
				if(!reg.test(phone)){
					uni.showToast({
						title:"手机号格式错误",
						icon:"none"
					})
					return
				}
				
				// 2.执行 请求验证码
				let res = await this.$api._getSms({phone})
				console.log(res);
				// 3.倒计时效果--
				let num = 20
				if(res.data.code == 200){
					this.time = setInterval(()=>{
						num--
						if(num <0){
							clearInterval(this.time)
							this.msg="重新获取手机验证码"
							
							this.isSend = false
							
							uni.removeStorageSync('code')
							uni.removeStorageSync('phone')
							
							return
						}
						this.msg=num+"s后重新发送"
						
					},1000)
					this.isSend = true
					
				}
				
				// 4.存到本地缓存,code   phone
				uni.setStorageSync('code',res.data.list.code)
				uni.setStorageSync('phone',phone)
				
			},
			// 登录
			async login(){
				// 1.验证手机号 验证码
				// 是不是之前发送验证码 的手机号
				let code = uni.getStorageSync('code')
				let phone = uni.getStorageSync('phone')
				console.log(code,phone);
				// 2.输入框的验证码 和后台返回的code 是否相等
	
				if(this.phone != phone || this.code !=code){
					
					uni.showToast({
						title:"手机号或者验证码不正确",
						icon:"none"
					})
					
					return
				}
				
				// 3.执行登录
				let res = await this.$api._login({phone})
				console.log(res);
				if(res.data.code == 200){
					uni.showToast({
						title:res.data.msg
					})
					
					// 个人信息 uid token phone
					uni.setStorageSync('userInfo',res.data.list)
					
					// 跳转
					uni.navigateBack({
						delta:1
					})
				}
				
			}
			
		}
	}

3.登录检测

一进入项目,检测是否登录,如果登录了,个人中心tab文本显示【我的】,如果没登录,个人中心tab文本显示【未登录】

参数:

参数名说明
authorizationheader头 中需要添加 token 后台验证条件

接口


// 10登录验证
const _checkToken = (header)=>{
	let option={
		url:"/api/checktoken",
		header
	}
	return http(option)
}


export default {
	_getFirstCate,_getBanner,_getSeckill,_getIndexGoods,_search,_getCates,
    _getCateGoodPage,_getGoodsInfo,_getSms,_login,_checkToken
}

(1)封装检测方法

代码案例:checkToken.js

const checkToken = async (_this)=>{
	
	// 1.参数
	let token = uni.getStorageSync('userInfo').token || null
	
	// 1).判断有没有登录
	if(!token){
		uni.setTabBarItem({
			index:2,
			text:"未登录"
		})
		
		return false
	}
	
	// 2)登录
		// 2.执行请求  判断登录是否过期
	let res = await _this.$api._checkToken({authorization:token})
	// console.log(res);
		// 登录过期  设置未登录状态 
	if(res.data.code == 403 || res.data.code == 500){
		
		uni.setTabBarItem({
			index:2,
			text:"未登录"
		})
		// 移除过期的用户token
		uni.removeStorageSync('userInfo')
		
		return false
	}
	
	uni.setTabBarItem({
		index:2,
		text:"我的"
	})
	return true
	
}

export default checkToken

(2)app.vue进行检测

代码案例

<script>
	import checkToken from "utils/checkToken.js"
	// 项目的根组件
	export default {
		async onShow() {
			checkToken(this)
			// // 1.参数
			// let token = uni.getStorageSync('userInfo').token || null
			
			// // 1).判断有没有登录
			// if(!token){
			// 	uni.setTabBarItem({
			// 		index:2,
			// 		text:"未登录"
			// 	})
				
			// 	return
			// }
			
			// // 2)登录
			// 	// 2.执行请求  判断登录是否过期
			// let res = await this.$api._checkToken({authorization:token})
			// console.log(res);
			// 	// 登录过期  设置未登录状态 
			// if(res.data.code == 403 || res.data.code == 500){
			// 	uni.setTabBarItem({
			// 		index:2,
			// 		text:"未登录"
			// 	})
			// 	// 移除过期的用户token
			// 	uni.removeStorageSync('userInfo')
			// 	return
			// }
			
			// uni.setTabBarItem({
			// 	index:2,
			// 	text:"我的"
			// })
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>

(3)个人中心进行检测

代码案例

import checkToken from "utils/checkToken.js"
.....

	data(){
        return {
            personMsgList: [......太长了 删除了],
            //是否登录 的状态
            isLogin:false,
            // 个人信息
            phone:""
        }
    },
    async onShow(){
        // 显示 检测是否登录
        let res = await checkToken(this)
        // console.log(res);
        if(res){
            this.isLogin = true
            this.phone = uni.getStorageSync('userInfo').phone

        }else{
            //uni.navigateTo({
                //url:"../send/send"
           // })
        }
    },
    methods:{
            toSendPage(){
                uni.navigateTo({
                    url:"../send/send"
                })
            }
        }

四.购物车添加

参数:

参数名说明
uid用户编号,必填项
goodsid商品编号,必填项
num数量,必填项
checked是否选中,必填项 ,默认1 选中 0 不选中
authorization放在请求头:header头 中需要添加token后台验证条件

接口

// 11.购物车 添加 
const _cartAdd = (header,data)=>{
	let option={
		url:"/api/cartadd",
		header,
		data
	}
	return http(option)
}


export default {
	_getFirstCate,_getBanner,_getSeckill,_getIndexGoods,_search,_getCates,
	_getCateGoodPage,_getGoodsInfo,_getSms,_login,_checkToken,_cartAdd
}

代码案例:detail.vue

// 购物车添加
async cartAdd(){

    // 1.参数
    let {uid=null,token=null} = uni.getStorageSync('userInfo')
    let data = {
        uid,
        goodsid:this.$mp.query.id,
        num:this.num,
        checked:0
    }
    let header = {authorization:token}

    // 2.执行添加
    let res = await this.$api._cartAdd(header,data)
    console.log(res);
    if(res.data.code == 403 || res.data.code == 500){
        uni.showToast({
            title:res.data.msg,
            icon:"none"
        })
        setTimeout(()=>{
            uni.navigateTo({
                url:"../send/send"
            })
        },1500)

        return
    }
    uni.showToast({
        title:"添加购物车成功"
    })

}

},

五.购物车获取

参数:

参数名说明
uid用户编号,必填项
authorizationheader头中需要添加token后台验证条件

接口

// 12.购物车 获取
const _cartList = (header,data)=>{
	let option={
		url:"/api/cartlist",
		header,
		data
	}
	return http(option)
}


export default {
	_getFirstCate,_getBanner,_getSeckill,_getIndexGoods,_search,_getCates,
	_getCateGoodPage,_getGoodsInfo,_getSms,_login,_checkToken,_cartAdd,_cartList
}

案例: cart.vue

import checkToken from "../../utils/checkToken.js"
	export default {
		data(){
			return {
				isLogin:false,
				cartLists:[],
				baseUrl:""
			}
		},
		methods:{
			toSendPage(){
				uni.navigateTo({
					url:"../send/send"
				})
			},
			async cartList(){
				// 1.找参数
				let {uid,token} = uni.getStorageSync('userInfo')
				let data = {uid}
				let header = {authorization:token}
				
				// 2.执行查询
				let res = await this.$api._cartList(header,data)
				console.log(res);
				this.cartLists = res.data.list || []
			},
			// 加加
			add(index){
				this.cartLists[index].num++;
				
			},
			// 减减
			dec(index){
				this.cartLists[index].num--;
				if(this.cartLists[index].num<1){
					this.cartLists[index].num = 1
				}
					
			}
		},
		async onShow(){
			// 检测是否登录
			this.isLogin = await checkToken(this)
			// console.log(this.isLogin);
			
			// 登录成功,再去获取购物车信息
			if(this.isLogin){
				this.cartList()
				this.baseUrl = this.$config.baseUrl
			}
		}
	}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用: 第1 '================================================= '建立购物车对象,该对象用于直接在程序中调用 '================================================= dim uCart set uCart= new UserCart 第二 建立一个购物车 uCart.CreateCart (可以重复建立,因为里面有IsArray判断。所以建议这句在建立购物车对象后必写) 第三 增加购物车里的商品,在客户端点了某产品后,服务器端处理的ASP文件中接受传过来的产品标志,并访问数据库。分别把AddItem(aID产品标 志如ID,aName产品名称,aPrice1产品价格一,如单价,aPrice2产品价格二如会员价,aPrice3产品价格三如金牌会员价,如果没这么多可以置空 或置0,aCount购买数量,一般是一个,多个的话后面可以用修改函数修改,aImage产品图片地址) 使用方法:aa=uCart.AddItem(aID产品标志如ID,aName产品名称,aPrice1产品价格一,如单价,aPrice2产品价格二如会员价,aPrice3产品价格 三如金牌会员价,如果没这么多可以置空或置0,aCount购买数量,一般是一个,多个的话后面可以用修改函数修改,aImage产品图片地址),返回 true表示成功,false表示失败 第四 增加了以后进如显示页面,就要用到查看购物车 mycart=uCart.ViewCart() For i =LBound(myCart,2) to UBound(myCart,2) if myCart(0,i)"" then myCart(0,i) '获取标号 myCart(1,i) '获取单价 。。。以此类推 end if next 第五 查看了,可以修改购物车,如更改数量等,或是删除其中的 call uCart.ModifItem(mID唯一标志号,mCount产品数量,mFlag-标志 0-添加 1-删除,2-修改 3-清空) '先用给后面参数赋值 修改其中的商品 可以用第四个显示,先接受session的值,然后循环修改 或清空购物车 uCart.RemoveAll() 然后结帐,很简单 myprice=uCart.TPrice() 然后myprice(0)是产品单价的总价格,myprice(1)是产品会员价的总价格,myprice(2)是高级会员的总价格,myprice(3)是产品总数量 将商品装入购物车,这时需要用cookie或session来做一个不同页面间传递的全局变量,也就是说关了浏览器(针对session)或清楚了cookie等原因,本次购物车会消失,就象你今天在商场买了一车的东西,最后没结帐,明天肯定没了,又归位了,当然要有特殊需要保存,可以写数据库!所以这里记录的只需要是该商品的相关信息就可以了,这里我们记录他的 物品ID, 物品单价, 物品名称, 物品数量

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值