user.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package user
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "sfbase/global"
  7. sutils "sfbase/utils"
  8. "sfis/model"
  9. "sfis/model/response"
  10. "sfis/service"
  11. "sfis/utils"
  12. "strconv"
  13. "time"
  14. "github.com/gin-gonic/gin"
  15. "go.uber.org/zap"
  16. )
  17. func DevUserManageRegister(router *gin.Engine) {
  18. userGroup := router.Group("/manage/user/")
  19. userGroup.Use()
  20. {
  21. userGroup.POST("/create", userCreate)
  22. userGroup.POST("/list", userList)
  23. userGroup.POST("/userProductChoose", userProductChoose)
  24. userGroup.POST("/userProductList", userProductList)
  25. userGroup.POST("/moneyRecharge", moneyRecharge)
  26. userGroup.POST("/productRecharge", productRecharge)
  27. userGroup.POST("/concurrentTest", concurrentTest)
  28. }
  29. }
  30. // 创建用户
  31. func userCreate(c *gin.Context) {
  32. var user model.User
  33. if err := c.ShouldBind(&user); err != nil {
  34. response.FailWithMessage("参数错误", c)
  35. return
  36. }
  37. global.Logger.Info("manage userCreate接口参数:", zap.Any("param", user))
  38. t := time.Now()
  39. appId := utils.GetAppID(t.Unix())
  40. key := sutils.GetComplexRandom(8, 3, 5)
  41. user.SecretKey = key
  42. user.AppID = appId
  43. // 创建用户
  44. userData, err := service.CreateUser(user)
  45. if err != nil {
  46. response.FailWithMessage(err.Error(), c)
  47. } else {
  48. response.OkWithData(userData, c)
  49. }
  50. }
  51. // 用户列表
  52. func userList(c *gin.Context) {
  53. page, _ := strconv.Atoi(c.PostForm("page"))
  54. limit, _ := strconv.Atoi(c.PostForm("limit"))
  55. appId := c.PostForm("app_id")
  56. name := c.PostForm("name")
  57. phone := c.PostForm("phone")
  58. condMap := map[string]interface{}{
  59. "app_id": appId,
  60. "name": name,
  61. "phone": phone,
  62. }
  63. userList, totalCount, err := service.ListUser(condMap, page, limit)
  64. if err != nil {
  65. response.FailWithMessage(err.Error(), c)
  66. } else {
  67. c.JSON(http.StatusOK, gin.H{
  68. "code": response.SUCCESS,
  69. "data": userList,
  70. "msg": "查询成功",
  71. "totalCount": totalCount,
  72. })
  73. }
  74. }
  75. func userProductChoose(c *gin.Context) {
  76. bs, err := ioutil.ReadAll(c.Request.Body)
  77. if err != nil {
  78. panic(err)
  79. response.FailWithMessage("读取参数出错", c)
  80. return
  81. }
  82. var dataMap map[string]interface{}
  83. json.Unmarshal([]byte(bs), &dataMap)
  84. //log.Println("获取前端参数:", dataMap)
  85. appId := sutils.ObjToString(dataMap["appId"])
  86. buyType := sutils.IntAll(dataMap["buyType"])
  87. products := dataMap["products"]
  88. productArr := make([]map[string]interface{}, 0)
  89. if _products, ok := products.([]interface{}); ok {
  90. productArr = sutils.ObjArrToMapArr(_products)
  91. //log.Println(productArr)
  92. }
  93. if appId != "" && len(productArr) > 0 {
  94. status, haveProductId, err := service.CreateUserProduct(appId, productArr, buyType)
  95. //log.Println(status, haveProductId, err)
  96. if status == 1 && len(err) == 0 {
  97. if haveProductId != "" {
  98. response.OkWithMessage("用户已购买过"+haveProductId+"产品,请去充值", c)
  99. } else {
  100. response.Ok(c)
  101. }
  102. } else {
  103. response.Fail(c)
  104. }
  105. } else {
  106. response.FailWithMessage("缺少参数", c)
  107. }
  108. }
  109. func userProductList(c *gin.Context) {
  110. appId := c.PostForm("appId")
  111. global.Logger.Info("manage userProductList接口参数:", zap.Any("appId", appId))
  112. if appId != "" {
  113. userProduct, err := service.UserProductList(appId, c)
  114. if len(userProduct) == 0 && err != nil {
  115. response.FailWithMessage("查询出错", c)
  116. } else {
  117. response.OkWithData(userProduct, c)
  118. }
  119. } else {
  120. response.FailWithMessage("缺少参数", c)
  121. }
  122. }