优化了代码
parent
bc2de61c4d
commit
f12f001664
|
@ -1,4 +1,4 @@
|
||||||
package mail
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -8,7 +8,11 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func OnUpload(c *gin.Context) {
|
type FileController struct {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (this *FileController) OnUpload(c *gin.Context) {
|
||||||
file, header, err := c.Request.FormFile("upload")
|
file, header, err := c.Request.FormFile("upload")
|
||||||
filename := header.Filename
|
filename := header.Filename
|
||||||
fmt.Println(header.Filename)
|
fmt.Println(header.Filename)
|
||||||
|
@ -23,7 +27,7 @@ func OnUpload(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func OnDownLoad(c *gin.Context) {
|
func (this *FileController) OnDownLoad(c *gin.Context) {
|
||||||
file, header, err := c.Request.FormFile("upload")
|
file, header, err := c.Request.FormFile("upload")
|
||||||
filename := header.Filename
|
filename := header.Filename
|
||||||
fmt.Println(header.Filename)
|
fmt.Println(header.Filename)
|
|
@ -1,4 +1,4 @@
|
||||||
package mail
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
@ -15,10 +15,11 @@ import (
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
"user/config"
|
"user/config"
|
||||||
"user/controller"
|
|
||||||
"user/db"
|
"user/db"
|
||||||
)
|
)
|
||||||
|
type MailController struct {
|
||||||
|
|
||||||
|
}
|
||||||
type RespJson struct {
|
type RespJson struct {
|
||||||
Msg string `json:"msg"`
|
Msg string `json:"msg"`
|
||||||
Data interface{} `json:"data,omitempty"`
|
Data interface{} `json:"data,omitempty"`
|
||||||
|
@ -67,7 +68,7 @@ type ReqSendEmail struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendToMail(title, user, password, host, to, content string) error {
|
func SendToMailWithUserInfo(title, user, password, host, to, content string) error {
|
||||||
var content_type string
|
var content_type string
|
||||||
hp := strings.Split(host, ":")
|
hp := strings.Split(host, ":")
|
||||||
auth := smtp.PlainAuth("", user, password, hp[0])
|
auth := smtp.PlainAuth("", user, password, hp[0])
|
||||||
|
@ -235,7 +236,7 @@ func OnSendEmailTpl(c *gin.Context) {
|
||||||
//content :网页模板的参数 key-value结构
|
//content :网页模板的参数 key-value结构
|
||||||
//temp_data 模板内具体要替换的变量名字 Key-value结构
|
//temp_data 模板内具体要替换的变量名字 Key-value结构
|
||||||
//generate 是否生成静态html
|
//generate 是否生成静态html
|
||||||
func OnSendEmailCode(c *gin.Context) {
|
func (this *MailController)OnSendEmailCode(c *gin.Context) {
|
||||||
var req ReqSendEmail
|
var req ReqSendEmail
|
||||||
var resp RespJson
|
var resp RespJson
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -268,11 +269,11 @@ func OnSendEmailCode(c *gin.Context) {
|
||||||
//抄送给自己
|
//抄送给自己
|
||||||
//e = SendToMail(user,password,host,req.From,req.Template,req.Content,"html")
|
//e = SendToMail(user,password,host,req.From,req.Template,req.Content,"html")
|
||||||
//发送
|
//发送
|
||||||
verCode := controller.CreateVerify(6)
|
verCode := CreateVerify(6)
|
||||||
content := "您的验证码是" + verCode
|
content := "您的验证码是" + verCode
|
||||||
userKey := fmt.Sprintf("user_%s_verify", req.Email)
|
userKey := fmt.Sprintf("user_%s_verify", req.Email)
|
||||||
config.RedisOne().Set(userKey, verCode, time.Hour*24)
|
config.RedisOne().Set(userKey, verCode, time.Hour*24)
|
||||||
e = SendToMail("后台管理系统验证码", user, password, host, req.Email, content)
|
e = SendToMailWithUserInfo("后台管理系统验证码", user, password, host, req.Email, content)
|
||||||
if nil != e {
|
if nil != e {
|
||||||
log.Println(e.Error())
|
log.Println(e.Error())
|
||||||
resp.Msg = "Error"
|
resp.Msg = "Error"
|
|
@ -25,6 +25,9 @@ import (
|
||||||
"user/model"
|
"user/model"
|
||||||
"user/redis"
|
"user/redis"
|
||||||
)
|
)
|
||||||
|
type UserController struct{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
type ReqSendEmailCode struct {
|
type ReqSendEmailCode struct {
|
||||||
EmailAdress string `json:"email_address"`
|
EmailAdress string `json:"email_address"`
|
||||||
|
@ -42,7 +45,7 @@ type RespBase struct {
|
||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Auth(c *gin.Context) {
|
func (this *UserController)Auth(c *gin.Context) {
|
||||||
var resp RespBase
|
var resp RespBase
|
||||||
var statuscode int
|
var statuscode int
|
||||||
|
|
||||||
|
@ -78,14 +81,14 @@ func Auth(c *gin.Context) {
|
||||||
// @Param q query string false "name search by q"
|
// @Param q query string false "name search by q"
|
||||||
// @Success 200 {array} util.RespBase
|
// @Success 200 {array} util.RespBase
|
||||||
// @Router /accounts [get]
|
// @Router /accounts [get]
|
||||||
func SetUser(c *gin.Context) {
|
func (this *UserController) SetUser(c *gin.Context) {
|
||||||
|
|
||||||
}
|
}
|
||||||
func DelUser(c *gin.Context) {
|
func (this *UserController) DelUser(c *gin.Context) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUser(c *gin.Context) {
|
func (this *UserController) GetUser(c *gin.Context) {
|
||||||
var resp RespBase
|
var resp RespBase
|
||||||
resp.Msg = "操作失败"
|
resp.Msg = "操作失败"
|
||||||
resp.Status = 20
|
resp.Status = 20
|
||||||
|
@ -121,7 +124,7 @@ func GetUser(c *gin.Context) {
|
||||||
// @Param department_id query string false "name search by q"
|
// @Param department_id query string false "name search by q"
|
||||||
// @Param permission_type query string false "name search by q"
|
// @Param permission_type query string false "name search by q"
|
||||||
// @Router /api/users [get]
|
// @Router /api/users [get]
|
||||||
func Users(c *gin.Context) {
|
func (this *UserController) Users(c *gin.Context) {
|
||||||
var statuscode int
|
var statuscode int
|
||||||
var resp RespBase
|
var resp RespBase
|
||||||
|
|
||||||
|
@ -163,7 +166,7 @@ func Users(c *gin.Context) {
|
||||||
// @Param department_id query string false "name search by q"
|
// @Param department_id query string false "name search by q"
|
||||||
// @Param permission_type query string false "name search by q"
|
// @Param permission_type query string false "name search by q"
|
||||||
// @Router /api/users [get]
|
// @Router /api/users [get]
|
||||||
func SerarchUsers(c *gin.Context) {
|
func (this *UserController) SerarchUsers(c *gin.Context) {
|
||||||
var statuscode int
|
var statuscode int
|
||||||
var resp RespBase
|
var resp RespBase
|
||||||
|
|
||||||
|
@ -248,7 +251,7 @@ func DefaultOption(c *gin.Context) {
|
||||||
// @Failure 404 {object} util.RespBase
|
// @Failure 404 {object} util.RespBase
|
||||||
// @Failure 500 {object} util.RespBase
|
// @Failure 500 {object} util.RespBase
|
||||||
// @Router /api/login [post]
|
// @Router /api/login [post]
|
||||||
func Login(c *gin.Context) {
|
func (this *UserController) Login(c *gin.Context) {
|
||||||
type LoginReq struct {
|
type LoginReq struct {
|
||||||
RememberMe int32 `json:"remember_me"`
|
RememberMe int32 `json:"remember_me"`
|
||||||
UserName string `json:"user_name"`
|
UserName string `json:"user_name"`
|
||||||
|
@ -324,7 +327,7 @@ func Login(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Register(c *gin.Context) {
|
func (this *UserController) Register(c *gin.Context) {
|
||||||
type RegisterReq struct {
|
type RegisterReq struct {
|
||||||
DisplayName string `json:"display_name"`
|
DisplayName string `json:"display_name"`
|
||||||
EmailAdress string `json:"email_address"`
|
EmailAdress string `json:"email_address"`
|
||||||
|
@ -405,7 +408,7 @@ func Register(c *gin.Context) {
|
||||||
resp.Status = 0
|
resp.Status = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func Logout(c *gin.Context) {
|
func (this *UserController) Logout(c *gin.Context) {
|
||||||
var resp RespBase
|
var resp RespBase
|
||||||
|
|
||||||
resp.Msg = "退出成功"
|
resp.Msg = "退出成功"
|
||||||
|
@ -489,7 +492,7 @@ func SendToMail(title, user string, password string, host string, to string, con
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendEmailCode(c *gin.Context) {
|
func (this *UserController) SendEmailCode(c *gin.Context) {
|
||||||
var req ReqSendEmailCode
|
var req ReqSendEmailCode
|
||||||
var resp RespBase = RespBase{Msg: "邮件已经存在", Status: 0}
|
var resp RespBase = RespBase{Msg: "邮件已经存在", Status: 0}
|
||||||
statusCode := 200
|
statusCode := 200
|
||||||
|
@ -506,7 +509,12 @@ func SendEmailCode(c *gin.Context) {
|
||||||
}
|
}
|
||||||
//判断邮箱是否存在
|
//判断邮箱是否存在
|
||||||
var users []model.Users
|
var users []model.Users
|
||||||
db.GetMysqlClient().Query2("select * from users where email_adress = ?", &users, req.EmailAdress)
|
e = db.GetMysqlClient().Query2("select * from users where email_adress = ?", &users, req.EmailAdress)
|
||||||
|
if nil != e{
|
||||||
|
log.Print(e.Error())
|
||||||
|
resp.Msg = "email address error"
|
||||||
|
return
|
||||||
|
}
|
||||||
if len(users) != 0 {
|
if len(users) != 0 {
|
||||||
statusCode = 422
|
statusCode = 422
|
||||||
return
|
return
|
62
main.go
62
main.go
|
@ -7,11 +7,21 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"user/config"
|
"user/config"
|
||||||
"user/controller"
|
"user/controller"
|
||||||
"user/controller/mail"
|
|
||||||
"user/db"
|
"user/db"
|
||||||
"user/logs"
|
"user/logs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
userController = controller.UserController{}
|
||||||
|
mailContoller = controller.MailController{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitConfig() {
|
||||||
|
e := config.Init("user.yaml")
|
||||||
|
if nil != e {
|
||||||
|
log.Println(e.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
func InitMysql() {
|
func InitMysql() {
|
||||||
c := config.GetMysqlConfig()
|
c := config.GetMysqlConfig()
|
||||||
if c == nil {
|
if c == nil {
|
||||||
|
@ -20,6 +30,16 @@ func InitMysql() {
|
||||||
db.Init()
|
db.Init()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func InitRedis() {
|
||||||
|
e := config.InitRedis()
|
||||||
|
if nil != e {
|
||||||
|
logs.Error(e.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func InitLogs() {
|
||||||
|
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
|
||||||
|
}
|
||||||
func CORSMiddleware(c *gin.Context) {
|
func CORSMiddleware(c *gin.Context) {
|
||||||
ori := c.Request.Header.Get("Origin")
|
ori := c.Request.Header.Get("Origin")
|
||||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
||||||
|
@ -37,41 +57,37 @@ func CORSMiddleware(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
e := config.Init("user.yaml")
|
InitConfig()
|
||||||
if nil != e {
|
InitLogs()
|
||||||
log.Println(e.Error())
|
InitRedis()
|
||||||
}
|
InitMysql()
|
||||||
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
|
|
||||||
db.Init()
|
|
||||||
e = config.InitRedis()
|
|
||||||
if nil != e {
|
|
||||||
logs.Error(e.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
store := sessions.NewCookieStore([]byte("secret123"))
|
store := sessions.NewCookieStore([]byte("secret123"))
|
||||||
r.Use(sessions.Middleware("my_session", store))
|
r.Use(sessions.Middleware("sess_store", store))
|
||||||
r.Use(CORSMiddleware)
|
r.Use(CORSMiddleware)
|
||||||
|
api := r.Group("/api")
|
||||||
{
|
{
|
||||||
/** 添加或修改用户 **/
|
/** 添加或修改用户 **/
|
||||||
r.POST("/api/user", controller.SetUser)
|
api.POST("/api/user", userController.SetUser)
|
||||||
/** 删除用户 **/
|
/** 删除用户 **/
|
||||||
r.DELETE("/api/user", controller.DelUser)
|
api.DELETE("/api/user", userController.DelUser)
|
||||||
/** 获取单独用户详情信息 methods(id) **/
|
/** 获取单独用户详情信息 methods(id) **/
|
||||||
r.GET("/api/user", controller.GetUser)
|
api.GET("/api/user", userController.GetUser)
|
||||||
/** 获取所有用户 **/
|
/** 获取所有用户 **/
|
||||||
r.GET("/api/users", controller.Users)
|
api.GET("/api/users", userController.Users)
|
||||||
r.POST("/api/search_users", controller.SerarchUsers)
|
api.POST("/api/search_users", userController.SerarchUsers)
|
||||||
/** 用户登录 **/
|
/** 用户登录 **/
|
||||||
r.POST("/api/login", controller.Login)
|
api.POST("/api/login", userController.Login)
|
||||||
/** 用户注册 **/
|
/** 用户注册 **/
|
||||||
r.POST("/api/register", controller.Register)
|
api.POST("/api/register", userController.Register)
|
||||||
/** 用户退出登陆 **/
|
/** 用户退出登陆 **/
|
||||||
r.GET("/api/logout", controller.Logout)
|
api.GET("/api/logout", userController.Logout)
|
||||||
r.POST("/api/verify", mail.OnSendEmailCode)
|
api.POST("/api/verify", mailContoller.OnSendEmailCode)
|
||||||
}
|
}
|
||||||
e = r.Run(":" + strconv.Itoa(config.GetPort()))
|
e := r.Run(":" + strconv.Itoa(config.GetPort()))
|
||||||
if nil != e {
|
if nil != e {
|
||||||
log.Print(e.Error())
|
log.Print(e.Error())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue