feat:完善信息

This commit is contained in:
henry
2021-09-28 18:23:34 +08:00
parent ef9213f261
commit b876eab301
9 changed files with 319 additions and 4 deletions

View File

@ -1,3 +1,3 @@
# IOT
# SciencesServer
物联网服务
中科元系统管理

99
app/common/api/api.go Normal file
View File

@ -0,0 +1,99 @@
package api
import (
"SciencesServer/config"
"SciencesServer/serve/logger"
"SciencesServer/utils"
"errors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"net/http"
)
const (
SuccessCode = 200 //成功的状态码
FailureCode = 999 //失败的状态码
)
type response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type ApiHandle func(c *gin.Context) interface{}
func GetSession() ApiHandle {
return func(c *gin.Context) interface{} {
value, _ := c.Get(config.TokenForSession)
return value
}
}
func Bind(req interface{}) ApiHandle {
return func(c *gin.Context) interface{} {
var err error
if c.Request.Method == "GET" {
err = c.ShouldBindQuery(req)
} else {
err = c.ShouldBindBodyWith(req, binding.JSON)
}
if err != nil {
logger.ErrorF("Request URL【%s】Params Bind Error%v", c.Request.URL, err)
return errors.New("参数异常/缺失")
}
c.Set("params", utils.AnyToJSON(req))
return nil
}
}
func APISuccess(data ...interface{}) ApiHandle {
return func(c *gin.Context) interface{} {
resp := &response{
Code: SuccessCode,
Message: "ok",
}
if len(data) > 0 {
resp.Data = data[0]
}
c.JSON(http.StatusOK, resp)
return nil
}
}
func APIFailure(err error) ApiHandle {
return func(c *gin.Context) interface{} {
resp := &response{
Code: FailureCode,
Message: "failure",
}
if err != nil {
resp.Message = err.Error()
}
c.JSON(http.StatusOK, resp)
c.Abort()
return nil
}
}
func APIResponse(err error, data ...interface{}) ApiHandle {
return func(c *gin.Context) interface{} {
resp := &response{
Code: SuccessCode,
Message: "ok",
}
if err != nil {
resp.Code = FailureCode
resp.Message = err.Error()
c.JSON(http.StatusOK, resp)
c.Abort()
return nil
}
if len(data) > 0 {
resp.Data = data[0]
}
c.JSON(http.StatusOK, resp)
return nil
}
}

View File

@ -0,0 +1,4 @@
package model
type SysSms struct {
}

View File

@ -0,0 +1,21 @@
package model
type TenantUser struct {
Model
ModelTenant
UUID uint64 `gorm:"column:uuid;uniqueIndex:idx_sys_user_uuid;type:int;default:0;comment:用户唯一UUID" json:"-"`
Avatar string `gorm:"column:avatar;type:varchar(255);default:null;comment:头像" json:"avatar"`
Name string `gorm:"column:name;type:varchar(20);default:null;comment:真实姓名" json:"name"`
Mobile string `gorm:"column:mobile;index:idx_sys_user_mobile;type:varchar(15);default:null;comment:联系方式" json:"mobile"`
Email string `gorm:"column:email;type:varchar(50);default:null;comment:邮箱" json:"email"`
ModelDeleted
ModelAt
}
func (m *TenantUser) TableName() string {
return m.NewTableName("tenant_user")
}
func NewTenantUser() *TenantUser {
return &TenantUser{}
}

6
app/enterprise/README.md Normal file
View File

@ -0,0 +1,6 @@
## 企业后台
- 企业后台
- 专家后台
- 研究机构后台
- 实验室后台

View File

@ -0,0 +1,45 @@
package api
import (
"SciencesServer/app/common/api"
"SciencesServer/app/enterprise/controller/account"
"github.com/gin-gonic/gin"
)
type Account struct{}
type (
accountLoginForm struct {
Mode int `json:"mode" form:"mode" binding:"required"`
}
accountRegisterForm struct {
}
)
func (a *Account) Login(c *gin.Context) {
form := new(accountLoginForm)
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
data, err := account.NewLogin().Login()(account.LoginMode(form.Mode), nil)
api.APIResponse(err, data)
}
func (a *Account) Register() {
}
func (c *Account) BindName() {
}
func (c *Account) BindMobile() {
}
func (a *Account) Logout() {
}

View File

@ -0,0 +1,121 @@
package account
import (
model2 "SciencesServer/app/common/model"
"SciencesServer/app/enterprise/model"
"SciencesServer/app/handle"
"SciencesServer/config"
"SciencesServer/utils"
"errors"
)
type Login struct{}
type (
LoginHandle func(LoginMode, *LoginRequest) (*LoginResponse, error)
)
type (
LoginRequest struct {
Captcha struct {
Mobile string `json:"mobile"`
Captcha string `json:"captcha"`
}
Password struct {
Mobile string `json:"mobile"`
Password string `json:"password"`
}
Platform struct {
OpenID string `json:"open_id"`
}
}
LoginResponse struct {
Token string `json:"token"`
EffectTime int `json:"effect_time"`
}
)
// LoginMode 登陆模式
type LoginMode int
const (
LoginModeForCaptcha LoginMode = iota + 1e2 + 1 // 验证码登陆
LoginModeForPassword // 密码登陆
LoginModeForWechat // 微信登陆
LoginModeForQQ // QQ登陆
)
var loginHandle = map[LoginMode]func(*LoginRequest) (*model.TenantUser, error){
LoginModeForCaptcha: loginForCaptcha, LoginModeForPassword: loginForPassword,
}
// loginForCaptcha 验证码登陆
func loginForCaptcha(req *LoginRequest) (*model.TenantUser, error) {
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: req.Captcha.Mobile,
Captcha: req.Captcha.Captcha,
})
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
var isExist bool
mTenantUsr := model.NewTenantUser()
if isExist, err = model2.FirstField(mTenantUsr.TenantUser, []string{"id", "name", "status"},
model2.NewWhere("mobile", req.Captcha.Mobile)); err != nil {
return nil, err
} else if isExist {
return nil, errors.New("当前手机号码未注册")
}
return mTenantUsr, nil
}
// loginForPassword 密码登陆
func loginForPassword(req *LoginRequest) (*model.TenantUser, error) {
mTenantUsr := model.NewTenantUser()
isExist, err := model2.FirstField(mTenantUsr.TenantUser, []string{"id", "name", "status"},
model2.NewWhere("mobile", req.Password.Mobile))
if err != nil {
return nil, err
} else if isExist {
return nil, errors.New("当前手机号码未注册")
}
if !mTenantUsr.ValidatePassword(req.Password.Password) {
return nil, errors.New("账户或密码错误")
}
return mTenantUsr, nil
}
func loginForPlatform(req *LoginRequest) error {
return nil
}
func (c *Login) Login() LoginHandle {
return func(mode LoginMode, req *LoginRequest) (*LoginResponse, error) {
handle, has := loginHandle[mode]
if !has {
return nil, errors.New("未知的登陆模式")
}
user, err := handle(req)
if err != nil {
return nil, err
}
token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{
config.TokenForUID: user.UUID,
})
return &LoginResponse{
Token: token, EffectTime: config.SettingInfo.TokenEffectTime,
}, nil
}
}
func NewLogin() *Login {
return &Login{}
}

View File

@ -0,0 +1,19 @@
package model
import "SciencesServer/app/common/model"
type TenantUser struct {
*model.TenantUser
}
func (m *TenantUser) UUIDToString() string {
return ""
}
func (m *TenantUser) ValidatePassword(password string) bool {
return true
}
func NewTenantUser() *TenantUser {
return &TenantUser{TenantUser: model.NewTenantUser()}
}

View File

@ -7,7 +7,7 @@ token_effect_time: 604800
multiple_login: true
server:
port: 8090
port: 8000
read_timeout: 5
write_timeout: 5
idle_timeout: 5
@ -38,7 +38,7 @@ engine:
port: 3306
user: appuser
password: ABCabc01!
db_name: iot_test
db_name: sciences
parameters: charset=utf8mb4,utf8&parseTime=True&loc=Asia%2FShanghai
# SQLITE 配置
sqlite: