PublishCode.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package controllers
  2. import (
  3. "PublishCode/lib"
  4. "PublishCode/models"
  5. beego "github.com/beego/beego/v2/server/web"
  6. "go.mongodb.org/mongo-driver/bson"
  7. )
  8. type PublishCodeController struct {
  9. beego.Controller
  10. }
  11. func (c *PublishCodeController) PublishCode() {
  12. // 验证秘钥
  13. //ProductKey := c.GetString("ProductKey")
  14. //ProductID := c.GetString("ProductID")
  15. //ProductType_r := models.PublishCode_R{T_ProductID: ProductID}
  16. if len(string(c.Ctx.Input.RequestBody)) > 9999 {
  17. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "json 过大,请减少内容!"}
  18. c.ServeJSON()
  19. return
  20. }
  21. //println(string(c.Ctx.Input.RequestBody))
  22. if len(c.Ctx.Input.RequestBody) == 0 {
  23. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "json 不能为空!"}
  24. c.ServeJSON()
  25. return
  26. }
  27. //// 解析json
  28. //var datajson map[string]interface{}
  29. //err := json.Unmarshal(c.Ctx.Input.RequestBody, &datajson)
  30. //if err != nil {
  31. // c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "json 解析失败!"}
  32. // c.ServeJSON()
  33. // return
  34. //}
  35. //// 将数据编码为JSON
  36. //jsonData, err := json.Marshal(datajson)
  37. //if err != nil {
  38. // fmt.Println("Error encoding JSON:", err)
  39. // return
  40. //}
  41. GetRandstringI := len(c.Ctx.Input.RequestBody) % 100
  42. var CodeNum, JointTab string
  43. for true {
  44. JointTab = lib.WeekByDate()
  45. CodeNum = lib.GetRandstring(15, "0123456789123456789", int64(GetRandstringI))
  46. bson_r := bson.M{"CodeNum": CodeNum, "Data": c.Ctx.Input.RequestBody}
  47. //for key, value := range datajson {
  48. // //fmt.Println(key, "->:", value)
  49. // if key != "CodeNum" { // 防止被强行改变 CodeNum
  50. // bson_r[key] = value
  51. // }
  52. //}
  53. if models.PublishCode_Add(JointTab, &bson_r) == nil {
  54. break
  55. }
  56. //println("!!!!!!!!!!!",CodeNum)
  57. GetRandstringI += 1
  58. }
  59. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: "9" + JointTab + CodeNum}
  60. c.ServeJSON()
  61. return
  62. }
  63. func (c *PublishCodeController) CodeNum_Read() {
  64. CodeNum := c.Ctx.Input.Param(":CodeNum")
  65. r, err := models.CodeNum_Read(CodeNum)
  66. if err != nil {
  67. c.Data["json"] = lib.JSONR{Code: lib.Error, Msg: "无此码!"}
  68. c.ServeJSON()
  69. return
  70. }
  71. c.Data["json"] = lib.JSONR{Code: lib.Success, Msg: "ok!", Data: r}
  72. c.ServeJSON()
  73. return
  74. }