1. 定义用户表结构
package model
import "time"
import "gorm.io/gorm"
type BaseModel struct {
ID int32 `gorm:"primary_key;AUTO_INCREMENT"`
CreatedAt time.Time `gorm:"column:add_time"`
UpdatedAt time.Time `gorm:"column:update_time"`
DeletedAt gorm.DeletedAt
IsDeleted bool
}
type User struct {
BaseModel
Mobile string `gorm:"index:idx_mobile;unique;type:varchar(11);not null"`
Password string `gorm:"type:varchar(100);not null"`
Nickname string `gorm:"type:varchar(20)"`
Birthday *time.Time `gorm:"type:datetime"`
Gender string `gorm:"column:gender;default:male;type:varchar(6) comment 'female表示女,male表示男'"`
Role int `gorm:"column:role;default:1;not null;type:int comment '1表示普通用户,2表示管理员'"`
}
2.同步表结构
package main
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"log"
"mxshop_server/usr_srv/model"
"os"
"time"
)
func main() {
dsn := "root:password@tcp(119.23.xxx.xxx:3306)/mxshop_user_srv?charset=utf8mb4&parseTime=True&loc=Local"
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Info,
Colorful: true,
})
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
Logger: newLogger,
})
if err != nil {
panic(err)
}
_ = db.AutoMigrate(&model.User{
})
}
3. 使用md5盐值加密密码
options := &password.Options{
16, 100, 32, sha512.New}
salt, encodePwd := password.Encode("generic password", options)
newPassword := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodePwd)
fmt.Println(len(newPassword))
fmt.Println(newPassword)
passwordInfo := strings.Split(newPassword, "$")
fmt.Println(passwordInfo)
check := password.Verify("generic password", passwordInfo[2], passwordInfo[3], options)
fmt.Println(check)
4. proto结构表定义
syntax = "proto3";
option go_package = ".;proto";
import "google/protobuf/empty.proto";
service User {
rpc GetUserList(PageInfo) returns (UserListResponse);
rpc GetUserByMobile(MobileRequest) returns (UserInfoResponse);
rpc GetUserById(IdRequest) returns (UserInfoResponse);
rpc CreateUser(CreateUserInfo) returns (UserInfoResponse);
rpc UpdateUser(UpdateUserInfo) returns (google.protobuf.Empty);
rpc CheckPassword(PasswordCheckInfo) returns (CheckReponse);
rpc DeleteUser(IdRequest) returns (google.protobuf.Empty);
}
message PasswordCheckInfo {
string password = 1;
string encryptedPassword = 2;
}
message CheckReponse {
bool success = 1;