common.go
4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package models
import (
db "client/friendSystem/database"
"crypto/md5"
"encoding/hex"
"errors"
"image"
"image/png"
"log"
"net/http"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"github.com/gin-gonic/gin"
)
const (
endpoint string = "oss-cn-beijing.aliyuncs.com"
accessKeyID string = "LTAIgKS9JC0vLIqH"
accessKeySecret string = "9VDu6RNRC8IdPRV0yrsPi6a64SMkxz"
bucketName string = "iceplay"
)
func SetQrCodeImage(uuid string) (err error) {
code, err := qr.Encode(uuid, qr.L, qr.Unicode)
// code, err := code39.Encode(uuid)
if err != nil {
return
}
if uuid != code.Content() {
err = errors.New("data differs")
return
}
code, err = barcode.Scale(code, 300, 300)
if err != nil {
return
}
objectKey := "userInfo/Qrcode/" + MD5(uuid)
log.Println(objectKey)
if err = writeAndPutPng("Qrcode.png", code, objectKey); err != nil {
return
}
return
}
func writeAndPutPng(filename string, img image.Image, objectKey string) (err error) {
file, err := os.Create(filename)
if err != nil {
return
}
err = png.Encode(file, img)
// err = jpeg.Encode(file, img, &jpeg.Options{100}) //图像质量值为100,是最好的图像显示
if err != nil {
return
}
file.Close()
client, err := oss.New(endpoint, accessKeyID, accessKeySecret)
if err != nil {
return
}
bucket, err := client.Bucket(bucketName)
if err != nil {
return
}
err = bucket.PutObjectFromFile(objectKey, filename)
if err != nil {
return
}
err = os.Remove(filename)
if err != nil {
return
}
return
}
func GetTableFieldName(table string, index int) (result string, err error) {
sql := `select COLUMN_NAME from information_schema.COLUMNS where table_name = ?`
log.Println(sql, index)
r, err := db.Engine.Query(sql, table)
if err != nil {
return
}
if index > len(r) || index <= 4 {
err = errors.New("不存在的索引值")
return
}
result = string(r[index-1]["COLUMN_NAME"])
return
}
func GetTableName(gameId int) (result string, err error) {
sql := ` select table_game_tableName from ice_game_table where table_game_id=? limit 1`
r, err := db.Engine.Query(sql, gameId)
if err != nil {
return
}
result = string(r[0]["table_game_tableName"])
return
}
func MD5(str string) string {
w := md5.New()
w.Write([]byte(str))
has := w.Sum(nil)
str = hex.EncodeToString(has)
return str
}
func GetFriendList(uuid string) (result string, err error) {
friendRelationship := make([]FriendRelationship, 0)
sql := ` select friend_relationship_a_id ,friend_relationship_b_id from ice_friend_relationship where friend_relationship_a_id=? or friend_relationship_b_id =? and relationship_value =1`
err = db.Engine.Sql(sql, uuid, uuid).Find(&friendRelationship)
if err != nil {
return
}
/*取出好友uuid*/
result = "in("
for _, v := range friendRelationship {
if v.FriendRelationshipAId == uuid {
result = result + "'" + v.FriendRelationshipBId + "',"
// result = result + v.FriendRelationshipBId + ","
} else {
result = result + "'" + v.FriendRelationshipAId + "',"
// result = result + v.FriendRelationshipAId + ","
}
}
result = result + "'" + uuid + "')"
// result = result + uuid + ")"
return
}
func ReturnRespones(c *gin.Context, code string, result interface{}) {
var (
respondObjectName string
respondObjectValue interface{}
)
switch code {
case "1003":
respondObjectName = "msg"
msg := "参数错误"
respondObjectValue = msg
break
case "0":
respondObjectName = "obj"
res := make(map[string]interface{})
res["result"] = result
respondObjectValue = res
break
case "1000":
respondObjectName = "msg"
msg := result.(error).Error()
respondObjectValue = msg
break
default:
log.Println("code error")
}
c.JSON(http.StatusOK, gin.H{
"code": code,
respondObjectName: respondObjectValue,
})
return
}
/*Id是否存在校验*/
func CheckIdExist(uuid string) (err error) {
log.Println(uuid)
sql := ` select id from users where guid=?`
r, err := db.Engine.Query(sql, uuid)
if err != nil {
return
}
if len(r) == 0 {
err = errors.New("无效的用户ID")
return
}
return
}