var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')
const App = getApp()
Page({
data: {
userInfo: {},
items: [
{
icon: '../../assets/images/iconfont-order.png',
text: '我的订单',
path: '/pages/order/list/index'
},
{
icon: '../../assets/images/iconfont-addr.png',
text: '收货地址',
path: '/pages/address/list/index'
},
{
icon: '../../assets/images/iconfont-kefu.png',
text: '联系客服',
path: '15219941518',
}
],
settings: [
{
icon: '../../assets/images/iconfont-clear.png',
text: '清除缓存',
path: '0.0KB'
},
{
icon: '../../assets/images/iconfont-about.png',
text: '关于我们',
path: '/pages/about/about'
}
]
},
onLoad:function(options){
const openId = wx.getStorageSync('Session3rd')
if(openId){ //如果登录
this.getuserInfo()
this.getStorageInfo()
}else{
this.login()
}
},
login:function(){
wx.redirectTo({
URL:"/pages/login/login"
});
},
getUserInfo:function() {
var that=this
console.log(App.globalData.userInfo);
if (App.globalData.userInfo) {
this.setData({
userInfo:App.globalData.userInfo
})
return
}else{
wx.getUserInfo({
success: function(res) {
var userInfo = res.userInfo
that.setData({
userInfo: userInfo
})
}
})
}
},
getStorageInfo:function() {
var that=this
wx.getStorageInfo({
success: function(res) {
that.setData({
'settings[0].path': `${res.currentSize}KB`
})
}
})
},
navigateTo:function(e) {
const index = e.currentTarget.dataset.index
const path = e.currentTarget.dataset.path
switch(index) {
case 2:
wx.makePhoneCall({
phoneNumber: path
})
break
DEFAULT:
wx.navigateTo({
URL:path
})
}
},
bindtap:function(e) {
const index = e.currentTarget.dataset.index
const path = e.currentTarget.dataset.path
switch(index) {
case 0:
wx.showModal({
title: '友情提示',
content: '确定要清除缓存吗?',
success: function(res) {
if (res.confirm) {
wx.clearStorage()
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
break
DEFAULT:
wx.navigateTo({
url:path
})
}
},
LOGOut:function() {
var that=this
wx.showModal({
title: '友情提示',
content: '确定要登出吗?',
success: function(res) {
if (res.confirm) {
that.signOut()
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
},
signOut:function() {
wx.request({
url:config.service.signOutUrl,
data:{Session3rd:'session3rd'},
success:function(res){
if (res.Meta.code == 0) {
wx.removeStorageSync('session3rd')
wx.redirectTo({
url:"/pages/login/login"
})
}
}
})
},
})
【核心代码】
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')
const App = getApp()
Page({
data: {
logged: !1,
LOGO:'http://lib.diygw.com/static/img/Favicon.png',
title: '小商场'
},
onLoad() {},
onShow() {
const openId = wx.getStorageSync('session3rd')
this.setData({
logged: !!openId
})
openId && setTimeout(this.goIndex, 1500)
},
login() {
this.wechatDecryptData(this.goIndex)
},
goIndex() {
wx.switchTab({
url: '/pages/start/start'
})
},
showModal() {
wx.showModal({
title: '友情提示',
content: '获取用户登录状态失败,请重新登录',
showCancel: !1,
})
},
wechatDecryptData(cb) {
var that= this;
let code;
wx.login({
success: function(res) {
if (res.code) {
wx.getUserInfo({
success:function(result){
// 可以将 res 发送给后台解码出 unionId
App.globalData.userInfo = result.userInfo
wx.request({
url: config.service.sessionUrl,
data: {code: res.code},
header:{'content-type': 'application/json'},
success: function (res) {
wx.setStorageSync('session3rd', res.data.session3rd)
cb();
}
})
}
});
}
}
});
}
})
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')
const App = getApp()
Page({
data: {
canEdit: !1,
totalPrice:0,
hasList:false, // 列表是否有数据
selectAllStatus:true, // 全选状态,默认全选
obj:{
name:"hello"
},
carts: {
items: [],
params: {
page : 1,
limit: 10,
},
total:1
}
},
onShow() {
this.getCarts()
},
/**
* 加载购物车商品列表
*/
getCarts() {
var that=this
const session3rd = wx.getStorageSync('session3rd')
const carts = this.data.carts;
const params = carts.params;
wx.request({
url:config.service.cartUrl,
data:{act:'list',session3rd:session3rd,page:params.page,limit:params.limit},
success:function(result){
var totalprice=0;
if (result.data.code == 0) {
result.data.data.items.forEach(function(n){
n.num=Math.abs(n.num)
totalprice=totalprice (n.price*n.num);
});
that.setData({
totalPrice:totalprice,
hasList: result.data.data.total,
'carts.items': result.data.data.items,
});
}else if(result.data.code == -4){
that.showToast(result.data.msg)
wx.removeStorageSync('session3rd');
}
}
})
},
/**
* 加载更多商品
*/
onPullDownRefresh() {
this.getCarts()
},
/**
* 查看商品详情
*/
navigateTo(e) {
wx.navigateTo({
url:'/pages/goods/detail/index?id=' e.currentTarget.dataset.id
})
},
/**
* 删除商品
*/
del(e) {
var that=this;
const session3rd = wx.getStorageSync('session3rd')
const id = e.currentTarget.dataset.id
wx.showModal({
title: '友情提示',
content: '确定要删除这个宝贝吗?',
success:function(res){
if (res.confirm == 1) {
wx.request({
url: config.service.cartUrl,
data:{act:'del',id:id,session3rd:session3rd},
success:function(result){
if (result.data.code == 0) {
that.getCarts()
}
}
});
}
}
});
},
/**
* 提示
*/
showToast(message) {
wx.showToast({
title: message,
icon: 'success',
duration: 1500,
})
},
/**
* 商品数量改变价格重新计算
* id 商品id
* total 商品数量
*/
chanGECartTotal:function(id,total){
var that=this
var totalprice=0;
var items = this.data.carts.items;
console.log(id)
items.forEach(function (n) {
if(n.selected){ //选中
if(n.id==id){
n.num=total;
totalprice=totalprice (n.price*n.num);
}else{
totalprice=totalprice (n.price*n.num);
}
}
});
that.setData({
'carts.items': items,
totalPrice:totalprice
});
},
/**
* 减少数量
*/
minusCount(e) {
const id = e.currentTarget.dataset.id
const total = Math.abs(e.currentTarget.dataset.total)
if (total == 1) return
e.currentTarget.dataset.total = total-1;
this.chanGECartTotal(id, total-1);
},
/**
* 增加数量
*/
addCount(e) {
const id = e.currentTarget.dataset.id
const total = Math.abs(e.currentTarget.dataset.total)
if (total == 100) return
e.currentTarget.dataset.total = total 1;
this.changeCartTotal(id, total 1);
},
/**
* 当前商品选中事件
*/
selectList(e) {
const index = e.currentTarget.dataset.index;
let carts = this.data.carts.items;
const selected = carts[index].selected;
carts[index].selected = !selected;
this.setData({
'carts.items': carts
});
this.changeCartTotal();
},
/**
* 购物车全选事件
*/
selectAll(e) {
let selectAllStatus = this.data.selectAllStatus;
selectAllStatus = !selectAllStatus;
let carts = this.data.carts.items;
for (let i = 0; i < carts.length; i ) {
carts[i].selected = selectAllStatus;
}
this.setData({
selectAllStatus: selectAllStatus,
'carts.items': carts
});
this.changeCartTotal();
},
/**
* 确认订单
*/
confirmOrder(e) {
var that=this
const session3rd = wx.getStorageSync('session3rd')
var cart=[]
var cartid=[]
this.data.carts.items.forEach(function (n) {
if(n.selected){//选中
n.totalPrice=n.price*n.num;
cart.push(n);
cartid.push({good_id:n.good_id,num:n.num})
}else{
cartid.push({good_id:n.good_id,num:n.num})
}
});
if(cart.length!=0){ //如果为空
wx.request({
url:config.service.cartUrl,
data:{act:'updates',session3rd:session3rd,cartid:cartid},
success:function(result){
}
})
wx.setStorageSync('confirmOrder', cart)
wx.navigateTo({
url:'/pages/order/confirm/index'
})
}else{
that.showToast('请选项中商品!')
}
},
})
来源:搜素材网素材
搜素材网所有素材均为本站用户上传,仅供学习与参考,请勿用于商业用途,如有侵犯您的版权请联系客服服务QQ
本站提供各类html5响应式模板,前端js素材,网站模板,后台模板素材,程序源码素材。
由于技术有限本站不提供安装服务与bug修复,各类源码只提供分享服务,感谢您的理解。
如果对本站有任何意见请点击右侧侧边栏的反馈意见,我们会及时处理。