Commit c1e5407407cdb8467f1e261b67e8755c9ac57388
1 parent
2f326a5c
Exists in
v1.2
and in
2 other branches
替换mongodb驱动
Showing
20 changed files
with
654 additions
and
276 deletions
Show diff stats
.gitignore
API/Complaint.go
@@ -4,11 +4,13 @@ import ( | @@ -4,11 +4,13 @@ import ( | ||
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2/bson" | 7 | + "go.mongodb.org/mongo-driver/bson" |
8 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
8 | "letu/DB" | 9 | "letu/DB" |
9 | "math" | 10 | "math" |
10 | "regexp" | 11 | "regexp" |
11 | "strconv" | 12 | "strconv" |
13 | + "time" | ||
12 | ) | 14 | ) |
13 | 15 | ||
14 | // @Title 增加投诉 | 16 | // @Title 增加投诉 |
@@ -27,10 +29,8 @@ import ( | @@ -27,10 +29,8 @@ import ( | ||
27 | // @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" | 29 | // @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" |
28 | // @Router /CreateComplaint? [post] | 30 | // @Router /CreateComplaint? [post] |
29 | func CreateComplaint(c *gin.Context) { | 31 | func CreateComplaint(c *gin.Context) { |
30 | - c.Header("Access-Control-Allow-Origin",c.Request.Header.Get("Origin")) | ||
31 | - c.Header("Access-Control-Allow-Credentials","true") | ||
32 | - | ||
33 | - | 32 | + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
33 | + c.Header("Access-Control-Allow-Credentials", "true") | ||
34 | 34 | ||
35 | reg := regexp.MustCompile(Regular) | 35 | reg := regexp.MustCompile(Regular) |
36 | if !reg.MatchString(c.PostForm("Mobile")) { | 36 | if !reg.MatchString(c.PostForm("Mobile")) { |
@@ -42,7 +42,7 @@ func CreateComplaint(c *gin.Context) { | @@ -42,7 +42,7 @@ func CreateComplaint(c *gin.Context) { | ||
42 | return | 42 | return |
43 | } | 43 | } |
44 | 44 | ||
45 | - if c.PostForm("Mobile") == ""{ | 45 | + if c.PostForm("Mobile") == "" { |
46 | c.JSON(200, tools.ResponseError{ | 46 | c.JSON(200, tools.ResponseError{ |
47 | 1, | 47 | 1, |
48 | "手机号为空", | 48 | "手机号为空", |
@@ -50,9 +50,8 @@ func CreateComplaint(c *gin.Context) { | @@ -50,9 +50,8 @@ func CreateComplaint(c *gin.Context) { | ||
50 | return | 50 | return |
51 | } | 51 | } |
52 | 52 | ||
53 | - | ||
54 | // 检查验证码 | 53 | // 检查验证码 |
55 | - cacheCode := DB.Redis.Get("code_"+c.PostForm("Mobile")) | 54 | + cacheCode := DB.Redis.Get("code_" + c.PostForm("Mobile")) |
56 | if cacheCode != c.PostForm("Code") { | 55 | if cacheCode != c.PostForm("Code") { |
57 | 56 | ||
58 | c.JSON(200, tools.ResponseError{ | 57 | c.JSON(200, tools.ResponseError{ |
@@ -67,7 +66,7 @@ func CreateComplaint(c *gin.Context) { | @@ -67,7 +66,7 @@ func CreateComplaint(c *gin.Context) { | ||
67 | 66 | ||
68 | json.Unmarshal([]byte(c.PostForm("Image")), &images) | 67 | json.Unmarshal([]byte(c.PostForm("Image")), &images) |
69 | 68 | ||
70 | - DB.CComplaint.Insert(DB.SComplaint{ | 69 | + DB.CComplaint.InsertOne(tools.GetContext(), DB.SComplaint{ |
71 | c.PostForm("Type"), | 70 | c.PostForm("Type"), |
72 | c.PostForm("ScenicId"), | 71 | c.PostForm("ScenicId"), |
73 | c.PostForm("Mobile"), | 72 | c.PostForm("Mobile"), |
@@ -75,6 +74,8 @@ func CreateComplaint(c *gin.Context) { | @@ -75,6 +74,8 @@ func CreateComplaint(c *gin.Context) { | ||
75 | c.PostForm("Sex"), | 74 | c.PostForm("Sex"), |
76 | c.PostForm("Content"), | 75 | c.PostForm("Content"), |
77 | images, | 76 | images, |
77 | + "", | ||
78 | + time.Now().Unix(), | ||
78 | }) | 79 | }) |
79 | 80 | ||
80 | c.JSON(200, tools.ResponseSeccess{ | 81 | c.JSON(200, tools.ResponseSeccess{ |
@@ -82,13 +83,8 @@ func CreateComplaint(c *gin.Context) { | @@ -82,13 +83,8 @@ func CreateComplaint(c *gin.Context) { | ||
82 | "ok", | 83 | "ok", |
83 | }) | 84 | }) |
84 | 85 | ||
85 | - | ||
86 | } | 86 | } |
87 | 87 | ||
88 | - | ||
89 | - | ||
90 | - | ||
91 | - | ||
92 | // @Title 查询所有投诉 | 88 | // @Title 查询所有投诉 |
93 | // @Description 投诉 - 查询所有投诉 | 89 | // @Description 投诉 - 查询所有投诉 |
94 | // @Accept json | 90 | // @Accept json |
@@ -101,19 +97,28 @@ func AllComplaint(c *gin.Context) { | @@ -101,19 +97,28 @@ func AllComplaint(c *gin.Context) { | ||
101 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 97 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
102 | c.Header("Access-Control-Allow-Credentials", "true") | 98 | c.Header("Access-Control-Allow-Credentials", "true") |
103 | 99 | ||
104 | - total,_ := DB.CComplaint.Find(bson.M{}).Count() | ||
105 | - limit,_ := strconv.Atoi(c.Query("Limit")) | 100 | + total, _ := DB.CComplaint.CountDocuments(tools.GetContext(), bson.M{}) |
101 | + limit, _ := strconv.ParseInt(c.Query("Limit"),10,64) | ||
106 | if limit == 0 { | 102 | if limit == 0 { |
107 | limit = 50 | 103 | limit = 50 |
108 | } | 104 | } |
109 | - currPage, _ := strconv.Atoi(c.Query("Page")) | 105 | + currPage, _ := strconv.ParseInt(c.Query("Page"),10,64) |
110 | if currPage == 0 { | 106 | if currPage == 0 { |
111 | currPage = 1 | 107 | currPage = 1 |
112 | } | 108 | } |
113 | skip := (currPage - 1) * limit | 109 | skip := (currPage - 1) * limit |
114 | 110 | ||
115 | - var aComplaint = []DB.SComplaint{} | ||
116 | - DB.CComplaint.Find(bson.M{}).Limit(limit).Skip(int(skip)).Sort("-_id").All(&aComplaint) | 111 | + var aComplaint = []bson.M{} |
112 | + | ||
113 | + cur, err := DB.CComplaint.Find(tools.GetContext(), bson.M{}, &options.FindOptions{Limit: &limit, Skip: &skip, Sort: bson.M{"_id": -1}}) | ||
114 | + defer cur.Close(tools.GetContext()) | ||
115 | + if err == nil { | ||
116 | + for cur.Next(tools.GetContext()) { | ||
117 | + var e bson.M | ||
118 | + cur.Decode(&e) | ||
119 | + aComplaint = append(aComplaint,e) | ||
120 | + } | ||
121 | + } | ||
117 | 122 | ||
118 | c.JSON(200, tools.Page{ | 123 | c.JSON(200, tools.Page{ |
119 | 0, | 124 | 0, |
@@ -124,4 +129,4 @@ func AllComplaint(c *gin.Context) { | @@ -124,4 +129,4 @@ func AllComplaint(c *gin.Context) { | ||
124 | aComplaint, | 129 | aComplaint, |
125 | }) | 130 | }) |
126 | 131 | ||
127 | -} | ||
128 | \ No newline at end of file | 132 | \ No newline at end of file |
133 | +} |
API/DealyMessage.go
@@ -3,7 +3,8 @@ package Api | @@ -3,7 +3,8 @@ package Api | ||
3 | import ( | 3 | import ( |
4 | "github.com/aarongao/tools" | 4 | "github.com/aarongao/tools" |
5 | "github.com/gin-gonic/gin" | 5 | "github.com/gin-gonic/gin" |
6 | - "gopkg.in/mgo.v2/bson" | 6 | + "go.mongodb.org/mongo-driver/bson" |
7 | + "go.mongodb.org/mongo-driver/bson/primitive" | ||
7 | "letu/Lib/DelayMessage" | 8 | "letu/Lib/DelayMessage" |
8 | "letu/Lib/Token" | 9 | "letu/Lib/Token" |
9 | ) | 10 | ) |
@@ -21,7 +22,8 @@ func DealyMessageInfo(c *gin.Context) { | @@ -21,7 +22,8 @@ func DealyMessageInfo(c *gin.Context) { | ||
21 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 22 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
22 | c.Header("Access-Control-Allow-Credentials", "true") | 23 | c.Header("Access-Control-Allow-Credentials", "true") |
23 | 24 | ||
24 | - if c.Query("Token") == "" || bson.IsObjectIdHex(c.Query("UserId")) == false { | 25 | + _, err := primitive.ObjectIDFromHex(c.Query("UserId")) |
26 | + if c.Query("Token") == "" || err != nil { | ||
25 | c.JSON(200, tools.ResponseError{ | 27 | c.JSON(200, tools.ResponseError{ |
26 | 1, | 28 | 1, |
27 | "Token或者用户id不正确", | 29 | "Token或者用户id不正确", |
@@ -38,7 +40,15 @@ func DealyMessageInfo(c *gin.Context) { | @@ -38,7 +40,15 @@ func DealyMessageInfo(c *gin.Context) { | ||
38 | } | 40 | } |
39 | 41 | ||
40 | var aDelayMessage []DelayMessage.Message | 42 | var aDelayMessage []DelayMessage.Message |
41 | - DelayMessage.CDelayMessage.Find(bson.M{"UserId": c.Query("UserId")}).All(&aDelayMessage) | 43 | + cur, err := DelayMessage.CDelayMessage.Find(tools.GetContext(), bson.M{"UserId": c.Query("UserId")}) |
44 | + defer cur.Close(tools.GetContext()) | ||
45 | + if err == nil { | ||
46 | + for cur.Next(tools.GetContext()) { | ||
47 | + var e DelayMessage.Message | ||
48 | + cur.Decode(&e) | ||
49 | + aDelayMessage = append(aDelayMessage,e) | ||
50 | + } | ||
51 | + } | ||
42 | 52 | ||
43 | if aDelayMessage == nil { | 53 | if aDelayMessage == nil { |
44 | aDelayMessage = []DelayMessage.Message{} | 54 | aDelayMessage = []DelayMessage.Message{} |
@@ -66,7 +76,8 @@ func CreateDealyMessage(c *gin.Context) { | @@ -66,7 +76,8 @@ func CreateDealyMessage(c *gin.Context) { | ||
66 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 76 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
67 | c.Header("Access-Control-Allow-Credentials", "true") | 77 | c.Header("Access-Control-Allow-Credentials", "true") |
68 | 78 | ||
69 | - if c.PostForm("Token") == "" || bson.IsObjectIdHex(c.PostForm("UserId")) == false { | 79 | + _,err := primitive.ObjectIDFromHex(c.PostForm("UserId")) |
80 | + if c.PostForm("Token") == "" || err != nil { | ||
70 | c.JSON(200, tools.ResponseError{ | 81 | c.JSON(200, tools.ResponseError{ |
71 | 1, | 82 | 1, |
72 | "Token或者用户id不正确", | 83 | "Token或者用户id不正确", |
@@ -82,7 +93,7 @@ func CreateDealyMessage(c *gin.Context) { | @@ -82,7 +93,7 @@ func CreateDealyMessage(c *gin.Context) { | ||
82 | return | 93 | return |
83 | } | 94 | } |
84 | 95 | ||
85 | - err := DelayMessage.GlobalDM.AddTaskForAppMessage(c.PostForm("DelayTime"), c.PostForm("UDID"), c.PostForm("Title"), c.PostForm("Content"), c.PostForm("UserId")) | 96 | + err = DelayMessage.GlobalDM.AddTaskForAppMessage(c.PostForm("DelayTime"), c.PostForm("UDID"), c.PostForm("Title"), c.PostForm("Content"), c.PostForm("UserId")) |
86 | 97 | ||
87 | if err == nil { | 98 | if err == nil { |
88 | 99 | ||
@@ -114,7 +125,9 @@ func RemoveDealyMessage(c *gin.Context) { | @@ -114,7 +125,9 @@ func RemoveDealyMessage(c *gin.Context) { | ||
114 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 125 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
115 | c.Header("Access-Control-Allow-Credentials", "true") | 126 | c.Header("Access-Control-Allow-Credentials", "true") |
116 | 127 | ||
117 | - if c.PostForm("Token") == "" || bson.IsObjectIdHex(c.PostForm("UserId")) == false { | 128 | + |
129 | + _,err := primitive.ObjectIDFromHex(c.PostForm("UserId")) | ||
130 | + if c.PostForm("Token") == "" || err != nil { | ||
118 | c.JSON(200, tools.ResponseError{ | 131 | c.JSON(200, tools.ResponseError{ |
119 | 1, | 132 | 1, |
120 | "Token或者用户id不正确", | 133 | "Token或者用户id不正确", |
API/Icon.go
@@ -3,7 +3,9 @@ package Api | @@ -3,7 +3,9 @@ package Api | ||
3 | import ( | 3 | import ( |
4 | "github.com/aarongao/tools" | 4 | "github.com/aarongao/tools" |
5 | "github.com/gin-gonic/gin" | 5 | "github.com/gin-gonic/gin" |
6 | - "gopkg.in/mgo.v2/bson" | 6 | + "go.mongodb.org/mongo-driver/bson" |
7 | + "go.mongodb.org/mongo-driver/bson/primitive" | ||
8 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
7 | "letu/DB" | 9 | "letu/DB" |
8 | ) | 10 | ) |
9 | 11 | ||
@@ -28,7 +30,8 @@ func IconInfo(c *gin.Context) { | @@ -28,7 +30,8 @@ func IconInfo(c *gin.Context) { | ||
28 | } | 30 | } |
29 | 31 | ||
30 | var SIcon *DB.SIcons | 32 | var SIcon *DB.SIcons |
31 | - DB.CIcons.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SIcon) | 33 | + objID, _ := primitive.ObjectIDFromHex(c.Query("id")) |
34 | + DB.CIcons.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&SIcon) | ||
32 | 35 | ||
33 | c.JSON(200, tools.ResponseSeccess{ | 36 | c.JSON(200, tools.ResponseSeccess{ |
34 | 0, | 37 | 0, |
@@ -58,20 +61,23 @@ func UpdateIcon(c *gin.Context) { | @@ -58,20 +61,23 @@ func UpdateIcon(c *gin.Context) { | ||
58 | return | 61 | return |
59 | } | 62 | } |
60 | 63 | ||
61 | - var id bson.ObjectId | 64 | + var id primitive.ObjectID |
62 | if pid := c.PostForm("id"); pid == "null" { | 65 | if pid := c.PostForm("id"); pid == "null" { |
63 | - id = bson.NewObjectId() | 66 | + id = primitive.NewObjectID() |
64 | } else { | 67 | } else { |
65 | - id = bson.ObjectIdHex(pid) | 68 | + id, _ = primitive.ObjectIDFromHex(pid) |
66 | } | 69 | } |
67 | 70 | ||
68 | - DB.CIcons.UpsertId( | ||
69 | - id, | 71 | + upsert := true |
72 | + DB.CIcons.FindOneAndUpdate(tools.GetContext(), | ||
73 | + bson.M{"_id": id}, | ||
70 | bson.M{"$set": bson.M{ | 74 | bson.M{"$set": bson.M{ |
71 | "Name": c.PostForm("Name"), | 75 | "Name": c.PostForm("Name"), |
72 | "Picture": c.PostForm("Picture"), | 76 | "Picture": c.PostForm("Picture"), |
73 | "ScenicId": ScenicId, | 77 | "ScenicId": ScenicId, |
74 | - }}, | 78 | + }}, &options.FindOneAndUpdateOptions{ |
79 | + Upsert: &upsert, | ||
80 | + }, | ||
75 | ) | 81 | ) |
76 | 82 | ||
77 | c.JSON(200, tools.ResponseSeccess{ | 83 | c.JSON(200, tools.ResponseSeccess{ |
@@ -102,8 +108,17 @@ func AllIcons(c *gin.Context) { | @@ -102,8 +108,17 @@ func AllIcons(c *gin.Context) { | ||
102 | return | 108 | return |
103 | } | 109 | } |
104 | 110 | ||
105 | - var SIcons = []*DB.SIcons{} | ||
106 | - DB.CIcons.Find(bson.M{"ScenicId": ScenicId}).All(&SIcons) | 111 | + var SIcons = []DB.SIcons{} |
112 | + cur, err := DB.CIcons.Find(tools.GetContext(), bson.M{"ScenicId": ScenicId}) | ||
113 | + defer cur.Close(tools.GetContext()) | ||
114 | + if err == nil { | ||
115 | + for cur.Next(tools.GetContext()) { | ||
116 | + var e DB.SIcons | ||
117 | + cur.Decode(&e) | ||
118 | + SIcons = append(SIcons,e) | ||
119 | + } | ||
120 | + } | ||
121 | + | ||
107 | 122 | ||
108 | c.JSON(200, tools.ResponseSeccess{ | 123 | c.JSON(200, tools.ResponseSeccess{ |
109 | 0, | 124 | 0, |
API/Investigation.go
@@ -4,7 +4,8 @@ import ( | @@ -4,7 +4,8 @@ import ( | ||
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2/bson" | 7 | + "go.mongodb.org/mongo-driver/bson" |
8 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
8 | "letu/DB" | 9 | "letu/DB" |
9 | "math" | 10 | "math" |
10 | "strconv" | 11 | "strconv" |
@@ -27,7 +28,7 @@ func SaveInvestigation(c *gin.Context) { | @@ -27,7 +28,7 @@ func SaveInvestigation(c *gin.Context) { | ||
27 | var Data map[string]interface{} | 28 | var Data map[string]interface{} |
28 | json.Unmarshal([]byte(c.PostForm("Data")), &Data) | 29 | json.Unmarshal([]byte(c.PostForm("Data")), &Data) |
29 | 30 | ||
30 | - DB.CInvestigation.Insert(DB.SInvestigation{ | 31 | + DB.CInvestigation.InsertOne(tools.GetContext(),DB.SInvestigation{ |
31 | c.PostForm("UserId"), | 32 | c.PostForm("UserId"), |
32 | c.PostForm("Mobile"), | 33 | c.PostForm("Mobile"), |
33 | Data, | 34 | Data, |
@@ -53,20 +54,28 @@ func AllInvestigation(c *gin.Context) { | @@ -53,20 +54,28 @@ func AllInvestigation(c *gin.Context) { | ||
53 | c.Header("Access-Control-Allow-Credentials", "true") | 54 | c.Header("Access-Control-Allow-Credentials", "true") |
54 | 55 | ||
55 | 56 | ||
56 | - total,_ := DB.CComplaint.Find(bson.M{}).Count() | ||
57 | - limit,_ := strconv.Atoi(c.Query("Limit")) | 57 | + total,_ := DB.CComplaint.CountDocuments(tools.GetContext(), bson.M{}) |
58 | + limit, _ := strconv.ParseInt(c.Query("Limit"),10,64) | ||
58 | if limit == 0 { | 59 | if limit == 0 { |
59 | limit = 50 | 60 | limit = 50 |
60 | } | 61 | } |
61 | - currPage, _ := strconv.Atoi(c.Query("Page")) | 62 | + currPage, _ := strconv.ParseInt(c.Query("Page"),10,64) |
62 | if currPage == 0 { | 63 | if currPage == 0 { |
63 | currPage = 1 | 64 | currPage = 1 |
64 | } | 65 | } |
65 | skip := (currPage - 1) * limit | 66 | skip := (currPage - 1) * limit |
66 | 67 | ||
67 | var aInvestigation []DB.SInvestigation | 68 | var aInvestigation []DB.SInvestigation |
68 | - DB.CInvestigation.Find(bson.M{}).Limit(limit).Skip(int(skip)).Sort("-_id").All(&aInvestigation) | ||
69 | 69 | ||
70 | + cur, err := DB.CInvestigation.Find(tools.GetContext(), bson.M{}, &options.FindOptions{Limit: &limit, Skip: &skip, Sort: bson.M{"_id": -1}}) | ||
71 | + defer cur.Close(tools.GetContext()) | ||
72 | + if err == nil { | ||
73 | + for cur.Next(tools.GetContext()) { | ||
74 | + var e DB.SInvestigation | ||
75 | + cur.Decode(&e) | ||
76 | + aInvestigation = append(aInvestigation,e) | ||
77 | + } | ||
78 | + } | ||
70 | c.JSON(200, tools.Page{ | 79 | c.JSON(200, tools.Page{ |
71 | 0, | 80 | 0, |
72 | total, | 81 | total, |
API/Item.go
@@ -4,7 +4,9 @@ import ( | @@ -4,7 +4,9 @@ import ( | ||
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2/bson" | 7 | + "go.mongodb.org/mongo-driver/bson" |
8 | + "go.mongodb.org/mongo-driver/bson/primitive" | ||
9 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
8 | "letu/DB" | 10 | "letu/DB" |
9 | "strconv" | 11 | "strconv" |
10 | "time" | 12 | "time" |
@@ -31,7 +33,8 @@ func ItemInfo(c *gin.Context) { | @@ -31,7 +33,8 @@ func ItemInfo(c *gin.Context) { | ||
31 | } | 33 | } |
32 | 34 | ||
33 | var SItem DB.SItem | 35 | var SItem DB.SItem |
34 | - DB.CItem.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SItem) | 36 | + objID,_ := primitive.ObjectIDFromHex(c.Query("id")) |
37 | + DB.CItem.FindOne(tools.GetContext(),bson.M{"_id": objID}).Decode(&SItem) | ||
35 | 38 | ||
36 | c.JSON(200, tools.ResponseSeccess{ | 39 | c.JSON(200, tools.ResponseSeccess{ |
37 | 0, | 40 | 0, |
@@ -52,7 +55,15 @@ func AllItems(c *gin.Context) { | @@ -52,7 +55,15 @@ func AllItems(c *gin.Context) { | ||
52 | c.Header("Access-Control-Allow-Credentials", "true") | 55 | c.Header("Access-Control-Allow-Credentials", "true") |
53 | 56 | ||
54 | var aItems = []DB.SItem{} | 57 | var aItems = []DB.SItem{} |
55 | - DB.CItem.Find(bson.M{}).All(&aItems) | 58 | + cur, err := DB.CItem.Find(tools.GetContext(), bson.M{}) |
59 | + defer cur.Close(tools.GetContext()) | ||
60 | + if err == nil { | ||
61 | + for cur.Next(tools.GetContext()) { | ||
62 | + var e DB.SItem | ||
63 | + cur.Decode(&e) | ||
64 | + aItems = append(aItems,e) | ||
65 | + } | ||
66 | + } | ||
56 | 67 | ||
57 | c.JSON(200, aItems) | 68 | c.JSON(200, aItems) |
58 | 69 | ||
@@ -78,16 +89,18 @@ func UpdateItem(c *gin.Context) { | @@ -78,16 +89,18 @@ func UpdateItem(c *gin.Context) { | ||
78 | var Picture []string | 89 | var Picture []string |
79 | json.Unmarshal([]byte(c.PostForm("Picture")), &Picture) | 90 | json.Unmarshal([]byte(c.PostForm("Picture")), &Picture) |
80 | 91 | ||
81 | - var id bson.ObjectId | 92 | + var id primitive.ObjectID |
82 | if pid := c.PostForm("id"); pid == "null" { | 93 | if pid := c.PostForm("id"); pid == "null" { |
83 | - id = bson.NewObjectId() | 94 | + id = primitive.NewObjectID() |
84 | } else { | 95 | } else { |
85 | - id = bson.ObjectIdHex(pid) | 96 | + id,_ = primitive.ObjectIDFromHex(pid) |
86 | } | 97 | } |
87 | 98 | ||
88 | poststate, _ := strconv.Atoi(c.PostForm("State")) | 99 | poststate, _ := strconv.Atoi(c.PostForm("State")) |
89 | - DB.CItem.UpsertId( | ||
90 | - id, | 100 | + |
101 | + upsert := true | ||
102 | + DB.CItem.FindOneAndUpdate(tools.GetContext(), | ||
103 | + bson.M{"_id": id}, | ||
91 | bson.M{"$set": bson.M{ | 104 | bson.M{"$set": bson.M{ |
92 | "Name": c.PostForm("Name"), | 105 | "Name": c.PostForm("Name"), |
93 | "SubName": c.PostForm("SubName"), | 106 | "SubName": c.PostForm("SubName"), |
@@ -106,7 +119,9 @@ func UpdateItem(c *gin.Context) { | @@ -106,7 +119,9 @@ func UpdateItem(c *gin.Context) { | ||
106 | "LocationDescription": c.PostForm("LocationDescription"), | 119 | "LocationDescription": c.PostForm("LocationDescription"), |
107 | "Reminder": c.PostForm("Reminder"), | 120 | "Reminder": c.PostForm("Reminder"), |
108 | "State": poststate, | 121 | "State": poststate, |
109 | - }}, | 122 | + }}, &options.FindOneAndUpdateOptions{ |
123 | + Upsert: &upsert, | ||
124 | + }, | ||
110 | ) | 125 | ) |
111 | 126 | ||
112 | 127 |
API/Line.go
@@ -4,7 +4,9 @@ import ( | @@ -4,7 +4,9 @@ import ( | ||
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2/bson" | 7 | + "go.mongodb.org/mongo-driver/bson" |
8 | + "go.mongodb.org/mongo-driver/bson/primitive" | ||
9 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
8 | "letu/DB" | 10 | "letu/DB" |
9 | ) | 11 | ) |
10 | 12 | ||
@@ -29,7 +31,8 @@ func LineInfo(c *gin.Context) { | @@ -29,7 +31,8 @@ func LineInfo(c *gin.Context) { | ||
29 | } | 31 | } |
30 | 32 | ||
31 | var SLine DB.SLine | 33 | var SLine DB.SLine |
32 | - DB.CLine.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SLine) | 34 | + objID, _ := primitive.ObjectIDFromHex(c.Query("id")) |
35 | + DB.CLine.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&SLine) | ||
33 | 36 | ||
34 | c.JSON(200, tools.ResponseSeccess{ | 37 | c.JSON(200, tools.ResponseSeccess{ |
35 | 0, | 38 | 0, |
@@ -50,7 +53,15 @@ func AllLine(c *gin.Context) { | @@ -50,7 +53,15 @@ func AllLine(c *gin.Context) { | ||
50 | c.Header("Access-Control-Allow-Credentials", "true") | 53 | c.Header("Access-Control-Allow-Credentials", "true") |
51 | 54 | ||
52 | var aLine []DB.SLine | 55 | var aLine []DB.SLine |
53 | - DB.CLine.Find(bson.M{}).All(&aLine) | 56 | + cur, err := DB.CLine.Find(tools.GetContext(), bson.M{}) |
57 | + defer cur.Close(tools.GetContext()) | ||
58 | + if err == nil { | ||
59 | + for cur.Next(tools.GetContext()) { | ||
60 | + var e DB.SLine | ||
61 | + cur.Decode(&e) | ||
62 | + aLine = append(aLine,e) | ||
63 | + } | ||
64 | + } | ||
54 | 65 | ||
55 | c.JSON(200, aLine) | 66 | c.JSON(200, aLine) |
56 | 67 | ||
@@ -73,15 +84,16 @@ func UpdateLine(c *gin.Context) { | @@ -73,15 +84,16 @@ func UpdateLine(c *gin.Context) { | ||
73 | var Annotations []string | 84 | var Annotations []string |
74 | json.Unmarshal([]byte(c.PostForm("Annotations")), &Annotations) | 85 | json.Unmarshal([]byte(c.PostForm("Annotations")), &Annotations) |
75 | 86 | ||
76 | - var id bson.ObjectId | 87 | + var id primitive.ObjectID |
77 | if pid := c.PostForm("id"); pid == "null" { | 88 | if pid := c.PostForm("id"); pid == "null" { |
78 | - id = bson.NewObjectId() | 89 | + id = primitive.NewObjectID() |
79 | } else { | 90 | } else { |
80 | - id = bson.ObjectIdHex(pid) | 91 | + id,_ = primitive.ObjectIDFromHex(pid) |
81 | } | 92 | } |
82 | 93 | ||
83 | - DB.CLine.UpsertId( | ||
84 | - id, | 94 | + upsert := true |
95 | + DB.CLine.FindOneAndUpdate(tools.GetContext(), | ||
96 | + bson.M{"_id": id}, | ||
85 | bson.M{"$set": bson.M{ | 97 | bson.M{"$set": bson.M{ |
86 | "Name": c.PostForm("Name"), | 98 | "Name": c.PostForm("Name"), |
87 | "SubName": c.PostForm("SubName"), | 99 | "SubName": c.PostForm("SubName"), |
@@ -91,7 +103,9 @@ func UpdateLine(c *gin.Context) { | @@ -91,7 +103,9 @@ func UpdateLine(c *gin.Context) { | ||
91 | "Distance": c.PostForm("Distance"), | 103 | "Distance": c.PostForm("Distance"), |
92 | "Annotations": Annotations, | 104 | "Annotations": Annotations, |
93 | "Location": Location, | 105 | "Location": Location, |
94 | - }}, | 106 | + }}, &options.FindOneAndUpdateOptions{ |
107 | + Upsert: &upsert, | ||
108 | + }, | ||
95 | ) | 109 | ) |
96 | 110 | ||
97 | c.JSON(200, tools.ResponseSeccess{ | 111 | c.JSON(200, tools.ResponseSeccess{ |
API/Scenic.go
@@ -4,7 +4,9 @@ import ( | @@ -4,7 +4,9 @@ import ( | ||
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2/bson" | 7 | + "go.mongodb.org/mongo-driver/bson" |
8 | + "go.mongodb.org/mongo-driver/bson/primitive" | ||
9 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
8 | "letu/DB" | 10 | "letu/DB" |
9 | ) | 11 | ) |
10 | 12 | ||
@@ -29,7 +31,8 @@ func ScenicInfo(c *gin.Context) { | @@ -29,7 +31,8 @@ func ScenicInfo(c *gin.Context) { | ||
29 | } | 31 | } |
30 | 32 | ||
31 | var Scenic *DB.SScenic | 33 | var Scenic *DB.SScenic |
32 | - DB.CScenic.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&Scenic) | 34 | + objID,_ := primitive.ObjectIDFromHex(c.Query("id")) |
35 | + DB.CScenic.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&Scenic) | ||
33 | 36 | ||
34 | c.JSON(200, tools.ResponseSeccess{ | 37 | c.JSON(200, tools.ResponseSeccess{ |
35 | 0, | 38 | 0, |
@@ -71,18 +74,19 @@ func UpdateScenic(c *gin.Context) { | @@ -71,18 +74,19 @@ func UpdateScenic(c *gin.Context) { | ||
71 | var VideoList []DB.SVideo | 74 | var VideoList []DB.SVideo |
72 | json.Unmarshal([]byte(c.PostForm("VideoList")), &VideoList) | 75 | json.Unmarshal([]byte(c.PostForm("VideoList")), &VideoList) |
73 | 76 | ||
74 | - var id bson.ObjectId | 77 | + var id primitive.ObjectID |
75 | if pid := c.PostForm("id"); pid == "null" { | 78 | if pid := c.PostForm("id"); pid == "null" { |
76 | - id = bson.NewObjectId() | ||
77 | - | 79 | + id = primitive.NewObjectID() |
78 | // 新景区,初始化 | 80 | // 新景区,初始化 |
79 | initScenic(id.Hex()) | 81 | initScenic(id.Hex()) |
80 | } else { | 82 | } else { |
81 | - id = bson.ObjectIdHex(pid) | 83 | + id,_ = primitive.ObjectIDFromHex(pid) |
82 | } | 84 | } |
83 | 85 | ||
84 | - DB.CScenic.UpsertId( | ||
85 | - id, | 86 | + |
87 | + upsert := true | ||
88 | + DB.CScenic.FindOneAndUpdate(tools.GetContext(), | ||
89 | + bson.M{"_id": id}, | ||
86 | bson.M{"$set": bson.M{ | 90 | bson.M{"$set": bson.M{ |
87 | "Name": c.PostForm("Name"), | 91 | "Name": c.PostForm("Name"), |
88 | "Describe": c.PostForm("Describe"), | 92 | "Describe": c.PostForm("Describe"), |
@@ -97,7 +101,9 @@ func UpdateScenic(c *gin.Context) { | @@ -97,7 +101,9 @@ func UpdateScenic(c *gin.Context) { | ||
97 | "ItemScenicPicture": ItemScenicPicture, | 101 | "ItemScenicPicture": ItemScenicPicture, |
98 | "ActivityPicture": ActivityPicture, | 102 | "ActivityPicture": ActivityPicture, |
99 | "VideoList": VideoList, | 103 | "VideoList": VideoList, |
100 | - }}, | 104 | + }}, &options.FindOneAndUpdateOptions{ |
105 | + Upsert: &upsert, | ||
106 | + }, | ||
101 | ) | 107 | ) |
102 | 108 | ||
103 | c.JSON(200, tools.ResponseSeccess{ | 109 | c.JSON(200, tools.ResponseSeccess{ |
@@ -109,23 +115,28 @@ func UpdateScenic(c *gin.Context) { | @@ -109,23 +115,28 @@ func UpdateScenic(c *gin.Context) { | ||
109 | 115 | ||
110 | func initScenic(id string) { | 116 | func initScenic(id string) { |
111 | 117 | ||
112 | - DB.CTags.Insert(DB.STag{ | 118 | + var dba []interface{} |
119 | + dba = append(dba,DB.STag{ | ||
113 | id, | 120 | id, |
114 | "type", | 121 | "type", |
115 | "服务设施", | 122 | "服务设施", |
116 | - }, DB.STag{ | 123 | + }) |
124 | + dba = append(dba,DB.STag{ | ||
117 | id, | 125 | id, |
118 | "type", | 126 | "type", |
119 | "游乐设施", | 127 | "游乐设施", |
120 | - }, DB.STag{ | 128 | + }) |
129 | + dba = append(dba,DB.STag{ | ||
121 | id, | 130 | id, |
122 | "type", | 131 | "type", |
123 | "餐饮", | 132 | "餐饮", |
124 | - }, DB.STag{ | 133 | + }) |
134 | + dba = append(dba,DB.STag{ | ||
125 | id, | 135 | id, |
126 | "type", | 136 | "type", |
127 | "购物", | 137 | "购物", |
128 | }) | 138 | }) |
139 | + DB.CTags.InsertMany(tools.GetContext(),dba[1:]) | ||
129 | } | 140 | } |
130 | 141 | ||
131 | // @Title 所有景区基础信息 | 142 | // @Title 所有景区基础信息 |
@@ -139,11 +150,19 @@ func AllScenic(c *gin.Context) { | @@ -139,11 +150,19 @@ func AllScenic(c *gin.Context) { | ||
139 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 150 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
140 | c.Header("Access-Control-Allow-Credentials", "true") | 151 | c.Header("Access-Control-Allow-Credentials", "true") |
141 | 152 | ||
142 | - var Scenic []*DB.SScenic | ||
143 | - DB.CScenic.Find(bson.M{}).All(&Scenic) | 153 | + var Scenic []DB.SScenic |
154 | + cur, err := DB.CScenic.Find(tools.GetContext(), bson.M{}) | ||
155 | + defer cur.Close(tools.GetContext()) | ||
156 | + if err == nil { | ||
157 | + for cur.Next(tools.GetContext()) { | ||
158 | + var e DB.SScenic | ||
159 | + cur.Decode(&e) | ||
160 | + Scenic = append(Scenic,e) | ||
161 | + } | ||
162 | + } | ||
144 | 163 | ||
145 | if Scenic == nil { | 164 | if Scenic == nil { |
146 | - Scenic = []*DB.SScenic{} | 165 | + Scenic = []DB.SScenic{} |
147 | } | 166 | } |
148 | 167 | ||
149 | c.JSON(200, tools.ResponseSeccess{ | 168 | c.JSON(200, tools.ResponseSeccess{ |
API/Shop.go
@@ -4,7 +4,9 @@ import ( | @@ -4,7 +4,9 @@ import ( | ||
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2/bson" | 7 | + "go.mongodb.org/mongo-driver/bson" |
8 | + "go.mongodb.org/mongo-driver/bson/primitive" | ||
9 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
8 | "letu/DB" | 10 | "letu/DB" |
9 | ) | 11 | ) |
10 | 12 | ||
@@ -29,7 +31,8 @@ func CommodityInfo(c *gin.Context) { | @@ -29,7 +31,8 @@ func CommodityInfo(c *gin.Context) { | ||
29 | } | 31 | } |
30 | 32 | ||
31 | var SCommodity DB.SCommodity | 33 | var SCommodity DB.SCommodity |
32 | - DB.CCommodity.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SCommodity) | 34 | + objID, _ := primitive.ObjectIDFromHex(c.Query("id")) |
35 | + DB.CCommodity.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&SCommodity) | ||
33 | 36 | ||
34 | c.JSON(200, tools.ResponseSeccess{ | 37 | c.JSON(200, tools.ResponseSeccess{ |
35 | 0, | 38 | 0, |
@@ -50,7 +53,15 @@ func AllCommodity(c *gin.Context) { | @@ -50,7 +53,15 @@ func AllCommodity(c *gin.Context) { | ||
50 | c.Header("Access-Control-Allow-Credentials", "true") | 53 | c.Header("Access-Control-Allow-Credentials", "true") |
51 | 54 | ||
52 | var aCommoditys []DB.SCommodity | 55 | var aCommoditys []DB.SCommodity |
53 | - DB.CCommodity.Find(bson.M{}).All(&aCommoditys) | 56 | + cur, err := DB.CCommodity.Find(tools.GetContext(), bson.M{}) |
57 | + defer cur.Close(tools.GetContext()) | ||
58 | + if err == nil { | ||
59 | + for cur.Next(tools.GetContext()) { | ||
60 | + var e DB.SCommodity | ||
61 | + cur.Decode(&e) | ||
62 | + aCommoditys = append(aCommoditys,e) | ||
63 | + } | ||
64 | + } | ||
54 | 65 | ||
55 | c.JSON(200, aCommoditys) | 66 | c.JSON(200, aCommoditys) |
56 | 67 | ||
@@ -76,15 +87,17 @@ func UpdateCommodity(c *gin.Context) { | @@ -76,15 +87,17 @@ func UpdateCommodity(c *gin.Context) { | ||
76 | //var Location DB.SLocation | 87 | //var Location DB.SLocation |
77 | //json.Unmarshal([]byte(c.PostForm("Location")), &Location) | 88 | //json.Unmarshal([]byte(c.PostForm("Location")), &Location) |
78 | 89 | ||
79 | - var id bson.ObjectId | 90 | + |
91 | + var id primitive.ObjectID | ||
80 | if pid := c.PostForm("id"); pid == "null" { | 92 | if pid := c.PostForm("id"); pid == "null" { |
81 | - id = bson.NewObjectId() | 93 | + id = primitive.NewObjectID() |
82 | } else { | 94 | } else { |
83 | - id = bson.ObjectIdHex(pid) | 95 | + id,_ = primitive.ObjectIDFromHex(pid) |
84 | } | 96 | } |
85 | 97 | ||
86 | - DB.CCommodity.UpsertId( | ||
87 | - id, | 98 | + upsert := true |
99 | + DB.CCommodity.FindOneAndUpdate(tools.GetContext(), | ||
100 | + bson.M{"_id": id}, | ||
88 | bson.M{"$set": bson.M{ | 101 | bson.M{"$set": bson.M{ |
89 | "Name": c.PostForm("Name"), | 102 | "Name": c.PostForm("Name"), |
90 | "Price": c.PostForm("Price"), | 103 | "Price": c.PostForm("Price"), |
@@ -93,7 +106,9 @@ func UpdateCommodity(c *gin.Context) { | @@ -93,7 +106,9 @@ func UpdateCommodity(c *gin.Context) { | ||
93 | "TopPhoto": TopPhoto, | 106 | "TopPhoto": TopPhoto, |
94 | "ItemId": c.PostForm("ItemId"), | 107 | "ItemId": c.PostForm("ItemId"), |
95 | "Images": Picture, | 108 | "Images": Picture, |
96 | - }}, | 109 | + }}, &options.FindOneAndUpdateOptions{ |
110 | + Upsert: &upsert, | ||
111 | + }, | ||
97 | ) | 112 | ) |
98 | 113 | ||
99 | c.JSON(200, tools.ResponseSeccess{ | 114 | c.JSON(200, tools.ResponseSeccess{ |
API/Sms.go
@@ -75,7 +75,7 @@ func Send(c *gin.Context) { | @@ -75,7 +75,7 @@ func Send(c *gin.Context) { | ||
75 | json.Unmarshal([]byte(c.PostForm("Location")), &Location) | 75 | json.Unmarshal([]byte(c.PostForm("Location")), &Location) |
76 | 76 | ||
77 | //go func(res *dysmsapi.SendSmsResponse) { | 77 | //go func(res *dysmsapi.SendSmsResponse) { |
78 | - DB.CSystemLog.Insert(DB.SSystemLog{ | 78 | + DB.CSystemLog.InsertOne(tools.GetContext(),DB.SSystemLog{ |
79 | "", | 79 | "", |
80 | "", | 80 | "", |
81 | c.PostForm("Mobile"), | 81 | c.PostForm("Mobile"), |
API/Tag.go
@@ -3,7 +3,7 @@ package Api | @@ -3,7 +3,7 @@ package Api | ||
3 | import ( | 3 | import ( |
4 | "github.com/aarongao/tools" | 4 | "github.com/aarongao/tools" |
5 | "github.com/gin-gonic/gin" | 5 | "github.com/gin-gonic/gin" |
6 | - "gopkg.in/mgo.v2/bson" | 6 | + "go.mongodb.org/mongo-driver/bson" |
7 | "letu/DB" | 7 | "letu/DB" |
8 | "letu/Lib/LeYouTu" | 8 | "letu/Lib/LeYouTu" |
9 | "time" | 9 | "time" |
@@ -26,11 +26,21 @@ func AllTag(c *gin.Context) { | @@ -26,11 +26,21 @@ func AllTag(c *gin.Context) { | ||
26 | return | 26 | return |
27 | } | 27 | } |
28 | 28 | ||
29 | - var Stags []*DB.STag | ||
30 | - DB.CTags.Find(bson.M{"ScenicId": ScenicId}).All(&Stags) | 29 | + var Stags []DB.STag |
30 | + cur, err := DB.CTags.Find(tools.GetContext(), bson.M{"ScenicId": ScenicId}) | ||
31 | + defer cur.Close(tools.GetContext()) | ||
32 | + if err == nil { | ||
33 | + for cur.Next(tools.GetContext()) { | ||
34 | + var e DB.STag | ||
35 | + cur.Decode(&e) | ||
36 | + Stags = append(Stags,e) | ||
37 | + } | ||
38 | + } | ||
39 | + | ||
40 | + | ||
31 | 41 | ||
32 | if Stags == nil { | 42 | if Stags == nil { |
33 | - Stags = []*DB.STag{} | 43 | + Stags = []DB.STag{} |
34 | } | 44 | } |
35 | 45 | ||
36 | c.JSON(200, tools.ResponseSeccess{ | 46 | c.JSON(200, tools.ResponseSeccess{ |
@@ -67,11 +77,19 @@ func AllTagGroup(c *gin.Context) { | @@ -67,11 +77,19 @@ func AllTagGroup(c *gin.Context) { | ||
67 | return | 77 | return |
68 | } | 78 | } |
69 | 79 | ||
70 | - var Stags []*DB.STag | ||
71 | - DB.CTags.Find(bson.M{"ScenicId": ScenicId}).All(&Stags) | 80 | + var Stags []DB.STag |
81 | + cur, err := DB.CTags.Find(tools.GetContext(), bson.M{"ScenicId": ScenicId}) | ||
82 | + defer cur.Close(tools.GetContext()) | ||
83 | + if err == nil { | ||
84 | + for cur.Next(tools.GetContext()) { | ||
85 | + var e DB.STag | ||
86 | + cur.Decode(&e) | ||
87 | + Stags = append(Stags,e) | ||
88 | + } | ||
89 | + } | ||
72 | 90 | ||
73 | if Stags == nil { | 91 | if Stags == nil { |
74 | - Stags = []*DB.STag{} | 92 | + Stags = []DB.STag{} |
75 | } | 93 | } |
76 | 94 | ||
77 | Group := make(map[string][]string) | 95 | Group := make(map[string][]string) |
@@ -141,7 +159,7 @@ func CreateTag(c *gin.Context) { | @@ -141,7 +159,7 @@ func CreateTag(c *gin.Context) { | ||
141 | return | 159 | return |
142 | } | 160 | } |
143 | 161 | ||
144 | - DB.CTags.Insert(DB.STag{ | 162 | + DB.CTags.InsertOne(tools.GetContext(),DB.STag{ |
145 | ScenicId, | 163 | ScenicId, |
146 | c.PostForm("TagGroup"), | 164 | c.PostForm("TagGroup"), |
147 | c.PostForm("TagName"), | 165 | c.PostForm("TagName"), |
@@ -204,7 +222,7 @@ func RemoveTag(c *gin.Context) { | @@ -204,7 +222,7 @@ func RemoveTag(c *gin.Context) { | ||
204 | return | 222 | return |
205 | } | 223 | } |
206 | 224 | ||
207 | - DB.CTags.Remove(bson.M{"ScenicId": ScenicId,"Name":c.PostForm("TagName"),"Type":c.PostForm("TagGroup")}) | 225 | + DB.CTags.DeleteOne(tools.GetContext(), bson.M{"ScenicId": ScenicId,"Name":c.PostForm("TagName"),"Type":c.PostForm("TagGroup")}) |
208 | 226 | ||
209 | DB.Redis.Delete("Tags_" + ScenicId) | 227 | DB.Redis.Delete("Tags_" + ScenicId) |
210 | println("清楚缓存Tags") | 228 | println("清楚缓存Tags") |
API/TopMenus.go
@@ -4,7 +4,9 @@ import ( | @@ -4,7 +4,9 @@ import ( | ||
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2/bson" | 7 | + "go.mongodb.org/mongo-driver/bson" |
8 | + "go.mongodb.org/mongo-driver/bson/primitive" | ||
9 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
8 | "letu/DB" | 10 | "letu/DB" |
9 | "letu/Lib/LeYouTu" | 11 | "letu/Lib/LeYouTu" |
10 | ) | 12 | ) |
@@ -25,11 +27,19 @@ func AllTopMenus(c *gin.Context) { | @@ -25,11 +27,19 @@ func AllTopMenus(c *gin.Context) { | ||
25 | return | 27 | return |
26 | } | 28 | } |
27 | 29 | ||
28 | - var STopMenus []*DB.STopMenus | ||
29 | - DB.CTopMenus.Find(bson.M{"ScenicId": ScenicId}).All(&STopMenus) | 30 | + var STopMenus []DB.STopMenus |
31 | + cur, err := DB.CTopMenus.Find(tools.GetContext(), bson.M{"ScenicId": ScenicId}) | ||
32 | + defer cur.Close(tools.GetContext()) | ||
33 | + if err == nil { | ||
34 | + for cur.Next(tools.GetContext()) { | ||
35 | + var e DB.STopMenus | ||
36 | + cur.Decode(&e) | ||
37 | + STopMenus = append(STopMenus,e) | ||
38 | + } | ||
39 | + } | ||
30 | 40 | ||
31 | if STopMenus == nil { | 41 | if STopMenus == nil { |
32 | - STopMenus = []*DB.STopMenus{} | 42 | + STopMenus = []DB.STopMenus{} |
33 | } | 43 | } |
34 | 44 | ||
35 | c.JSON(200, tools.ResponseSeccess{ | 45 | c.JSON(200, tools.ResponseSeccess{ |
@@ -66,20 +76,23 @@ func UpdateTopMenus(c *gin.Context) { | @@ -66,20 +76,23 @@ func UpdateTopMenus(c *gin.Context) { | ||
66 | var Tags []string | 76 | var Tags []string |
67 | json.Unmarshal([]byte(c.PostForm("Tags")), &Tags) | 77 | json.Unmarshal([]byte(c.PostForm("Tags")), &Tags) |
68 | 78 | ||
69 | - var id bson.ObjectId | 79 | + var id primitive.ObjectID |
70 | if pid := c.PostForm("id"); pid == "null" { | 80 | if pid := c.PostForm("id"); pid == "null" { |
71 | - id = bson.NewObjectId() | 81 | + id = primitive.NewObjectID() |
72 | } else { | 82 | } else { |
73 | - id = bson.ObjectIdHex(pid) | 83 | + id,_ = primitive.ObjectIDFromHex(pid) |
74 | } | 84 | } |
75 | 85 | ||
76 | - DB.CTopMenus.UpsertId( | ||
77 | - id, | 86 | + upsert := true |
87 | + DB.CTopMenus.FindOneAndUpdate(tools.GetContext(), | ||
88 | + bson.M{"_id": id}, | ||
78 | bson.M{"$set": bson.M{ | 89 | bson.M{"$set": bson.M{ |
79 | "ScenicId": ScenicId, | 90 | "ScenicId": ScenicId, |
80 | "Title": c.PostForm("Title"), | 91 | "Title": c.PostForm("Title"), |
81 | "Tags": Tags, | 92 | "Tags": Tags, |
82 | - }}, | 93 | + }}, &options.FindOneAndUpdateOptions{ |
94 | + Upsert: &upsert, | ||
95 | + }, | ||
83 | ) | 96 | ) |
84 | 97 | ||
85 | c.JSON(200, tools.ResponseSeccess{ | 98 | c.JSON(200, tools.ResponseSeccess{ |
API/Trajectory.go
@@ -24,7 +24,7 @@ func SaveTrajectory(c *gin.Context) { | @@ -24,7 +24,7 @@ func SaveTrajectory(c *gin.Context) { | ||
24 | var Location DB.SLocation | 24 | var Location DB.SLocation |
25 | json.Unmarshal([]byte(c.PostForm("Location")), &Location) | 25 | json.Unmarshal([]byte(c.PostForm("Location")), &Location) |
26 | 26 | ||
27 | - DB.CTrajectory.Insert(DB.STrajectory{ | 27 | + DB.CTrajectory.InsertOne(tools.GetContext(),DB.STrajectory{ |
28 | c.PostForm("UserId"), | 28 | c.PostForm("UserId"), |
29 | Location, | 29 | Location, |
30 | time.Now().Unix(), | 30 | time.Now().Unix(), |
API/User.go
@@ -5,7 +5,9 @@ import ( | @@ -5,7 +5,9 @@ import ( | ||
5 | "encoding/hex" | 5 | "encoding/hex" |
6 | "github.com/aarongao/tools" | 6 | "github.com/aarongao/tools" |
7 | "github.com/gin-gonic/gin" | 7 | "github.com/gin-gonic/gin" |
8 | - "gopkg.in/mgo.v2/bson" | 8 | + "go.mongodb.org/mongo-driver/bson" |
9 | + "go.mongodb.org/mongo-driver/bson/primitive" | ||
10 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
9 | "letu/DB" | 11 | "letu/DB" |
10 | "letu/Lib/Token" | 12 | "letu/Lib/Token" |
11 | "regexp" | 13 | "regexp" |
@@ -63,11 +65,11 @@ func LoginUser(c *gin.Context) { | @@ -63,11 +65,11 @@ func LoginUser(c *gin.Context) { | ||
63 | var User *DB.SMember | 65 | var User *DB.SMember |
64 | if cacheCode == c.PostForm("Code") { | 66 | if cacheCode == c.PostForm("Code") { |
65 | selected["Mobile"] = c.PostForm("Mobile") | 67 | selected["Mobile"] = c.PostForm("Mobile") |
66 | - DB.CMember.Find(selected).One(&User) | 68 | + DB.CMember.FindOne(tools.GetContext(), selected).Decode(&User) |
67 | 69 | ||
68 | // 验证码匹配,但手机号不存在 | 70 | // 验证码匹配,但手机号不存在 |
69 | if User == nil { | 71 | if User == nil { |
70 | - objectID := bson.NewObjectId() | 72 | + objectID := primitive.NewObjectID() |
71 | User := DB.SMember{ | 73 | User := DB.SMember{ |
72 | &objectID, | 74 | &objectID, |
73 | "", | 75 | "", |
@@ -88,7 +90,7 @@ func LoginUser(c *gin.Context) { | @@ -88,7 +90,7 @@ func LoginUser(c *gin.Context) { | ||
88 | c.Request.Header.Get("DeviceToken"), | 90 | c.Request.Header.Get("DeviceToken"), |
89 | }, | 91 | }, |
90 | } | 92 | } |
91 | - DB.CMember.Insert(User) | 93 | + DB.CMember.InsertOne(tools.GetContext(),User) |
92 | } | 94 | } |
93 | 95 | ||
94 | } else { | 96 | } else { |
@@ -142,7 +144,8 @@ func RegisterDevice(c *gin.Context) { | @@ -142,7 +144,8 @@ func RegisterDevice(c *gin.Context) { | ||
142 | return | 144 | return |
143 | } | 145 | } |
144 | 146 | ||
145 | - DB.CDevice.Upsert( | 147 | + upsert := true |
148 | + DB.CDevice.FindOneAndUpdate(tools.GetContext(), | ||
146 | bson.M{"DeviceId":c.Request.Header.Get("DeviceId")}, | 149 | bson.M{"DeviceId":c.Request.Header.Get("DeviceId")}, |
147 | bson.M{"$set": bson.M{ | 150 | bson.M{"$set": bson.M{ |
148 | "Mac":c.Request.Header.Get("Mac"), | 151 | "Mac":c.Request.Header.Get("Mac"), |
@@ -152,7 +155,9 @@ func RegisterDevice(c *gin.Context) { | @@ -152,7 +155,9 @@ func RegisterDevice(c *gin.Context) { | ||
152 | "SystemModel":c.Request.Header.Get("SystemModel"), | 155 | "SystemModel":c.Request.Header.Get("SystemModel"), |
153 | "AppVersion":c.Request.Header.Get("AppVersion"), | 156 | "AppVersion":c.Request.Header.Get("AppVersion"), |
154 | "DeviceToken":c.Request.Header.Get("DeviceToken"), | 157 | "DeviceToken":c.Request.Header.Get("DeviceToken"), |
155 | - }}, | 158 | + }}, &options.FindOneAndUpdateOptions{ |
159 | + Upsert: &upsert, | ||
160 | + }, | ||
156 | ) | 161 | ) |
157 | 162 | ||
158 | c.JSON(200, tools.ResponseSeccess{ | 163 | c.JSON(200, tools.ResponseSeccess{ |
@@ -175,7 +180,8 @@ func UserInfo(c *gin.Context) { | @@ -175,7 +180,8 @@ func UserInfo(c *gin.Context) { | ||
175 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 180 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
176 | c.Header("Access-Control-Allow-Credentials", "true") | 181 | c.Header("Access-Control-Allow-Credentials", "true") |
177 | 182 | ||
178 | - if c.Query("Token") == "" || bson.IsObjectIdHex(c.Query("id")) == false { | 183 | + objID,err := primitive.ObjectIDFromHex(c.Query("id")) |
184 | + if c.Query("Token") == "" || err!=nil { | ||
179 | c.JSON(200, tools.ResponseError{ | 185 | c.JSON(200, tools.ResponseError{ |
180 | 1, | 186 | 1, |
181 | "Token或者用户id不正确", | 187 | "Token或者用户id不正确", |
@@ -192,7 +198,7 @@ func UserInfo(c *gin.Context) { | @@ -192,7 +198,7 @@ func UserInfo(c *gin.Context) { | ||
192 | } | 198 | } |
193 | 199 | ||
194 | var User DB.SMember | 200 | var User DB.SMember |
195 | - DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&User) | 201 | + DB.CMember.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&User) |
196 | 202 | ||
197 | User.Device = DB.SDevice{} | 203 | User.Device = DB.SDevice{} |
198 | c.JSON(200, tools.ResponseSeccess{ | 204 | c.JSON(200, tools.ResponseSeccess{ |
@@ -215,7 +221,8 @@ func CheckToken(c *gin.Context) { | @@ -215,7 +221,8 @@ func CheckToken(c *gin.Context) { | ||
215 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 221 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
216 | c.Header("Access-Control-Allow-Credentials", "true") | 222 | c.Header("Access-Control-Allow-Credentials", "true") |
217 | 223 | ||
218 | - if c.PostForm("Token") == "" || bson.IsObjectIdHex(c.PostForm("id")) == false { | 224 | + _,err := primitive.ObjectIDFromHex(c.PostForm("id")) |
225 | + if c.PostForm("Token") == "" || err != nil { | ||
219 | c.JSON(200, tools.ResponseError{ | 226 | c.JSON(200, tools.ResponseError{ |
220 | 1, | 227 | 1, |
221 | "Token或者用户id不正确", | 228 | "Token或者用户id不正确", |
@@ -257,7 +264,8 @@ func UpdateUser(c *gin.Context) { | @@ -257,7 +264,8 @@ func UpdateUser(c *gin.Context) { | ||
257 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 264 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
258 | c.Header("Access-Control-Allow-Credentials", "true") | 265 | c.Header("Access-Control-Allow-Credentials", "true") |
259 | 266 | ||
260 | - if c.PostForm("Token") == "" || bson.IsObjectIdHex(c.PostForm("id")) == false { | 267 | + _,err := primitive.ObjectIDFromHex(c.PostForm("id")) |
268 | + if c.PostForm("Token") == "" || err != nil { | ||
261 | c.JSON(200, tools.ResponseError{ | 269 | c.JSON(200, tools.ResponseError{ |
262 | 1, | 270 | 1, |
263 | "Token或者用户id不正确", | 271 | "Token或者用户id不正确", |
@@ -309,8 +317,9 @@ func UpdateUser(c *gin.Context) { | @@ -309,8 +317,9 @@ func UpdateUser(c *gin.Context) { | ||
309 | return | 317 | return |
310 | } | 318 | } |
311 | 319 | ||
312 | - err := DB.CMember.Update( | ||
313 | - bson.M{"_id": bson.ObjectIdHex(c.PostForm("id"))}, | 320 | + objID,_ := primitive.ObjectIDFromHex(c.PostForm("id")) |
321 | + _, err = DB.CMember.UpdateOne(tools.GetContext(), | ||
322 | + bson.M{"_id": objID}, | ||
314 | bson.M{"$set": bson.M{ | 323 | bson.M{"$set": bson.M{ |
315 | "Birthday": c.PostForm("Birthday"), | 324 | "Birthday": c.PostForm("Birthday"), |
316 | "FullName": c.PostForm("FullName"), | 325 | "FullName": c.PostForm("FullName"), |
@@ -321,7 +330,8 @@ func UpdateUser(c *gin.Context) { | @@ -321,7 +330,8 @@ func UpdateUser(c *gin.Context) { | ||
321 | 330 | ||
322 | if err == nil { | 331 | if err == nil { |
323 | var User *DB.SMember | 332 | var User *DB.SMember |
324 | - DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.PostForm("id"))}).One(&User) | 333 | + objID,_ := primitive.ObjectIDFromHex(c.PostForm("id")) |
334 | + DB.CMember.FindOne(tools.GetContext(), bson.M{"_id": objID}).Decode(&User) | ||
325 | 335 | ||
326 | c.JSON(200, tools.ResponseSeccess{ | 336 | c.JSON(200, tools.ResponseSeccess{ |
327 | 0, | 337 | 0, |
@@ -350,7 +360,8 @@ func RemoveUser(c *gin.Context) { | @@ -350,7 +360,8 @@ func RemoveUser(c *gin.Context) { | ||
350 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | 360 | c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) |
351 | c.Header("Access-Control-Allow-Credentials", "true") | 361 | c.Header("Access-Control-Allow-Credentials", "true") |
352 | 362 | ||
353 | - if c.PostForm("Token") == "" || bson.IsObjectIdHex(c.PostForm("id")) == false { | 363 | + objID,err := primitive.ObjectIDFromHex(c.PostForm("id")) |
364 | + if c.PostForm("Token") == "" || err != nil { | ||
354 | c.JSON(200, tools.ResponseError{ | 365 | c.JSON(200, tools.ResponseError{ |
355 | 1, | 366 | 1, |
356 | "Token或者用户id不正确", | 367 | "Token或者用户id不正确", |
@@ -366,11 +377,10 @@ func RemoveUser(c *gin.Context) { | @@ -366,11 +377,10 @@ func RemoveUser(c *gin.Context) { | ||
366 | return | 377 | return |
367 | } | 378 | } |
368 | 379 | ||
369 | - | ||
370 | - err := DB.CMember.Remove(bson.M{"_id": bson.ObjectIdHex(c.PostForm("id"))}) | 380 | + _, err = DB.CMember.DeleteOne(tools.GetContext(), bson.M{"_id": objID}) |
371 | 381 | ||
372 | if err == nil { | 382 | if err == nil { |
373 | - | 383 | + |
374 | c.JSON(200, tools.ResponseSeccess{ | 384 | c.JSON(200, tools.ResponseSeccess{ |
375 | 0, | 385 | 0, |
376 | "ok", | 386 | "ok", |
API/UserLog.go
@@ -4,8 +4,11 @@ import ( | @@ -4,8 +4,11 @@ import ( | ||
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2/bson" | 7 | + "go.mongodb.org/mongo-driver/bson" |
8 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
8 | "letu/DB" | 9 | "letu/DB" |
10 | + "math" | ||
11 | + "strconv" | ||
9 | "time" | 12 | "time" |
10 | ) | 13 | ) |
11 | 14 | ||
@@ -46,7 +49,7 @@ func UserLog(c *gin.Context) { | @@ -46,7 +49,7 @@ func UserLog(c *gin.Context) { | ||
46 | var Location DB.SLocation | 49 | var Location DB.SLocation |
47 | json.Unmarshal([]byte(c.PostForm("Location")), &Location) | 50 | json.Unmarshal([]byte(c.PostForm("Location")), &Location) |
48 | 51 | ||
49 | - DB.CUserLog.Insert(DB.SUserLog{ | 52 | + DB.CUserLog.InsertOne(tools.GetContext(),DB.SUserLog{ |
50 | c.PostForm("Type"), | 53 | c.PostForm("Type"), |
51 | c.PostForm("SubType"), | 54 | c.PostForm("SubType"), |
52 | c.PostForm("ScenicId"), | 55 | c.PostForm("ScenicId"), |
@@ -69,7 +72,8 @@ func UserLog(c *gin.Context) { | @@ -69,7 +72,8 @@ func UserLog(c *gin.Context) { | ||
69 | }, | 72 | }, |
70 | }) | 73 | }) |
71 | 74 | ||
72 | - DB.CDevice.Upsert( | 75 | + upsert := true |
76 | + DB.CDevice.FindOneAndUpdate(tools.GetContext(), | ||
73 | bson.M{"DeviceId": c.Request.Header.Get("DeviceId")}, | 77 | bson.M{"DeviceId": c.Request.Header.Get("DeviceId")}, |
74 | bson.M{"$set": bson.M{ | 78 | bson.M{"$set": bson.M{ |
75 | "Mac": c.Request.Header.Get("Mac"), | 79 | "Mac": c.Request.Header.Get("Mac"), |
@@ -79,7 +83,9 @@ func UserLog(c *gin.Context) { | @@ -79,7 +83,9 @@ func UserLog(c *gin.Context) { | ||
79 | "SystemModel": c.Request.Header.Get("SystemModel"), | 83 | "SystemModel": c.Request.Header.Get("SystemModel"), |
80 | "AppVersion": c.Request.Header.Get("AppVersion"), | 84 | "AppVersion": c.Request.Header.Get("AppVersion"), |
81 | "DeviceToken": c.Request.Header.Get("DeviceToken"), | 85 | "DeviceToken": c.Request.Header.Get("DeviceToken"), |
82 | - }}, | 86 | + }}, &options.FindOneAndUpdateOptions{ |
87 | + Upsert: &upsert, | ||
88 | + }, | ||
83 | ) | 89 | ) |
84 | 90 | ||
85 | c.JSON(200, tools.ResponseSeccess{ | 91 | c.JSON(200, tools.ResponseSeccess{ |
@@ -88,3 +94,51 @@ func UserLog(c *gin.Context) { | @@ -88,3 +94,51 @@ func UserLog(c *gin.Context) { | ||
88 | }) | 94 | }) |
89 | 95 | ||
90 | } | 96 | } |
97 | + | ||
98 | + | ||
99 | +// @Title 查询所有用户行为 | ||
100 | +// @Description 查询所有用户行为 | ||
101 | +// @Accept json | ||
102 | +// @Produce json | ||
103 | +// @Param Page 1 int true "当前第几页" | ||
104 | +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"total":1,"currpage":1,"totalpages":1,"prepage":20,"result":}" | ||
105 | +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" | ||
106 | +// @Router /AllUserLog? [get] | ||
107 | +func AllUserLog(c *gin.Context) { | ||
108 | + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | ||
109 | + c.Header("Access-Control-Allow-Credentials", "true") | ||
110 | + | ||
111 | + | ||
112 | + total,_ := DB.CUserLog.CountDocuments(tools.GetContext(), bson.M{}) | ||
113 | + limit, _ := strconv.ParseInt(c.Query("Limit"),10,64) | ||
114 | + if limit == 0 { | ||
115 | + limit = 50 | ||
116 | + } | ||
117 | + currPage, _ := strconv.ParseInt(c.Query("Page"),10,64) | ||
118 | + if currPage == 0 { | ||
119 | + currPage = 1 | ||
120 | + } | ||
121 | + skip := (currPage - 1) * limit | ||
122 | + | ||
123 | + var aUserLog []DB.SUserLog | ||
124 | + cur, err := DB.CUserLog.Find(tools.GetContext(), bson.M{}, &options.FindOptions{Limit: &limit, Skip: &skip, Sort: bson.M{"_id": -1}}) | ||
125 | + defer cur.Close(tools.GetContext()) | ||
126 | + if err == nil { | ||
127 | + for cur.Next(tools.GetContext()) { | ||
128 | + var e DB.SUserLog | ||
129 | + cur.Decode(&e) | ||
130 | + aUserLog = append(aUserLog,e) | ||
131 | + } | ||
132 | + } | ||
133 | + | ||
134 | + | ||
135 | + c.JSON(200, tools.Page{ | ||
136 | + 0, | ||
137 | + total, | ||
138 | + currPage, | ||
139 | + int(math.Ceil(float64(total) / float64(limit))), | ||
140 | + limit, | ||
141 | + aUserLog, | ||
142 | + }) | ||
143 | + | ||
144 | +} | ||
91 | \ No newline at end of file | 145 | \ No newline at end of file |
@@ -0,0 +1,70 @@ | @@ -0,0 +1,70 @@ | ||
1 | +package main | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "github.com/aarongao/tools" | ||
6 | + "github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi" | ||
7 | + "time" | ||
8 | +) | ||
9 | + | ||
10 | +var lastState = 0 | ||
11 | + | ||
12 | +func main() { | ||
13 | + | ||
14 | + for { | ||
15 | + time.Sleep(30 * time.Second) | ||
16 | + httpState, body, error := tools.GET("http://leyoutu.st-i.com.cn/AllScenic") | ||
17 | + if httpState == 200 && error == nil { | ||
18 | + | ||
19 | + oBody := tools.ResponseSeccess{} | ||
20 | + json.Unmarshal([]byte(body), &oBody) | ||
21 | + | ||
22 | + rlen := len(oBody.Result.([]interface{})) | ||
23 | + if oBody.ErrCode != 0 || rlen == 0 { | ||
24 | + sms(1) | ||
25 | + } else { | ||
26 | + sms(2) | ||
27 | + } | ||
28 | + } else { | ||
29 | + | ||
30 | + sms(1) | ||
31 | + } | ||
32 | + | ||
33 | + } | ||
34 | + | ||
35 | +} | ||
36 | + | ||
37 | +func sms(state int) { | ||
38 | + | ||
39 | + stateString := "" | ||
40 | + if state == 1{ | ||
41 | + stateString = "Fail" | ||
42 | + } | ||
43 | + if state == 2{ | ||
44 | + stateString = "Success" | ||
45 | + } | ||
46 | + println("state:",stateString) | ||
47 | + | ||
48 | + | ||
49 | + if lastState == 0 { | ||
50 | + lastState = state | ||
51 | + } | ||
52 | + | ||
53 | + if lastState != state{ | ||
54 | + | ||
55 | + lastState = state | ||
56 | + | ||
57 | + client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", "LTAI4FdQeNMQXRU6u5J3EFQc", "PwvyF5rRNBWLDya41WrCpvENevYZGi") | ||
58 | + request := dysmsapi.CreateSendSmsRequest() | ||
59 | + request.Scheme = "https" | ||
60 | + request.PhoneNumbers = "18616619599" | ||
61 | + request.SignName = "乐游图" | ||
62 | + request.TemplateCode = "SMS_182595013" | ||
63 | + request.TemplateParam = "{\"code\":\"" + stateString +"\"}" | ||
64 | + response, err := client.SendSms(request) | ||
65 | + if err != nil { | ||
66 | + println(err.Error()) | ||
67 | + } | ||
68 | + println("SMS:", response.Code) | ||
69 | + } | ||
70 | +} |
DB/db.go
1 | package DB | 1 | package DB |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "gopkg.in/mgo.v2" | ||
5 | - "gopkg.in/mgo.v2/bson" | 4 | + "go.mongodb.org/mongo-driver/bson/primitive" |
5 | + "go.mongodb.org/mongo-driver/mongo" | ||
6 | "letu/Lib/Cache" | 6 | "letu/Lib/Cache" |
7 | ) | 7 | ) |
8 | 8 | ||
9 | var Redis *Cache.Redis | 9 | var Redis *Cache.Redis |
10 | 10 | ||
11 | -var DBSession *mgo.Session | ||
12 | -var CItem *mgo.Collection //所有游玩项目内容 | ||
13 | -var CComplaint *mgo.Collection //投诉 | ||
14 | -var CInvestigation *mgo.Collection //调查 | ||
15 | -var CMember *mgo.Collection //会员 | ||
16 | -var CCommodity *mgo.Collection //商城 | ||
17 | -var CTags *mgo.Collection //标签 | ||
18 | -var CScenic *mgo.Collection //景区 | ||
19 | -var CLine *mgo.Collection //推荐线路 | ||
20 | -var CUserLog *mgo.Collection //用户行为记录 | ||
21 | -var CSystemLog *mgo.Collection //操作记录 | ||
22 | -var CTrajectory *mgo.Collection //移动轨迹 | ||
23 | -var CIcons *mgo.Collection //图标信息 | ||
24 | -var CTopMenus *mgo.Collection //菜单 | ||
25 | -var CDevice *mgo.Collection //设备清单 | ||
26 | -var DB *mgo.Database | 11 | +var CItem *mongo.Collection //所有游玩项目内容 |
12 | +var CComplaint *mongo.Collection //投诉 | ||
13 | +var CInvestigation *mongo.Collection //调查 | ||
14 | +var CMember *mongo.Collection //会员 | ||
15 | +var CCommodity *mongo.Collection //商城 | ||
16 | +var CTags *mongo.Collection //标签 | ||
17 | +var CScenic *mongo.Collection //景区 | ||
18 | +var CLine *mongo.Collection //推荐线路 | ||
19 | +var CUserLog *mongo.Collection //用户行为记录 | ||
20 | +var CSystemLog *mongo.Collection //操作记录 | ||
21 | +var CTrajectory *mongo.Collection //移动轨迹 | ||
22 | +var CIcons *mongo.Collection //图标信息 | ||
23 | +var CTopMenus *mongo.Collection //菜单 | ||
24 | +var CDevice *mongo.Collection //设备清单 | ||
25 | +var DB *mongo.Database | ||
27 | 26 | ||
28 | type SItem struct { | 27 | type SItem struct { |
29 | - Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` | ||
30 | - Name string `bson:"Name" json:"Name"` | ||
31 | - SubName string `bson:"SubName" json:"SubName"` | ||
32 | - Location SLocation `bson:"Location" json:"Location"` | ||
33 | - Tags []STag `bson:"Tags" json:"Tags"` | ||
34 | - Icon string `bson:"Icon" json:"Icon"` | ||
35 | - LimitHeight string `bson:"LimitHeight" json:"LimitHeight"` //限高 | ||
36 | - PlayDuration string `bson:"PlayDuration" json:"PlayDuration"` //游玩时长 | ||
37 | - SceneTime string `bson:"SceneTime" json:"SceneTime"` //场次时间 | ||
38 | - Picture []string `bson:"Picture" json:"Picture"` | ||
39 | - Voice string `bson:"Voice" json:"Voice"` //音频 | ||
40 | - Tel string `bson:"Tel" json:"Tel"` | ||
41 | - AverageConsumption string `bson:"AverageConsumption" json:"AverageConsumption"` //人均消费 | ||
42 | - Menu string `bson:"Menu" json:"Menu"` //目录 | ||
43 | - Time string `bson:"Time" json:"Time"` | ||
44 | - OpenHours string `bson:"OpenHours" json:"OpenHours"` //开放时间 | ||
45 | - LocationDescription string `bson:"LocationDescription" json:"LocationDescription"` //位置描述 | ||
46 | - Reminder string `bson:"Reminder" json:"Reminder"` //温馨提示 | ||
47 | - State int `bson:"State" json:"State"` // 运行状态0=正常1=停运 | 28 | + Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` |
29 | + Name string `bson:"Name" json:"Name"` | ||
30 | + SubName string `bson:"SubName" json:"SubName"` | ||
31 | + Location SLocation `bson:"Location" json:"Location"` | ||
32 | + Tags []STag `bson:"Tags" json:"Tags"` | ||
33 | + Icon string `bson:"Icon" json:"Icon"` | ||
34 | + LimitHeight string `bson:"LimitHeight" json:"LimitHeight"` //限高 | ||
35 | + PlayDuration string `bson:"PlayDuration" json:"PlayDuration"` //游玩时长 | ||
36 | + SceneTime string `bson:"SceneTime" json:"SceneTime"` //场次时间 | ||
37 | + Picture []string `bson:"Picture" json:"Picture"` | ||
38 | + Voice string `bson:"Voice" json:"Voice"` //音频 | ||
39 | + Tel string `bson:"Tel" json:"Tel"` | ||
40 | + AverageConsumption string `bson:"AverageConsumption" json:"AverageConsumption"` //人均消费 | ||
41 | + Menu string `bson:"Menu" json:"Menu"` //目录 | ||
42 | + Time string `bson:"Time" json:"Time"` | ||
43 | + OpenHours string `bson:"OpenHours" json:"OpenHours"` //开放时间 | ||
44 | + LocationDescription string `bson:"LocationDescription" json:"LocationDescription"` //位置描述 | ||
45 | + Reminder string `bson:"Reminder" json:"Reminder"` //温馨提示 | ||
46 | + State int `bson:"State" json:"State"` // 运行状态0=正常1=停运 | ||
48 | } | 47 | } |
49 | type SIcons struct { | 48 | type SIcons struct { |
50 | - Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` | ||
51 | - ScenicId string `bson:"ScenicId" json:"ScenicId"` | ||
52 | - Name string `bson:"Name" json:"Name"` | ||
53 | - Picture string `bson:"Picture" json:"Picture"` | 49 | + Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` |
50 | + ScenicId string `bson:"ScenicId" json:"ScenicId"` | ||
51 | + Name string `bson:"Name" json:"Name"` | ||
52 | + Picture string `bson:"Picture" json:"Picture"` | ||
54 | } | 53 | } |
55 | type STrajectory struct { | 54 | type STrajectory struct { |
56 | UserId string `bson:"UserId" json:"UserId"` // 用户ID | 55 | UserId string `bson:"UserId" json:"UserId"` // 用户ID |
@@ -62,10 +61,10 @@ type SLocation struct { | @@ -62,10 +61,10 @@ type SLocation struct { | ||
62 | Longitude float64 `bson:"Longitude" json:"Longitude"` //经度 | 61 | Longitude float64 `bson:"Longitude" json:"Longitude"` //经度 |
63 | } | 62 | } |
64 | type STopMenus struct { | 63 | type STopMenus struct { |
65 | - Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` | ||
66 | - ScenicId string `bson:"ScenicId" json:"ScenicId"` | ||
67 | - Title string `bson:"Title" json:"Title"` //菜单标题 | ||
68 | - Tags []string `bson:"Tags" json:"Tags"` //标签 | 64 | + Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` |
65 | + ScenicId string `bson:"ScenicId" json:"ScenicId"` | ||
66 | + Title string `bson:"Title" json:"Title"` //菜单标题 | ||
67 | + Tags []string `bson:"Tags" json:"Tags"` //标签 | ||
69 | } | 68 | } |
70 | 69 | ||
71 | type SDevice struct { | 70 | type SDevice struct { |
@@ -104,25 +103,25 @@ type SSystemLog struct { | @@ -104,25 +103,25 @@ type SSystemLog struct { | ||
104 | Error interface{} `bson:"Error" json:"Error"` //错误信息 | 103 | Error interface{} `bson:"Error" json:"Error"` //错误信息 |
105 | } | 104 | } |
106 | type SCommodity struct { | 105 | type SCommodity struct { |
107 | - Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` | ||
108 | - Name string `bson:"Name" json:"Name"` | ||
109 | - Price string `bson:"Price" json:"Price"` | ||
110 | - ShopName string `bson:"ShopName" json:"ShopName"` | ||
111 | - ItemId string `bson:"ItemId" json:"ItemId"` //项目id | ||
112 | - KvPhoto string `bson:"KvPhoto" json:"KvPhoto"` //用于列表页的图片 | ||
113 | - TopPhoto []SPicture `bson:"TopPhoto" json:"TopPhoto"` //详情页最上面的轮播图 | ||
114 | - Images []string `bson:"Images" json:"Images"` //详情页下面的产品详细图 | 106 | + Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` |
107 | + Name string `bson:"Name" json:"Name"` | ||
108 | + Price string `bson:"Price" json:"Price"` | ||
109 | + ShopName string `bson:"ShopName" json:"ShopName"` | ||
110 | + ItemId string `bson:"ItemId" json:"ItemId"` //项目id | ||
111 | + KvPhoto string `bson:"KvPhoto" json:"KvPhoto"` //用于列表页的图片 | ||
112 | + TopPhoto []SPicture `bson:"TopPhoto" json:"TopPhoto"` //详情页最上面的轮播图 | ||
113 | + Images []string `bson:"Images" json:"Images"` //详情页下面的产品详细图 | ||
115 | } | 114 | } |
116 | type SLine struct { | 115 | type SLine struct { |
117 | - Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` | ||
118 | - Name string `bson:"Name" json:"Name"` | ||
119 | - SubName string `bson:"SubName" json:"SubName"` //游玩时长 | ||
120 | - Location []SLocation `bson:"Location" json:"Location"` //线路点坐标 | ||
121 | - PlayDuration string `bson:"PlayDuration" json:"PlayDuration"` | ||
122 | - Suitable string `bson:"Suitable" json:"Suitable"` //适合人群 | ||
123 | - Content string `bson:"Content" json:"Content"` | ||
124 | - Distance string `bson:"Distance" json:"Distance"` // 距离 | ||
125 | - Annotations []string `bson:"Annotations" json:"Annotations"` //需要点亮的设施id | 116 | + Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` |
117 | + Name string `bson:"Name" json:"Name"` | ||
118 | + SubName string `bson:"SubName" json:"SubName"` //游玩时长 | ||
119 | + Location []SLocation `bson:"Location" json:"Location"` //线路点坐标 | ||
120 | + PlayDuration string `bson:"PlayDuration" json:"PlayDuration"` | ||
121 | + Suitable string `bson:"Suitable" json:"Suitable"` //适合人群 | ||
122 | + Content string `bson:"Content" json:"Content"` | ||
123 | + Distance string `bson:"Distance" json:"Distance"` // 距离 | ||
124 | + Annotations []string `bson:"Annotations" json:"Annotations"` //需要点亮的设施id | ||
126 | } | 125 | } |
127 | 126 | ||
128 | type SComplaint struct { | 127 | type SComplaint struct { |
@@ -133,6 +132,8 @@ type SComplaint struct { | @@ -133,6 +132,8 @@ type SComplaint struct { | ||
133 | Sex string `bson:"Sex" json:"Sex"` | 132 | Sex string `bson:"Sex" json:"Sex"` |
134 | Content string `bson:"Content" json:"Content"` | 133 | Content string `bson:"Content" json:"Content"` |
135 | Image []string `bson:"Image" json:"Image"` | 134 | Image []string `bson:"Image" json:"Image"` |
135 | + State string `bson:"State" json:"State"` // 处理状态(未处理,已处理) | ||
136 | + DateTime int64 `bson:"DateTime" json:"DateTime"` //时间戳 | ||
136 | } | 137 | } |
137 | 138 | ||
138 | type SInvestigation struct { | 139 | type SInvestigation struct { |
@@ -141,15 +142,15 @@ type SInvestigation struct { | @@ -141,15 +142,15 @@ type SInvestigation struct { | ||
141 | Data interface{} `bson:"Data" json:"Data"` | 142 | Data interface{} `bson:"Data" json:"Data"` |
142 | } | 143 | } |
143 | type SMember struct { | 144 | type SMember struct { |
144 | - Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` | ||
145 | - Password string `bson:"Password" json:"Password"` | ||
146 | - Birthday string `bson:"Birthday" json:"Birthday"` | ||
147 | - FullName string `bson:"FullName" json:"FullName"` | ||
148 | - Mobile string `bson:"Mobile" json:"Mobile"` | ||
149 | - Openid string `bson:"Openid" json:"Openid"` | ||
150 | - Token string `bson:"Token" json:"Token"` | ||
151 | - Sex string `bson:"Sex" json:"Sex"` | ||
152 | - Device SDevice `bson:"Device" json:"Device"` //设备信息 | 145 | + Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` |
146 | + Password string `bson:"Password" json:"Password"` | ||
147 | + Birthday string `bson:"Birthday" json:"Birthday"` | ||
148 | + FullName string `bson:"FullName" json:"FullName"` | ||
149 | + Mobile string `bson:"Mobile" json:"Mobile"` | ||
150 | + Openid string `bson:"Openid" json:"Openid"` | ||
151 | + Token string `bson:"Token" json:"Token"` | ||
152 | + Sex string `bson:"Sex" json:"Sex"` | ||
153 | + Device SDevice `bson:"Device" json:"Device"` //设备信息 | ||
153 | } | 154 | } |
154 | 155 | ||
155 | type STag struct { | 156 | type STag struct { |
@@ -169,18 +170,18 @@ type SVideo struct { | @@ -169,18 +170,18 @@ type SVideo struct { | ||
169 | Title string `bson:"Title" json:"Title"` // 标题 | 170 | Title string `bson:"Title" json:"Title"` // 标题 |
170 | } | 171 | } |
171 | type SScenic struct { | 172 | type SScenic struct { |
172 | - Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` | ||
173 | - Name string `bson:"Name" json:"Name"` | ||
174 | - Describe string `bson:"Describe" json:"Describe"` | ||
175 | - OpenHours string `bson:"OpenHours" json:"OpenHours"` //营业时间 | ||
176 | - Mobile string `bson:"Mobile" json:"Mobile"` | ||
177 | - Address string `bson:"Address" json:"Address"` | ||
178 | - InvestigationUrl string `bson:"InvestigationUrl" json:"InvestigationUrl"` //问券调查的url地址 | ||
179 | - Location SLocation `bson:"Location" json:"Location"` | ||
180 | - Picture []SPicture `bson:"Picture" json:"Picture"` | ||
181 | - ShopAdPicture []SPicture `bson:"ShopAdPicture" json:"ShopAdPicture"` //商城列表页图片 | ||
182 | - ItemScenicPicture []SPicture `bson:"ItemScenicPicture" json:"ItemScenicPicture"` //项目场次照片 | ||
183 | - ActivityPicture []SPicture `bson:"ActivityPicture" json:"ActivityPicture"` //活动照片 | ||
184 | - VideoList []SVideo `bson:"VideoList" json:"VideoList"` | ||
185 | - RangeLocation []SLocation `bson:"RangeLocation" json:"RangeLocation"` //景区范围 | 173 | + Id *primitive.ObjectID `bson:"_id" json:"Id" valid:"required"` |
174 | + Name string `bson:"Name" json:"Name"` | ||
175 | + Describe string `bson:"Describe" json:"Describe"` | ||
176 | + OpenHours string `bson:"OpenHours" json:"OpenHours"` //营业时间 | ||
177 | + Mobile string `bson:"Mobile" json:"Mobile"` | ||
178 | + Address string `bson:"Address" json:"Address"` | ||
179 | + InvestigationUrl string `bson:"InvestigationUrl" json:"InvestigationUrl"` //问券调查的url地址 | ||
180 | + Location SLocation `bson:"Location" json:"Location"` | ||
181 | + Picture []SPicture `bson:"Picture" json:"Picture"` | ||
182 | + ShopAdPicture []SPicture `bson:"ShopAdPicture" json:"ShopAdPicture"` //商城列表页图片 | ||
183 | + ItemScenicPicture []SPicture `bson:"ItemScenicPicture" json:"ItemScenicPicture"` //项目场次照片 | ||
184 | + ActivityPicture []SPicture `bson:"ActivityPicture" json:"ActivityPicture"` //活动照片 | ||
185 | + VideoList []SVideo `bson:"VideoList" json:"VideoList"` | ||
186 | + RangeLocation []SLocation `bson:"RangeLocation" json:"RangeLocation"` //景区范围 | ||
186 | } | 187 | } |
Lib/DelayMessage/delaymessage.go
1 | package DelayMessage | 1 | package DelayMessage |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "context" | ||
4 | "encoding/json" | 5 | "encoding/json" |
5 | "fmt" | 6 | "fmt" |
7 | + "github.com/aarongao/tools" | ||
6 | "github.com/pkg/errors" | 8 | "github.com/pkg/errors" |
7 | - "gopkg.in/mgo.v2" | ||
8 | - "gopkg.in/mgo.v2/bson" | 9 | + "go.mongodb.org/mongo-driver/bson/primitive" |
10 | + "go.mongodb.org/mongo-driver/mongo" | ||
11 | + "go.mongodb.org/mongo-driver/bson" | ||
9 | "io/ioutil" | 12 | "io/ioutil" |
10 | "net/http" | 13 | "net/http" |
11 | "strconv" | 14 | "strconv" |
@@ -14,12 +17,12 @@ import ( | @@ -14,12 +17,12 @@ import ( | ||
14 | ) | 17 | ) |
15 | 18 | ||
16 | // 延迟消息 | 19 | // 延迟消息 |
17 | -var CDelayMessage *mgo.Collection | ||
18 | -var CDelayErrorLog *mgo.Collection | 20 | +var CDelayMessage *mongo.Collection |
21 | +var CDelayErrorLog *mongo.Collection | ||
19 | var GlobalDM *DelayMessage | 22 | var GlobalDM *DelayMessage |
20 | 23 | ||
21 | type Message struct { | 24 | type Message struct { |
22 | - Id *bson.ObjectId `bson:"_id" json:"_id"` | 25 | + Id *primitive.ObjectID `bson:"_id" json:"_id"` |
23 | //延时时间 | 26 | //延时时间 |
24 | DelayTime int64 | 27 | DelayTime int64 |
25 | //callbackUrl | 28 | //callbackUrl |
@@ -50,10 +53,11 @@ func (dm *DelayMessage) AddTaskForGetUrl(delayTime string, userid string, callba | @@ -50,10 +53,11 @@ func (dm *DelayMessage) AddTaskForGetUrl(delayTime string, userid string, callba | ||
50 | return errors.New("callbackUrl error...") | 53 | return errors.New("callbackUrl error...") |
51 | } | 54 | } |
52 | 55 | ||
53 | - objectID := bson.NewObjectId() | 56 | + objectID := primitive.NewObjectID() |
54 | _Message := &Message{&objectID, i64Time, callbackUrl, 0, 0, "", "", "", userid} | 57 | _Message := &Message{&objectID, i64Time, callbackUrl, 0, 0, "", "", "", userid} |
55 | 58 | ||
56 | - CDelayMessage.Insert(_Message) | 59 | + ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) |
60 | + CDelayMessage.InsertOne(ctx, _Message) | ||
57 | 61 | ||
58 | //添加任务 | 62 | //添加任务 |
59 | //iDelayTIme = 3 | 63 | //iDelayTIme = 3 |
@@ -87,10 +91,10 @@ func (dm *DelayMessage) AddTaskForAppMessage(delayTime string, udid string, titl | @@ -87,10 +91,10 @@ func (dm *DelayMessage) AddTaskForAppMessage(delayTime string, udid string, titl | ||
87 | return errors.New("userid error...") | 91 | return errors.New("userid error...") |
88 | } | 92 | } |
89 | 93 | ||
90 | - objectID := bson.NewObjectId() | 94 | + objectID := primitive.NewObjectID() |
91 | _Message := &Message{&objectID, i64Time, "", 0, 1, title, content, udid, userid} | 95 | _Message := &Message{&objectID, i64Time, "", 0, 1, title, content, udid, userid} |
92 | 96 | ||
93 | - CDelayMessage.Insert(_Message) | 97 | + CDelayMessage.InsertOne(tools.GetContext(), _Message) |
94 | 98 | ||
95 | //添加任务 | 99 | //添加任务 |
96 | //iDelayTIme = 3 | 100 | //iDelayTIme = 3 |
@@ -110,9 +114,10 @@ func (dm *DelayMessage) DelTaskForId(id string) { | @@ -110,9 +114,10 @@ func (dm *DelayMessage) DelTaskForId(id string) { | ||
110 | } | 114 | } |
111 | }() | 115 | }() |
112 | 116 | ||
113 | - CDelayMessage.Remove(bson.M{"_id": bson.ObjectIdHex(id)}) | 117 | + objID, _ := primitive.ObjectIDFromHex(id) |
118 | + CDelayMessage.DeleteOne(tools.GetContext(), bson.M{"_id": objID}) | ||
114 | i := dm.DelTask(id) | 119 | i := dm.DelTask(id) |
115 | - println("删除定时任务:",strconv.Itoa(i)) | 120 | + println("删除定时任务:", strconv.Itoa(i)) |
116 | } | 121 | } |
117 | 122 | ||
118 | func (dm *DelayMessage) Show() { | 123 | func (dm *DelayMessage) Show() { |
@@ -129,7 +134,7 @@ func (dm *DelayMessage) Show() { | @@ -129,7 +134,7 @@ func (dm *DelayMessage) Show() { | ||
129 | 134 | ||
130 | } | 135 | } |
131 | 136 | ||
132 | -func Callback(key *bson.ObjectId, message *Message) { | 137 | +func Callback(key *primitive.ObjectID, message *Message) { |
133 | 138 | ||
134 | var body string | 139 | var body string |
135 | var err error | 140 | var err error |
@@ -156,7 +161,7 @@ func Callback(key *bson.ObjectId, message *Message) { | @@ -156,7 +161,7 @@ func Callback(key *bson.ObjectId, message *Message) { | ||
156 | 161 | ||
157 | json, _ := json.Marshal(message) | 162 | json, _ := json.Marshal(message) |
158 | if body != "ok" { | 163 | if body != "ok" { |
159 | - CDelayMessage.Remove(bson.M{"_id": *key}) | 164 | + CDelayMessage.DeleteOne(tools.GetContext(), bson.M{"_id": *key}) |
160 | 165 | ||
161 | fmt.Println("完成任务:", string(json)) | 166 | fmt.Println("完成任务:", string(json)) |
162 | } else { | 167 | } else { |
@@ -164,7 +169,7 @@ func Callback(key *bson.ObjectId, message *Message) { | @@ -164,7 +169,7 @@ func Callback(key *bson.ObjectId, message *Message) { | ||
164 | //if message.Fail == 3 { | 169 | //if message.Fail == 3 { |
165 | // fmt.Println(color.Red("放弃任务:"), message.CallbackUrl) | 170 | // fmt.Println(color.Red("放弃任务:"), message.CallbackUrl) |
166 | // CDelayMessage.Remove(bson.M{"_id": *key}) | 171 | // CDelayMessage.Remove(bson.M{"_id": *key}) |
167 | - // dbErrorLog.Insert(message) | 172 | + // dbErrorLog.InsertOne(tools.GetContext(),message) |
168 | //} else { | 173 | //} else { |
169 | // fmt.Println("重新添加任务:", message) | 174 | // fmt.Println("重新添加任务:", message) |
170 | // dm.AddTask(time.Now().Add(time.Second*10), key, callback, message) | 175 | // dm.AddTask(time.Now().Add(time.Second*10), key, callback, message) |
@@ -172,7 +177,7 @@ func Callback(key *bson.ObjectId, message *Message) { | @@ -172,7 +177,7 @@ func Callback(key *bson.ObjectId, message *Message) { | ||
172 | 177 | ||
173 | fmt.Println("放弃任务:", string(json)) | 178 | fmt.Println("放弃任务:", string(json)) |
174 | //CDelayMessage.Remove(bson.M{"_id": *key}) | 179 | //CDelayMessage.Remove(bson.M{"_id": *key}) |
175 | - CDelayErrorLog.Insert(message) | 180 | + CDelayErrorLog.InsertOne(tools.GetContext(), message) |
176 | } | 181 | } |
177 | 182 | ||
178 | } | 183 | } |
@@ -182,13 +187,13 @@ type DelayMessage struct { | @@ -182,13 +187,13 @@ type DelayMessage struct { | ||
182 | curIndex int | 187 | curIndex int |
183 | //环形槽 | 188 | //环形槽 |
184 | sync.RWMutex | 189 | sync.RWMutex |
185 | - slots [3600]map[*bson.ObjectId]*Task | 190 | + slots [3600]map[*primitive.ObjectID]*Task |
186 | //启动时间 | 191 | //启动时间 |
187 | startTime time.Time | 192 | startTime time.Time |
188 | } | 193 | } |
189 | 194 | ||
190 | //执行的任务函数 | 195 | //执行的任务函数 |
191 | -type TaskFunc func(key *bson.ObjectId, message *Message) | 196 | +type TaskFunc func(key *primitive.ObjectID, message *Message) |
192 | 197 | ||
193 | //任务 | 198 | //任务 |
194 | type Task struct { | 199 | type Task struct { |
@@ -206,7 +211,7 @@ func NewDelayMessage() *DelayMessage { | @@ -206,7 +211,7 @@ func NewDelayMessage() *DelayMessage { | ||
206 | startTime: time.Now(), | 211 | startTime: time.Now(), |
207 | } | 212 | } |
208 | for i := 0; i < 3600; i++ { | 213 | for i := 0; i < 3600; i++ { |
209 | - dm.slots[i] = make(map[*bson.ObjectId]*Task) | 214 | + dm.slots[i] = make(map[*primitive.ObjectID]*Task) |
210 | } | 215 | } |
211 | return dm | 216 | return dm |
212 | } | 217 | } |
main.go
@@ -3,14 +3,17 @@ package main | @@ -3,14 +3,17 @@ package main | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | "github.com/aarongao/tools" | 5 | "github.com/aarongao/tools" |
6 | + "github.com/davecgh/go-spew/spew" | ||
6 | "github.com/gin-gonic/gin" | 7 | "github.com/gin-gonic/gin" |
7 | - "gopkg.in/mgo.v2" | ||
8 | - "gopkg.in/mgo.v2/bson" | 8 | + "go.mongodb.org/mongo-driver/bson" |
9 | + "go.mongodb.org/mongo-driver/mongo" | ||
10 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
9 | "letu/Api" | 11 | "letu/Api" |
10 | "letu/Config" | 12 | "letu/Config" |
11 | "letu/DB" | 13 | "letu/DB" |
12 | "letu/Lib/Cache" | 14 | "letu/Lib/Cache" |
13 | "letu/Lib/DelayMessage" | 15 | "letu/Lib/DelayMessage" |
16 | + "log" | ||
14 | "os" | 17 | "os" |
15 | "time" | 18 | "time" |
16 | ) | 19 | ) |
@@ -22,7 +25,6 @@ func main() { | @@ -22,7 +25,6 @@ func main() { | ||
22 | 25 | ||
23 | // 读取配置文件 | 26 | // 读取配置文件 |
24 | dir, _ := os.Getwd() | 27 | dir, _ := os.Getwd() |
25 | - //println(dir) | ||
26 | file, _ := os.Open(dir + "/Config/config.json") | 28 | file, _ := os.Open(dir + "/Config/config.json") |
27 | defer file.Close() | 29 | defer file.Close() |
28 | decoder := json.NewDecoder(file) | 30 | decoder := json.NewDecoder(file) |
@@ -31,49 +33,57 @@ func main() { | @@ -31,49 +33,57 @@ func main() { | ||
31 | tools.CheckError(err) | 33 | tools.CheckError(err) |
32 | 34 | ||
33 | // 连接数据库 | 35 | // 连接数据库 |
34 | - DB.DBSession, err = mgo.Dial(conf.DbPath) | 36 | + // Set client options |
37 | + clientOptions := options.Client().ApplyURI("mongodb://" + conf.DbPath) | ||
38 | + clientOptions.SetLocalThreshold(3 * time.Second) //只使用与mongo操作耗时小于3秒的 | ||
39 | + clientOptions.SetMaxConnIdleTime(5 * time.Second) //指定连接可以保持空闲的最大毫秒数 | ||
40 | + clientOptions.SetMaxPoolSize(4096) //使用最大的连接数 | ||
41 | + | ||
42 | + // Connect to MongoDB | ||
43 | + client, err := mongo.Connect(tools.GetContext(), clientOptions) | ||
44 | + if err != nil { | ||
45 | + log.Fatal(err) | ||
46 | + } | ||
47 | + | ||
48 | + // Check the connection | ||
49 | + err = client.Ping(tools.GetContext(), nil) | ||
50 | + if err != nil { | ||
51 | + log.Fatal(err) | ||
52 | + } | ||
53 | + log.Println("Connected to MongoDB!") | ||
35 | 54 | ||
36 | - defer DB.DBSession.Close() | 55 | + //获取文档集 |
56 | + DB.DB = client.Database("LeYouTu") | ||
57 | + //DB.DB.Login(conf.DbUser, conf.DbPassword) | ||
58 | + | ||
59 | + DB.CItem = DB.DB.Collection("Item") | ||
60 | + DB.CComplaint = DB.DB.Collection("Complaint") | ||
61 | + DB.CInvestigation = DB.DB.Collection("Investigation") | ||
62 | + DB.CMember = DB.DB.Collection("Member") | ||
63 | + DB.CCommodity = DB.DB.Collection("Commodity") | ||
64 | + DB.CTags = DB.DB.Collection("Tags") | ||
65 | + DB.CScenic = DB.DB.Collection("Scenic") | ||
66 | + DB.CLine = DB.DB.Collection("Line") | ||
67 | + DB.CUserLog = DB.DB.Collection("UserLog") | ||
68 | + DB.CSystemLog = DB.DB.Collection("SystemLog") | ||
69 | + DB.CInvestigation = DB.DB.Collection("Investigation") | ||
70 | + DB.CTrajectory = DB.DB.Collection("Trajectory") | ||
71 | + DB.CIcons = DB.DB.Collection("Icons") | ||
72 | + DB.CTopMenus = DB.DB.Collection("TopMenus") | ||
73 | + DB.CDevice = DB.DB.Collection("Device") | ||
74 | + DelayMessage.CDelayMessage = DB.DB.Collection("DelayMessage") | ||
75 | + DelayMessage.CDelayErrorLog = DB.DB.Collection("DelayErrorLog") | ||
37 | 76 | ||
38 | // 连接redis | 77 | // 连接redis |
39 | DB.Redis = Cache.NewRedis(&Cache.RedisOpts{ | 78 | DB.Redis = Cache.NewRedis(&Cache.RedisOpts{ |
40 | conf.RedisPath, | 79 | conf.RedisPath, |
41 | "", | 80 | "", |
42 | 0, | 81 | 0, |
43 | - 20, | 82 | + 200, |
44 | 20, | 83 | 20, |
45 | 0, | 84 | 0, |
46 | }) | 85 | }) |
47 | 86 | ||
48 | - //设置模式 | ||
49 | - DB.DBSession.SetMode(mgo.Monotonic, true) | ||
50 | - //获取文档集 | ||
51 | - DB.DB = DB.DBSession.DB(conf.DbName) | ||
52 | - DB.DB.Login(conf.DbUser, conf.DbPassword) | ||
53 | - | ||
54 | - DB.CItem = DB.DB.C("Item") | ||
55 | - DB.CComplaint = DB.DB.C("Complaint") | ||
56 | - DB.CInvestigation = DB.DB.C("Investigation") | ||
57 | - DB.CMember = DB.DB.C("Member") | ||
58 | - DB.CCommodity = DB.DB.C("Commodity") | ||
59 | - DB.CTags = DB.DB.C("Tags") | ||
60 | - DB.CScenic = DB.DB.C("Scenic") | ||
61 | - DB.CLine = DB.DB.C("Line") | ||
62 | - DB.CUserLog = DB.DB.C("UserLog") | ||
63 | - DB.CSystemLog = DB.DB.C("SystemLog") | ||
64 | - DB.CInvestigation = DB.DB.C("Investigation") | ||
65 | - DB.CTrajectory = DB.DB.C("Trajectory") | ||
66 | - DB.CIcons = DB.DB.C("Icons") | ||
67 | - DB.CTopMenus = DB.DB.C("TopMenus") | ||
68 | - DB.CDevice = DB.DB.C("Device") | ||
69 | - DelayMessage.CDelayMessage = DB.DB.C("DelayMessage") | ||
70 | - DelayMessage.CDelayErrorLog = DB.DB.C("DelayErrorLog") | ||
71 | - | ||
72 | - | ||
73 | - // 设置接口地址 | ||
74 | - //controllers := LeYouTu.Controllers{} | ||
75 | - //controllers.SetLayout(Api.Layout) | ||
76 | - | ||
77 | r := gin.Default() | 87 | r := gin.Default() |
78 | //r.Static("/.well-known", "./.well-known/") | 88 | //r.Static("/.well-known", "./.well-known/") |
79 | r.GET("/AllItems", Api.AllItems) | 89 | r.GET("/AllItems", Api.AllItems) |
@@ -103,6 +113,7 @@ func main() { | @@ -103,6 +113,7 @@ func main() { | ||
103 | r.POST("/UpdateItemTime", Api.UpdateItemTime) | 113 | r.POST("/UpdateItemTime", Api.UpdateItemTime) |
104 | r.GET("/AllScenic", Api.AllScenic) | 114 | r.GET("/AllScenic", Api.AllScenic) |
105 | r.POST("/UserLog", Api.UserLog) | 115 | r.POST("/UserLog", Api.UserLog) |
116 | + r.GET("/AllUserLog", Api.AllUserLog) | ||
106 | r.POST("/Sms/Send", Api.Send) | 117 | r.POST("/Sms/Send", Api.Send) |
107 | r.POST("/Investigation/Save", Api.SaveInvestigation) | 118 | r.POST("/Investigation/Save", Api.SaveInvestigation) |
108 | r.GET("/Investigation/List", Api.AllInvestigation) | 119 | r.GET("/Investigation/List", Api.AllInvestigation) |
@@ -118,13 +129,13 @@ func main() { | @@ -118,13 +129,13 @@ func main() { | ||
118 | r.POST("/TopMenus/Update", Api.UpdateTopMenus) | 129 | r.POST("/TopMenus/Update", Api.UpdateTopMenus) |
119 | r.GET("/TopMenus/All", Api.AllTopMenus) | 130 | r.GET("/TopMenus/All", Api.AllTopMenus) |
120 | r.POST("/RegisterDevice", Api.RegisterDevice) | 131 | r.POST("/RegisterDevice", Api.RegisterDevice) |
132 | + r.POST("/RemoveUser", Api.RemoveUser) | ||
121 | //r.GET("/ws", Api.WsPage) | 133 | //r.GET("/ws", Api.WsPage) |
122 | 134 | ||
123 | r.Static("/Upload", "./Upload") | 135 | r.Static("/Upload", "./Upload") |
124 | r.Static("/Console", "./Console") | 136 | r.Static("/Console", "./Console") |
125 | r.Static("/Policy", dir+"/Policy") | 137 | r.Static("/Policy", dir+"/Policy") |
126 | 138 | ||
127 | - | ||
128 | r.GET("MP_verify_R9xuhLXYcVbdDDNk.txt", func(c *gin.Context) { | 139 | r.GET("MP_verify_R9xuhLXYcVbdDDNk.txt", func(c *gin.Context) { |
129 | c.String(200, "R9xuhLXYcVbdDDNk") | 140 | c.String(200, "R9xuhLXYcVbdDDNk") |
130 | }) | 141 | }) |
@@ -139,18 +150,25 @@ func main() { | @@ -139,18 +150,25 @@ func main() { | ||
139 | }() | 150 | }() |
140 | 151 | ||
141 | // -初始化数据 | 152 | // -初始化数据 |
142 | - var aMessage []DelayMessage.Message | ||
143 | - DelayMessage.CDelayMessage.Find(bson.M{}).All(&aMessage) | ||
144 | - nowTimeU := time.Now().Unix() | ||
145 | - for i := 0; i < len(aMessage); i++ { | ||
146 | - iDelayTIme := aMessage[i].DelayTime - nowTimeU | ||
147 | - | ||
148 | - if iDelayTIme < 0 { | ||
149 | - iDelayTIme = 1 | 153 | + if cur, err := DelayMessage.CDelayMessage.Find(tools.GetContext(), bson.M{}); err == nil { |
154 | + defer cur.Close(tools.GetContext()) | ||
155 | + for cur.Next(tools.GetContext()) { | ||
156 | + var message DelayMessage.Message | ||
157 | + err := cur.Decode(&message) | ||
158 | + tools.CheckError(err) | ||
159 | + | ||
160 | + nowTimeU := time.Now().Unix() | ||
161 | + iDelayTIme := message.DelayTime - nowTimeU | ||
162 | + | ||
163 | + if iDelayTIme < 0 { | ||
164 | + iDelayTIme = 1 | ||
165 | + } | ||
166 | + DelayMessage.GlobalDM.AddTask(time.Now().Add(time.Second*time.Duration(iDelayTIme)), DelayMessage.Callback, &message) | ||
167 | + log.Println("增加提醒任务", message) | ||
150 | } | 168 | } |
151 | - DelayMessage.GlobalDM.AddTask(time.Now().Add(time.Second*time.Duration(iDelayTIme)), DelayMessage.Callback, &aMessage[i]) | 169 | + } else { |
170 | + spew.Dump(err) | ||
152 | } | 171 | } |
153 | - println("增加", len(aMessage), "条提醒任务") | ||
154 | 172 | ||
155 | r.Run(":8080") | 173 | r.Run(":8080") |
156 | } | 174 | } |
@@ -0,0 +1,83 @@ | @@ -0,0 +1,83 @@ | ||
1 | +package main | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "fmt" | ||
6 | + "github.com/gin-gonic/gin" | ||
7 | + "go.mongodb.org/mongo-driver/mongo" | ||
8 | + "go.mongodb.org/mongo-driver/mongo/options" | ||
9 | + "log" | ||
10 | + "time" | ||
11 | +) | ||
12 | + | ||
13 | +var ( | ||
14 | + client *mongo.Client | ||
15 | + err error | ||
16 | + result *mongo.InsertOneResult | ||
17 | + collection *mongo.Collection | ||
18 | +) | ||
19 | + | ||
20 | +// @APIVersion 1.0.0 | ||
21 | +// @APITitle 乐游图后端接口文档 | ||
22 | +// @BasePath 正式 leyoutu.st-i.com.cn; 测试 letu.api.imagchina.com | ||
23 | +func main() { | ||
24 | + | ||
25 | + // Set client options | ||
26 | + clientOptions := options.Client().ApplyURI("mongodb://localhost:27017") | ||
27 | + clientOptions.SetLocalThreshold(3 * time.Second) //只使用与mongo操作耗时小于3秒的 | ||
28 | + clientOptions.SetMaxConnIdleTime(5 * time.Second) //指定连接可以保持空闲的最大毫秒数 | ||
29 | + clientOptions.SetMaxPoolSize(4096) //使用最大的连接数 | ||
30 | + | ||
31 | + // Connect to MongoDB | ||
32 | + client, err = mongo.Connect(context.TODO(), clientOptions) | ||
33 | + | ||
34 | + if err != nil { | ||
35 | + log.Fatal(err) | ||
36 | + } | ||
37 | + | ||
38 | + // Check the connection | ||
39 | + err = client.Ping(context.TODO(), nil) | ||
40 | + | ||
41 | + if err != nil { | ||
42 | + log.Fatal(err) | ||
43 | + } | ||
44 | + | ||
45 | + fmt.Println("Connected to MongoDB!") | ||
46 | + | ||
47 | + collection = client.Database("LeYouTu").Collection("LogRecord") | ||
48 | + | ||
49 | + r := gin.Default() | ||
50 | + r.GET("/AllScenic", func(c *gin.Context) { | ||
51 | + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) | ||
52 | + c.Header("Access-Control-Allow-Credentials", "true") | ||
53 | + | ||
54 | + //var aItems = DB.SItem{} | ||
55 | + //if err = collection.FindOne(context.TODO(), bson.D{{}}).Decode(&aItems); err != nil { | ||
56 | + // println(err) | ||
57 | + //} | ||
58 | + | ||
59 | + record := &LogRecord{ | ||
60 | + JobName: "job10", | ||
61 | + Command: "echo hello", | ||
62 | + Err: "", | ||
63 | + Content: "hello", | ||
64 | + } | ||
65 | + | ||
66 | + if result, err = collection.InsertOne(context.TODO(), record); err != nil { | ||
67 | + fmt.Println(err) | ||
68 | + return | ||
69 | + } | ||
70 | + | ||
71 | + c.JSON(200, "ok") | ||
72 | + | ||
73 | + }) | ||
74 | + | ||
75 | + r.Run(":8080") | ||
76 | +} | ||
77 | + | ||
78 | +type LogRecord struct { | ||
79 | + JobName string `bson:"jobName"` // 任务名 | ||
80 | + Command string `bson:"command"` // shell命令 | ||
81 | + Err string `bson:"err"` // 脚本错误 | ||
82 | + Content string `bson:"content"` // 脚本输出 | ||
83 | +} |