优化了代码
parent
bc2de61c4d
commit
f12f001664
|
@ -1,4 +1,4 @@
|
|||
package mail
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -8,7 +8,11 @@ import (
|
|||
"os"
|
||||
)
|
||||
|
||||
func OnUpload(c *gin.Context) {
|
||||
type FileController struct {
|
||||
|
||||
}
|
||||
|
||||
func (this *FileController) OnUpload(c *gin.Context) {
|
||||
file, header, err := c.Request.FormFile("upload")
|
||||
filename := 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")
|
||||
filename := header.Filename
|
||||
fmt.Println(header.Filename)
|
|
@ -1,4 +1,4 @@
|
|||
package mail
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
|
@ -15,10 +15,11 @@ import (
|
|||
"text/template"
|
||||
"time"
|
||||
"user/config"
|
||||
"user/controller"
|
||||
"user/db"
|
||||
)
|
||||
type MailController struct {
|
||||
|
||||
}
|
||||
type RespJson struct {
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
|
@ -67,7 +68,7 @@ type ReqSendEmail struct {
|
|||
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
|
||||
hp := strings.Split(host, ":")
|
||||
auth := smtp.PlainAuth("", user, password, hp[0])
|
||||
|
@ -235,7 +236,7 @@ func OnSendEmailTpl(c *gin.Context) {
|
|||
//content :网页模板的参数 key-value结构
|
||||
//temp_data 模板内具体要替换的变量名字 Key-value结构
|
||||
//generate 是否生成静态html
|
||||
func OnSendEmailCode(c *gin.Context) {
|
||||
func (this *MailController)OnSendEmailCode(c *gin.Context) {
|
||||
var req ReqSendEmail
|
||||
var resp RespJson
|
||||
defer func() {
|
||||
|
@ -268,11 +269,11 @@ func OnSendEmailCode(c *gin.Context) {
|
|||
//抄送给自己
|
||||
//e = SendToMail(user,password,host,req.From,req.Template,req.Content,"html")
|
||||
//发送
|
||||
verCode := controller.CreateVerify(6)
|
||||
verCode := CreateVerify(6)
|
||||
content := "您的验证码是" + verCode
|
||||
userKey := fmt.Sprintf("user_%s_verify", req.Email)
|
||||
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 {
|
||||
log.Println(e.Error())
|
||||
resp.Msg = "Error"
|
|
@ -1,30 +1,33 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/fatih/structs"
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/tommy351/gin-sessions"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/smtp"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"user/config"
|
||||
"user/db"
|
||||
"user/logs"
|
||||
"user/model"
|
||||
"user/redis"
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/fatih/structs"
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/tommy351/gin-sessions"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/smtp"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"user/config"
|
||||
"user/db"
|
||||
"user/logs"
|
||||
"user/model"
|
||||
"user/redis"
|
||||
)
|
||||
type UserController struct{
|
||||
|
||||
}
|
||||
|
||||
type ReqSendEmailCode struct {
|
||||
EmailAdress string `json:"email_address"`
|
||||
|
@ -42,7 +45,7 @@ type RespBase struct {
|
|||
Data interface{}
|
||||
}
|
||||
|
||||
func Auth(c *gin.Context) {
|
||||
func (this *UserController)Auth(c *gin.Context) {
|
||||
var resp RespBase
|
||||
var statuscode int
|
||||
|
||||
|
@ -78,14 +81,14 @@ func Auth(c *gin.Context) {
|
|||
// @Param q query string false "name search by q"
|
||||
// @Success 200 {array} util.RespBase
|
||||
// @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
|
||||
resp.Msg = "操作失败"
|
||||
resp.Status = 20
|
||||
|
@ -121,7 +124,7 @@ func GetUser(c *gin.Context) {
|
|||
// @Param department_id query string false "name search by q"
|
||||
// @Param permission_type query string false "name search by q"
|
||||
// @Router /api/users [get]
|
||||
func Users(c *gin.Context) {
|
||||
func (this *UserController) Users(c *gin.Context) {
|
||||
var statuscode int
|
||||
var resp RespBase
|
||||
|
||||
|
@ -163,7 +166,7 @@ func Users(c *gin.Context) {
|
|||
// @Param department_id query string false "name search by q"
|
||||
// @Param permission_type query string false "name search by q"
|
||||
// @Router /api/users [get]
|
||||
func SerarchUsers(c *gin.Context) {
|
||||
func (this *UserController) SerarchUsers(c *gin.Context) {
|
||||
var statuscode int
|
||||
var resp RespBase
|
||||
|
||||
|
@ -248,7 +251,7 @@ func DefaultOption(c *gin.Context) {
|
|||
// @Failure 404 {object} util.RespBase
|
||||
// @Failure 500 {object} util.RespBase
|
||||
// @Router /api/login [post]
|
||||
func Login(c *gin.Context) {
|
||||
func (this *UserController) Login(c *gin.Context) {
|
||||
type LoginReq struct {
|
||||
RememberMe int32 `json:"remember_me"`
|
||||
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 {
|
||||
DisplayName string `json:"display_name"`
|
||||
EmailAdress string `json:"email_address"`
|
||||
|
@ -405,7 +408,7 @@ func Register(c *gin.Context) {
|
|||
resp.Status = 0
|
||||
}
|
||||
|
||||
func Logout(c *gin.Context) {
|
||||
func (this *UserController) Logout(c *gin.Context) {
|
||||
var resp RespBase
|
||||
|
||||
resp.Msg = "退出成功"
|
||||
|
@ -489,7 +492,7 @@ func SendToMail(title, user string, password string, host string, to string, con
|
|||
return err
|
||||
}
|
||||
|
||||
func SendEmailCode(c *gin.Context) {
|
||||
func (this *UserController) SendEmailCode(c *gin.Context) {
|
||||
var req ReqSendEmailCode
|
||||
var resp RespBase = RespBase{Msg: "邮件已经存在", Status: 0}
|
||||
statusCode := 200
|
||||
|
@ -506,7 +509,12 @@ func SendEmailCode(c *gin.Context) {
|
|||
}
|
||||
//判断邮箱是否存在
|
||||
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 {
|
||||
statusCode = 422
|
||||
return
|
64
main.go
64
main.go
|
@ -7,11 +7,21 @@ import (
|
|||
"strconv"
|
||||
"user/config"
|
||||
"user/controller"
|
||||
"user/controller/mail"
|
||||
"user/db"
|
||||
"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() {
|
||||
c := config.GetMysqlConfig()
|
||||
if c == nil {
|
||||
|
@ -20,6 +30,16 @@ func InitMysql() {
|
|||
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) {
|
||||
ori := c.Request.Header.Get("Origin")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
||||
|
@ -37,42 +57,38 @@ func CORSMiddleware(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
e := config.Init("user.yaml")
|
||||
if nil != e {
|
||||
log.Println(e.Error())
|
||||
}
|
||||
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
|
||||
}
|
||||
InitConfig()
|
||||
InitLogs()
|
||||
InitRedis()
|
||||
InitMysql()
|
||||
|
||||
r := gin.Default()
|
||||
store := sessions.NewCookieStore([]byte("secret123"))
|
||||
r.Use(sessions.Middleware("my_session", store))
|
||||
r.Use(sessions.Middleware("sess_store", store))
|
||||
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) **/
|
||||
r.GET("/api/user", controller.GetUser)
|
||||
api.GET("/api/user", userController.GetUser)
|
||||
/** 获取所有用户 **/
|
||||
r.GET("/api/users", controller.Users)
|
||||
r.POST("/api/search_users", controller.SerarchUsers)
|
||||
api.GET("/api/users", userController.Users)
|
||||
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)
|
||||
r.POST("/api/verify", mail.OnSendEmailCode)
|
||||
api.GET("/api/logout", userController.Logout)
|
||||
api.POST("/api/verify", mailContoller.OnSendEmailCode)
|
||||
}
|
||||
e = r.Run(":" + strconv.Itoa(config.GetPort()))
|
||||
if nil != e{
|
||||
e := r.Run(":" + strconv.Itoa(config.GetPort()))
|
||||
if nil != e {
|
||||
log.Print(e.Error())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue