微信小程序商城分享,砍价功能(前端开发)
2023-02-05 本文已影响0人
遇见wn
1.了解分享,砍一刀结构流程
(1).分享
创建点击事件触发分享事件》创建需要分享的页面》使用官方的页面生命周期函数onShareAppMessage》监听分享事件构建后续业务逻辑
(2).砍一刀
接收分享的页面》验证登录情况》点击砍价事件》判断是否已经砍过》砍价成功后与分享前页面的交互
2.分享逻辑代码
(1).创建分享事件 使用button标签类型为share
<button class="btn" open-type="share">
<text>邀请好友砍价</text>
</button>
(2).分享逻辑
// 页面生命周期函数,自定义此页面的转发给好友(已经有全局的分享方法,此处会覆盖全局)
onShareAppMessage(res) {
return {
//分享预览页的标题
title: '朋友邀请您来砍价哦!',
//需要分享的页面路径和需要传递的参数,例如:当前需要分享的页面路径是/pagesA/share/share
path:
'/pagesA/share/share?orderId=' + orderId + '&userIds=' + userIds
//分享预览页的图片
imageUrl: ' '
};
},
3.砍一刀逻辑代码
(1).调用后端接口,判断已砍和未砍
getmyFriendBargainInfo() {
//具体请求参数和后端协商 例如:bargainOrDoughId:传过来的订单id,friendUserId:帮助砍价人的账号id,driverUserId:发起砍价人的账号id
this.api.myFriendBargainInfo({ bargainOrDoughId: this.options.orderId, friendUserId: uni.getStorageSync('userId'), driverUserId: this.options.userIds }).then(res => {
console.log(res);
this.message = res.data.data;
});
},
(2).砍一刀逻辑
bargainHelp() {
//判断是否已经砍过了
if (this.message.myFriendBargainIdStatus == 0) {
this.api
.UserBargaining({ bargainId: this.options.id, driverUserId:uni.getStorageSync('userId') })
.then(red => {
console.log(red);
this.getmyFriendBargainInfo();
//砍价成功后向外发出一个事件,为了与分享前的页面做交互,分享前的页面接受参数后,会更新砍价进度,重新刷新数据渲染页面
uni.$emit('bargainOrderUpdate', { msg: '砍价页面更新' });
uni.showToast({
title: '砍价成功',
duration: 2000
});
})
.catch(() => {
this.tui.toast('砍价失败');
});
} else if (this.message.myFriendBargainIdStatus == 1) {
this.tui.toast('您已经砍过了');
}
},
4.交互
//接受砍价页发送出来的事件
uni.$on("bargainUpdate",data => {
//帮助砍价成功后执行的后续逻辑,比如页面砍价进度的刷新...
})