登录
用户登录后,可以进行文章的编写,修改,以及删除
1. 登录页面
package views
import (
"ms-go-blog/common"
"ms-go-blog/config"
"net/http"
)
func (*HTMLApi) Login(w http.ResponseWriter,r *http.Request) {
login := common.Template.Login
login.WriteData(w,config.Cfg.Viewer)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
http.HandleFunc("/login",views.HTML.Login)
1
2. 登录接口
package models
import "time"
type User struct {
Uid int `json:"uid"`
UserName string `json:"userName"`
Passwd string `json:"passwd"`
Avatar string `json:"avatar"`
CreateAt time.Time `json:"create_at"`
UpdateAt time.Time `json:"update_at"`
}
type UserInfo struct {
Uid int `json:"uid"`
UserName string `json:"userName"`
Avatar string `json:"avatar"`
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2.1 jwt工具
package utils
import (
gojwt "github.com/dgrijalva/jwt-go"
"os"
"time"
)
var jwtKey []byte
func init() {
jwtKey = []byte(os.Getenv("JWT_SECRET"))
}
type Claims struct {
Uid int
gojwt.StandardClaims
}
// 生成Token
func Award(uid *int) (string, error) {
// 过期时间 默认7天
expireTime := time.Now().Add(7 * 24 * time.Hour)
claims := &Claims{
Uid: *uid,
StandardClaims: gojwt.StandardClaims{
ExpiresAt: expireTime.Unix(),
IssuedAt: time.Now().Unix(),
},
}
// 生成token
token := gojwt.NewWithClaims(gojwt.SigningMethodHS256, claims)
tokenStr, err := token.SignedString(jwtKey)
if err != nil {
return "", err
}
return tokenStr, nil
}
// 解析token
func ParseToken(tokenStr string) (*gojwt.Token, *Claims, error) {
claims := &Claims{}
token, err := gojwt.ParseWithClaims(tokenStr, claims, func(t *gojwt.Token) (interface{}, error) {
return jwtKey, nil
})
if err != nil {
return nil, nil, err
}
return token, claims, err
}
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
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
2.2 md5加密
package utils
import (
"crypto/md5"
"fmt"
"strings"
)
//给字符串生成md5
//@params str 需要加密的字符串
//@params salt interface{} 加密的盐
//@return str 返回md5码
func Md5Crypt(str string, salt ...interface{}) (CryptStr string) {
if l := len(salt); l > 0 {
slice := make([]string, l+1)
str = fmt.Sprintf(str+strings.Join(slice, "%v"), salt...)
}
return fmt.Sprintf("%x", md5.Sum([]byte(str)))
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2.3 获取json参数
func GetRequestJsonParam(r *http.Request) map[string]interface{} {
var params map[string]interface{}
body, _ := ioutil.ReadAll(r.Body)
_ = json.Unmarshal(body, ¶ms)
return params
}
1
2
3
4
5
6
2
3
4
5
6
2.4 接口实现
package api
import (
"ms-go-blog/common"
"ms-go-blog/service"
"net/http"
)
func (*Api) Login(w http.ResponseWriter, r *http.Request) {
//接收用户名和密码 返回 对应的json数据
params := common.GetRequestJsonParam(r)
userName := params["username"].(string)
passwd := params["passwd"].(string)
loginRes,err := service.Login(userName,passwd)
if err != nil {
common.Error(w,err)
return
}
common.Success(w,loginRes)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package service
import (
"errors"
"ms-go-blog/dao"
"ms-go-blog/models"
"ms-go-blog/utils"
)
func Login(userName,passwd string) (*models.LoginRes,error) {
passwd = utils.Md5Crypt(passwd,"mszlu")
user := dao.GetUser(userName,passwd);
if user == nil {
return nil,errors.New("账号密码不正确")
}
uid := user.Uid
//生成token jwt技术进行生成 令牌 A.B.C
token,err := utils.Award(&uid)
if err != nil {
return nil,errors.New("token未能生成")
}
var userInfo models.UserInfo
userInfo.Uid = user.Uid
userInfo.UserName = user.UserName
userInfo.Avatar = user.Avatar
var lr = &models.LoginRes{
token,
userInfo,
}
return lr,nil
}
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
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
http.HandleFunc("/api/v1/login",api.API.Login)
1
package models
type Result struct {
Error string `json:"error"`
Data interface{} `json:"data"`
Code int `json:"code"`
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
3. 文章详情
package views
import (
"errors"
"ms-go-blog/common"
"ms-go-blog/service"
"net/http"
"strconv"
"strings"
)
func (*HTMLApi) Detail(w http.ResponseWriter, r *http.Request) {
detail := common.Template.Detail
//获取路径参数
path := r.URL.Path
pIdStr := strings.TrimPrefix(path,"/p/")
//7.html
pIdStr = strings.TrimSuffix(pIdStr,".html")
pid,err := strconv.Atoi(pIdStr)
if err != nil {
detail.WriteError(w,errors.New("不识别此请求路径"))
return
}
postRes,err := service.GetPostDetail(pid)
if err != nil {
detail.WriteError(w,errors.New("查询出错"))
return
}
detail.WriteData(w,postRes)
}
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
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
package service
import (
"html/template"
"ms-go-blog/config"
"ms-go-blog/dao"
"ms-go-blog/models"
)
func GetPostDetail(pid int) (*models.PostRes,error) {
post,err := dao.GetPostById(pid)
if err != nil {
return nil,err
}
categoryName := dao.GetCategoryNameById(post.CategoryId)
userName := dao.GetUserNameById(post.UserId)
postMore := models.PostMore{
post.Pid,
post.Title,
post.Slug,
template.HTML(post.Content),
post.CategoryId,
categoryName,
post.UserId,
userName,
post.ViewCount,
post.Type,
models.DateDay(post.CreateAt),
models.DateDay(post.UpdateAt),
}
var postRes = &models.PostRes{
config.Cfg.Viewer,
config.Cfg.System,
postMore,
}
return postRes,nil
}
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
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
package service
import (
"html/template"
"ms-go-blog/config"
"ms-go-blog/dao"
"ms-go-blog/models"
)
func GetPostDetail(pid int) (*models.PostRes,error) {
post,err := dao.GetPostById(pid)
if err != nil {
return nil,err
}
categoryName := dao.GetCategoryNameById(post.CategoryId)
userName := dao.GetUserNameById(post.UserId)
postMore := models.PostMore{
post.Pid,
post.Title,
post.Slug,
template.HTML(post.Content),
post.CategoryId,
categoryName,
post.UserId,
userName,
post.ViewCount,
post.Type,
models.DateDay(post.CreateAt),
models.DateDay(post.UpdateAt),
}
var postRes = &models.PostRes{
config.Cfg.Viewer,
config.Cfg.System,
postMore,
}
return postRes,nil
}
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
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