Shop.go
2.95 KB
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
103
104
105
106
107
108
109
110
111
112
113
package Api
import (
"encoding/json"
"github.com/aarongao/tools"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
"letu/DB"
)
// @Title 查询商品信息
// @Description 查询商品信息
// @Accept json
// @Produce json
// @Param id 5dfb03070a9ac17ac7a82054 string true "id"
// @Success 200 {object} tools.ResponseSeccess "Price=价格;ShopName=店铺名称;KvPhoto用于列表页的图片;TopPhoto详情页最上面的轮播图;Images详情页下面的产品详细图"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /CommodityInfo? [get]
func CommodityInfo(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
if c.Query("id") == "" {
c.JSON(200, tools.ResponseError{
1,
"空",
})
return
}
var SCommodity *DB.SCommodity
DB.CCommodity.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SCommodity)
if SCommodity == nil {
c.JSON(200, tools.ResponseError{
1,
"空",
})
} else {
c.JSON(200, tools.ResponseSeccess{
0,
SCommodity,
})
}
}
// @Title 查询所有商品
// @Description 查询所有商品
// @Accept json
// @Produce json
// @Success 200 {object} tools.ResponseSeccess "Price=价格;ShopName=店铺名称;KvPhoto用于列表页的图片;TopPhoto详情页最上面的轮播图;Images详情页下面的产品详细图"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllCommodity? [get]
func AllCommodity(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
var aCommoditys []DB.SCommodity
DB.CCommodity.Find(bson.M{}).All(&aCommoditys)
c.JSON(200, aCommoditys)
}
// @Title 更新商品
// @Description 更新商品
// @Accept json
// @Produce json
// @Success 200 {object} tools.ResponseSeccess ""
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UpdateCommodity? [post]
func UpdateCommodity(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
c.Header("Access-Control-Allow-Credentials", "true")
var Picture []string
json.Unmarshal([]byte(c.PostForm("Images")), &Picture)
var TopPhoto []string
json.Unmarshal([]byte(c.PostForm("TopPhoto")), &TopPhoto)
var Location DB.SLocation
json.Unmarshal([]byte(c.PostForm("Location")), &Location)
var id bson.ObjectId
if pid := c.PostForm("id"); pid == "null" {
id = bson.NewObjectId()
} else {
id = bson.ObjectIdHex(pid)
}
DB.CCommodity.UpsertId(
id,
bson.M{"$set": bson.M{
"Name": c.PostForm("Name"),
"Price": c.PostForm("Price"),
"ShopName": c.PostForm("ShopName"),
"KvPhoto": c.PostForm("KvPhoto"),
"TopPhoto": TopPhoto,
"Location": Location,
"Images": Picture,
}},
)
c.JSON(200, tools.ResponseSeccess{
0,
"ok",
})
}