-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmodel.go
68 lines (60 loc) · 1.12 KB
/
model.go
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
package models
import (
"crypto/md5"
"fmt"
"io"
)
var (
globalnum int
)
func init() {
globalnum = 100000000
}
func GetMD5(lurl string) string {
h := md5.New()
salt1 := "salt4shorturl"
io.WriteString(h, lurl+salt1)
urlmd5 := fmt.Sprintf("%x", h.Sum(nil))
return urlmd5
}
func Generate() (tiny string) {
globalnum++
num := globalnum
fmt.Println(num)
alpha := merge(getRange(48, 57), getRange(65, 90))
alpha = merge(alpha, getRange(97, 122))
if num < 62 {
tiny = string(alpha[num])
return tiny
} else {
var runes []rune
runes = append(runes, alpha[num%62])
num = num / 62
for num >= 1 {
if num < 62 {
runes = append(runes, alpha[num-1])
} else {
runes = append(runes, alpha[num%62])
}
num = num / 62
}
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
tiny = string(runes)
return tiny
}
return tiny
}
func getRange(start, end rune) (ran []rune) {
for i := start; i <= end; i++ {
ran = append(ran, i)
}
return ran
}
func merge(a, b []rune) []rune {
c := make([]rune, len(a)+len(b))
copy(c, a)
copy(c[len(a):], b)
return c
}