feat: user & article: dev complete

This commit is contained in:
fallen-angle
2022-02-27 16:36:33 +08:00
parent 4f3b16ab9d
commit 80ca1cd46e
33 changed files with 2373 additions and 185 deletions

View File

@@ -1,9 +1,12 @@
package models
import "time"
import (
"time"
)
// BackArticle article struct
type BackArticle struct {
ID int `gorm:"primaryKey;column:id" json:"id"` // 文章id
ID int `gorm:"primaryKey;column:id" json:"-"` // 文章id
CreateTime time.Time `gorm:"column:create_time" json:"createTime"` // 文章新建时间
CreateUser string `gorm:"column:create_user" json:"createUser"` // 文章创建者id
ModifyTime time.Time `gorm:"column:modify_time" json:"modifyTime"` // 文章最后更新时间
@@ -13,4 +16,20 @@ type BackArticle struct {
Resume string `gorm:"column:resume" json:"resume"` // 文章简述
Cover string `gorm:"column:cover" json:"cover"` // 文章封面
Content string `gorm:"column:content" json:"content"` // 文章内容(如有需要可迁移至对象存储)
IsPublish int8 `gorm:"column:is_publish" json:"isPublish"` // 发布状态(0:未发布, 1: 发布)
IsDelete int8 `gorm:"column:is_delete" json:"isDelete"` // 删除标志
}
func init() {
initJcMap[BackArticle]()
}
//func ArticleMapJ2c(jsonMap map[string]interface{}, ignoreNil bool) map[string]interface{} {
// colMap := make(map[string]interface{})
// for k, v := range jsonMap {
// if colKey := colKey != "" && (!ignoreNil && v == nil) {
// colMap[colKey] = v
// }
// }
// return colMap
//}

View File

@@ -3,13 +3,43 @@ package models
import "time"
type BackUser struct {
ID int `gorm:"primaryKey;column:id" json:"-"` // 用户ID
Username string `gorm:"column:username" json:"username"` // 用户真实姓名
Password string `gorm:"column:password" json:"password"` // 用户密码
Role int `gorm:"column:role" json:"role"` // 用户角色
Email string `gorm:"unique;column:email" json:"email"` // 用户邮箱
Phone string `gorm:"unique;column:phone" json:"phone"` // 用户手机号码
Aptitude string `gorm:"column:aptitude" json:"aptitude"` // 用户资质证明(图片URL)
RegisterTime time.Time `gorm:"column:register_time" json:"registerTime"` // 用户注册时间
Approver int `gorm:"column:approver" json:"approver"` // 注册审核人ID
ID int `gorm:"primaryKey;column:id" json:"-"` // 用户ID
Username string `gorm:"column:username" json:"username"` // 用户真实姓名
Password string `gorm:"column:password" json:"password"` // 用户密码
Role int `gorm:"column:role" json:"role"` // 用户角色
Email string `gorm:"column:email" json:"email"` // 用户邮箱
Phone string `gorm:"column:phone" json:"phone"` // 用户手机号码
Aptitude string `gorm:"column:aptitude" json:"aptitude"` // 用户资质证明(图片URL)
CreateTime time.Time `gorm:"column:create_time" json:"createTime"` // 用户注册时间
Approver int `gorm:"column:approver" json:"approver"` // 注册审核人ID
ModifyTime time.Time `gorm:"column:modify_time" json:"modifyTime"`
IsDelete int8 `gorm:"column:is_delete" json:"isDelete"` // 删除标志
}
type UserLogin struct {
Account string `json:"account"`
Password string `json:"password"`
}
type UserRegister struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
Phone string `json:"phone"`
Aptitude string `json:"aptitude"`
}
type UserChangePwd struct {
Email string `json:"email"`
Code string `json:"code"`
NewPassword string `json:"newPassword"`
}
type UserApprove struct {
Email string `json:"email"`
Pass bool `json:"pass"`
}
func init() {
initJcMap[BackUser]()
}

View File

@@ -1,14 +1,181 @@
package models
type UtilRequestInfo struct {
Url string
Header string
Body string
Timeout int
import (
"fmt"
"gorm.io/gorm"
"nCovTrack-Backend/global"
"reflect"
"regexp"
"time"
)
var colNameReg, _ = regexp.Compile(".*column:(.*);?")
var j2cMap = make(map[string]map[string]string)
var c2jMap = make(map[string]map[string]string)
const IS_DELETE = "is_delete"
// initJcMap the gorm models need to call this function in init function
func initJcMap[T any]() {
t := reflect.TypeOf(new(T)).Elem()
tJ2cMap, tC2jMap := make(map[string]string), make(map[string]string)
for i := 0; i < t.NumField(); i++ {
colName := columnName(t.Field(i).Tag.Get("gorm"))
// TODO: Deal with (-)
jsonName := t.Field(i).Tag.Get("json")
if colName == "" || jsonName == "" {
continue
}
tJ2cMap[jsonName] = colName
tC2jMap[colName] = jsonName
}
j2cMap[t.Name()] = tJ2cMap
c2jMap[t.Name()] = tC2jMap
}
type GinResponse struct {
Code int `json:"code"`
Msg interface{} `json:"msg"`
Data interface{} `json:"data"`
// columnName get the mysql column name of the tag
func columnName(gormTag string) string {
colNames := colNameReg.FindSubmatch([]byte(gormTag))
if len(colNames) != 2 {
panic("Model tag regex error")
}
return string(colNames[1])
}
// MapJ2c convert jsonMap to colMap, which will used by gorm
func MapJ2c[T any](jsonMap map[string]interface{}, ignoreNil bool) (colMap map[string]interface{}) {
tName := reflect.TypeOf(new(T)).Elem().Name()
tJ2cMap := j2cMap[tName]
if tJ2cMap == nil {
panic(tName + " is not init registered int j2cMap")
}
colMap = make(map[string]interface{})
for k, v := range jsonMap {
//TODO 无法转换
if colName := tJ2cMap[k]; colName != "" && (!ignoreNil || v != nil) {
colMap[colName] = v
}
}
return colMap
}
// BeforeSave need to set some field while insert or update
func BeforeSave(colMap map[string]interface{}, user int) {
if colMap["id"] == nil {
colMap["create_time"] = time.Now()
if user != -1 {
colMap["create_user"] = user
}
}
colMap["modify_time"] = time.Now()
if user != -1 {
colMap["modify_user"] = user
}
}
/*-----------------------------------------------<Gorm functions>-----------------------------------------------------------*/
// Due to gorm can't deal with the zero value, so we use gorm with map.
// The generic will make the function is generally used to gorm models
// TODO: add uniqueKey map, which can be used when Upsert
func Upsert[T any](colMap map[string]interface{}) (ok bool, rowsAffected int64) {
var tx *gorm.DB
if colMap["id"] == nil {
tx = global.Db.Model(new(T)).Create(colMap)
} else {
tx = global.Db.Model(new(T)).Where("id = ?", colMap["id"]).Updates(colMap)
}
if tx.Error != nil {
fmt.Println(tx.Error)
return false, 0
}
return true, tx.RowsAffected
}
// DeleteById will delete by id, not delete the record from database, only set the field "is_delete" as 1
func DeleteById[T any](id int) (ok bool, rowsAffected int64) {
tx := global.Db.Model(new(T)).Where("id = ?", id).Update("is_delete", 1)
if tx.Error != nil {
return false, 0
}
return true, rowsAffected
}
func List[T any](queryMap []map[string]interface{}) *[]map[string]interface{} {
tx := global.Db.Model(new(T))
for _, e := range queryMap {
e[IS_DELETE] = 0
tx = tx.Or(e)
}
return ListByOrm(tx)
}
func ListField[T any](queryMap []map[string]interface{}, isOmit bool, queryField ...string) *[]map[string]interface{} {
tx := global.Db.Model(new(T))
for _, e := range queryMap {
e[IS_DELETE] = 0
tx = tx.Or(e)
}
if len(queryMap) == 0 {
return ListByOrm(tx)
}
if isOmit {
tx = tx.Omit(queryField...)
} else {
tx = tx.Select(queryField[0], queryField[1:])
}
return ListByOrm(tx)
}
func ListByOrm(tx *gorm.DB) *[]map[string]interface{} {
var res []map[string]interface{}
tx.Find(&res)
return &res
}
func Get[T any](queryMap []map[string]interface{}) map[string]interface{} {
tx := global.Db.Model(new(T))
for _, e := range queryMap {
e[IS_DELETE] = 0
tx = tx.Or(e)
}
return GetByOrm(tx)
}
func GetField[T any](queryMap []map[string]interface{}, isOmit bool, queryField ...string) map[string]interface{} {
tx := global.Db.Model(new(T))
for _, e := range queryMap {
e[IS_DELETE] = 0
tx = tx.Or(e)
}
if len(queryMap) == 0 {
return GetByOrm(tx)
}
if isOmit {
tx = tx.Omit(queryField...)
} else {
tx = tx.Select(queryField[0], queryField[1:])
}
return GetByOrm(tx)
}
func GetByOrm(tx *gorm.DB) map[string]interface{} {
var res map[string]interface{}
tx.Limit(1).Find(&res)
return res
}
func Count[T any](queryMap []map[string]interface{}) int64 {
tx := global.Db.Model(new(T))
for _, e := range queryMap {
e[IS_DELETE] = 0
tx = tx.Or(e)
}
return CountByOrm(tx)
}
func CountByOrm(tx *gorm.DB) int64 {
var count int64
tx.Count(&count)
return count
}