loginserver.go
3.16 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
package apis
import (
. "loginserver/models"
// "encoding/base64"
// "encoding/json"
// "github.com/tidwall/gjson"
"github.com/gin-gonic/gin"
// "io"
// "io/ioutil"
"log"
"net/http"
// "net/url"
// "os"
// "reflect"
// "strconv"
// "strings"
// "time"
)
//登录接口
func UserLoginApi(c *gin.Context) {
// buf := make([]byte, 1024)
// n, _ := c.Request.Body.Read(buf)
// log.Println(string(buf[0:n]))
loginType := c.Request.FormValue("login_type")
loginName := c.Request.FormValue("login_name")
loginPassword := c.Request.FormValue("login_password")
l := UserLoginInfo{
Type: loginType,
Name: loginName,
Password: loginPassword,
}
if loginType == "" || (loginType == "normal" && (loginName == "" || loginPassword == "")) {
log.Printf("%+v", l)
ReturnRespones(c, "104002", "参数错误")
return
}
result, err := l.UserLogin()
if err != nil {
ReturnRespones(c, result.(string), err.Error())
return
}
cookie := &http.Cookie{
Name: "icesimbaToken",
Value: result.(map[string]interface{})["token"].(string),
Path: "/",
HttpOnly: true,
}
res := map[string]string{
"user_id": result.(map[string]interface{})["user_id"].(string),
}
http.SetCookie(c.Writer, cookie)
ReturnRespones(c, "0", res)
return
}
//注册接口
func UserRegisterApi(c *gin.Context) {
registerType := c.Request.FormValue("register_type")
registerName := c.Request.FormValue("register_name")
registerPassword := c.Request.FormValue("register_password")
r := UserRegisterInfo{
Type: registerType,
Name: registerName,
Password: registerPassword,
}
if registerType == "" || registerName == "" || registerPassword == "" {
log.Printf("%+v", r)
ReturnRespones(c, "104002", "参数错误")
return
}
result, err := r.UserRegister()
if err != nil {
log.Println(err)
ReturnRespones(c, result.(string), err.Error())
return
}
ReturnRespones(c, "0", result)
return
}
//校验用户名接口
func CheckUserNameRepeatApi(c *gin.Context) {
userName := c.Request.FormValue("user_name")
if userName == "" {
log.Println("用户名为空")
ReturnRespones(c, "104002", "参数错误")
return
}
result, err := CheckUserNameRepeat(userName)
if err != nil {
log.Println(err)
ReturnRespones(c, result.(string), err.Error())
return
}
ReturnRespones(c, "0", result)
return
}
//修改密码接口
func ChangeUserPasswordApi(c *gin.Context) {
userName := c.Request.FormValue("user_name")
userOriginPassword := c.Request.FormValue("user_origin_password")
userNewPassword := c.Request.FormValue("user_new_password")
if userName == "" || userOriginPassword == "" || userNewPassword == "" {
log.Println("userName:", userName, "userOriginPassword:", userOriginPassword, "userNewPassword:", userNewPassword)
ReturnRespones(c, "104002", "参数错误")
return
}
result, err := ChangeUserPassword(userName, userOriginPassword, userNewPassword)
if err != nil {
log.Println(err)
ReturnRespones(c, result.(string), err.Error())
return
}
ReturnRespones(c, "0", result)
return
}
//返回函数
func ReturnRespones(c *gin.Context, code string, result interface{}) {
c.JSON(http.StatusOK, gin.H{
"code": code,
"data": result,
})
return
}