写文章

1. 写文章页面

http.HandleFunc("/writing",views.HTML.Writing)
1
package views

import (
	"ms-go-blog/common"
	"ms-go-blog/service"
	"net/http"
)

func (*HTMLApi) Writing(w http.ResponseWriter, r *http.Request)  {
	writing := common.Template.Writing
	wr := service.Writing()
	writing.WriteData(w,wr)
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func Writing() (wr models.WritingRes) {
	wr.Title = config.Cfg.Viewer.Title
	wr.CdnURL = config.Cfg.System.CdnURL
	category, err := dao.GetAllCategory()
	if err != nil {
		log.Println(err)
		return
	}
	wr.Categorys = category
	return
}
1
2
3
4
5
6
7
8
9
10
11
type WritingRes struct {
	Title string
	CdnURL string
	Categorys []Category
}
1
2
3
4
5

2. 发布文章和编辑文章

http.HandleFunc("/api/v1/post",api.API.SaveAndUpdatePost)
	http.HandleFunc("/api/v1/post/",api.API.GetPost)
1
2
package api

import (
	"errors"
	"ms-go-blog/common"
	"ms-go-blog/dao"
	"ms-go-blog/models"
	"ms-go-blog/service"
	"ms-go-blog/utils"
	"net/http"
	"strconv"
	"strings"
	"time"
)

func (*Api) GetPost(w http.ResponseWriter,r *http.Request)  {
	path := r.URL.Path
	pIdStr := strings.TrimPrefix(path,"/api/v1/post/")
	pid,err := strconv.Atoi(pIdStr)
	if err != nil {
		common.Error(w,errors.New("不识别此请求路径"))
		return
	}
	post,err := dao.GetPostById(pid)
	if err != nil {
		common.Error(w,err)
		return
	}
	common.Success(w,post)
}

func (*Api) SaveAndUpdatePost(w http.ResponseWriter,r *http.Request)  {
	//获取用户id,判断用户是否登录
	token := r.Header.Get("Authorization")
	_,claim,err := utils.ParseToken(token)
	if err != nil {
		common.Error(w,errors.New("登录已过期"))
		return
	}
	uid := claim.Uid
	//POST  save
	method := r.Method
	switch method {
	case http.MethodPost:
		params := common.GetRequestJsonParam(r)
		cId := params["categoryId"].(string)
		categoryId,_ := strconv.Atoi(cId)
		content := params["content"].(string)
		markdown := params["markdown"].(string)
		slug := params["slug"].(string)
		title := params["title"].(string)
		postType := params["type"].(float64)
		pType := int(postType)
		post := &models.Post{
			-1,
			title,
			slug,
			content,
			markdown,
			categoryId,
			uid,
			0,
			pType,
			time.Now(),
			time.Now(),

		}
		service.SavePost(post)
		common.Success(w,post)
	case http.MethodPut:
		// update
		params := common.GetRequestJsonParam(r)
		cId := params["categoryId"].(string)
		categoryId,_ := strconv.Atoi(cId)
		content := params["content"].(string)
		markdown := params["markdown"].(string)
		slug := params["slug"].(string)
		title := params["title"].(string)
		postType := params["type"].(float64)
		pidFloat := params["pid"].(float64)
		pType := int(postType)
		pid := int(pidFloat)
		post := &models.Post{
			pid,
			title,
			slug,
			content,
			markdown,
			categoryId,
			uid,
			0,
			pType,
			time.Now(),
			time.Now(),

		}
		service.UpdatePost(post)
		common.Success(w,post)
	}


}

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

func SavePost(post *models.Post)  {
	dao.SavePost(post)
}
func UpdatePost(post *models.Post)  {
	dao.UpdatePost(post)
}
1
2
3
4
5
6
7
func UpdatePost(post *models.Post)  {
	_ ,err :=DB.Exec("update blog_post set title=?,content=?,markdown=?,category_id=?,type=?,slug=?,update_at=? where pid=?",
		post.Title,
		post.Content,
		post.Markdown,
		post.CategoryId,
		post.Type,
		post.Slug,
		post.UpdateAt,
		post.Pid,
		)
	if err != nil {
		log.Println(err)
	}
}
func SavePost(post *models.Post)  {
	ret ,err := DB.Exec("insert into blog_post " +
		"(title,content,markdown,category_id,user_id,view_count,type,slug,create_at,update_at) " +
		"values(?,?,?,?,?,?,?,?,?,?)",
		post.Title,
		post.Content,
		post.Markdown,
		post.CategoryId,
		post.UserId,
		post.ViewCount,
		post.Type,
		post.Slug,
		post.CreateAt,
		post.UpdateAt,
		)
	if err != nil {
		log.Println(err)
	}
	pid, _ := ret.LastInsertId()
	post.Pid = int(pid)
}
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

3. 写文章-图片上传

package controller

import (
	"github.com/qiniu/go-sdk/v7/auth/qbox"
	"github.com/qiniu/go-sdk/v7/storage"
	"goblog/common"
	"goblog/config"
	"goblog/dao"
	"net/http"
)

func Writing(w http.ResponseWriter, r *http.Request)  {
	writing := common.Template.Writing
	categorys := dao.GetCategorys()
	m := make(map[string]interface{})
	m["categorys"] = categorys
	m["CdnURL"] = config.Cfg.System.CdnURL
	m["Title"] = config.Cfg.Viewer.Title
	writing.WriteData(w,m)
}

func QiniuToken(w http.ResponseWriter,r *http.Request)  {
	//自定义凭证有效期(示例2小时,Expires 单位为秒,为上传凭证的有效时间)
	bucket := "mszlu"
	putPolicy := storage.PutPolicy{
		Scope: bucket,
	}
	putPolicy.Expires = 7200 //示例2小时有效期
	mac := qbox.NewMac(config.Cfg.System.QiniuAccessKey, config.Cfg.System.QiniuSecretKey)
	upToken := putPolicy.UploadToken(mac)
	common.Success(w,upToken)
}
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