@@ -0,0 +1,9 @@
|
||||
package routers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func V1_cookie_api(r *gin.RouterGroup) {
|
||||
r.GET("/test", func(ctx *gin.Context) {
|
||||
ctx.SetCookie("test", "testcookie", 100, "/", "127.0.0.1", false, true)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var Error_code map[string]interface{}
|
||||
|
||||
func init() {
|
||||
//读取默认配置
|
||||
fmt.Println("尝试读取错误码文件")
|
||||
data, err := os.ReadFile("./def_config/error_codes.json")
|
||||
if err != nil {
|
||||
|
||||
fmt.Println("读取错误码文件失败", err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &Error_code); err != nil {
|
||||
fmt.Println("解析错误码文件失败", err)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"saas/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func V1_file_api(r *gin.RouterGroup) {
|
||||
r.GET("/", func(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, map[string]interface{}{
|
||||
"error": "you need use Post",
|
||||
})
|
||||
})
|
||||
r.POST("/upload", func(ctx *gin.Context) {
|
||||
file, err := ctx.FormFile("file")
|
||||
if err == nil {
|
||||
dst := path.Join("./data/upload", file.Filename)
|
||||
ctx.SaveUploadedFile(file, dst)
|
||||
}
|
||||
})
|
||||
|
||||
//接收头像的接口,
|
||||
r.POST("/avatar", func(ctx *gin.Context) {
|
||||
//返回前端的数据
|
||||
err_msg := "user_api_error"
|
||||
err_code := Error_code[err_msg]
|
||||
data := map[string]interface{}{}
|
||||
|
||||
//判断权限是否可以接收
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
if is_login == true {
|
||||
//读取用户id信息
|
||||
user, _ := ctx.Get("user")
|
||||
//保存这个头像
|
||||
file, err := ctx.FormFile("file")
|
||||
if err == nil {
|
||||
//限制文件大小
|
||||
if file.Size > 512 {
|
||||
if file.Size < 1024000 {
|
||||
// 2. 安全获取文件名并处理路径问题
|
||||
filename := filepath.Base(file.Filename) // 防御性处理路径分隔符
|
||||
// 3. 获取标准后缀名(含点)
|
||||
extWithDot := filepath.Ext(filename)
|
||||
//判断后缀名类型是否是允许的
|
||||
if models.Allowed_avatar_ext[extWithDot] {
|
||||
//判断文件mime是否合法
|
||||
// 打开文件流
|
||||
src_mime, _ := file.Open()
|
||||
defer src_mime.Close()
|
||||
// 读取前512字节用于MIME检测
|
||||
buffer := make([]byte, 512)
|
||||
io.ReadFull(src_mime, buffer)
|
||||
// 检测MIME类型
|
||||
mimeType := http.DetectContentType(buffer)
|
||||
if models.Allowed_avatar_mime[mimeType] {
|
||||
// 打开文件流
|
||||
src, _ := file.Open()
|
||||
defer src.Close()
|
||||
// 创建SHA256哈希器
|
||||
hasher := sha256.New()
|
||||
|
||||
// 计算哈希值
|
||||
io.Copy(hasher, src)
|
||||
// 获取哈希结果
|
||||
hashBytes := hasher.Sum(nil)
|
||||
hashString := hex.EncodeToString(hashBytes)
|
||||
|
||||
new_filename := fmt.Sprintf("%d_%s%s", user.(*models.User).ID, hashString, extWithDot)
|
||||
file.Filename = new_filename
|
||||
|
||||
//这是上传的真实路径
|
||||
dst := path.Join("./data/avatar", file.Filename)
|
||||
|
||||
//这是经过gin路由的路径
|
||||
gin_dat := path.Join("/avatar", file.Filename)
|
||||
|
||||
//判断文件是否存在避免重复保存
|
||||
if models.File_exists(dst) {
|
||||
//fmt.Println("文件存在")
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
//返回gin路由的路径
|
||||
data["path"] = gin_dat
|
||||
data["new_path"] = false
|
||||
} else {
|
||||
//fmt.Println("文件no存在")
|
||||
ferr := ctx.SaveUploadedFile(file, dst)
|
||||
if ferr == nil {
|
||||
//文件保存成功
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
//返回gin路由的路径
|
||||
data["path"] = gin_dat
|
||||
data["new_path"] = true
|
||||
|
||||
} else {
|
||||
err_msg = "file_save_err"
|
||||
err_code = Error_code[err_msg]
|
||||
fmt.Println(ferr)
|
||||
data["err"] = ferr
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err_msg = "file_mime_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
err_msg = "file_type_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
} else {
|
||||
err_msg = "file_size_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
} else {
|
||||
err_msg = "file_size_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
err_msg = "file_get_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
} else {
|
||||
err_msg = "user_no_sign"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
"data": data,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Api_router(r *gin.RouterGroup) {
|
||||
|
||||
r.GET("/", func(ctx *gin.Context) {
|
||||
ctx.String(http.StatusOK, "api")
|
||||
})
|
||||
|
||||
v1_api := r.Group("/v1/")
|
||||
{
|
||||
V1_user_api(v1_api.Group("/user/"))
|
||||
V1_file_api(v1_api.Group("/file/"))
|
||||
V1_cookie_api(v1_api.Group("/cookie/"))
|
||||
V1_warehouses_api(v1_api.Group("/warehouses_api/"))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,349 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"saas/models"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func V1_user_api(r *gin.RouterGroup) {
|
||||
|
||||
var err_code = Error_code["api_ok"]
|
||||
var err_msg string
|
||||
|
||||
r.POST("/add", func(ctx *gin.Context) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||
//转换字段
|
||||
newUser := models.User{
|
||||
Name: jsonData["username"].(string),
|
||||
Email: jsonData["useremail"].(string),
|
||||
Pass: jsonData["userpass"].(string), // 实际应替换为哈希值
|
||||
Date: time.Now(),
|
||||
// Date 字段无需赋值,数据库会自动填充默认值
|
||||
}
|
||||
//对用户的密码进行哈希替换
|
||||
newUser.Pass = models.Hash_user_pass(newUser.Pass)
|
||||
|
||||
//用户名是唯一的,先读取是否有这个用户名
|
||||
var user models.User
|
||||
user.Name = newUser.Name
|
||||
|
||||
if models.DB.Where(&user).First(&user).Error == nil {
|
||||
//fmt.Println("找到用户:", user.ID)
|
||||
err_msg = "user_name_dup"
|
||||
err_code = Error_code[err_msg]
|
||||
} else {
|
||||
//fmt.Println("用户不存在")
|
||||
models.DB.Create(&newUser) // 传入指针
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
r.POST("/login", func(ctx *gin.Context) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err != nil {
|
||||
fmt.Println("解析JSON ERROR:", err)
|
||||
panic(err)
|
||||
}
|
||||
//转换字段
|
||||
newUser := models.User{
|
||||
Name: jsonData["username"].(string),
|
||||
Pass: jsonData["userpass"].(string), // 实际应替换为哈希值
|
||||
// Date 字段无需赋值,数据库会自动填充默认值
|
||||
}
|
||||
//对用户的密码进行哈希替换
|
||||
newUser.Pass = models.Hash_user_pass(newUser.Pass)
|
||||
|
||||
var user models.User
|
||||
user.Name = newUser.Name
|
||||
if models.DB.Where(&user).First(&user).Error == nil {
|
||||
// 有数据
|
||||
|
||||
if user.Pass == newUser.Pass {
|
||||
//成功登录
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
//发送cookie
|
||||
//cookie时间
|
||||
var cookie_time = 0
|
||||
if jsonData["keep_login"].(bool) {
|
||||
cookie_time = models.User_configs["cookie_timeout"].(int)
|
||||
}
|
||||
|
||||
cookie := models.Rand_str_32() //生成32字节cookie
|
||||
//cookie := "testcookie"
|
||||
//fmt.Println(cookie)
|
||||
//将cookie写进数据库
|
||||
new_cookie := models.Cookie{}
|
||||
new_cookie.Domain = models.Wed_configs["host"].(string)
|
||||
new_cookie.Name = "user"
|
||||
new_cookie.Value = cookie
|
||||
new_cookie.UserID = user.ID
|
||||
|
||||
//cookie时间
|
||||
new_cookie.CreatedAt = time.Now()
|
||||
new_cookie.UpdatedAt = new_cookie.CreatedAt
|
||||
//计算cookie失效时间
|
||||
new_cookie.ExpiresAt = time.Now().Add(time.Duration(models.User_configs["cookie_timeout"].(int)) * time.Second) //计算过期时间
|
||||
new_cookie.SecureFlag = models.Wed_configs["tls"].(bool)
|
||||
ctx.SetCookie("user", cookie, cookie_time, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
|
||||
models.DB.Create(&new_cookie) // 传入指针
|
||||
|
||||
} else {
|
||||
//密码错误
|
||||
err_msg = "user_password_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("用户不存在")
|
||||
err_msg = "user_name_nofind"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
r.POST("/logout", func(ctx *gin.Context) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
if is_login == true {
|
||||
//fmt.Println("loged")
|
||||
cookie_vel, _ := ctx.Cookie("user") //这个cookie在中间件已经判断为有效的,否则is_login不可能为true,所以直接在数据库删除应该是安全的
|
||||
//删除数据库里的cookie
|
||||
var cookie models.Cookie
|
||||
cookie.Value = cookie_vel
|
||||
models.DB.Where(&cookie).Delete(&cookie)
|
||||
//删除前端cookie
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
} else {
|
||||
//fmt.Println("no loged")
|
||||
err_msg = "user_no_sign"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
})
|
||||
|
||||
r.POST("/updata_info", func(ctx *gin.Context) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
if is_login == true {
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||
|
||||
user_info_, _ := ctx.Get("user_info")
|
||||
user_info, _ := user_info_.(*models.User_info) //这个数据本身就是从数据库读出来的,理论上结构转换不会出错
|
||||
user_info_find := models.User_info{
|
||||
ID: user_info.ID,
|
||||
}
|
||||
|
||||
new_user_info := models.User_info{
|
||||
AvatarPath: jsonData["avatar"].(string),
|
||||
FirstName: jsonData["first_name"].(string),
|
||||
Username: jsonData["username"].(string),
|
||||
Birthdate: models.Time_date_str_to_time(jsonData["birthday"].(string)),
|
||||
}
|
||||
|
||||
//需要验证传入数据的合法性 例如头像url是否站内的
|
||||
if strings.HasPrefix(new_user_info.AvatarPath, models.User_configs["def_avatar_ginrouter_path"].(string)) {
|
||||
|
||||
} else {
|
||||
new_user_info.AvatarPath = models.User_configs["def_avatar_path"].(string)
|
||||
}
|
||||
|
||||
//fmt.Printf("%%#v: %#v\n", new_user_info)
|
||||
models.DB.Where(&user_info_find).Updates(&new_user_info)
|
||||
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
} else {
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("no loged")
|
||||
err_msg = "user_no_sign"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
})
|
||||
|
||||
r.POST("/change_email", func(ctx *gin.Context) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
if is_login == true {
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||
|
||||
//需要验证传入数据的合法性
|
||||
if models.Is_email_valid(jsonData["new_email"].(string)) {
|
||||
user_, _ := ctx.Get("user")
|
||||
user, _ := user_.(*models.User)
|
||||
user_find := models.User{
|
||||
ID: user.ID,
|
||||
}
|
||||
user_new := models.User{
|
||||
Email: jsonData["new_email"].(string),
|
||||
}
|
||||
models.DB.Where(&user_find).Updates(&user_new)
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
} else {
|
||||
err_msg = "email_error"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("no loged")
|
||||
err_msg = "user_no_sign"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
})
|
||||
|
||||
r.POST("/change_pass", func(ctx *gin.Context) {
|
||||
//返回前端的数据
|
||||
err_msg = "user_api_error"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
if is_login == true {
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||
|
||||
//需要验证传入数据的合法性
|
||||
|
||||
//读取已登录的用户信息
|
||||
user_, _ := ctx.Get("user")
|
||||
user, _ := user_.(*models.User)
|
||||
user_find := models.User{
|
||||
ID: user.ID,
|
||||
}
|
||||
|
||||
models.DB.Where(&user_find).First(&user_find)
|
||||
|
||||
pass_old := jsonData["pass_old"].(string)
|
||||
pass_new := jsonData["pass_new"].(string)
|
||||
//对用户的密码进行哈希替换
|
||||
pass_old = models.Hash_user_pass(pass_old)
|
||||
pass_new = models.Hash_user_pass(pass_new)
|
||||
|
||||
if user_find.Pass == pass_old {
|
||||
|
||||
new_user := models.User{
|
||||
Pass: pass_new,
|
||||
}
|
||||
|
||||
//修改密码
|
||||
models.DB.Where(&user_find).Updates(&new_user)
|
||||
|
||||
//密码修改后所有cookie都应该失效
|
||||
cookie_find := models.Cookie{
|
||||
UserID: user.ID,
|
||||
}
|
||||
models.DB.Where(&cookie_find).Delete(&cookie_find)
|
||||
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
} else {
|
||||
err_msg = "user_password_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("no loged")
|
||||
err_msg = "user_no_sign"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"saas/models"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 定义接收JSON数据的结构体
|
||||
type Add_item_json struct {
|
||||
// 使用结构体标签指定JSON字段名
|
||||
WarehouseID uint `json:"warehouse_id"`
|
||||
Name string `json:"item_name"`
|
||||
Info string `json:"item_info"`
|
||||
SN string `json:"item_sn"`
|
||||
Who string `json:"item_who"`
|
||||
Int int `json:"item_int"`
|
||||
Consts float32 `json:"item_consts"`
|
||||
}
|
||||
|
||||
func V1_warehouses_api(r *gin.RouterGroup) {
|
||||
var err_code = Error_code["api_ok"]
|
||||
var err_msg string
|
||||
|
||||
r.POST("/create", func(ctx *gin.Context) {
|
||||
err_msg = "warehouses_api_err"
|
||||
err_code = Error_code[err_msg]
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
if is_login == true {
|
||||
user_info, _ := ctx.Get("user_info")
|
||||
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||
//fmt.Println(jsonData)
|
||||
if jsonData["warehouses_name"].(string) != "" {
|
||||
warehouses_data := models.Warehouse{
|
||||
Name: jsonData["warehouses_name"].(string),
|
||||
Info: jsonData["warehouses_info"].(string),
|
||||
CreatorID: user_info.(*models.User_info).UserID,
|
||||
Capacity: 0,
|
||||
UsedCapacity: 0,
|
||||
Location: "local",
|
||||
}
|
||||
models.DB.Create(&warehouses_data) // 传入指针
|
||||
//fmt.Println(dberr.Error)
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
} else {
|
||||
err_msg = "warehouses_name_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
} else {
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("no loged")
|
||||
err_msg = "user_no_sign"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
r.POST("/add_item", func(ctx *gin.Context) {
|
||||
err_msg = "warehouses_api_err"
|
||||
err_code = Error_code[err_msg]
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
if is_login == true {
|
||||
|
||||
//转换传进来的数据
|
||||
var item Add_item_json
|
||||
if err := ctx.ShouldBindJSON(&item); err == nil {
|
||||
//先判断是否有权限
|
||||
|
||||
//(还没弄)
|
||||
|
||||
//后插入数据
|
||||
user_info_, _ := ctx.Get("user_info")
|
||||
user_info := user_info_.(*models.User_info)
|
||||
var add_item_temp models.WarehouseItem
|
||||
add_item_temp.CreatedByID = user_info.UserID
|
||||
add_item_temp.WarehouseID = item.WarehouseID
|
||||
add_item_temp.Name = item.Name
|
||||
add_item_temp.SerialNumber = item.SN
|
||||
add_item_temp.Description = item.Info
|
||||
add_item_temp.Destiny = item.Who
|
||||
add_item_temp.Quantity = item.Int
|
||||
add_item_temp.ItemValue = int(item.Consts * 100)
|
||||
//插入一条数据
|
||||
models.DB.Create(&add_item_temp)
|
||||
//更新仓库信息
|
||||
var seach_wh models.Warehouse
|
||||
seach_wh.ID = item.WarehouseID
|
||||
var out_wh models.Warehouse
|
||||
models.DB.Where(&seach_wh).First(&out_wh)
|
||||
out_wh.UsedCapacity += 1
|
||||
models.DB.Where(&seach_wh).Updates(&out_wh)
|
||||
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
} else {
|
||||
err_msg = "json_error"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("no loged")
|
||||
err_msg = "user_no_sign"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
r.GET("/get_items/:wh_id", func(ctx *gin.Context) {
|
||||
err_msg = "warehouses_api_err"
|
||||
err_code = Error_code[err_msg]
|
||||
var seachf []models.WarehouseItem
|
||||
//先判断是否已经登录
|
||||
//获取中间件处理的结果
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
if is_login == true {
|
||||
id := ctx.Param("wh_id")
|
||||
id_int, err := strconv.ParseInt(id, 10, 0)
|
||||
if err == nil {
|
||||
if id_int > 0 {
|
||||
|
||||
seachf = models.Warehouse_get_items_from_whid(uint(id_int))
|
||||
//fmt.Println(seachf)
|
||||
|
||||
err_msg = "api_ok"
|
||||
err_code = Error_code[err_msg]
|
||||
|
||||
} else {
|
||||
err_msg = "warehouses_id_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
err_msg = "warehouses_id_err"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
} else {
|
||||
//fmt.Println("no loged")
|
||||
err_msg = "user_no_sign"
|
||||
err_code = Error_code[err_msg]
|
||||
}
|
||||
|
||||
ctx.JSON(200, map[string]interface{}{
|
||||
"api": "ok",
|
||||
"err_code": err_code,
|
||||
"err_msg": err_msg,
|
||||
"data": seachf,
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"saas/models"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Def_router(r *gin.RouterGroup) {
|
||||
r.Use(func(ctx *gin.Context) {
|
||||
ctx.Set("is_login", false)
|
||||
//读取用户cookie,判断用户是否已登录
|
||||
cookie_vel, _ := ctx.Cookie("user")
|
||||
//fmt.Println(cookie_vel)
|
||||
if cookie_vel != "" {
|
||||
var cookie models.Cookie
|
||||
cookie.Value = cookie_vel
|
||||
if models.DB.Where(&cookie).First(&cookie).Error == nil {
|
||||
// 有数据
|
||||
//有cookie,判断cookie有效性
|
||||
if cookie.ExpiresAt.After(time.Now()) {
|
||||
// ExpiresAt 在当前时间之后(未过期)
|
||||
//fmt.Println("Cookie 未过期")
|
||||
//cookie有效,说明已经登录,cookie过期时间延长,避免大量写入数据库,先判断还有多久过期,小于一天才刷新
|
||||
// 计算过期时间与当前时间的差值
|
||||
remaining := time.Until(cookie.ExpiresAt) // 直接使用 time.Until
|
||||
|
||||
if remaining < 24*time.Hour {
|
||||
//fmt.Println("剩余时间不足 1 天")
|
||||
var cookie_up models.Cookie
|
||||
cookie_up.UpdatedAt = time.Now()
|
||||
cookie_up.ExpiresAt = time.Now().Add(time.Duration(models.User_configs["cookie_timeout"].(int)) * time.Second) //计算过期时间
|
||||
models.DB.Model(&models.Cookie{}).Where(&cookie).Updates(&cookie_up)
|
||||
} else {
|
||||
//fmt.Println("cookie时间大于一天")
|
||||
}
|
||||
|
||||
//读取用户权限信息
|
||||
var user models.User
|
||||
user.ID = cookie.UserID
|
||||
if models.DB.Where(&user).First(&user).Error == nil {
|
||||
//找到登录权限
|
||||
//清除一些重要数据,避免传递的时候泄露
|
||||
user.Pass = ""
|
||||
// 读取用户info
|
||||
var user_info models.User_info
|
||||
user_info.UserID = cookie.UserID
|
||||
if models.DB.Where(&user_info).First(&user_info).Error == nil {
|
||||
// 有数据
|
||||
//fmt.Println(user_info)
|
||||
|
||||
} else {
|
||||
// 无数据
|
||||
//创建一个默认info
|
||||
user_info.AvatarPath = models.User_configs["def_avatar_path"].(string)
|
||||
user_info.UserID = cookie.UserID
|
||||
models.DB.Create(&user_info) // 传入指针
|
||||
}
|
||||
|
||||
ctx.Set("is_login", true)
|
||||
ctx.Set("user_info", &user_info)
|
||||
ctx.Set("user", &user)
|
||||
|
||||
} else {
|
||||
//找不到登录权限?? 可能被封号?
|
||||
//删除前端cookie
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
}
|
||||
|
||||
} else {
|
||||
// ExpiresAt 在当前时间之前或等于(已过期)
|
||||
//fmt.Println("Cookie 已过期")
|
||||
//删除数据库的cookie
|
||||
models.DB.Delete(&cookie)
|
||||
//删除前端cookie
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
}
|
||||
} else {
|
||||
//找不到cookie,未登录
|
||||
//删除前端cookie
|
||||
ctx.SetCookie("user", "", -1, "/", models.Wed_configs["host"].(string), models.Wed_configs["tls"].(bool), true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
Api_router(r.Group("/api/")) //分组路由传递到api_routers。go
|
||||
|
||||
//无需权限的页面
|
||||
r.GET("/", func(ctx *gin.Context) {
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
user_info, _ := ctx.Get("user_info")
|
||||
ctx.HTML(http.StatusOK, "index.html", gin.H{
|
||||
"is_login": is_login,
|
||||
"user_info": user_info,
|
||||
})
|
||||
})
|
||||
|
||||
r.GET("/sign-up", func(ctx *gin.Context) {
|
||||
ctx.HTML(http.StatusOK, "sign-up.html", gin.H{})
|
||||
})
|
||||
r.GET("/sign-in", func(ctx *gin.Context) {
|
||||
ctx.HTML(http.StatusOK, "sign-in-01.html", gin.H{})
|
||||
})
|
||||
r.GET("/test", func(ctx *gin.Context) {
|
||||
ctx.HTML(http.StatusOK, "empty.html", gin.H{})
|
||||
})
|
||||
|
||||
//需要权限的页面
|
||||
// r.Use(func(ctx *gin.Context) {
|
||||
// is_login, _ := ctx.Get("is_login")
|
||||
// //判断是否登录
|
||||
// if is_login == true {
|
||||
|
||||
// } else {
|
||||
// ctx.Redirect(302, "/sign-in")
|
||||
// }
|
||||
|
||||
// })
|
||||
r.GET("/workorders", func(ctx *gin.Context) {
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
user_info, _ := ctx.Get("user_info")
|
||||
//判断是否登录
|
||||
if is_login == true {
|
||||
|
||||
ctx.HTML(http.StatusOK, "workorders.html", gin.H{
|
||||
"is_login": is_login,
|
||||
"user_info": user_info,
|
||||
})
|
||||
} else {
|
||||
ctx.Redirect(302, "/sign-in")
|
||||
}
|
||||
})
|
||||
|
||||
r.GET("/warehouses", func(ctx *gin.Context) {
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
user_info, _ := ctx.Get("user_info")
|
||||
//判断是否登录
|
||||
if is_login == true {
|
||||
total_pages := models.Warehouse_get_total_pages() //获取总页数
|
||||
disabled_next_page := false
|
||||
if total_pages <= 1 {
|
||||
disabled_next_page = true
|
||||
} else {
|
||||
disabled_next_page = false
|
||||
}
|
||||
|
||||
ctx.HTML(http.StatusOK, "warehouses.html", gin.H{
|
||||
"total_pages": total_pages,
|
||||
"this_page": 1,
|
||||
"prev_page": fmt.Sprintf("/warehouses/%d", 1),
|
||||
"next_page": fmt.Sprintf("/warehouses/%d", 2),
|
||||
"disabled_prev_page": true,
|
||||
"disabled_next_page": disabled_next_page,
|
||||
"page_range": models.Page_range(1, total_pages, 1, "/warehouses/"),
|
||||
"Warehouses": models.Warehouse_get_warehouses(1),
|
||||
"is_login": is_login,
|
||||
"user_info": user_info,
|
||||
})
|
||||
} else {
|
||||
ctx.Redirect(302, "/sign-in")
|
||||
}
|
||||
})
|
||||
r.GET("/warehouses/:id", func(ctx *gin.Context) {
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
user_info, _ := ctx.Get("user_info")
|
||||
//判断是否登录
|
||||
if is_login == true {
|
||||
id := ctx.Param("id")
|
||||
id_int64, err := strconv.ParseInt(id, 10, 64)
|
||||
if err == nil {
|
||||
if id_int64 > 0 {
|
||||
total_pages := models.Warehouse_get_total_pages() //获取总页数
|
||||
disabled_next_page := false
|
||||
disabled_prev_page := false
|
||||
if total_pages == id_int64 {
|
||||
disabled_next_page = true
|
||||
} else {
|
||||
disabled_next_page = false
|
||||
}
|
||||
if id_int64 > 1 {
|
||||
disabled_prev_page = false
|
||||
} else {
|
||||
disabled_prev_page = true
|
||||
}
|
||||
|
||||
//制作页码标签
|
||||
|
||||
ctx.HTML(http.StatusOK, "warehouses.html", gin.H{
|
||||
"total_pages": total_pages,
|
||||
"this_page": id_int64,
|
||||
"prev_page": fmt.Sprintf("/warehouses/%d", id_int64-1),
|
||||
"next_page": fmt.Sprintf("/warehouses/%d", id_int64+1),
|
||||
"disabled_prev_page": disabled_prev_page,
|
||||
"disabled_next_page": disabled_next_page,
|
||||
"page_range": models.Page_range(1, total_pages, id_int64, "/warehouses/"),
|
||||
"Warehouses": models.Warehouse_get_warehouses(id_int64),
|
||||
"is_login": is_login,
|
||||
"user_info": user_info,
|
||||
})
|
||||
} else {
|
||||
ctx.Redirect(302, "/warehouses")
|
||||
}
|
||||
|
||||
} else {
|
||||
ctx.Redirect(302, "/warehouses")
|
||||
}
|
||||
|
||||
} else {
|
||||
ctx.Redirect(302, "/sign-in")
|
||||
}
|
||||
})
|
||||
|
||||
r.GET("/warehouse/:id", func(ctx *gin.Context) {
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
user_info, _ := ctx.Get("user_info")
|
||||
//判断是否登录
|
||||
if is_login == true {
|
||||
id := ctx.Param("id")
|
||||
id_int64, err := strconv.ParseInt(id, 10, 64)
|
||||
if err == nil {
|
||||
if id_int64 > 0 {
|
||||
|
||||
ctx.HTML(http.StatusOK, "warehouses_show_item.html", gin.H{
|
||||
"warehouse_id": id_int64,
|
||||
"is_login": is_login,
|
||||
"user_info": user_info,
|
||||
"items": models.Warehouse_get_items_from_whid(uint(id_int64)),
|
||||
})
|
||||
} else {
|
||||
ctx.Redirect(302, "/warehouses")
|
||||
}
|
||||
|
||||
} else {
|
||||
ctx.Redirect(302, "/warehouses")
|
||||
}
|
||||
|
||||
} else {
|
||||
ctx.Redirect(302, "/sign-in")
|
||||
}
|
||||
})
|
||||
r.GET("/setting-my", func(ctx *gin.Context) {
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
user_info, _ := ctx.Get("user_info")
|
||||
user, _ := ctx.Get("user")
|
||||
//判断是否登录
|
||||
if is_login == true {
|
||||
|
||||
ctx.HTML(http.StatusOK, "setting-my.html", gin.H{
|
||||
"is_login": is_login,
|
||||
"user_info": user_info,
|
||||
"user": user,
|
||||
})
|
||||
} else {
|
||||
ctx.HTML(404, "error_404.html", gin.H{})
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
r.GET("/setting-security", func(ctx *gin.Context) {
|
||||
is_login, _ := ctx.Get("is_login")
|
||||
user_info, _ := ctx.Get("user_info")
|
||||
user, _ := ctx.Get("user")
|
||||
//判断是否登录
|
||||
if is_login == true {
|
||||
|
||||
ctx.HTML(http.StatusOK, "setting-security.html", gin.H{
|
||||
"is_login": is_login,
|
||||
"user_info": user_info,
|
||||
"user": user,
|
||||
})
|
||||
} else {
|
||||
ctx.HTML(404, "error_404.html", gin.H{})
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user