user_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "net/url"
  9. sutil "sfbase/utils"
  10. "strings"
  11. "testing"
  12. "time"
  13. )
  14. /*var (
  15. product1 = &model.Product{
  16. BaseModel: model.BaseModel{ID: 1003},
  17. Name: "行业招标数据",
  18. Path: "/path1",
  19. UnitPrice: 50, //单价精确到分 5毛
  20. MinUnit: 1, //最小单位1,即 5毛/条
  21. ProductType: 1, //产品类型 0-按次 1-按条
  22. TestNum: 100,
  23. }
  24. product2 = &model.Product{
  25. Name: "中国移动招标数据",
  26. Path: "/path2",
  27. UnitPrice: 1500, //单价精确到分 15快
  28. MinUnit: 1, //最小单位1,即 15快/次
  29. ProductType: 0, //产品类型 0-按次 1-按条
  30. TestNum: 500,
  31. }
  32. )*/
  33. func init() {
  34. //todo init connection db operation
  35. }
  36. func Test_CreateUser(t *testing.T) {
  37. log.Println("devUserCreate testing......")
  38. //appID := utils.GetAppID(time.Now().Unix())
  39. //secretKey := sutil.GetComplexRandom(8, 3, 5)
  40. data := make(url.Values)
  41. data["name"] = []string{"拓普公司测试3"}
  42. data["phone"] = []string{"18238182402"}
  43. data["ip_white_list"] = []string{"*"}
  44. bs, _ := sutil.HttpPostForm("http://localhost:8080/manage/user/create", map[string]string{}, data)
  45. log.Print(string(bs))
  46. //log.Printf("create successful appID:[%s],secretKey:[%s]", appID, secretKey)
  47. //创建用户、给用户开通接口产品时候有以下几种情况
  48. //1.线上给账户充值10000块(user_account),不去实际购买产品,无购买记录,用户产品表无剩余量(left_num为0).此时又分两种情况
  49. //1.1 按次扣账户余额
  50. //1.2 按量扣账户余额
  51. //appID := ""
  52. //tradeMoney := 1 * 100 * 10000 //充值1万块钱
  53. }
  54. func Test_UserProductChoose(t *testing.T) {
  55. /*appId := "sfGSRYRQMABwMAAgcBHjQt"
  56. productIds := "1000"
  57. startTime := "2022-01-11 00:00:00"
  58. endTime := "2024-01-11 00:00:00"
  59. leftNum := "0"
  60. costModel := "1"
  61. interfaceStatus := "0"
  62. callTimesLimitDay := "100"
  63. dataNumOneTimes := "100"
  64. data := make(url.Values)
  65. data["appId"] = []string{appId}
  66. data["productIds"] = []string{productIds}
  67. data["startTime"] = []string{startTime}
  68. data["endTime"] = []string{endTime}
  69. data["leftNum"] = []string{leftNum}
  70. data["costModel"] = []string{costModel}
  71. data["interfaceStatus"] = []string{interfaceStatus}
  72. data["callTimesLimitDay"] = []string{callTimesLimitDay}
  73. data["dataNumOneTimes"] = []string{dataNumOneTimes}
  74. data["tradeMoney"] = []string{"10000"}
  75. data["buyType"] = []string{"1"}*/
  76. //data["historyUnitPrice"] = []string{"18"}
  77. appId := "sfGSRYRQMABwMAAgcBHjQt"
  78. buyType := 1
  79. products := make([]map[string]interface{}, 0)
  80. p := map[string]interface{}{
  81. "productId": 1001,
  82. "costModel": 1,
  83. "leftNum": 1000,
  84. "tradeMoney": 100,
  85. "startTime": "2022-01-11 00:00:00",
  86. "endTime": "2023-01-11 00:00:00",
  87. "interfaceStatus": 0,
  88. "callTimesLimitDay": 100,
  89. "dataNumOneTimes": 1000,
  90. }
  91. p2 := map[string]interface{}{
  92. "productId": 1002,
  93. "costModel": 1,
  94. "leftNum": 1000,
  95. "tradeMoney": 100,
  96. "startTime": "2022-01-11 00:00:00",
  97. "endTime": "2023-01-11 00:00:00",
  98. "interfaceStatus": 0,
  99. "callTimesLimitDay": 100,
  100. "dataNumOneTimes": 1000,
  101. }
  102. products = append(products, p, p2)
  103. data := map[string]interface{}{}
  104. data["appId"] = appId
  105. data["buyType"] = buyType
  106. data["products"] = products
  107. buf, _ := json.MarshalIndent(data, "", "")
  108. bs, _ := sutil.HttpPostJson("http://localhost:8080/manage/user/userProductChoose", string(buf))
  109. dataMap := map[string]interface{}{}
  110. json.Unmarshal(bs, &dataMap)
  111. log.Print("结果:", dataMap)
  112. }
  113. func Test_UserProductList(t *testing.T) {
  114. appId := "sfPQRYRQMAAwcGBwYBCgcA"
  115. data := make(url.Values)
  116. data["appId"] = []string{appId}
  117. now := time.Now().Unix()
  118. bs, _ := sutil.HttpPostForm("http://localhost:8080/manage/user/userProductList", map[string]string{
  119. "timestamp": fmt.Sprint(now),
  120. }, data)
  121. log.Print(string(bs))
  122. }
  123. func post(url string, form map[string]string) (data map[string]interface{}) {
  124. str := ""
  125. for k, v := range form {
  126. str += "&" + k + "=" + v
  127. }
  128. log.Println(str)
  129. res, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(str))
  130. if err != nil {
  131. log.Println("post err:", err.Error())
  132. } else if res.Body != nil {
  133. defer res.Body.Close()
  134. bs, _ := ioutil.ReadAll(res.Body)
  135. json.Unmarshal(bs, &data)
  136. }
  137. return
  138. }