123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package controllers
- import (
- "PublishCode/lib"
- "PublishCode/models"
- beego "github.com/beego/beego/v2/server/web"
- "go.mongodb.org/mongo-driver/bson"
- )
- type PublishCodeController struct {
- beego.Controller
- }
- func (c *PublishCodeController) PublishCode() {
- // 验证秘钥
- //ProductKey := c.GetString("ProductKey")
- //ProductID := c.GetString("ProductID")
- //ProductType_r := models.PublishCode_R{T_ProductID: ProductID}
- if len(string(c.Ctx.Input.RequestBody)) > 9999 {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "json 过大,请减少内容!"}
- c.ServeJSON()
- return
- }
- //println(string(c.Ctx.Input.RequestBody))
- if len(c.Ctx.Input.RequestBody) == 0 {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "json 不能为空!"}
- c.ServeJSON()
- return
- }
- //// 解析json
- //var datajson map[string]interface{}
- //err := json.Unmarshal(c.Ctx.Input.RequestBody, &datajson)
- //if err != nil {
- // c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "json 解析失败!"}
- // c.ServeJSON()
- // return
- //}
- //// 将数据编码为JSON
- //jsonData, err := json.Marshal(datajson)
- //if err != nil {
- // fmt.Println("Error encoding JSON:", err)
- // return
- //}
- GetRandstringI := len(c.Ctx.Input.RequestBody) % 100
- var CodeNum, JointTab string
- for true {
- JointTab = lib.WeekByDate()
- CodeNum = lib.GetRandstring(15, "0123456789123456789", int64(GetRandstringI))
- bson_r := bson.M{"CodeNum": CodeNum, "Data": c.Ctx.Input.RequestBody}
- //for key, value := range datajson {
- // //fmt.Println(key, "->:", value)
- // if key != "CodeNum" { // 防止被强行改变 CodeNum
- // bson_r[key] = value
- // }
- //}
- if models.PublishCode_Add(JointTab, &bson_r) == nil {
- break
- }
- //println("!!!!!!!!!!!",CodeNum)
- GetRandstringI += 1
- }
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: "9" + JointTab + CodeNum}
- c.ServeJSON()
- return
- }
- func (c *PublishCodeController) CodeNum_Read() {
- CodeNum := c.Ctx.Input.Param(":CodeNum")
- r, err := models.CodeNum_Read(CodeNum)
- if err != nil {
- c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "无此码!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: r}
- c.ServeJSON()
- return
- }
|