微信小程序京东商城
app.json
{
"pages": [
"pages/index/index",
"pages/shopping/shopping",
"pages/my/my"
],
"window": {
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#f23030",
"navigationBarTitleText": "京东购物",
"navigationBarTextStyle": "white",
"enablePullDownRefresh":true
},
"sitemapLocation": "sitemap.json",
"tabBar":{
"selectedColor":"#f23030",
"list":[{
"pagePath":"pages/index/index",
"text":"首页",
"iconPath": "image/index.png",
"selectedIconPath":"image/indexFull.png"
},{
"pagePath":"pages/shopping/shopping",
"text":"购物车",
"iconPath":"image/cart.png",
"selectedIconPath":"image/cartFull.png"
},{
"pagePath": "pages/my/my",
"text": "我的",
"iconPath": "image/my.png",
"selectedIconPath": "image/myFull.png"
}]
}
}
*app.js*
```js
//app.js
App({
onLaunch: function () {
// 展示本地存储能力
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
}
})
// 获取用户信息
wx.getSetting({
success: res => {
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
this.globalData.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
globalData: {
userInfo: null
}
})
page
index.js
```js
Page({
data: {
txt: ['京东超市', '一元超市', '手机数码', '家用电器', '超值实惠', '生鲜蔬菜', '家居生活', '京东服饰', '京东到家', '充值缴费'],
imgurl: [
'../../image/ico1.png', '../../image/ico2.png', '../../image/ico3.png', '../../image/ico4.png', '../../image/ico5.png', '../../image/ico6.png', '../../image/ico7.png', '../../image/ico8.png', '../../image/ico9.png', '../../image/ico10.png'
], //京东超市小图片
imgSrc: ['../../image/slider1.jpg', '../../image/slider2.jpg', '../../image/slider3.jpg'],//轮播图图片
adHidden:false, //显示隐藏
flag:true, //开关
tuijianList: [],
num:0
},
// 轮播图的小圆点事件
changeimgsrc:function(e){
var num = e.detail.current
this.setData({
num:num
})
},
// 消失隐藏开关事件
bindchange:function(){
this.setData({
adHidden:true,
flag:false
})
},
// 数据请求地址
// onReady:function(){
// wx.request({
// url:'http://www.xingqb.com/jd/products.json',
// header: {
// // 默认值
// 'content-type': 'application/json'
// },
// success:res=>{
// this.setData({
// goods: res.data.goods
// })
// console.log(this.data.goods)
// }
// })
// },
//显示隐藏
setIconHidden:function(){
this.setData({
adHidden : !this.data.adHidden
})
},
//页面滚动监听
onPageScroll:function(e){
if(this.data.flag){
this.setData({
adHidden: e.scrollTop >= 100 ? true : false
})
}
},
//携带商品的id 跳转到商品详情页
jumpDetail(options){
let goods_id = options.currentTarget.dataset.goods_id;
wx.reLaunch({
url: '/pages/my/my?goods_id='+goods_id,
})
},
onLoad: function (options) {
//监听页面加载
//请求数据
wx.showLoading({
title: '正在加载...',
})
let that = this;
wx.request({
url: "http://134.175.165.10:3001/getmsg1",
success: (res) =>{
//隐藏loading
wx.hideLoading()
//把数据赋值一个变量
this.setData({
tuijianList: res.data.goods
})
}
})
},
})
## Index.wxml
```vue
<view class='nav flexCenter' hidden='{{adHidden}}'>
<icon class='nav_img' type='clear' size='16' bindtap='bindchange'></icon>
<image class='nav_logo' src='../../image/logo.png'></image>
<text class='navTxet'>打开京东app,购物更轻松</text>
<view class='nav_text'><text>立即打开</text></view>
</view>
<!-- 轮播图 -->
<view class='swiper_content'>
<swiper class='poster'
autoplay='true'
interval='3000'
circular='true'
bindchange='changeimgsrc'
>
<swiper-item wx:for='{{imgSrc}}' wx:key='item.src'>
<image src='{{item}}'></image>
</swiper-item>
</swiper>
<!-- 小圆点 -->
<view class='dots'>
<view wx:for='{{imgSrc}}' wx:key='item.src' class="{{index==num?'active':''}}"></view>
</view>
</view>
<!-- 京东超市 -->
<swiper class='placard'
indicator-dots='true'>
<swiper-item>
<view class='box clearfix'>
<view class='small' wx:for='{{imgurl}}' wx:key='{{i}}'>
<image src='{{item}}'></image>
<view><text>{{txt[index]}}</text></view>
</view>
</view>
</swiper-item>
<swiper-item>
<view class='box clearfix'>
<view class='small' wx:for='{{imgurl}}' wx:key='{{i}}'>
<image src='{{item}}'></image>
<view><text>{{txt[index]}}</text></view>
</view>
</view>
</swiper-item>
</swiper>
<view class='Img'><image src='../../image/index_wntj.png'></image></view>
<!-- -->
<view class='listBox' wx:for='{{tuijianList}}' wx:key='{{item.goods_id}}'
bindtap='jumpDetail'
id = "{{item.pid}}"
data-goods_id= "{{item.pid}}" >
<image src='https://img12.360buyimg.com/mobilecms/s372x372_{{item.img}}'></image>
<view class='title'>{{item.t}}</view>
<view class='character '>
<text>¥{{item.jp}}</text>
<text class='minus' >{{item.goods_tags}} </text>
<view class='similarity'>看相似</view>
</view>
</view>
css
.nav{
width: 100%;
height: 74rpx;
line-height: 74rpx;
z-index: 999;
display: flex;
position: fixed;
font-size: 24rpx;
color: white;
}
.flexCenter{
/* 主轴方向 两端对齐 中间留白 */
justify-content: space-between;
/* 纵轴方向 居中 */
align-items: center;
background-color: #333333;
}
.nav_img{
width: 10%;
/* height: 74rpx; */
margin-left: 10rpx;
/* text-align: center;
line-height: 104rpx; */
}
.nav_logo{
width: 50rpx;
height: 50rpx;
}
.navTxet{
width: 50%;
height: 74rpx;
}
.nav_text{
background-color: #f23030;
width: 35%;
text-align: center;
}
/* 轮播图 */
.poster{
height:400rpx;
width: 100%;
/* border: 1px solid grey; */
}
.swiper_content{
position: relative;
}
.poster image{
height:400rpx;
width: 100%;
}
.dots{
height: 20rpx;
display: flex;
width: 100%;
position: absolute;
bottom: 10rpx;
justify-content: center;
}
.dots view{
width: 20rpx;
height: 20rpx;
border-radius: 10rpx;
border:2rpx solid white;
margin-right:10rpx;
}
.dots .active{
background: white;
}
/* 京东超市 */
.box{
/* display: flex; */
margin-top: 45rpx;
}
.small{
width: 120rpx;
height: 120rpx;
text-align: center;
line-height: 40rpx;
float: left;
margin-left: 28rpx;
margin-bottom: 70rpx;
}
.small image{
width: 70rpx;
height: 70rpx;
}
.small text{
font-size: 12px;
}
.placard{
height: 420rpx;
}
/* 清除浮动 */
.clearfix:after{
content:"";
height:0;
line-height:0;
display:block;
visibility:hidden;
clear:both;
}
.Img image{
width: 750rpx;
height: 70rpx;
}
/* */
/* .list{
height: 0 auto;
background-color: #f1f2f6;
width: 100%;
margin-bottom: 20rpx; */
/* } */
/* .arrange{
width: 100%; */
/* } */
.listBox{
float: left;
width: 340rpx;
height: 534rpx;
margin-left: 25rpx;
background-color: white;
margin-bottom: 20rpx;
}
.listBox image{
width: 340rpx;
height: 400rpx;
}
.character{
position: relative;
top: 0;
left: 0;
font-size: 14px;
}
.title{
height: 66rpx;
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;
font-size: 28rpx;
}
/* .character text{
color: red;
} */
.minus{
/* position: absolute;
top: 197rpx;
left: 77rpx; */
border: 1px solid red;
color: red;
margin-left: 10rpx;
}
.similarity{
position: absolute;
top: 1rpx;
right: 0;
color: #888888;
border: 1px solid #efefef;
width: 100rpx;
}
my.js
```js
// pages/my/my.js
Page({
/**
* 页面的初始数据
*/
data: {
index:1,
products:[],
product: {},
goods_id: ""
},
addToCart(){
let goods_id = this.data.products;
let storageCarts = wx.getStorageSync("carts") || []
var flag=false;
for (var i = 0; i < storageCarts.length;i++){
if (storageCarts[i].pid == goods_id.pid){
flag=true;
break;
}
}
let index = storageCarts.findIndex((item)=>{
return item = this.data.products
})
console.log(index)
if (flag ){
storageCarts[index].num++;
}else{
let product = this.data.products;
product.num = 1;
product.checked = true;
storageCarts.push(product)
}
wx.setStorageSync("carts", storageCarts)
wx.switchTab({
url:"/pages/shopping/shopping"
})
// })
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log(options)
let goods_id = options.goods_id || "52111606316"
wx.request({
url: 'http://134.175.165.10:3001/getdetails?pid=' + goods_id,
success: (res) => {
this.setData({
products: res.data,
goods_id: goods_id
})
},
})
},
tabIndex(e) {
let index = e.currentTarget.dataset.index
console.log(index);
this.setData({
index
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
## my.xml
```vue
<view class='Img'>
<image src='https://img12.360buyimg.com/mobilecms/s372x372_{{products.img}}'></image>
<view class='col'>¥{{products.jp}}元</view>
<text>{{products.t}}</text>
</view>
<button type='warn' class='btn' hover-class='btn_active' bindtap="addToCart">加入购物车</button>
<view class="detail_info2">
<view
class="{{index==1?'active':''}}"
bindtap="tabIndex"
id=""
data-index="1"
>商品详情</view>
<view
class="{{index==2?'active':''}}"
bindtap="tabIndex"
data-index="2"
>产品参数</view>
<view
class="{{index==3?'active':''}}"
bindtap="tabIndex"
data-index="3"
>售后服务</view>
</view>
<view class="detail_info_container">
<view class="{{index==1?'active':''}}" >商品详情内容</view>
<view class="{{index==2?'active':''}}">产品参数内容</view>
<view class="{{index==3?'active':''}}">售后服务内容</view>
</view>
## my.wxss
```css
/* pages/my/my.wxss */
.Img{
width: 100%;
height: 900rpx;
background-color: #efefef;
}
.Img image{
width: 100%;
height: 600rpx;
}
.col{
color: #f23030;
font-size: 30px;
margin: 20rpx 10rpx;
}
.btn_active{
background: rgba(255,0,0,0.6)
}
.detail_info2{
display: flex;
justify-content: space-between;
border-bottom:1rpx solid #eee;
background: #fff;
height: 60rpx;
line-height: 60rpx;
}
.detail_info2 view{
flex:1;
text-align: center;
font-size:30rpx;
height: 100%;
}
.detail_info2 .active{
border-bottom:4rpx solid red;
}
.detail_info_container{
background: #fff;
}
.detail_info_container view{
display: none;
}
.detail_info_container .active{
display: block;
padding: 20rpx;
font-size:30rpx;
}
## shopping.js
```js
// pages/shopping/shopping.js
Page({
/**
* 页面的初始数据
*/
data: {
length: 0,
storageCarts: [],
isAllChecked: true,
totalPrice: 0
},
singleHandelCheck(e) {
//获取索引
//获取点击的产品的index
let index = e.currentTarget.dataset.index
let storageCarts = this.data.storageCarts
//选中切换
storageCarts[index].checked = !storageCarts[index].checked
//更新缓存
this.setStorage()
//是否改变了全选
this.isAllChecked()
//计算总价
this.computedTotal()
},
isAllChecked() {
//循环迭代购物车列表 缓冲中 item checked
// 如果checked 属性都为true
// isAllChecked true
let storageCarts = this.data.storageCarts
let allChecked = storageCarts.every((item) => {
return item.checked
})
//更新数据
this.setData({
isAllChecked: allChecked
})
},
handelAllCheck() {
let checked = !this.data.isAllChecked
let storageCarts = this.data.storageCarts
//循环迭代
storageCarts.forEach((item) => {
item.checked = checked
})
this.setData({
isAllChecked: checked,
storageCarts
})
//更新缓存
this.setStorage()
//计算总价
this.computedTotal()
},
computedTotal() {
//计算总价
let storageCarts = this.data.storageCarts
//默认值
let totalPrice = 0
storageCarts.forEach((item) => {
if (item.checked) {
totalPrice += (item.num * item.jp)
}
})
this.setData({
totalPrice
})
},
//减少数量
handelReduce(e) {
//获取点击的产品的index
let index = e.currentTarget.dataset.index
let storageCarts = this.data.storageCarts
storageCarts[index].num--
// wx.getStorageSync("carts")
//判断 如果为1 不能再减
if (storageCarts[index].num < 1) {
storageCarts[index].num = 1
}
//设置更新缓存
this.setData({
storageCarts
})
//更新缓存
this.setStorage()
//计算总价
this.computedTotal()
},
handleAdd(e) {
//获取点击的产品的index
let index = e.currentTarget.dataset.index
let storageCarts = this.data.storageCarts
storageCarts[index].num++
// wx.getStorageSync("carts")
//如果num 大于库存数量 提示信息
//storageCarts 缓存 100个 1个 下架 90000
if (storageCarts[index].num > storageCarts[index].c3) {
wx.showModal({
title: '超出商品库存',
content: '',
})
//将库存直接最值给num
storageCarts[index].num = storageCarts[index].c3
}
//设置更新缓存
this.setData({
storageCarts
})
//更新缓存
this.setStorage()
//计算总价
this.computedTotal()
},
handelDele(e) {
let index = e.currentTarget.dataset.index
let storageCarts = this.data.storageCarts
storageCarts.splice(index, 1)
//如果没有改商品的信息
//缺一个判断
//实时更新数据
this.setData({
storageCarts,
length: storageCarts.length
})
//更新缓存
this.setStorage()
//计算总价
this.computedTotal()
},
setStorage() {
wx.setStorageSync("carts", this.data.storageCarts)
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//计算总价
this.computedTotal()
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
//读取缓存
let storageCarts = wx.getStorageSync("carts")
//获取缓存的长度
let length = storageCarts.length
//如果长度为0
let allchecked = storageCarts.every((item) => {
return item.checked
})
// //判断一下 是否全部选中
//缓存中数量为0 或者 没有全部选中的时候
if (length == 0 || allchecked == false) {
this.setData({
isAllChecked: false
})
}
this.setData({
storageCarts,
length
})
//计算总价
this.computedTotal()
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
## shopping.wxss
/* pages/shopping/shopping.wxss */
.content{
position: relative;
top: 0;
left: 0;
width: 100%;
height: 220rpx;
border: 1px solid #efefef;
}
.cbox{
position: absolute;
top: 100rpx;
left: 50rpx;
}
.content image{
position: absolute;
top: 5rpx;
left: 120rpx;
width: 200rpx;
height: 200rpx;
}
.content_title{
position: absolute;
top: 15rpx;
left: 340rpx;
width: 300rpx;
height: 90rpx;
font-size: 16px;
text-overflow:ellipsis;
overflow: hidden;
/* white-space: nowrap; */
}
.price{
position: absolute;
top: 15rpx;
left: 650rpx;
}
.operation{
position: absolute;
top: 150rpx;
left: 240rpx;
}
.minus{
margin: 0 100rpx;
}
.add{
margin: 0 100rpx;
}
.red{
position: fixed;
left: 0;
bottom: 0;
width: 100%;
height: 160rpx;
line-height: 160rpx;
background-color: #f23030;
color: white;
}
.cbox2{
/* background-color: white; */
position: absolute;
left: 15rpx;
top: 0rpx;
z-index: 3;
}
.left_text{
position: absolute;
left: 80rpx;
top: 10rpx;
z-index: 3;
}
.red_text{
position: absolute;
right: 10rpx;
top: 10rpx;
}
.cart_container{
padding-bottom: 120rpx;
}
.cart_message{
width:70%;
height: 80rpx;
line-height: 80rpx;
text-align: center;
background:#eee;
margin:30px auto;
border-radius: 40rpx;
}
.cart_item{
display: flex;
align-items: center;
padding: 20rpx;
border-bottom: 1rpx solid #eee;
}
.cart_item image{
width: 150rpx;
height: 150rpx;
margin: 0 15rpx;
}
.cart_pro_name{
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2; /*多行*/
-webkit-box-orient: vertical;
word-break: break-word;
font-size: 30rpx;
}
.cart_info{
width:500rpx;
height: 150rpx;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.cart_info_hd,.cart_info_bd{
display: flex;
justify-content: space-between;
}
.cart_bottom{
display: flex;
align-items: center;
justify-content: space-between;
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 90rpx;
background: red;
color: white;
}
.cart_bottom label{
margin-left: 15rpx;
}
.cart_bottom view{
margin-right: 15rpx;
}
## 效果展示图
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200329230728653.jpeg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNTI5OTY1,size_16,color_FFFFFF,t_70)