佳宸学习和分享笔记的地方

0%

vue.js商城项目(七)

vue.js商城项目(七)

数据提纯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fs.readFile('./goods.json', 'utf8', function(err, data) {
let newData = JSON.parse(data)
let i = 0
let pushData = []
newData.RECORDS.map(function(value, index) {
if (value.IMAGE1 != null) {
i++
console.log(value.NAME)
pushData.push(value)
}
})
fs.writeFile('./newGoods.json', JSON.stringify(pushData), function(err) {
if (err) console.log('写文件操作失败');
else console.log('写文件操作成功');
});
});

提取有图的json数据

存入mongoose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
fs.readFile('./newGoods.json', 'utf8', (err, data) => {
data = JSON.parse(data)
let saveCount = 0
const Goods = mongoose.model('Goods')
data.map((value, index) => {
//console.log(value)
let newGoods = new Goods(value)
newGoods.save().then(() => {
saveCount++
console.log('成功' + saveCount)
}).catch(error => {
console.log('失败:' + error)
})
})

1570762124307

双栏列表

1571132512573

前端axios请求后台,后台查询数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  getCategory() {
axios({
url: url.getCategoryList,
method: 'get',
})
.then(response => {
//console.log(response)
if (response.data.code == 200 && response.data.message) {
this.category = response.data.message
//console.log(this.category)
this.getCategorySubByCategoryId(this.category[0].ID)
} else {
Toast('服务器错误,数据取得失败')
}
})
.catch(error => {
console.log(error)
})
},
clickCategory(index, categoryId) {
this.categoryIndex = index
this.page = 1
this.finished = false
this.goodList = []
this.getCategorySubByCategoryId(categoryId)
},
//根据大类ID读取小类类别列表
getCategorySubByCategoryId(categoryId) {

axios({
url: url.getCategorySubList,
method: 'post',
data: { categoryId: categoryId }
})
.then(response => {
//console.log(response)
if (response.data.code == 200 && response.data.message) {
this.categorySub = response.data.message
this.active = 0
this.categorySubId = this.categorySub[0].ID
this.onLoad()
} else {
Toast('服务器错误,数据取得失败')
}
})
.catch(error => {
console.log(error)
})
},
onLoad() {
setTimeout(() => {
this.categorySubId = this.categorySubId?this.categorySubId:this.categorySub[0].ID
this.getGoodList()
}, 500)
},
onRefresh() {
setTimeout(() => {
this.isRefresh = false
this.finished = false
this.goodList=[]
this.page=1
this.onLoad()
}, 500)
},
// 获取商品列表
getGoodList() {
axios({
url: url.getGoodsListByCategorySubID,
method: 'post',
data: {
categorySubId: this.categorySubId,
page: this.page
}
})
.then(response => {
console.log(response)
if (response.data.code == 200 && response.data.message.length) {
this.page++
this.goodList = this.goodList.concat(response.data.message)
} else {
this.finished = true;
}
this.loading = false;
console.log(this.finished)
})
.catch(error => {
console.log(error)
})
},
//点击子类获取商品信息
onClickCategorySub(index, title) {
//console.log( this.categorySub)
this.categorySubId = this.categorySub[index].ID
console.log(this.categorySubId)
this.goodList = []
this.finished = false
this.page = 1
this.onLoad()

},

}