From ad6cabe89d6d512701ef8b78d26c70a56d01df46 Mon Sep 17 00:00:00 2001 From: gazebo Date: Wed, 5 Sep 2018 10:04:00 +0800 Subject: [PATCH] - localpass added. --- business/jxcallback/auth/auth.go | 1 + .../jxcallback/auth/localpass/localpass.go | 37 +++++++++++++++++++ business/jxcallback/auth/weixin/weixin.go | 16 ++++++-- business/model/legacy.go | 33 +++++++++++++++++ controllers/auth_controller.go | 1 + globals/gormdb/gormdb.go | 1 + 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 business/jxcallback/auth/localpass/localpass.go create mode 100644 business/model/legacy.go diff --git a/business/jxcallback/auth/auth.go b/business/jxcallback/auth/auth.go index 2601232fd..683dd5f43 100644 --- a/business/jxcallback/auth/auth.go +++ b/business/jxcallback/auth/auth.go @@ -23,6 +23,7 @@ var ( var ( ErrLoginTypeNotSupported = errors.New("不支持指定的登录类型") + ErrUIDAndPassNotMatch = errors.New("用户与密码不匹配") ) type LoginInfo struct { diff --git a/business/jxcallback/auth/localpass/localpass.go b/business/jxcallback/auth/localpass/localpass.go new file mode 100644 index 000000000..3b889d687 --- /dev/null +++ b/business/jxcallback/auth/localpass/localpass.go @@ -0,0 +1,37 @@ +package localpass + +import ( + "crypto/md5" + "fmt" + + "git.rosy.net.cn/jx-callback/business/jxcallback/auth" + "git.rosy.net.cn/jx-callback/business/model" + "git.rosy.net.cn/jx-callback/globals/gormdb" +) + +const ( + LoginType = "localpass" +) + +type Auther struct { +} + +func init() { + auth.RegisterAuther(LoginType, new(Auther)) +} + +func (a *Auther) Login(uname, password string) (err error) { + user := &model.JxBackendUser{} + db := gormdb.GetDB() + if err = db.Where("uname = ?", uname).First(user).Error; err == nil { + if fmt.Sprintf("%x", md5.Sum([]byte(password))) == user.UPass { + return nil + } + err = auth.ErrUIDAndPassNotMatch + } + return err +} + +func (a *Auther) Logout(openid string) error { + return nil +} diff --git a/business/jxcallback/auth/weixin/weixin.go b/business/jxcallback/auth/weixin/weixin.go index 330300aab..74c56d7ab 100644 --- a/business/jxcallback/auth/weixin/weixin.go +++ b/business/jxcallback/auth/weixin/weixin.go @@ -8,8 +8,10 @@ import ( "git.rosy.net.cn/baseapi/platformapi/weixinsnsapi" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxcallback/auth" + "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/globals" "git.rosy.net.cn/jx-callback/globals/api" + "git.rosy.net.cn/jx-callback/globals/gormdb" ) const ( @@ -55,14 +57,20 @@ func GetUserInfo(code string, state string) (token *UserInfoExt, err error) { return nil, err } -func (a *Auther) Login(openid, password string) error { +func (a *Auther) Login(openid, password string) (err error) { if value := globals.Cacher.Get(openid); value != nil { if password == value.(string) { - globals.Cacher.Del(openid) - return nil + wxUser := &model.WeiXins{} + db := gormdb.GetDB() + if err = db.Where("openid = ?", openid).First(wxUser).Error; err == nil { + globals.Cacher.Del(openid) + return nil + } } + } else { + err = ErrLoginFailed } - return ErrLoginFailed + return err } func (a *Auther) Logout(openid string) error { diff --git a/business/model/legacy.go b/business/model/legacy.go new file mode 100644 index 000000000..fa4d2dc29 --- /dev/null +++ b/business/model/legacy.go @@ -0,0 +1,33 @@ +package model + +type WeiXins struct { + ID int + JxStoreID int `gorm:"column:jxstoreid"` + OpenID string `gorm:"column:openid;type:varchar(70);index"` + Tel string `gorm:"type:varchar(15);index"` + ParentID int `gorm:"column:parentid"` + NickName string `gorm:"column:nickname;type:varchar(30)"` +} + +func (WeiXins) TableName() string { + return "weixins" +} + +type JxBackendUser struct { + UID int `gorm:"PRIMARY_KEY"` + UName string `gorm:"column:uname;type:varchar(64);unique_index"` + UPass string `gorm:"column:upass;type:varchar(64)"` + Tel string `gorm:"type:varchar(32);index"` + Position string `gorm:"type:varchar(255)"` + Enabled int8 `gorm:"default:1"` + SkuWidget int `gorm:"default:0"` + StoreWidget int `gorm:"default:0"` + UserWidget int `gorm:"default:0"` + BillinfoWidget int `gorm:"default:0"` + GroupWidget int `gorm:"default:0"` + CategoryWidget int `gorm:"default:0"` +} + +func (JxBackendUser) TableName() string { + return "jxbackenduser" +} diff --git a/controllers/auth_controller.go b/controllers/auth_controller.go index 24979be55..0f4e7b0cf 100644 --- a/controllers/auth_controller.go +++ b/controllers/auth_controller.go @@ -7,6 +7,7 @@ import ( "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxcallback/auth" + _ "git.rosy.net.cn/jx-callback/business/jxcallback/auth/localpass" // 加载本地用户密码 "git.rosy.net.cn/jx-callback/business/jxcallback/auth/weixin" "git.rosy.net.cn/jx-callback/globals" "github.com/astaxie/beego" diff --git a/globals/gormdb/gormdb.go b/globals/gormdb/gormdb.go index 21e49f6a3..f55e32a3d 100644 --- a/globals/gormdb/gormdb.go +++ b/globals/gormdb/gormdb.go @@ -37,4 +37,5 @@ func AutoMigrate() { db.AutoMigrate(&model.Store{}, &model.StoreSub{}, &model.StoreMap{}) db.AutoMigrate(&model.SkuVendorCategory{}, &model.StoreSkuCategoryMap{}, &model.SkuName{}, &model.Sku{}, &model.SkuNamePlaceBind{}, &model.StoreSkuBind{}) db.Set("gorm:table_options", "CHARSET=utf8mb4").AutoMigrate(&model.SkuCategory{}) + db.AutoMigrate(&model.WeiXins{}, &model.JxBackendUser{}) }