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 +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 1 package DB
2 2  
3 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 6 "letu/Lib/Cache"
7 7 )
8 8  
9 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 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 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 54 type STrajectory struct {
56 55 UserId string `bson:"UserId" json:"UserId"` // 用户ID
... ... @@ -62,10 +61,10 @@ type SLocation struct {
62 61 Longitude float64 `bson:"Longitude" json:"Longitude"` //经度
63 62 }
64 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 70 type SDevice struct {
... ... @@ -104,25 +103,25 @@ type SSystemLog struct {
104 103 Error interface{} `bson:"Error" json:"Error"` //错误信息
105 104 }
106 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 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 127 type SComplaint struct {
... ... @@ -133,6 +132,8 @@ type SComplaint struct {
133 132 Sex string `bson:"Sex" json:"Sex"`
134 133 Content string `bson:"Content" json:"Content"`
135 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 139 type SInvestigation struct {
... ... @@ -141,15 +142,15 @@ type SInvestigation struct {
141 142 Data interface{} `bson:"Data" json:"Data"`
142 143 }
143 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 156 type STag struct {
... ... @@ -169,18 +170,18 @@ type SVideo struct {
169 170 Title string `bson:"Title" json:"Title"` // 标题
170 171 }
171 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 1 package DelayMessage
2 2  
3 3 import (
  4 + "context"
4 5 "encoding/json"
5 6 "fmt"
  7 + "github.com/aarongao/tools"
6 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 12 "io/ioutil"
10 13 "net/http"
11 14 "strconv"
... ... @@ -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 22 var GlobalDM *DelayMessage
20 23  
21 24 type Message struct {
22   - Id *bson.ObjectId `bson:"_id" json:"_id"`
  25 + Id *primitive.ObjectID `bson:"_id" json:"_id"`
23 26 //延时时间
24 27 DelayTime int64
25 28 //callbackUrl
... ... @@ -50,10 +53,11 @@ func (dm *DelayMessage) AddTaskForGetUrl(delayTime string, userid string, callba
50 53 return errors.New("callbackUrl error...")
51 54 }
52 55  
53   - objectID := bson.NewObjectId()
  56 + objectID := primitive.NewObjectID()
54 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 63 //iDelayTIme = 3
... ... @@ -87,10 +91,10 @@ func (dm *DelayMessage) AddTaskForAppMessage(delayTime string, udid string, titl
87 91 return errors.New("userid error...")
88 92 }
89 93  
90   - objectID := bson.NewObjectId()
  94 + objectID := primitive.NewObjectID()
91 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 100 //iDelayTIme = 3
... ... @@ -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 119 i := dm.DelTask(id)
115   - println("删除定时任务:",strconv.Itoa(i))
  120 + println("删除定时任务:", strconv.Itoa(i))
116 121 }
117 122  
118 123 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 139 var body string
135 140 var err error
... ... @@ -156,7 +161,7 @@ func Callback(key *bson.ObjectId, message *Message) {
156 161  
157 162 json, _ := json.Marshal(message)
158 163 if body != "ok" {
159   - CDelayMessage.Remove(bson.M{"_id": *key})
  164 + CDelayMessage.DeleteOne(tools.GetContext(), bson.M{"_id": *key})
160 165  
161 166 fmt.Println("完成任务:", string(json))
162 167 } else {
... ... @@ -164,7 +169,7 @@ func Callback(key *bson.ObjectId, message *Message) {
164 169 //if message.Fail == 3 {
165 170 // fmt.Println(color.Red("放弃任务:"), message.CallbackUrl)
166 171 // CDelayMessage.Remove(bson.M{"_id": *key})
167   - // dbErrorLog.Insert(message)
  172 + // dbErrorLog.InsertOne(tools.GetContext(),message)
168 173 //} else {
169 174 // fmt.Println("重新添加任务:", message)
170 175 // dm.AddTask(time.Now().Add(time.Second*10), key, callback, message)
... ... @@ -172,7 +177,7 @@ func Callback(key *bson.ObjectId, message *Message) {
172 177  
173 178 fmt.Println("放弃任务:", string(json))
174 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 187 curIndex int
183 188 //环形槽
184 189 sync.RWMutex
185   - slots [3600]map[*bson.ObjectId]*Task
  190 + slots [3600]map[*primitive.ObjectID]*Task
186 191 //启动时间
187 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 199 type Task struct {
... ... @@ -206,7 +211,7 @@ func NewDelayMessage() *DelayMessage {
206 211 startTime: time.Now(),
207 212 }
208 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 216 return dm
212 217 }
... ...
main.go
... ... @@ -3,14 +3,17 @@ package main
3 3 import (
4 4 "encoding/json"
5 5 "github.com/aarongao/tools"
  6 + "github.com/davecgh/go-spew/spew"
6 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 11 "letu/Api"
10 12 "letu/Config"
11 13 "letu/DB"
12 14 "letu/Lib/Cache"
13 15 "letu/Lib/DelayMessage"
  16 + "log"
14 17 "os"
15 18 "time"
16 19 )
... ... @@ -22,7 +25,6 @@ func main() {
22 25  
23 26 // 读取配置文件
24 27 dir, _ := os.Getwd()
25   - //println(dir)
26 28 file, _ := os.Open(dir + "/Config/config.json")
27 29 defer file.Close()
28 30 decoder := json.NewDecoder(file)
... ... @@ -31,49 +33,57 @@ func main() {
31 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 77 // 连接redis
39 78 DB.Redis = Cache.NewRedis(&Cache.RedisOpts{
40 79 conf.RedisPath,
41 80 "",
42 81 0,
43   - 20,
  82 + 200,
44 83 20,
45 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 87 r := gin.Default()
78 88 //r.Static("/.well-known", "./.well-known/")
79 89 r.GET("/AllItems", Api.AllItems)
... ... @@ -103,6 +113,7 @@ func main() {
103 113 r.POST("/UpdateItemTime", Api.UpdateItemTime)
104 114 r.GET("/AllScenic", Api.AllScenic)
105 115 r.POST("/UserLog", Api.UserLog)
  116 + r.GET("/AllUserLog", Api.AllUserLog)
106 117 r.POST("/Sms/Send", Api.Send)
107 118 r.POST("/Investigation/Save", Api.SaveInvestigation)
108 119 r.GET("/Investigation/List", Api.AllInvestigation)
... ... @@ -118,13 +129,13 @@ func main() {
118 129 r.POST("/TopMenus/Update", Api.UpdateTopMenus)
119 130 r.GET("/TopMenus/All", Api.AllTopMenus)
120 131 r.POST("/RegisterDevice", Api.RegisterDevice)
  132 + r.POST("/RemoveUser", Api.RemoveUser)
121 133 //r.GET("/ws", Api.WsPage)
122 134  
123 135 r.Static("/Upload", "./Upload")
124 136 r.Static("/Console", "./Console")
125 137 r.Static("/Policy", dir+"/Policy")
126 138  
127   -
128 139 r.GET("MP_verify_R9xuhLXYcVbdDDNk.txt", func(c *gin.Context) {
129 140 c.String(200, "R9xuhLXYcVbdDDNk")
130 141 })
... ... @@ -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 173 r.Run(":8080")
156 174 }
... ...
main2.go 0 → 100644
... ... @@ -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 +}
... ...