zjxpcyc il y a 6 ans
Parent
révision
dfdcbf7bde
4 fichiers modifiés avec 98 ajouts et 1 suppressions
  1. 49
    0
      controllers/file.go
  2. 0
    1
      models/luckdraw/luckdraw.go
  3. 1
    0
      routers/common.go
  4. 48
    0
      utils/rand_test.go

+ 49
- 0
controllers/file.go Voir le fichier

@@ -72,3 +72,52 @@ func (c *BaseController) SaveToExcel(fn string, excel *utils.TinyXLSXEngine) {
72 72
 	c.destroyContext(true)
73 73
 	c.StopRun()
74 74
 }
75
+
76
+// UploadBase64Image Upload base64-image to ali-oss
77
+// @Title Upload image to ali-oss
78
+// @Description 上传base64图片到阿里云
79
+// @Param   UpImgStr     form    string  true        "图片控件name"
80
+// @Success 200 { Url } 图片URL
81
+// @Failure >300 error message
82
+func (c *BaseController) UploadBase64Image() {
83
+	base64Str, err := url.QueryUnescape(c.GetString("base64str"))
84
+	if err != nil {
85
+		c.ResponseError(utils.LogError("上传 Base64 图片失败", err))
86
+	}
87
+
88
+	imgURL, err := c.uploadStringToAliOSS(base64Str)
89
+	if err != nil {
90
+		c.ResponseError(err)
91
+	}
92
+
93
+	resp := map[string]interface{}{
94
+		"url": imgURL,
95
+	}
96
+
97
+	c.ResponseJSON(resp)
98
+}
99
+
100
+// uploadStringToAliOSS 上传文件到阿里云
101
+func (c *BaseController) uploadStringToAliOSS(fStr string) (string, error) {
102
+	aliConf, ok := c.Configer[AliYunConf]
103
+	if !ok {
104
+		return "", errors.New("没有找到阿里云相关配置")
105
+	}
106
+
107
+	endpoint := aliConf.String("oss::Endpoint")
108
+	accessKeyID := aliConf.String("oss::AccessKeyId")
109
+	accessKeySecret := aliConf.String("oss::AccessKeySecret")
110
+	bucket := aliConf.String("oss::Bucket")
111
+
112
+	aliCli, err := utils.GetOssClient(endpoint, accessKeyID, accessKeySecret)
113
+	if err != nil {
114
+		return "", utils.LogError("配置阿里云客户端失败", err)
115
+	}
116
+
117
+	fileURL, err := utils.UploadStringToBucket(aliCli, bucket, fStr)
118
+	if err != nil {
119
+		return "", utils.LogError("上传文件到阿里云失败", err)
120
+	}
121
+
122
+	return fileURL, nil
123
+}

+ 0
- 1
models/luckdraw/luckdraw.go Voir le fichier

@@ -722,7 +722,6 @@ func (m *LuckDrawDao) GetWinning(prizes []model.TaLuckdrawPrize) (*model.TaLuckd
722 722
 		}
723 723
 	}
724 724
 
725
-	utils.LogError(pList)
726 725
 	if len(pList) == 0 {
727 726
 		return nil, errors.New("所有奖品已抽完")
728 727
 	}

+ 1
- 0
routers/common.go Voir le fichier

@@ -287,6 +287,7 @@ func getCommonRoutes(prefix string) beego.LinkNamespace {
287 287
 
288 288
 		// 文件
289 289
 		beego.NSRouter("/file", &controllers.BaseController{}, "post:FileUpload"),
290
+		beego.NSRouter("/file/base64", &controllers.BaseController{}, "post:UploadBase64Image"),
290 291
 
291 292
 		// 会员
292 293
 		beego.NSRouter("/customer", &customer.CustomerController{}, "get:CustWXList"),

+ 48
- 0
utils/rand_test.go Voir le fichier

@@ -13,3 +13,51 @@ func TestGetRand(t *testing.T) {
13 13
 		t.Fatalf("TestGetRand error, %s, %s", res1, res2)
14 14
 	}
15 15
 }
16
+
17
+func TestLuckyDraw(t *testing.T) {
18
+	prizeList := []map[string]interface{}{
19
+		map[string]interface{}{
20
+			"prize": "A",
21
+			"prob":  20,
22
+		},
23
+		map[string]interface{}{
24
+			"prize": "B",
25
+			"prob":  20,
26
+		},
27
+		map[string]interface{}{
28
+			"prize": "C",
29
+			"prob":  30,
30
+		},
31
+		map[string]interface{}{
32
+			"prize": "D",
33
+			"prob":  30,
34
+		},
35
+	}
36
+
37
+	theA := 0
38
+	theB := 0
39
+	theC := 0
40
+	theD := 0
41
+
42
+	for i := 0; i < 10000; i++ {
43
+		res := utils.LuckyDraw(prizeList).(string)
44
+
45
+		switch res {
46
+		case "A":
47
+			theA += 1
48
+		case "B":
49
+			theB += 1
50
+		case "C":
51
+			theC += 1
52
+		case "D":
53
+			theD += 1
54
+		default:
55
+		}
56
+
57
+		// bingoList = append(bingoList, utils.LuckyDraw(prizeList))
58
+	}
59
+
60
+	resStr := `A ==> %d  B ==> %d C ==> %d D ==> %d`
61
+
62
+	t.Fatalf(resStr, theA, theB, theC, theD)
63
+}