package user import ( "encoding/json" "io/ioutil" "net/http" "sfbase/global" sutils "sfbase/utils" "sfis/model" "sfis/model/response" "sfis/service" "sfis/utils" "strconv" "time" "github.com/gin-gonic/gin" "go.uber.org/zap" ) func DevUserManageRegister(router *gin.Engine) { userGroup := router.Group("/manage/user/") userGroup.Use() { userGroup.POST("/create", userCreate) userGroup.POST("/list", userList) userGroup.POST("/userProductChoose", userProductChoose) userGroup.POST("/userProductList", userProductList) userGroup.POST("/moneyRecharge", moneyRecharge) userGroup.POST("/productRecharge", productRecharge) userGroup.POST("/concurrentTest", concurrentTest) } } // 创建用户 func userCreate(c *gin.Context) { var user model.User if err := c.ShouldBind(&user); err != nil { response.FailWithMessage("参数错误", c) return } global.Logger.Info("manage userCreate接口参数:", zap.Any("param", user)) t := time.Now() appId := utils.GetAppID(t.Unix()) key := sutils.GetComplexRandom(8, 3, 5) user.SecretKey = key user.AppID = appId // 创建用户 userData, err := service.CreateUser(user) if err != nil { response.FailWithMessage(err.Error(), c) } else { response.OkWithData(userData, c) } } // 用户列表 func userList(c *gin.Context) { page, _ := strconv.Atoi(c.PostForm("page")) limit, _ := strconv.Atoi(c.PostForm("limit")) appId := c.PostForm("app_id") name := c.PostForm("name") phone := c.PostForm("phone") condMap := map[string]interface{}{ "app_id": appId, "name": name, "phone": phone, } userList, totalCount, err := service.ListUser(condMap, page, limit) if err != nil { response.FailWithMessage(err.Error(), c) } else { c.JSON(http.StatusOK, gin.H{ "code": response.SUCCESS, "data": userList, "msg": "查询成功", "totalCount": totalCount, }) } } func userProductChoose(c *gin.Context) { bs, err := ioutil.ReadAll(c.Request.Body) if err != nil { panic(err) response.FailWithMessage("读取参数出错", c) return } var dataMap map[string]interface{} json.Unmarshal([]byte(bs), &dataMap) //log.Println("获取前端参数:", dataMap) appId := sutils.ObjToString(dataMap["appId"]) buyType := sutils.IntAll(dataMap["buyType"]) products := dataMap["products"] productArr := make([]map[string]interface{}, 0) if _products, ok := products.([]interface{}); ok { productArr = sutils.ObjArrToMapArr(_products) //log.Println(productArr) } if appId != "" && len(productArr) > 0 { status, haveProductId, err := service.CreateUserProduct(appId, productArr, buyType) //log.Println(status, haveProductId, err) if status == 1 && len(err) == 0 { if haveProductId != "" { response.OkWithMessage("用户已购买过"+haveProductId+"产品,请去充值", c) } else { response.Ok(c) } } else { response.Fail(c) } } else { response.FailWithMessage("缺少参数", c) } } func userProductList(c *gin.Context) { appId := c.PostForm("appId") global.Logger.Info("manage userProductList接口参数:", zap.Any("appId", appId)) if appId != "" { userProduct, err := service.UserProductList(appId, c) if len(userProduct) == 0 && err != nil { response.FailWithMessage("查询出错", c) } else { response.OkWithData(userProduct, c) } } else { response.FailWithMessage("缺少参数", c) } }