diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b41ac26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +main +tiles +upload +Console diff --git a/API/Complaint.go b/API/Complaint.go new file mode 100644 index 0000000..d15c386 --- /dev/null +++ b/API/Complaint.go @@ -0,0 +1,43 @@ +package Api + +import ( + "encoding/json" + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "letu/DB" +) + +// @Title 增加投诉 +// @Description 增加投诉 +// @Accept json +// @Produce json +// @Param mobile 18616619599 string true "联系电话" +// @Param type 1 string true "类型" +// @Param content 卫生不干净 string true "投诉内容" +// @Param image ["http://www.xx.com/123.jpg","http://www.xx.com/123.jpg"] string true "照片数组" +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /CreateComplaint? [post] +func CreateComplaint(c *gin.Context) { + c.Header("Access-Control-Allow-Origin",c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials","true") + + + var images []string + + json.Unmarshal([]byte(c.PostForm("image")), &images) + + DB.CComplaint.Insert(DB.SComplaint{ + c.PostForm("type"), + c.PostForm("mobile"), + c.PostForm("content"), + images, + }) + + c.JSON(200, tools.ResponseSeccess{ + 0, + "ok", + }) + + +} diff --git a/API/Item.go b/API/Item.go new file mode 100644 index 0000000..4b459b2 --- /dev/null +++ b/API/Item.go @@ -0,0 +1,157 @@ +package Api + +import ( + "encoding/json" + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "gopkg.in/mgo.v2/bson" + "letu/DB" +) + +// @Title 查询设备信息 +// @Description 查询设备信息 +// @Accept json +// @Produce json +// @Param id 5dfb03070a9ac17ac7a82054 string true "设备id" +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Name":"名称","Describe":"介绍","OpenHours":"开放时间","Mobile":"电话","Address":"地址","SLocation":{"Latitude":0,"Longitude":0},"Picture":["照片1","照片2"]}}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /ScenicInfo? [get] +func ItemInfo(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + if c.Query("id") == "" { + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + return + } + + var SItem *DB.SItem + DB.CItem.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SItem) + + if SItem == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + c.JSON(200, tools.ResponseSeccess{ + 0, + SItem, + }) + } + +} + +// @Title 查询所有游玩项目 +// @Description 查询所有游玩项目 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "Tags所属标签,标签有分类;LimitHeight限高;PlayDuration游玩时长;SceneTime场次时间;Picture照片;Voice音频;AverageConsumption平均消费;Menu菜单" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /AllItems? [get] +func AllItems(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var aItems []DB.SItem + DB.CItem.Find(bson.M{}).All(&aItems) + + c.JSON(200, aItems) + +} + +// @Title 更新设施 +// @Description 更新设施 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "Tags所属标签,标签有分类;LimitHeight限高;PlayDuration游玩时长;SceneTime场次时间;Picture照片;Voice音频;AverageConsumption平均消费;Menu菜单" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /UpdateItem? [post] +func UpdateItem(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var Location DB.SLocation + json.Unmarshal([]byte(c.PostForm("Location")), &Location) + + var Tags []DB.STag + json.Unmarshal([]byte(c.PostForm("Tags")), &Tags) + + var Picture []string + json.Unmarshal([]byte(c.PostForm("Picture")), &Picture) + + var id bson.ObjectId + if pid := c.PostForm("id"); pid == "null" { + id = bson.NewObjectId() + } else { + id = bson.ObjectIdHex(pid) + } + + DB.CItem.UpsertId( + id, + bson.M{"$set": bson.M{ + "Name": c.PostForm("Name"), + "SubName": c.PostForm("SubName"), + "Location": Location, + "Icon": c.PostForm("Icon"), + "LimitHeight": c.PostForm("LimitHeight"), + "PlayDuration": c.PostForm("PlayDuration"), + "SceneTime": c.PostForm("SceneTime"), + "Picture": Picture, + "Voice": c.PostForm("Voice"), + "Tel": c.PostForm("Tel"), + "AverageConsumption": c.PostForm("AverageConsumption"), + "Menu": c.PostForm("Menu"), + "Tags": Tags, + }}, + ) + + c.JSON(200, tools.ResponseSeccess{ + 0, + "ok", + }) + +} + +type ItemTime struct { + Id string `json:"id"` + Time string `json:"time"` +} + + +// @Title 更新等待时间 +// @Description 更新等待时间 +// @Accept json +// @Produce json +// @Param item [{"id":"5df864740a9ac17ac7a7feb8","time":"20"},{"id":"5df8660924e03417008b4567","time":"33"}] string true "设备列表" +// @Success 200 {object} tools.ResponseSeccess "{errcode: 0, result: "ok"}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /UpdateItemTime? [post] +func UpdateItemTime(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var ItemTime []ItemTime + json.Unmarshal([]byte(c.PostForm("items")), &ItemTime) + + for _, v := range ItemTime { + + DB.CItem.Update( + bson.M{"_id": bson.ObjectIdHex(v.Id)}, + bson.M{"$set": bson.M{ + "Time": v.Time, + }}, + ) + } + + c.JSON(200, tools.ResponseSeccess{ + 0, + "ok", + }) + +} diff --git a/API/Line.go b/API/Line.go new file mode 100644 index 0000000..ed354ca --- /dev/null +++ b/API/Line.go @@ -0,0 +1,110 @@ +package Api + +import ( + "encoding/json" + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "gopkg.in/mgo.v2/bson" + "letu/DB" +) + +// @Title 查询线路信息 +// @Description 查询线路信息 +// @Accept json +// @Produce json +// @Param id 5dfb03070a9ac17ac7a82054 string true "id" +// @Success 200 {object} tools.ResponseSeccess "Name名称;SubName副标题;PlayDuration游玩时长;Suitable适合人群;Location线路点坐标;Annotations需要点亮的设施id" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /LineInfo? [get] +func LineInfo(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + if c.Query("id") == "" { + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + return + } + + var SLine *DB.SLine + DB.CLine.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SLine) + + if SLine == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + c.JSON(200, tools.ResponseSeccess{ + 0, + SLine, + }) + } + +} + +// @Title 查询所有线路 +// @Description 查询所有线路 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "Name名称;SubName副标题;PlayDuration游玩时长;Suitable适合人群;Location线路点坐标;Annotations需要点亮的设施id" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /AllLine? [get] +func AllLine(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var aLine []DB.SLine + DB.CLine.Find(bson.M{}).All(&aLine) + + c.JSON(200, aLine) + +} + +// @Title 更新线路 +// @Description 更新线路 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /UpdateLine? [post] +func UpdateLine(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var Location []DB.SLocation + json.Unmarshal([]byte(c.PostForm("Location")), &Location) + + var Annotations []string + json.Unmarshal([]byte(c.PostForm("Annotations")), &Annotations) + + var id bson.ObjectId + if pid := c.PostForm("id"); pid == "null" { + id = bson.NewObjectId() + } else { + id = bson.ObjectIdHex(pid) + } + + DB.CLine.UpsertId( + id, + bson.M{"$set": bson.M{ + "Name": c.PostForm("Name"), + "SubName": c.PostForm("SubName"), + "PlayDuration": c.PostForm("PlayDuration"), + "Suitable": c.PostForm("Suitable"), + "Content": c.PostForm("Content"), + "Annotations": Annotations, + "Location": Location, + }}, + ) + + c.JSON(200, tools.ResponseSeccess{ + 0, + "ok", + }) + +} diff --git a/API/Scenic.go b/API/Scenic.go new file mode 100644 index 0000000..ecc32ab --- /dev/null +++ b/API/Scenic.go @@ -0,0 +1,123 @@ +package Api + +import ( + "encoding/json" + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "gopkg.in/mgo.v2/bson" + "letu/DB" +) + +// @Title 返回景区基础信息 +// @Description 基础信息 +// @Accept json +// @Produce json +// @Param id 5dfb03070a9ac17ac7a82054 string true "景区id" +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Name":"名称","Describe":"介绍","OpenHours":"开放时间","Mobile":"电话","Address":"地址","SLocation":{"Latitude":0,"Longitude":0},"Picture":["照片1","照片2"]}}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /ScenicInfo? [get] +func ScenicInfo(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + if c.Query("id") == "" { + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + return + } + + var Scenic *DB.SScenic + DB.CScenic.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&Scenic) + + if Scenic == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + c.JSON(200, tools.ResponseSeccess{ + 0, + Scenic, + }) + } + +} + +// @Title 更新景区基础信息 +// @Description 更新景区基础信息 +// @Accept json +// @Produce json +// @Param id 5dfb03070a9ac17ac7a82054 string true "景区id" +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Name":"名称","Describe":"介绍","OpenHours":"开放时间","Mobile":"电话","Address":"地址","SLocation":{"Latitude":0,"Longitude":0},"Picture":["照片1","照片2"]}}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /UpdateScenic? [post] +func UpdateScenic(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var Location DB.SLocation + json.Unmarshal([]byte(c.PostForm("Location")), &Location) + + var Picture []string + json.Unmarshal([]byte(c.PostForm("Picture")), &Picture) + + var id bson.ObjectId + if pid := c.PostForm("id"); pid == "null" { + id = bson.NewObjectId() + } else { + id = bson.ObjectIdHex(pid) + } + + DB.CScenic.UpsertId( + id, + bson.M{"$set": bson.M{ + "Name": c.PostForm("Name"), + "Describe": c.PostForm("Describe"), + "Location": Location, + "OpenHours": c.PostForm("OpenHours"), + "Mobile": c.PostForm("Mobile"), + "Address": c.PostForm("Address"), + "Picture": Picture, + }}, + ) + + c.JSON(200, tools.ResponseSeccess{ + 0, + "ok", + }) + +} + +// @Title 所有景区基础信息 +// @Description 所有景区基础信息 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /AllScenic? [get] +func AllScenic(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var Scenic []*DB.SScenic + DB.CScenic.Find(bson.M{}).All(&Scenic) + + if Scenic == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + c.JSON(200, tools.ResponseSeccess{ + 0, + Scenic, + }) + } + +} diff --git a/API/Shop.go b/API/Shop.go new file mode 100644 index 0000000..ccf30c1 --- /dev/null +++ b/API/Shop.go @@ -0,0 +1,113 @@ +package Api + +import ( + "encoding/json" + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "gopkg.in/mgo.v2/bson" + "letu/DB" +) + +// @Title 查询商品信息 +// @Description 查询商品信息 +// @Accept json +// @Produce json +// @Param id 5dfb03070a9ac17ac7a82054 string true "id" +// @Success 200 {object} tools.ResponseSeccess "Price=价格;ShopName=店铺名称;KvPhoto用于列表页的图片;TopPhoto详情页最上面的轮播图;Images详情页下面的产品详细图" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /CommodityInfo? [get] +func CommodityInfo(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + if c.Query("id") == "" { + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + return + } + + var SCommodity *DB.SCommodity + DB.CCommodity.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&SCommodity) + + if SCommodity == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + c.JSON(200, tools.ResponseSeccess{ + 0, + SCommodity, + }) + } + +} + +// @Title 查询所有商品 +// @Description 查询所有商品 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "Price=价格;ShopName=店铺名称;KvPhoto用于列表页的图片;TopPhoto详情页最上面的轮播图;Images详情页下面的产品详细图" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /AllCommodity? [get] +func AllCommodity(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var aCommoditys []DB.SCommodity + DB.CCommodity.Find(bson.M{}).All(&aCommoditys) + + c.JSON(200, aCommoditys) + +} + +// @Title 更新商品 +// @Description 更新商品 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /UpdateCommodity? [post] +func UpdateCommodity(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + var Picture []string + json.Unmarshal([]byte(c.PostForm("Images")), &Picture) + + var TopPhoto []string + json.Unmarshal([]byte(c.PostForm("TopPhoto")), &TopPhoto) + + var Location DB.SLocation + json.Unmarshal([]byte(c.PostForm("Location")), &Location) + + var id bson.ObjectId + if pid := c.PostForm("id"); pid == "null" { + id = bson.NewObjectId() + } else { + id = bson.ObjectIdHex(pid) + } + + DB.CCommodity.UpsertId( + id, + bson.M{"$set": bson.M{ + "Name": c.PostForm("Name"), + "Price": c.PostForm("Price"), + "ShopName": c.PostForm("ShopName"), + "KvPhoto": c.PostForm("KvPhoto"), + "TopPhoto": TopPhoto, + "Location": Location, + "Images": Picture, + }}, + ) + + c.JSON(200, tools.ResponseSeccess{ + 0, + "ok", + }) + +} diff --git a/API/Tag.go b/API/Tag.go new file mode 100644 index 0000000..2453612 --- /dev/null +++ b/API/Tag.go @@ -0,0 +1,105 @@ +package Api + +import ( + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "gopkg.in/mgo.v2/bson" + "letu/DB" +) + +// @Title 标签列表 +// @Description 标签列表 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Type":"menu","Name":"服务设施"},{"Type":"normal","Name":"不错"},{"Type":"thrilling","Name":"刺激"},{"Type":"recommend","Name":"必玩"}]}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /Tags? [get] +func AllTag(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + + var Stags []*DB.STag + DB.CTags.Find(bson.M{}).All(&Stags) + + if Stags == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + + c.JSON(200, tools.ResponseSeccess{ + 0, + Stags, + }) + } + +} + +// @Title 创建标签 +// @Description 创建标签 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Type":"menu","Name":"服务设施"},{"Type":"normal","Name":"不错"},{"Type":"thrilling","Name":"刺激"},{"Type":"recommend","Name":"必玩"}]}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /CreateTag? [post] +func CreateTag(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + + var Stags []*DB.STag + DB.CTags.Find(bson.M{}).All(&Stags) + + if Stags == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + + c.JSON(200, tools.ResponseSeccess{ + 0, + Stags, + }) + } + +} + + +// @Title 更新标签 +// @Description 更新标签 +// @Accept json +// @Produce json +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":[{"Type":"menu","Name":"服务设施"},{"Type":"normal","Name":"不错"},{"Type":"thrilling","Name":"刺激"},{"Type":"recommend","Name":"必玩"}]}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /UpdateTag? [post] +func UpdateTag(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + + var Stags []*DB.STag + DB.CTags.Find(bson.M{}).All(&Stags) + + if Stags == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + + c.JSON(200, tools.ResponseSeccess{ + 0, + Stags, + }) + } + +} diff --git a/API/Upload.go b/API/Upload.go new file mode 100644 index 0000000..1b81263 --- /dev/null +++ b/API/Upload.go @@ -0,0 +1,49 @@ +package Api + +import ( + "fmt" + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "path" + "strconv" + "time" +) + +// @Title 上传文件 +// @Description 上传 +// @Accept json +// @Produce json +// @Param file 1 file true "文件" +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"图片地址"}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /Upload? [post] +func Upload(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + file, err := c.FormFile("file") + if err != nil { + c.JSON(200, tools.ResponseError{ + 1, + "a Bad request", + }) + return + } + + fileName := file.Filename + fileExt := path.Ext(fileName) + filePath := "Upload/" + strconv.Itoa(int(time.Now().UnixNano())) + fileExt + + if err := c.SaveUploadedFile(file, filePath); err != nil { + fmt.Println(err) + c.JSON(200, tools.ResponseError{ + 1, + "upload file err", + }) + return + } + c.JSON(200, tools.ResponseSeccess{ + 0, + "/" + filePath, + }) +} diff --git a/API/User.go b/API/User.go new file mode 100644 index 0000000..75600f8 --- /dev/null +++ b/API/User.go @@ -0,0 +1,147 @@ +package Api + +import ( + "crypto/sha256" + "encoding/hex" + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "gopkg.in/mgo.v2/bson" + "letu/DB" + "strconv" + "time" +) + +// @Title 创建用户 +// @Description 用户注册 +// @Accept json +// @Produce json +// @Param password 1 string true "密码" +// @Param confirmpassword 1 string true "确认密码" +// @Param birthday 2010.10.10 string true "生日" +// @Param fullname aarongao string true "全名" +// @Param mobile 18616619599 string true "手机,同用户名" +// @Param openid 12345 string true "微信id" +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /CreateUser? [post] +func CreateUser(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + if c.PostForm("mobile") == "" || c.PostForm("password") != c.PostForm("confirmpassword") { + c.JSON(200, tools.ResponseError{ + 1, + "密码错误", + }) + return + } + + objectID := bson.NewObjectId() + DB.CMember.Insert(DB.SMember{ + &objectID, + c.PostForm("password"), + c.PostForm("birthday"), + c.PostForm("fullname"), + c.PostForm("mobile"), + c.PostForm("openid"), + "", + }) + + c.JSON(200, tools.ResponseSeccess{ + 0, + "ok", + }) + +} + +// @Title 登录 +// @Description 用户登录 +// @Accept json +// @Produce json +// @Param mobile aaron string true "用户名" +// @Param password 1 string true "密码" +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Username":"admin","Password":"123","Birthday":"","FullName":"","Mobile":"","Openid":"","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}} 调用其它需要登陆的接口时携带token,有过期时间" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /LoginUser? [post] +func LoginUser(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + if c.PostForm("mobile") == "" || c.PostForm("password") == "" { + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + return + } + + var User *DB.SMember + DB.CMember.Find(bson.M{"Mobile": c.PostForm("mobile"), "Password": c.PostForm("password")}).One(&User) + + if User == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + // 生成token + tokenunit8 := sha256.Sum256([]byte(c.PostForm("mobile") + c.PostForm("password") + strconv.FormatInt(time.Now().UnixNano(), 10))) + token := hex.EncodeToString(tokenunit8[:32]) + // 更新用户信息 + DB.CMember.Update( + bson.M{"_id": User.Id}, + bson.M{"$set": bson.M{"Token": token}}, + ) + + User.Token = token + c.JSON(200, tools.ResponseSeccess{ + 0, + User, + }) + } + +} + + + +// @Title 用户信息 +// @Description 获取用户信息 +// @Accept json +// @Produce json +// @Param id aaron string true "用户id" +// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":{"Id":"5e09c64c1c09c6f0f7ca2fa9","Username":"admin","Password":"123","Birthday":"","FullName":"","Mobile":"","Openid":"","Token":"640bf934e425aba5d3c90998b2641f2f0ca07261d334d9615d1cd4790b5f34e7"}}" +// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}" +// @Router /UserInfo? [get] +func UserInfo(c *gin.Context) { + c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin")) + c.Header("Access-Control-Allow-Credentials", "true") + + if c.Query("id") == ""{ + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + return + } + + var User *DB.SMember + DB.CMember.Find(bson.M{"_id": bson.ObjectIdHex(c.Query("id"))}).One(&User) + + if User == nil { + + c.JSON(200, tools.ResponseError{ + 1, + "空", + }) + } else { + + + c.JSON(200, tools.ResponseSeccess{ + 0, + User, + }) + } + +} diff --git a/API/Ws.go b/API/Ws.go new file mode 100644 index 0000000..c9707bc --- /dev/null +++ b/API/Ws.go @@ -0,0 +1,58 @@ +package Api + +import ( + "fmt" + "github.com/gin-gonic/gin" + "github.com/gorilla/websocket" + uuid "github.com/satori/go.uuid" + "letu/Lib/Ws" +) + +var wsupgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, +} + +func WsPage(c *gin.Context) { + + conn, err := wsupgrader.Upgrade(c.Writer, c.Request, nil) + if err != nil { + fmt.Println("Failed to set websocket upgrade: %+v", err) + return + } + + // websocket connect + _uuid, _ := uuid.NewV4() + client := &Ws.Client{ + _uuid.String(), + conn, + } + + Ws.Manager.Register <- client + + for { + _, msg, err := conn.ReadMessage() + if err != nil { + println(err.Error()) + Ws.Manager.Unregister <- client + client.Socket.Close() + break + } + + sMsg := string(msg) + println("收到消息:", sMsg) + + switch sMsg { + case "test": + client.Send([]byte("hahaha")) + case "test2": + client.Send([]byte("hahaha2")) + + + } + + + + } + +} diff --git a/Config/config.go b/Config/config.go new file mode 100644 index 0000000..a857481 --- /dev/null +++ b/Config/config.go @@ -0,0 +1,6 @@ +package Config + +type Config struct { + TagType []string + DbPath string +} diff --git a/Config/config.json b/Config/config.json new file mode 100644 index 0000000..60303a8 --- /dev/null +++ b/Config/config.json @@ -0,0 +1,4 @@ +{ + "tagType": ["menu","normal"], + "dbPath": "127.0.0.1:27017" +} diff --git a/DB/db.go b/DB/db.go new file mode 100644 index 0000000..8dffe9f --- /dev/null +++ b/DB/db.go @@ -0,0 +1,93 @@ +package DB + +import ( + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" +) + +var DBSession *mgo.Session +var CItem *mgo.Collection //所有游玩项目内容 +var CComplaint *mgo.Collection //投诉 +var CInvestigation *mgo.Collection //调查 +var CMember *mgo.Collection //会员 +var CCommodity *mgo.Collection //商城 +var CTags *mgo.Collection //标签 +var CScenic *mgo.Collection //景区 +var CLine *mgo.Collection //推荐线路 +var DB *mgo.Database + +type SItem struct { + Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` + Name string `bson:"Name" json:"Name"` + SubName string `bson:"SubName" json:"SubName"` + Location SLocation `bson:"Location" json:"Location"` + Tags []STag `bson:"Tags" json:"Tags"` + Icon string `bson:"Icon" json:"Icon"` + LimitHeight string `bson:"LimitHeight" json:"LimitHeight"` + PlayDuration string `bson:"PlayDuration" json:"PlayDuration"` + SceneTime string `bson:"SceneTime" json:"SceneTime"` + Picture []string `bson:"Picture" json:"Picture"` + Voice string `bson:"Voice" json:"Voice"` + Tel string `bson:"Tel" json:"Tel"` + AverageConsumption string `bson:"AverageConsumption" json:"AverageConsumption"` + Menu string `bson:"Menu" json:"Menu"` + Time string `bson:"Time" json:"Time"` +} +type SLocation struct { + Latitude float64 `bson:"Latitude" json:"Latitude"` + Longitude float64 `bson:"Longitude" json:"Longitude"` +} +type SCommodity struct { + Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` + Name string `bson:"Name" json:"Name"` + Price string `bson:"Price" json:"Price"` + ShopName string `bson:"ShopName" json:"ShopName"` + Location SLocation `bson:"Location" json:"Location"` + KvPhoto string `bson:"KvPhoto" json:"KvPhoto"` + TopPhoto []string `bson:"TopPhoto" json:"TopPhoto"` + Images []string `bson:"Images" json:"Images"` +} +type SLine struct { + Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` + Name string `bson:"Name" json:"Name"` + SubName string `bson:"SubName" json:"SubName"` + Location []SLocation `bson:"Location" json:"Location"` + PlayDuration string `bson:"PlayDuration" json:"PlayDuration"` + Suitable string `bson:"Suitable" json:"Suitable"` + Content string `bson:"Content" json:"Content"` + Annotations []string `bson:"Annotations" json:"Annotations"` +} + +type SComplaint struct { + Type string `bson:"Type" json:"Type"` + Mobile string `bson:"Mobile" json:"Mobile"` + Content string `bson:"Content" json:"Content"` + Image []string `bson:"Image" json:"Image"` +} + +type SInvestigation struct { +} +type SMember struct { + Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` + Password string `bson:"Password" json:"Password"` + Birthday string `bson:"Birthday" json:"Birthday"` + FullName string `bson:"FullName" json:"FullName"` + Mobile string `bson:"Mobile" json:"Mobile"` + Openid string `bson:"Openid" json:"Openid"` + Token string `bson:"Token" json:"Token"` +} + +type STag struct { + Type string `bson:"Type" json:"Type"` + Name string `bson:"Name" json:"Name"` +} +type SScenic struct { + Id *bson.ObjectId `bson:"_id" json:"Id" valid:"required"` + Name string `bson:"Name" json:"Name"` + Describe string `bson:"Describe" json:"Describe"` + OpenHours string `bson:"OpenHours" json:"OpenHours"` + Mobile string `bson:"Mobile" json:"Mobile"` + Address string `bson:"Address" json:"Address"` + Location SLocation `bson:"Location" json:"Location"` + Picture []string `bson:"Picture" json:"Picture"` +} diff --git a/Lib/Ws/Ws.go b/Lib/Ws/Ws.go new file mode 100644 index 0000000..e6ff22a --- /dev/null +++ b/Lib/Ws/Ws.go @@ -0,0 +1,71 @@ +package Ws + +import ( + "encoding/json" + "github.com/gorilla/websocket" +) + +// ClientManager is a websocket manager +type ClientManager struct { + Clients map[*Client]bool + Broadcast chan []byte + Register chan *Client + Unregister chan *Client +} + +// Client is a websocket client +type Client struct { + ID string + Socket *websocket.Conn +} + +// Message is an object for websocket message which is mapped to json type +type Message struct { + Sender string `json:"sender,omitempty"` + Recipient string `json:"recipient,omitempty"` + Content string `json:"content,omitempty"` +} + +// Manager define a ws server manager +var Manager = ClientManager{ + Broadcast: make(chan []byte), + Register: make(chan *Client), + Unregister: make(chan *Client), + Clients: make(map[*Client]bool), +} + +// Start is to start a ws server +func (manager *ClientManager) Start() { + for { + select { + case conn := <-manager.Register: + manager.Clients[conn] = true + jsonMessage, _ := json.Marshal(&Message{Content: "/A new socket has connected."}) + manager.Send(jsonMessage, conn) + case conn := <-manager.Unregister: + if _, ok := manager.Clients[conn]; ok { + delete(manager.Clients, conn) + jsonMessage, _ := json.Marshal(&Message{Content: "/A socket has disconnected."}) + manager.Send(jsonMessage, conn) + } + case message := <-manager.Broadcast: + for conn := range manager.Clients { + conn.Socket.WriteMessage(1, message) + } + } + } +} + +// Send is to send ws message to ws client +func (manager *ClientManager) Send(message []byte, ignore *Client) { + for conn := range manager.Clients { + if conn != ignore { + conn.Socket.WriteMessage(1, message) + } + } +} + + +func (c *Client) Send(message []byte) { + c.Socket.WriteMessage(websocket.TextMessage, message) +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..5df6a1d --- /dev/null +++ b/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "encoding/json" + "github.com/aarongao/tools" + "github.com/gin-gonic/gin" + "gopkg.in/mgo.v2" + "letu/Api" + "letu/Config" + "letu/DB" + "letu/Lib/Ws" + "os" +) + +// @APIVersion 1.0.0 +// @APITitle 乐游图后端接口文档 +// @BasePath https://letu.api.imagchina.com +func main() { + + // 读取配置文件 + dir, _ := os.Getwd() + //println(dir) + file, _ := os.Open(dir + "/Config/config.json") + defer file.Close() + decoder := json.NewDecoder(file) + conf := Config.Config{} + err := decoder.Decode(&conf) + tools.CheckError(err) + + // 连接数据库 + DB.DBSession, err = mgo.Dial(conf.DbPath) + defer DB.DBSession.Close() + //设置模式 + DB.DBSession.SetMode(mgo.Monotonic, true) + //获取文档集 + DB.DB = DB.DBSession.DB("LeYouTu") + DB.CItem = DB.DB.C("Item") + DB.CComplaint = DB.DB.C("Complaint") + DB.CInvestigation = DB.DB.C("Investigation") + DB.CMember = DB.DB.C("Member") + DB.CCommodity = DB.DB.C("Commodity") + DB.CTags = DB.DB.C("Tags") + DB.CScenic = DB.DB.C("Scenic") + DB.CLine = DB.DB.C("Line") + + r := gin.Default() + //r.Static("/.well-known", "./.well-known/") + r.Static("/tiles", dir+"/tiles") + r.GET("/AllItems", Api.AllItems) + r.GET("/AllCommodity", Api.AllCommodity) + r.GET("/AllLine", Api.AllLine) + r.GET("/ItemInfo", Api.ItemInfo) + r.GET("/CommodityInfo", Api.CommodityInfo) + r.POST("/CreateComplaint", Api.CreateComplaint) + r.POST("/CreateUser", Api.CreateUser) + r.POST("/LoginUser", Api.LoginUser) + r.GET("/UserInfo", Api.UserInfo) + r.GET("/ScenicInfo", Api.ScenicInfo) + r.GET("/LineInfo", Api.LineInfo) + r.GET("/AllTag", Api.AllTag) + r.POST("/CreateTag", Api.CreateTag) + r.POST("/UpdateTag", Api.UpdateTag) + r.POST("/Upload", Api.Upload) + r.POST("/UpdateItem", Api.UpdateItem) + r.POST("/UpdateCommodity", Api.UpdateCommodity) + r.POST("/UpdateLine", Api.UpdateLine) + r.POST("/UpdateScenic", Api.UpdateScenic) + r.POST("/UpdateItemTime", Api.UpdateItemTime) + r.GET("/AllScenic", Api.AllScenic) + //r.GET("/ws", Api.WsPage) + + r.Static("/Upload", "./Upload") + r.Static("/Console", "./Console") + go Ws.Manager.Start() + r.Run(":8080") +} -- libgit2 0.21.0