Skip to content

Commit bf4adfe

Browse files
committed
微信小程序首页商品列表接口对接完
1 parent c52f0bc commit bf4adfe

File tree

16 files changed

+248
-65
lines changed

16 files changed

+248
-65
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ dist
77
.bashrc
88
debug
99
wemall
10-
sdk
10+
sdk
11+
configuration.prod.json
12+
DESIGN.md
File renamed without changes.

go/controller/product/product.go

+85-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"wemall/go/model"
1313
)
1414

15-
// List 产品列表
15+
// List 商品列表
1616
func List(ctx *iris.Context) {
1717
var products []model.Product
1818
pageNo, err := strconv.Atoi(ctx.FormValue("pageNo"))
@@ -21,6 +21,90 @@ func List(ctx *iris.Context) {
2121
pageNo = 1
2222
}
2323

24+
offset := (pageNo - 1) * config.ServerConfig.PageSize
25+
26+
//默认按创建时间,降序来排序
27+
var orderStr = "created_at"
28+
if ctx.FormValue("order") == "1" {
29+
orderStr = "total_sale"
30+
} else if ctx.FormValue("order") == "2" {
31+
orderStr = "created_at"
32+
}
33+
if ctx.FormValue("asc") == "1" {
34+
orderStr += " asc"
35+
} else {
36+
orderStr += " desc"
37+
}
38+
39+
isError := false
40+
errMsg := ""
41+
cateID, err := strconv.Atoi(ctx.FormValue("cateId"))
42+
43+
if err != nil {
44+
fmt.Println(err.Error())
45+
isError = true
46+
errMsg = "分类ID不正确"
47+
}
48+
49+
var category model.Category
50+
51+
if model.DB.First(&category, cateID).Error != nil {
52+
isError = true
53+
errMsg = "分类ID不正确"
54+
}
55+
56+
if isError {
57+
ctx.JSON(iris.StatusOK, iris.Map{
58+
"errNo" : model.ErrorCode.ERROR,
59+
"msg" : errMsg,
60+
"data" : iris.Map{},
61+
})
62+
return
63+
}
64+
65+
pageSize := config.ServerConfig.PageSize
66+
queryErr := model.DB.Offset(offset).Limit(pageSize).Order(orderStr).Find(&products).Error
67+
68+
if queryErr != nil {
69+
ctx.JSON(iris.StatusOK, iris.Map{
70+
"errNo" : model.ErrorCode.ERROR,
71+
"msg" : "error",
72+
"data" : iris.Map{},
73+
})
74+
return
75+
}
76+
77+
for i := 0; i < len(products); i++ {
78+
err := model.DB.First(&products[i].Image, products[i].ImageID).Error
79+
if err != nil {
80+
fmt.Println(err.Error())
81+
ctx.JSON(iris.StatusOK, iris.Map{
82+
"errNo" : model.ErrorCode.ERROR,
83+
"msg" : "error",
84+
"data" : iris.Map{},
85+
})
86+
return
87+
}
88+
}
89+
90+
ctx.JSON(iris.StatusOK, iris.Map{
91+
"errNo" : model.ErrorCode.SUCCESS,
92+
"msg" : "success",
93+
"data" : iris.Map{
94+
"products": products,
95+
},
96+
})
97+
}
98+
99+
// AdminList 商品列表,后台管理提供的接口
100+
func AdminList(ctx *iris.Context) {
101+
var products []model.Product
102+
pageNo, err := strconv.Atoi(ctx.FormValue("pageNo"))
103+
104+
if err != nil || pageNo < 1 {
105+
pageNo = 1
106+
}
107+
24108
offset := (pageNo - 1) * config.ServerConfig.PageSize
25109

26110
//默认按创建时间,降序来排序

go/model/uservisit.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
// UserVisit 访客记录
99
type UserVisit struct {
10-
ID uint `gorm:"primary_key" json:"id"`
10+
ID uint `gorm:"primary_key" json:"id"`
1111
URL string `json:"url"`
1212
Referrer string `json:"referrer"`
1313
ClientID string `json:"clientID"`
@@ -40,7 +40,7 @@ func (userVisit UserVisit) Latest30DayPV() (PVPerDay) {
4040
before29Date := time.Unix(before29, 0)
4141

4242
sqlData := before29Date.Format("2006-01-02")
43-
sqlArr := []string{
43+
sqlArr := []string{
4444
"SELECT count(*) as pv, DATE_FORMAT(visit_time,'%Y-%m-%d') as date",
4545
"FROM user_visits",
4646
"WHERE visit_time >= ?",

go/route/route.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func Route(app *iris.Framework) {
2020
router := app.Party(apiPrefix)
2121
{
2222
router.Get("/categories", category.List)
23+
router.Get("/products", product.List)
2324
router.Get("/visit", visit.PV)
2425
router.Get("/ueditor", ueditor.Handler)
2526
router.Post("/ueditor", ueditor.Handler)
@@ -33,7 +34,7 @@ func Route(app *iris.Framework) {
3334
adminRouter.Post("/category/update", category.Update)
3435
adminRouter.Post("/category/status/update", category.UpdateStatus)
3536

36-
adminRouter.Get("/products", product.List)
37+
adminRouter.Get("/products", product.AdminList)
3738
adminRouter.Get("/product/:id", product.Info)
3839
adminRouter.Post("/product/create", product.Create)
3940
adminRouter.Post("/product/update", product.Update)

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ func main() {
6363
})
6464

6565
app.Listen(":" + strconv.Itoa(config.ServerConfig.Port))
66-
}
66+
}

nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ function receive(data) {
1313
export default function(product) {
1414
return dispatch => {
1515
var url = pageConfig.apiPath + '/admin/product/update';
16+
if (!product.id) {
17+
url = pageConfig.apiPath + '/admin/product/create';
18+
}
1619
var categories = [];
1720
for (var i = 0; i < product.categories.length; i++) {
1821
var idArr = product.categories[i].split('-');
@@ -21,7 +24,6 @@ export default function(product) {
2124
});
2225
}
2326
var reqData = {
24-
id : product.id,
2527
name : product.name,
2628
categories : categories,
2729
status : parseInt(product.status),
@@ -32,6 +34,9 @@ export default function(product) {
3234
remark : product.remark,
3335
detail : product.detail
3436
};
37+
if (product.id) {
38+
reqData.id = product.id;
39+
}
3540
return fetch(url, {
3641
method: 'POST',
3742
headers: {

nodejs/static/javascripts/admin/config/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var sidebarData = [
6969
{
7070
id : getId(),
7171
title : '商品管理',
72-
url : ['/product/manage', /^\/product\/edit\/[0-9]+$/]
72+
url : ['/product/manage', '/product/add', /^\/product\/edit\/[0-9]+$/]
7373
},
7474
{
7575
id : getId(),

nodejs/static/javascripts/admin/containers/ProductManage.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { connect } from 'react-redux';
33
import { Link } from 'react-router';
44

55
import {
6+
Button,
67
Icon,
78
Row,
89
Col,
@@ -139,7 +140,15 @@ class ProductManage extends Component {
139140
<Row gutter={24}>
140141
<Col span={24}>
141142
<div className="product-list-box">
142-
<div className="product-list-title">商品列表</div>
143+
<div className="product-list-title">商品列表
144+
<ul className="action-group">
145+
<li>
146+
<Link href={"#product/add"}>
147+
<Button type="primary">添加商品</Button>
148+
</Link>
149+
</li>
150+
</ul>
151+
</div>
143152
<Table rowKey="id" columns={columns}
144153
loading={isLoading} pagination={false}
145154
dataSource={data.products} bordered />

nodejs/static/javascripts/admin/containers/product/EditProduct.js

+55-36
Original file line numberDiff line numberDiff line change
@@ -107,45 +107,64 @@ class EditProduct extends Component {
107107
});
108108
}
109109
componentWillReceiveProps(nextProps) {
110+
var self = this;
110111
var product = nextProps.data.product;
111112
var allCategories = nextProps.data.categories;
112-
if (product && allCategories && allCategories.length > 0) {
113-
var categories = [];
114-
for (var i = 0; i < product.categories.length; i++) {
115-
var parentId = product.categories[i].parentId;
116-
var id = product.categories[i].id;
117-
categories.push(utils.parseTreeNodeKey(allCategories, id));
118-
}
119-
120-
var imageList = [];
121-
var pImageList = product.images || [];
122-
for (var i = 0; i < pImageList.length; i++) {
123-
imageList.push({
124-
uid : pImageList[i].id,
125-
name : pImageList[i].orignalTitle,
126-
status : 'done',
127-
url : pImageList[i].url
128-
});
129-
}
130-
131-
var imageURL = product.image && product.image.url || '';
132113

133-
this.setState({
134-
productId : product.id,
135-
categories : categories,
136-
name : product.name,
137-
detail : product.detail,
138-
originalPrice : product.originalPrice,
139-
price : product.price,
140-
remark : product.remark,
141-
status : product.status + '',
142-
imageID : product.imageID,
143-
imageData : imageURL,
144-
imageIDs : product.imageIDs,
145-
imageList : imageList,
114+
function onDataReady(data) {
115+
var product = data.product;
116+
self.loadUEditor();
117+
self.setState({
118+
productId : product && product.id || '',
119+
categories : data.categories || [],
120+
name : product && product.name || '',
121+
detail : product && product.detail || '',
122+
originalPrice : product && product.originalPrice,
123+
price : product && product.price,
124+
remark : product && product.remark || '',
125+
status : (product && product.status + '') || '3',
126+
imageID : product && product.imageID || '',
127+
imageData : data.imageURL || '',
128+
imageIDs : product && product.imageIDs || '[]',
129+
imageList : data.imageList || [],
146130
isLoading : false
147131
});
148-
this.loadUEditor();
132+
}
133+
if (allCategories && allCategories.length > 0) {
134+
if (this.state.productId) {
135+
if (product) {
136+
var categories = [];
137+
for (var i = 0; i < product.categories.length; i++) {
138+
var parentId = product.categories[i].parentId;
139+
var id = product.categories[i].id;
140+
categories.push(utils.parseTreeNodeKey(allCategories, id));
141+
}
142+
143+
var imageList = [];
144+
var pImageList = product.images || [];
145+
for (var i = 0; i < pImageList.length; i++) {
146+
imageList.push({
147+
uid : pImageList[i].id,
148+
name : pImageList[i].orignalTitle,
149+
status : 'done',
150+
url : pImageList[i].url
151+
});
152+
}
153+
154+
var imageURL = product.image && product.image.url || '';
155+
onDataReady({
156+
product : product,
157+
imageURL : imageURL,
158+
imageList : imageList,
159+
categories : categories
160+
});
161+
}
162+
} else {
163+
onDataReady({
164+
product : null,
165+
imageURL : ''
166+
});
167+
}
149168
}
150169
}
151170
onNameBlur(event) {
@@ -351,11 +370,11 @@ class EditProduct extends Component {
351370
</div>
352371
</FormItem>
353372
<FormItem {...formItemLayout} label="原价">
354-
<InputNumber min={0} max={100} defaultValue={originalPrice} step={0.01} onBlur={this.onOriginalPriceBlur} />
373+
<InputNumber min={0} max={1000000} defaultValue={originalPrice} step={0.01} onBlur={this.onOriginalPriceBlur} />
355374
356375
</FormItem>
357376
<FormItem {...formItemLayout} label="促销价">
358-
<InputNumber min={0} max={100} defaultValue={price} step={0.01} onBlur={this.onPriceBlur} />
377+
<InputNumber min={0} max={1000000} defaultValue={price} step={0.01} onBlur={this.onPriceBlur} />
359378
360379
</FormItem>
361380
<FormItem {...formItemLayout} label="备注">

nodejs/static/javascripts/admin/route/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ const EditProduct = (location, cb) => {
3838
}, 'admin/product/editProduct');
3939
};
4040

41+
const AddProduct = (location, cb) => {
42+
require.ensure([], require => {
43+
cb(null, require('../containers/product/EditProduct').default);
44+
}, 'admin/product/editProduct');
45+
};
46+
4147
const CategoryManage = (location, cb) => {
4248
require.ensure([], require => {
4349
cb(null, require('../containers/CategoryManage').default);
@@ -66,6 +72,7 @@ export default (history) => {
6672
<Route path="/product/analyze" getComponent={ProductAnalyze}/>
6773
<Route path="/product/manage" getComponent={ProductManage}/>
6874
<Route path="/product/edit/:id" getComponent={EditProduct}/>
75+
<Route path="/product/add" getComponent={AddProduct}/>
6976
<Route path="/category/manage" getComponent={CategoryManage}/>
7077
<Route path="/category/edit/:id" getComponent={EditCategory}/>
7178
</Route>

nodejs/static/styles/admin/productManage.css

+4
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111
font-size: 20px;
1212
margin-bottom: 6px;
1313
color: #000;
14+
}
15+
16+
.action-group {
17+
float: right;
1418
}

wexin/config/config.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
var apiPrefix = 'http://dev.wemall.com/api';
22

33
var config = {
4-
reqCategoryList: '/categories',
4+
static: {
5+
imageDomain: 'http://dev.wemall.com'
6+
},
7+
api: {
8+
reqCategoryList: '/categories',
9+
reqProductList: '/products'
10+
}
511
};
612

7-
for (var key in config) {
8-
config[key] = apiPrefix + config[key];
13+
for (var key in config.api) {
14+
config.api[key] = apiPrefix + config.api[key];
915
}
1016

1117
module.exports = config;

0 commit comments

Comments
 (0)