Commit c1e5407407cdb8467f1e261b67e8755c9ac57388

Authored by aarongao
1 parent 2f326a5c
Exists in v1.2 and in 2 other branches master, v1.1

替换mongodb驱动

.gitignore
... ... @@ -2,3 +2,4 @@ main
2 2 tiles
3 3 Upload
4 4 Console
  5 +Bin/Monitor
... ...
API/Complaint.go
... ... @@ -4,11 +4,13 @@ import (
4 4 "encoding/json"
5 5 "github.com/aarongao/tools"
6 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 9 "letu/DB"
9 10 "math"
10 11 "regexp"
11 12 "strconv"
  13 + "time"
12 14 )
13 15  
14 16 // @Title 增加投诉
... ... @@ -27,10 +29,8 @@ import (
27 29 // @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
28 30 // @Router /CreateComplaint? [post]
29 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 35 reg := regexp.MustCompile(Regular)
36 36 if !reg.MatchString(c.PostForm("Mobile")) {
... ... @@ -42,7 +42,7 @@ func CreateComplaint(c *gin.Context) {
42 42 return
43 43 }
44 44  
45   - if c.PostForm("Mobile") == ""{
  45 + if c.PostForm("Mobile") == "" {
46 46 c.JSON(200, tools.ResponseError{
47 47 1,
48 48 "手机号为空",
... ... @@ -50,9 +50,8 @@ func CreateComplaint(c *gin.Context) {
50 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 55 if cacheCode != c.PostForm("Code") {
57 56  
58 57 c.JSON(200, tools.ResponseError{
... ... @@ -67,7 +66,7 @@ func CreateComplaint(c *gin.Context) {
67 66  
68 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 70 c.PostForm("Type"),
72 71 c.PostForm("ScenicId"),
73 72 c.PostForm("Mobile"),
... ... @@ -75,6 +74,8 @@ func CreateComplaint(c *gin.Context) {
75 74 c.PostForm("Sex"),
76 75 c.PostForm("Content"),
77 76 images,
  77 + "",
  78 + time.Now().Unix(),
78 79 })
79 80  
80 81 c.JSON(200, tools.ResponseSeccess{
... ... @@ -82,13 +83,8 @@ func CreateComplaint(c *gin.Context) {
82 83 "ok",
83 84 })
84 85  
85   -
86 86 }
87 87  
88   -
89   -
90   -
91   -
92 88 // @Title 查询所有投诉
93 89 // @Description 投诉 - 查询所有投诉
94 90 // @Accept json
... ... @@ -101,19 +97,28 @@ func AllComplaint(c *gin.Context) {
101 97 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
102 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 102 if limit == 0 {
107 103 limit = 50
108 104 }
109   - currPage, _ := strconv.Atoi(c.Query("Page"))
  105 + currPage, _ := strconv.ParseInt(c.Query("Page"),10,64)
110 106 if currPage == 0 {
111 107 currPage = 1
112 108 }
113 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 123 c.JSON(200, tools.Page{
119 124 0,
... ... @@ -124,4 +129,4 @@ func AllComplaint(c *gin.Context) {
124 129 aComplaint,
125 130 })
126 131  
127   -}
128 132 \ No newline at end of file
  133 +}
... ...
API/DealyMessage.go
... ... @@ -3,7 +3,8 @@ package Api
3 3 import (
4 4 "github.com/aarongao/tools"
5 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 8 "letu/Lib/DelayMessage"
8 9 "letu/Lib/Token"
9 10 )
... ... @@ -21,7 +22,8 @@ func DealyMessageInfo(c *gin.Context) {
21 22 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
22 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 27 c.JSON(200, tools.ResponseError{
26 28 1,
27 29 "Token或者用户id不正确",
... ... @@ -38,7 +40,15 @@ func DealyMessageInfo(c *gin.Context) {
38 40 }
39 41  
40 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 53 if aDelayMessage == nil {
44 54 aDelayMessage = []DelayMessage.Message{}
... ... @@ -66,7 +76,8 @@ func CreateDealyMessage(c *gin.Context) {
66 76 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
67 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 81 c.JSON(200, tools.ResponseError{
71 82 1,
72 83 "Token或者用户id不正确",
... ... @@ -82,7 +93,7 @@ func CreateDealyMessage(c *gin.Context) {
82 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 98 if err == nil {
88 99  
... ... @@ -114,7 +125,9 @@ func RemoveDealyMessage(c *gin.Context) {
114 125 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
115 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 131 c.JSON(200, tools.ResponseError{
119 132 1,
120 133 "Token或者用户id不正确",
... ...
API/Icon.go
... ... @@ -3,7 +3,9 @@ package Api
3 3 import (
4 4 "github.com/aarongao/tools"
5 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 9 "letu/DB"
8 10 )
9 11  
... ... @@ -28,7 +30,8 @@ func IconInfo(c *gin.Context) {
28 30 }
29 31  
30 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 36 c.JSON(200, tools.ResponseSeccess{
34 37 0,
... ... @@ -58,20 +61,23 @@ func UpdateIcon(c *gin.Context) {
58 61 return
59 62 }
60 63  
61   - var id bson.ObjectId
  64 + var id primitive.ObjectID
62 65 if pid := c.PostForm("id"); pid == "null" {
63   - id = bson.NewObjectId()
  66 + id = primitive.NewObjectID()
64 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 74 bson.M{"$set": bson.M{
71 75 "Name": c.PostForm("Name"),
72 76 "Picture": c.PostForm("Picture"),
73 77 "ScenicId": ScenicId,
74   - }},
  78 + }}, &options.FindOneAndUpdateOptions{
  79 + Upsert: &upsert,
  80 + },
75 81 )
76 82  
77 83 c.JSON(200, tools.ResponseSeccess{
... ... @@ -102,8 +108,17 @@ func AllIcons(c *gin.Context) {
102 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 123 c.JSON(200, tools.ResponseSeccess{
109 124 0,
... ...
API/Investigation.go
... ... @@ -4,7 +4,8 @@ import (
4 4 "encoding/json"
5 5 "github.com/aarongao/tools"
6 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 9 "letu/DB"
9 10 "math"
10 11 "strconv"
... ... @@ -27,7 +28,7 @@ func SaveInvestigation(c *gin.Context) {
27 28 var Data map[string]interface{}
28 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 32 c.PostForm("UserId"),
32 33 c.PostForm("Mobile"),
33 34 Data,
... ... @@ -53,20 +54,28 @@ func AllInvestigation(c *gin.Context) {
53 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 59 if limit == 0 {
59 60 limit = 50
60 61 }
61   - currPage, _ := strconv.Atoi(c.Query("Page"))
  62 + currPage, _ := strconv.ParseInt(c.Query("Page"),10,64)
62 63 if currPage == 0 {
63 64 currPage = 1
64 65 }
65 66 skip := (currPage - 1) * limit
66 67  
67 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 79 c.JSON(200, tools.Page{
71 80 0,
72 81 total,
... ...
API/Item.go
... ... @@ -4,7 +4,9 @@ import (
4 4 "encoding/json"
5 5 "github.com/aarongao/tools"
6 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 10 "letu/DB"
9 11 "strconv"
10 12 "time"
... ... @@ -31,7 +33,8 @@ func ItemInfo(c *gin.Context) {
31 33 }
32 34  
33 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 39 c.JSON(200, tools.ResponseSeccess{
37 40 0,
... ... @@ -52,7 +55,15 @@ func AllItems(c *gin.Context) {
52 55 c.Header("Access-Control-Allow-Credentials", "true")
53 56  
54 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 68 c.JSON(200, aItems)
58 69  
... ... @@ -78,16 +89,18 @@ func UpdateItem(c *gin.Context) {
78 89 var Picture []string
79 90 json.Unmarshal([]byte(c.PostForm("Picture")), &Picture)
80 91  
81   - var id bson.ObjectId
  92 + var id primitive.ObjectID
82 93 if pid := c.PostForm("id"); pid == "null" {
83   - id = bson.NewObjectId()
  94 + id = primitive.NewObjectID()
84 95 } else {
85   - id = bson.ObjectIdHex(pid)
  96 + id,_ = primitive.ObjectIDFromHex(pid)
86 97 }
87 98  
88 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 104 bson.M{"$set": bson.M{
92 105 "Name": c.PostForm("Name"),
93 106 "SubName": c.PostForm("SubName"),
... ... @@ -106,7 +119,9 @@ func UpdateItem(c *gin.Context) {
106 119 "LocationDescription": c.PostForm("LocationDescription"),
107 120 "Reminder": c.PostForm("Reminder"),
108 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 4 "encoding/json"
5 5 "github.com/aarongao/tools"
6 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 10 "letu/DB"
9 11 )
10 12  
... ... @@ -29,7 +31,8 @@ func LineInfo(c *gin.Context) {
29 31 }
30 32  
31 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 37 c.JSON(200, tools.ResponseSeccess{
35 38 0,
... ... @@ -50,7 +53,15 @@ func AllLine(c *gin.Context) {
50 53 c.Header("Access-Control-Allow-Credentials", "true")
51 54  
52 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 66 c.JSON(200, aLine)
56 67  
... ... @@ -73,15 +84,16 @@ func UpdateLine(c *gin.Context) {
73 84 var Annotations []string
74 85 json.Unmarshal([]byte(c.PostForm("Annotations")), &Annotations)
75 86  
76   - var id bson.ObjectId
  87 + var id primitive.ObjectID
77 88 if pid := c.PostForm("id"); pid == "null" {
78   - id = bson.NewObjectId()
  89 + id = primitive.NewObjectID()
79 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 97 bson.M{"$set": bson.M{
86 98 "Name": c.PostForm("Name"),
87 99 "SubName": c.PostForm("SubName"),
... ... @@ -91,7 +103,9 @@ func UpdateLine(c *gin.Context) {
91 103 "Distance": c.PostForm("Distance"),
92 104 "Annotations": Annotations,
93 105 "Location": Location,
94   - }},
  106 + }}, &options.FindOneAndUpdateOptions{
  107 + Upsert: &upsert,
  108 + },
95 109 )
96 110  
97 111 c.JSON(200, tools.ResponseSeccess{
... ...
API/Scenic.go
... ... @@ -4,7 +4,9 @@ import (
4 4 "encoding/json"
5 5 "github.com/aarongao/tools"
6 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 10 "letu/DB"
9 11 )
10 12  
... ... @@ -29,7 +31,8 @@ func ScenicInfo(c *gin.Context) {
29 31 }
30 32  
31 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 37 c.JSON(200, tools.ResponseSeccess{
35 38 0,
... ... @@ -71,18 +74,19 @@ func UpdateScenic(c *gin.Context) {
71 74 var VideoList []DB.SVideo
72 75 json.Unmarshal([]byte(c.PostForm("VideoList")), &VideoList)
73 76  
74   - var id bson.ObjectId
  77 + var id primitive.ObjectID
75 78 if pid := c.PostForm("id"); pid == "null" {
76   - id = bson.NewObjectId()
77   -
  79 + id = primitive.NewObjectID()
78 80 // 新景区,初始化
79 81 initScenic(id.Hex())
80 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 90 bson.M{"$set": bson.M{
87 91 "Name": c.PostForm("Name"),
88 92 "Describe": c.PostForm("Describe"),
... ... @@ -97,7 +101,9 @@ func UpdateScenic(c *gin.Context) {
97 101 "ItemScenicPicture": ItemScenicPicture,
98 102 "ActivityPicture": ActivityPicture,
99 103 "VideoList": VideoList,
100   - }},
  104 + }}, &options.FindOneAndUpdateOptions{
  105 + Upsert: &upsert,
  106 + },
101 107 )
102 108  
103 109 c.JSON(200, tools.ResponseSeccess{
... ... @@ -109,23 +115,28 @@ func UpdateScenic(c *gin.Context) {
109 115  
110 116 func initScenic(id string) {
111 117  
112   - DB.CTags.Insert(DB.STag{
  118 + var dba []interface{}
  119 + dba = append(dba,DB.STag{
113 120 id,
114 121 "type",
115 122 "服务设施",
116   - }, DB.STag{
  123 + })
  124 + dba = append(dba,DB.STag{
117 125 id,
118 126 "type",
119 127 "游乐设施",
120   - }, DB.STag{
  128 + })
  129 + dba = append(dba,DB.STag{
121 130 id,
122 131 "type",
123 132 "餐饮",
124   - }, DB.STag{
  133 + })
  134 + dba = append(dba,DB.STag{
125 135 id,
126 136 "type",
127 137 "购物",
128 138 })
  139 + DB.CTags.InsertMany(tools.GetContext(),dba[1:])
129 140 }
130 141  
131 142 // @Title 所有景区基础信息
... ... @@ -139,11 +150,19 @@ func AllScenic(c *gin.Context) {
139 150 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
140 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 164 if Scenic == nil {
146   - Scenic = []*DB.SScenic{}
  165 + Scenic = []DB.SScenic{}
147 166 }
148 167  
149 168 c.JSON(200, tools.ResponseSeccess{
... ...
API/Shop.go
... ... @@ -4,7 +4,9 @@ import (
4 4 "encoding/json"
5 5 "github.com/aarongao/tools"
6 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 10 "letu/DB"
9 11 )
10 12  
... ... @@ -29,7 +31,8 @@ func CommodityInfo(c *gin.Context) {
29 31 }
30 32  
31 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 37 c.JSON(200, tools.ResponseSeccess{
35 38 0,
... ... @@ -50,7 +53,15 @@ func AllCommodity(c *gin.Context) {
50 53 c.Header("Access-Control-Allow-Credentials", "true")
51 54  
52 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 66 c.JSON(200, aCommoditys)
56 67  
... ... @@ -76,15 +87,17 @@ func UpdateCommodity(c *gin.Context) {
76 87 //var Location DB.SLocation
77 88 //json.Unmarshal([]byte(c.PostForm("Location")), &Location)
78 89  
79   - var id bson.ObjectId
  90 +
  91 + var id primitive.ObjectID
80 92 if pid := c.PostForm("id"); pid == "null" {
81   - id = bson.NewObjectId()
  93 + id = primitive.NewObjectID()
82 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 101 bson.M{"$set": bson.M{
89 102 "Name": c.PostForm("Name"),
90 103 "Price": c.PostForm("Price"),
... ... @@ -93,7 +106,9 @@ func UpdateCommodity(c *gin.Context) {
93 106 "TopPhoto": TopPhoto,
94 107 "ItemId": c.PostForm("ItemId"),
95 108 "Images": Picture,
96   - }},
  109 + }}, &options.FindOneAndUpdateOptions{
  110 + Upsert: &upsert,
  111 + },
97 112 )
98 113  
99 114 c.JSON(200, tools.ResponseSeccess{
... ...
API/Sms.go
... ... @@ -75,7 +75,7 @@ func Send(c *gin.Context) {
75 75 json.Unmarshal([]byte(c.PostForm("Location")), &Location)
76 76  
77 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 81 c.PostForm("Mobile"),
... ...
API/Tag.go
... ... @@ -3,7 +3,7 @@ package Api
3 3 import (
4 4 "github.com/aarongao/tools"
5 5 "github.com/gin-gonic/gin"
6   - "gopkg.in/mgo.v2/bson"
  6 + "go.mongodb.org/mongo-driver/bson"
7 7 "letu/DB"
8 8 "letu/Lib/LeYouTu"
9 9 "time"
... ... @@ -26,11 +26,21 @@ func AllTag(c *gin.Context) {
26 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 42 if Stags == nil {
33   - Stags = []*DB.STag{}
  43 + Stags = []DB.STag{}
34 44 }
35 45  
36 46 c.JSON(200, tools.ResponseSeccess{
... ... @@ -67,11 +77,19 @@ func AllTagGroup(c *gin.Context) {
67 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 91 if Stags == nil {
74   - Stags = []*DB.STag{}
  92 + Stags = []DB.STag{}
75 93 }
76 94  
77 95 Group := make(map[string][]string)
... ... @@ -141,7 +159,7 @@ func CreateTag(c *gin.Context) {
141 159 return
142 160 }
143 161  
144   - DB.CTags.Insert(DB.STag{
  162 + DB.CTags.InsertOne(tools.GetContext(),DB.STag{
145 163 ScenicId,
146 164 c.PostForm("TagGroup"),
147 165 c.PostForm("TagName"),
... ... @@ -204,7 +222,7 @@ func RemoveTag(c *gin.Context) {
204 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 227 DB.Redis.Delete("Tags_" + ScenicId)
210 228 println("清楚缓存Tags")
... ...
API/TopMenus.go
... ... @@ -4,7 +4,9 @@ import (
4 4 "encoding/json"
5 5 "github.com/aarongao/tools"
6 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 10 "letu/DB"
9 11 "letu/Lib/LeYouTu"
10 12 )
... ... @@ -25,11 +27,19 @@ func AllTopMenus(c *gin.Context) {
25 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 41 if STopMenus == nil {
32   - STopMenus = []*DB.STopMenus{}
  42 + STopMenus = []DB.STopMenus{}
33 43 }
34 44  
35 45 c.JSON(200, tools.ResponseSeccess{
... ... @@ -66,20 +76,23 @@ func UpdateTopMenus(c *gin.Context) {
66 76 var Tags []string
67 77 json.Unmarshal([]byte(c.PostForm("Tags")), &Tags)
68 78  
69   - var id bson.ObjectId
  79 + var id primitive.ObjectID
70 80 if pid := c.PostForm("id"); pid == "null" {
71   - id = bson.NewObjectId()
  81 + id = primitive.NewObjectID()
72 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 89 bson.M{"$set": bson.M{
79 90 "ScenicId": ScenicId,
80 91 "Title": c.PostForm("Title"),
81 92 "Tags": Tags,
82   - }},
  93 + }}, &options.FindOneAndUpdateOptions{
  94 + Upsert: &upsert,
  95 + },
83 96 )
84 97  
85 98 c.JSON(200, tools.ResponseSeccess{
... ...
API/Trajectory.go
... ... @@ -24,7 +24,7 @@ func SaveTrajectory(c *gin.Context) {
24 24 var Location DB.SLocation
25 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 28 c.PostForm("UserId"),
29 29 Location,
30 30 time.Now().Unix(),
... ...
API/User.go
... ... @@ -5,7 +5,9 @@ import (
5 5 "encoding/hex"
6 6 "github.com/aarongao/tools"
7 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 11 "letu/DB"
10 12 "letu/Lib/Token"
11 13 "regexp"
... ... @@ -63,11 +65,11 @@ func LoginUser(c *gin.Context) {
63 65 var User *DB.SMember
64 66 if cacheCode == c.PostForm("Code") {
65 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 71 if User == nil {
70   - objectID := bson.NewObjectId()
  72 + objectID := primitive.NewObjectID()
71 73 User := DB.SMember{
72 74 &objectID,
73 75 "",
... ... @@ -88,7 +90,7 @@ func LoginUser(c *gin.Context) {
88 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 96 } else {
... ... @@ -142,7 +144,8 @@ func RegisterDevice(c *gin.Context) {
142 144 return
143 145 }
144 146  
145   - DB.CDevice.Upsert(
  147 + upsert := true
  148 + DB.CDevice.FindOneAndUpdate(tools.GetContext(),
146 149 bson.M{"DeviceId":c.Request.Header.Get("DeviceId")},
147 150 bson.M{"$set": bson.M{
148 151 "Mac":c.Request.Header.Get("Mac"),
... ... @@ -152,7 +155,9 @@ func RegisterDevice(c *gin.Context) {
152 155 "SystemModel":c.Request.Header.Get("SystemModel"),
153 156 "AppVersion":c.Request.Header.Get("AppVersion"),
154 157 "DeviceToken":c.Request.Header.Get("DeviceToken"),
155   - }},
  158 + }}, &options.FindOneAndUpdateOptions{
  159 + Upsert: &upsert,
  160 + },
156 161 )
157 162  
158 163 c.JSON(200, tools.ResponseSeccess{
... ... @@ -175,7 +180,8 @@ func UserInfo(c *gin.Context) {
175 180 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
176 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 185 c.JSON(200, tools.ResponseError{
180 186 1,
181 187 "Token或者用户id不正确",
... ... @@ -192,7 +198,7 @@ func UserInfo(c *gin.Context) {
192 198 }
193 199  
194 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 203 User.Device = DB.SDevice{}
198 204 c.JSON(200, tools.ResponseSeccess{
... ... @@ -215,7 +221,8 @@ func CheckToken(c *gin.Context) {
215 221 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
216 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 226 c.JSON(200, tools.ResponseError{
220 227 1,
221 228 "Token或者用户id不正确",
... ... @@ -257,7 +264,8 @@ func UpdateUser(c *gin.Context) {
257 264 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
258 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 269 c.JSON(200, tools.ResponseError{
262 270 1,
263 271 "Token或者用户id不正确",
... ... @@ -309,8 +317,9 @@ func UpdateUser(c *gin.Context) {
309 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 323 bson.M{"$set": bson.M{
315 324 "Birthday": c.PostForm("Birthday"),
316 325 "FullName": c.PostForm("FullName"),
... ... @@ -321,7 +330,8 @@ func UpdateUser(c *gin.Context) {
321 330  
322 331 if err == nil {
323 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 336 c.JSON(200, tools.ResponseSeccess{
327 337 0,
... ... @@ -350,7 +360,8 @@ func RemoveUser(c *gin.Context) {
350 360 c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
351 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 365 c.JSON(200, tools.ResponseError{
355 366 1,
356 367 "Token或者用户id不正确",
... ... @@ -366,11 +377,10 @@ func RemoveUser(c *gin.Context) {
366 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 382 if err == nil {
373   -
  383 +
374 384 c.JSON(200, tools.ResponseSeccess{
375 385 0,
376 386 "ok",
... ...
API/UserLog.go
... ... @@ -4,8 +4,11 @@ import (
4 4 "encoding/json"
5 5 "github.com/aarongao/tools"
6 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 9 "letu/DB"
  10 + "math"
  11 + "strconv"
9 12 "time"
10 13 )
11 14  
... ... @@ -46,7 +49,7 @@ func UserLog(c *gin.Context) {
46 49 var Location DB.SLocation
47 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 53 c.PostForm("Type"),
51 54 c.PostForm("SubType"),
52 55 c.PostForm("ScenicId"),
... ... @@ -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 77 bson.M{"DeviceId": c.Request.Header.Get("DeviceId")},
74 78 bson.M{"$set": bson.M{
75 79 "Mac": c.Request.Header.Get("Mac"),
... ... @@ -79,7 +83,9 @@ func UserLog(c *gin.Context) {
79 83 "SystemModel": c.Request.Header.Get("SystemModel"),
80 84 "AppVersion": c.Request.Header.Get("AppVersion"),
81 85 "DeviceToken": c.Request.Header.Get("DeviceToken"),
82   - }},
  86 + }}, &options.FindOneAndUpdateOptions{
  87 + Upsert: &upsert,
  88 + },
83 89 )
84 90  
85 91 c.JSON(200, tools.ResponseSeccess{
... ... @@ -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 145 \ No newline at end of file
... ...
Bin/Monitor.go 0 → 100644
... ... @@ -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