user.go 3.3 KB

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