up
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
{
|
||||
"apiOK":0,
|
||||
"apiErr":-1
|
||||
"apiErr":-1,
|
||||
"postErr":-2,
|
||||
"jsonErr":-3,
|
||||
"userNameDup":-4
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export PATH=$PATH:$(go env GOPATH)/bin
|
||||
|
||||
fresh
|
||||
|
||||
+3
-2
@@ -66,6 +66,7 @@ func main() {
|
||||
|
||||
//统一初始化
|
||||
models.ConfigAllInit()
|
||||
routers.ApiInit()
|
||||
|
||||
//启动gin服务
|
||||
r := gin.Default()
|
||||
@@ -74,7 +75,7 @@ func main() {
|
||||
fs := http.FileServer(http.Dir("./dist"))
|
||||
// 中间件处理路由
|
||||
r.Use(func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/api/") {
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/api") {
|
||||
c.Next() // 继续处理API请求
|
||||
return
|
||||
}
|
||||
@@ -85,7 +86,7 @@ func main() {
|
||||
})
|
||||
|
||||
// API路由
|
||||
routers.ApiRoot(r.Group("/api/"))
|
||||
routers.ApiRoot(r.Group("/api"))
|
||||
|
||||
var http_port = models.ConfigsWed.Host + ":" + models.ConfigsWed.Port
|
||||
var gin_port = "0.0.0.0" + ":" + models.ConfigsWed.Port
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 获取当前时间字符串
|
||||
// 参数格式可选,默认"2006-01-02 15:04:05"
|
||||
func GetCurrentTimeString(format ...string) string {
|
||||
// 默认格式
|
||||
layout := "2006_01_02-15_04_05.999999999"
|
||||
|
||||
// 如果传入了格式参数则使用自定义格式
|
||||
if len(format) > 0 {
|
||||
layout = format[0]
|
||||
}
|
||||
|
||||
return time.Now().Format(layout)
|
||||
}
|
||||
|
||||
func RandStr32() string {
|
||||
// 生成 32 字节 (256 位) 随机数据
|
||||
b := make([]byte, 32)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 转换为 16 进制字符串 (长度 64)
|
||||
cookie := hex.EncodeToString(b)
|
||||
return cookie
|
||||
}
|
||||
|
||||
func Md5Str(str string) string {
|
||||
hashBytes2 := md5.Sum([]byte(str))
|
||||
hashString2 := hex.EncodeToString(hashBytes2[:]) // 注意数组转切片的[:]
|
||||
return hashString2
|
||||
}
|
||||
|
||||
func HashUserPass(str string) string {
|
||||
switch ConfigsUser.PassHashType {
|
||||
case "text":
|
||||
return str
|
||||
case "md5":
|
||||
return Md5Str(str)
|
||||
}
|
||||
|
||||
return GetCurrentTimeString() + RandStr32() //如果转换失败返回当前时间,避免撞库
|
||||
}
|
||||
@@ -29,9 +29,25 @@ type TabUser_ struct {
|
||||
Name string `gorm:"size:100;uniqueIndex"` // 唯一约束索引
|
||||
Email string `gorm:"size:255;index"` // 字符串长度限制100 索引
|
||||
Pass string `gorm:"size:128"` // 建议存储哈希后的密码
|
||||
Type string `gorm:"size:64;default:user"` //
|
||||
Date time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP"` // 默认当前时间
|
||||
}
|
||||
|
||||
type TabUserGroups_ struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"` // 自增主键
|
||||
Name string `gorm:"size:100;uniqueIndex"` // 唯一约束索引
|
||||
Email string `gorm:"size:255;index"` // 字符串长度限制100 索引
|
||||
Type string `gorm:"size:64;default:usergroup"` //
|
||||
Date time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP"` // 默认当前时间
|
||||
}
|
||||
|
||||
type TabUserGroupBinds_ struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"` // 自增主键
|
||||
UserID uint `gorm:"index"`
|
||||
GroupID uint `gorm:"index"`
|
||||
Date time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP"` // 默认当前时间
|
||||
}
|
||||
|
||||
type TabUserInfo_ struct {
|
||||
ID uint `gorm:"primaryKey;autoIncrement"`
|
||||
UserID uint `gorm:"not null;uniqueIndex"`
|
||||
@@ -89,6 +105,10 @@ func DatabaseInit() error {
|
||||
// 自动创建表结构
|
||||
DB.AutoMigrate(&TabUser_{})
|
||||
|
||||
DB.AutoMigrate(&TabUserGroups_{})
|
||||
|
||||
DB.AutoMigrate(&TabUserGroupBinds_{})
|
||||
|
||||
DB.AutoMigrate(&TabUserInfo_{})
|
||||
|
||||
DB.AutoMigrate(&TabCookie_{})
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
var ErrorCode map[string]interface{}
|
||||
@@ -27,6 +28,29 @@ func init() {
|
||||
|
||||
func ApiRoot(r *gin.RouterGroup) {
|
||||
|
||||
r.Use(func(ctx *gin.Context) {
|
||||
//转换传进来的数据
|
||||
var jsonData map[string]interface{}
|
||||
if err := ctx.ShouldBindJSON(&jsonData); err == nil {
|
||||
//分离数据
|
||||
|
||||
if jsonData["cookie"] != "" && jsonData["cookie"] != nil {
|
||||
ctx.Set("cookie_value", jsonData["cookie"])
|
||||
}
|
||||
|
||||
if jsonData["data"] != nil {
|
||||
//fmt.Println(jsonData["data"])
|
||||
var data_t map[string]interface{}
|
||||
if err = mapstructure.Decode(jsonData["data"], &data_t); err == nil {
|
||||
ctx.Set("data", &data_t)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
ApiUser(r.Group("/users"))
|
||||
|
||||
r.GET("/", func(ctx *gin.Context) {
|
||||
ReturnJson(ctx, "apiOK", nil)
|
||||
})
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package routers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"ops/models"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
func ApiInit() {
|
||||
//用户模块初始化init
|
||||
fmt.Println("users init")
|
||||
|
||||
//创建admin用户
|
||||
var user models.TabUser_
|
||||
user.Name = "admin"
|
||||
|
||||
if models.DB.Where(&user).First(&user).Error == nil {
|
||||
|
||||
} else {
|
||||
fmt.Println("用户不存在")
|
||||
user.Pass = models.HashUserPass("adminpassword")
|
||||
models.DB.Create(&user) // 传入指针
|
||||
}
|
||||
|
||||
//创建admin group
|
||||
var usergroup models.TabUserGroups_
|
||||
usergroup.Name = "admins"
|
||||
if models.DB.Where(&usergroup).First(&usergroup).Error == nil {
|
||||
|
||||
} else {
|
||||
fmt.Println("用户组不存在")
|
||||
models.DB.Create(&usergroup) // 传入指针
|
||||
}
|
||||
|
||||
//创建用户与用户组绑定
|
||||
var usergroupbind models.TabUserGroupBinds_
|
||||
usergroupbind.UserID = user.ID
|
||||
usergroupbind.GroupID = usergroup.ID
|
||||
|
||||
if models.DB.Where(&usergroupbind).First(&usergroupbind).Error == nil {
|
||||
|
||||
} else {
|
||||
models.DB.Create(&usergroupbind) // 传入指针
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
type From_user_add struct {
|
||||
Useremail string `json:"useremail"`
|
||||
Username string `json:"username"`
|
||||
Userpass string `json:"userpass"`
|
||||
}
|
||||
|
||||
func ApiUser(r *gin.RouterGroup) {
|
||||
|
||||
r.GET("/test", func(ctx *gin.Context) {
|
||||
ReturnJson(ctx, "apiOK", nil)
|
||||
})
|
||||
r.POST("/test", func(ctx *gin.Context) {
|
||||
ReturnJson(ctx, "apiOK", nil)
|
||||
})
|
||||
r.POST("/register", func(ctx *gin.Context) {
|
||||
//转换传进来的数据
|
||||
var jsonData From_user_add
|
||||
data, isHaveData := ctx.Get("data")
|
||||
|
||||
if isHaveData {
|
||||
if err := mapstructure.Decode(data, &jsonData); err == nil {
|
||||
//转换字段
|
||||
newUser := models.TabUser_{
|
||||
Name: jsonData.Username,
|
||||
Email: jsonData.Useremail,
|
||||
Pass: jsonData.Userpass, // 实际应替换为哈希值
|
||||
Date: time.Now(),
|
||||
// Date 字段无需赋值,数据库会自动填充默认值
|
||||
}
|
||||
if newUser.Name != "" && newUser.Pass != "" && newUser.Email != "" {
|
||||
//对用户的密码进行哈希替换
|
||||
newUser.Pass = models.HashUserPass(newUser.Pass)
|
||||
//用户名是唯一的,先读取是否有这个用户名
|
||||
var user models.TabUser_
|
||||
user.Name = newUser.Name
|
||||
|
||||
if models.DB.Where(&user).First(&user).Error == nil {
|
||||
//fmt.Println("找到用户:", user.ID)
|
||||
ReturnJson(ctx, "userNameDup", nil)
|
||||
} else {
|
||||
//fmt.Println("用户不存在")
|
||||
models.DB.Create(&newUser) // 传入指针
|
||||
|
||||
//创建info
|
||||
var user_info models.TabUserInfo_
|
||||
user_info.AvatarPath = models.ConfigsUser.AvatarPath
|
||||
user_info.UserID = newUser.ID
|
||||
models.DB.Create(&user_info) // 传入指针
|
||||
|
||||
ReturnJson(ctx, "apiOK", nil)
|
||||
}
|
||||
|
||||
} else {
|
||||
ReturnJson(ctx, "jsonErr", nil)
|
||||
}
|
||||
|
||||
} else {
|
||||
ReturnJson(ctx, "jsonErr", nil)
|
||||
|
||||
}
|
||||
} else {
|
||||
ReturnJson(ctx, "postErr", nil)
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
@@ -19,4 +19,6 @@ func ReturnJson(ctx *gin.Context, errMsg string, data map[string]interface{}) {
|
||||
|
||||
ctx.JSON(200, &returnData)
|
||||
|
||||
ctx.Abort()
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package routers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Root(r *gin.RouterGroup) {
|
||||
|
||||
}
|
||||
@@ -1,9 +1,25 @@
|
||||
import axios from "axios";
|
||||
import { myfuncs } from "./myfunc";
|
||||
var head_path = "/api";
|
||||
|
||||
export const my_network_func = {
|
||||
post_json(path, json, callback) {
|
||||
var head_path = "/api/v1";
|
||||
getJson(path, callback) {
|
||||
//get 方法一般不需要权限,不插入cookie
|
||||
var re_data = {};
|
||||
axios
|
||||
.get(head_path + path)
|
||||
.then((r) => {
|
||||
re_data["statusCode"] = r.status;
|
||||
re_data["data"] = r.data;
|
||||
callback(re_data);
|
||||
})
|
||||
.catch((error) => {
|
||||
re_data["statusCode"] = -1;
|
||||
re_data["error"] = error;
|
||||
callback(re_data);
|
||||
});
|
||||
},
|
||||
postJson(path, json, callback) {
|
||||
//把cookie插入json
|
||||
var data = {};
|
||||
data["data"] = json;
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
<script setup>
|
||||
import { my_network_func } from '@/my_network_func';
|
||||
|
||||
function t(){
|
||||
console.log("test")
|
||||
|
||||
my_network_func.postJson("/users/test",{
|
||||
a:"1"
|
||||
},(a)=>{
|
||||
console.log(a)
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main>
|
||||
111
|
||||
<button @click="t">222</button>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
@@ -1,71 +1,82 @@
|
||||
<script setup>
|
||||
import { onMounted, watch, ref } from 'vue'
|
||||
import MyOffcanvas from '@/components/MyOffcanvas.vue'
|
||||
import { myfuncs } from '@/myfunc.js'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { onMounted, watch, ref } from "vue";
|
||||
import MyOffcanvas from "@/components/MyOffcanvas.vue";
|
||||
import { myfuncs } from "@/myfunc.js";
|
||||
import { my_network_func } from "@/my_network_func";
|
||||
import { useI18n } from "vue-i18n";
|
||||
// 使用 vue-i18n 的 Composition API
|
||||
const { t, locale } = useI18n()
|
||||
const mos = ref()
|
||||
const isShowPassword = ref(false)
|
||||
const username = ref()
|
||||
const useremail = ref()
|
||||
const userpassword = ref()
|
||||
|
||||
const { t, locale } = useI18n();
|
||||
const mos = ref();
|
||||
const isShowPassword = ref(false);
|
||||
const username = ref();
|
||||
const useremail = ref();
|
||||
const userpassword = ref();
|
||||
|
||||
function functionupdataTitle() {
|
||||
document.title = 'Operations.' + t('appname.register')
|
||||
document.title = "Operations." + t("appname.register");
|
||||
}
|
||||
function togglePasswordVisibility() {
|
||||
isShowPassword.value = !isShowPassword.value
|
||||
isShowPassword.value = !isShowPassword.value;
|
||||
}
|
||||
function createAccount() {
|
||||
// 在这里处理创建新账户的逻辑
|
||||
const user = username.value?.value
|
||||
const email = useremail.value?.value
|
||||
const pass = userpassword.value?.value
|
||||
const user = username.value?.value;
|
||||
const email = useremail.value?.value;
|
||||
const pass = userpassword.value?.value;
|
||||
|
||||
username.value?.classList.remove('is-invalid');
|
||||
useremail.value?.classList.remove('is-invalid');
|
||||
userpassword.value?.classList.remove('is-invalid');
|
||||
username.value?.classList.remove("is-invalid");
|
||||
useremail.value?.classList.remove("is-invalid");
|
||||
userpassword.value?.classList.remove("is-invalid");
|
||||
|
||||
if (
|
||||
!user ||
|
||||
!email ||
|
||||
!pass
|
||||
) {
|
||||
|
||||
if(!user){
|
||||
username.value?.classList.add('is-invalid');
|
||||
if (!user || !email || !pass) {
|
||||
if (!user) {
|
||||
username.value?.classList.add("is-invalid");
|
||||
}
|
||||
if(!email){
|
||||
useremail.value?.classList.add('is-invalid');
|
||||
if (!email) {
|
||||
useremail.value?.classList.add("is-invalid");
|
||||
}
|
||||
if(!pass){
|
||||
userpassword.value?.classList.add('is-invalid');
|
||||
if (!pass) {
|
||||
userpassword.value?.classList.add("is-invalid");
|
||||
}
|
||||
|
||||
mos.value?.showAlert('info', t('message.please_enter_username_and_password'), 5000)
|
||||
return
|
||||
mos.value?.showAlert(
|
||||
"info",
|
||||
t("message.please_enter_username_and_password"),
|
||||
5000
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!myfuncs.isValidEmail(email)) {
|
||||
useremail.value?.classList.add('is-invalid');
|
||||
mos.value?.showAlert('warning', t('message.this_not_email'), 5000)
|
||||
return
|
||||
useremail.value?.classList.add("is-invalid");
|
||||
mos.value?.showAlert("warning", t("message.this_not_email"), 5000);
|
||||
return;
|
||||
}
|
||||
console.log('创建新账户信息:', {
|
||||
user: username.value?.value,
|
||||
email: useremail.value?.value,
|
||||
pass: userpassword.value?.value,
|
||||
})
|
||||
// console.log("创建新账户信息:", {
|
||||
// user: username.value?.value,
|
||||
// email: useremail.value?.value,
|
||||
// pass: userpassword.value?.value,
|
||||
// });
|
||||
|
||||
my_network_func.postJson(
|
||||
"/users/register",
|
||||
{
|
||||
username: username.value?.value,
|
||||
useremail: useremail.value?.value,
|
||||
userpass: userpassword.value?.value,
|
||||
},
|
||||
(r)=>{
|
||||
console.log(r)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
functionupdataTitle()
|
||||
})
|
||||
functionupdataTitle();
|
||||
});
|
||||
// 监听语言变化,更新标题
|
||||
watch(locale, () => {
|
||||
functionupdataTitle()
|
||||
})
|
||||
functionupdataTitle();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -73,7 +84,7 @@ watch(locale, () => {
|
||||
<div class="container container-tight py-4">
|
||||
<div class="text-center mb-4">
|
||||
<router-link to="/" class="navbar-brand navbar-brand-autodark">
|
||||
<img
|
||||
<img
|
||||
src="/static/logo.svg"
|
||||
width="110"
|
||||
height="32"
|
||||
@@ -81,13 +92,14 @@ watch(locale, () => {
|
||||
class="navbar-brand-image"
|
||||
/>
|
||||
</router-link>
|
||||
|
||||
</div>
|
||||
<div class="card card-md">
|
||||
<div class="card-body">
|
||||
<h2 class="card-title text-center mb-4">{{ t('message.create_new_account') }}</h2>
|
||||
<h2 class="card-title text-center mb-4">
|
||||
{{ t("message.create_new_account") }}
|
||||
</h2>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">{{ t('message.user_name') }}</label>
|
||||
<label class="form-label">{{ t("message.user_name") }}</label>
|
||||
<input
|
||||
ref="username"
|
||||
type="text"
|
||||
@@ -96,7 +108,7 @@ watch(locale, () => {
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">{{ t('message.email_address') }}</label>
|
||||
<label class="form-label">{{ t("message.email_address") }}</label>
|
||||
<input
|
||||
ref="useremail"
|
||||
type="email"
|
||||
@@ -105,7 +117,7 @@ watch(locale, () => {
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">{{ t('message.password') }}</label>
|
||||
<label class="form-label">{{ t("message.password") }}</label>
|
||||
<div class="input-group input-group-flat">
|
||||
<input
|
||||
ref="userpassword"
|
||||
@@ -115,7 +127,11 @@ watch(locale, () => {
|
||||
autocomplete="off"
|
||||
/>
|
||||
<span class="input-group-text">
|
||||
<div class="link-secondary" title="Show password" data-bs-toggle="tooltip">
|
||||
<div
|
||||
class="link-secondary"
|
||||
title="Show password"
|
||||
data-bs-toggle="tooltip"
|
||||
>
|
||||
<!-- Download SVG icon from http://tabler-icons.io/i/eye -->
|
||||
<svg
|
||||
v-if="!isShowPassword"
|
||||
@@ -170,14 +186,14 @@ watch(locale, () => {
|
||||
</div> -->
|
||||
<div class="form-footer">
|
||||
<button @click="createAccount" class="btn btn-primary w-100">
|
||||
{{ t('message.create_new_account') }}
|
||||
{{ t("message.create_new_account") }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center text-secondary mt-3">
|
||||
{{ t('message.already_have_an_account') }}
|
||||
<router-link to="/login">{{ t('message.back_to_login') }}</router-link>
|
||||
{{ t("message.already_have_an_account") }}
|
||||
<router-link to="/login">{{ t("message.back_to_login") }}</router-link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user