- 重构了dao.GetAuthBind和dao.GetUserBindAuthInfo

- AuthBind的唯一索引调整为"AuthID", "Type", "BindType", "DeletedAt"
This commit is contained in:
gazebo
2019-09-06 16:26:47 +08:00
parent a12f15af52
commit 47a0a892b6
9 changed files with 44 additions and 48 deletions

View File

@@ -435,5 +435,5 @@ func DisableUser(userID, operatorUserName string) (err error) {
}
func GetUserBindAuthInfo(userID string) (authList []*model.AuthBind, err error) {
return dao.GetUserBindAuthInfo(dao.GetDB(), userID, model.AuthBindTypeAuth, "", nil)
return dao.GetUserBindAuthInfo(dao.GetDB(), userID, model.AuthBindTypeAuth, nil, "", "")
}

View File

@@ -61,7 +61,7 @@ func (a *DefAuther) GetUserType() (userType int8) {
func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []string, openID, unionID string, authDetail interface{}) (authBindEx *auth2.AuthBindEx, err error) {
db := dao.GetDB()
var authBind *model.AuthBind
if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, curAuthType, openID); err == nil { // 直接找到了
if authBind, err = dao.GetAuthBind(db, model.AuthBindTypeAuth, curAuthType, openID); err == nil { // 直接找到了
authBindEx = &auth2.AuthBindEx{
AuthBind: *authBind,
}
@@ -71,7 +71,7 @@ func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []st
} else if dao.IsNoRowsError(err) { // 直接找不到尝试unionID
if unionID != "" { // 且有unionID
var authBindList []*model.AuthBind
if authBindList, err = dao.GetUserBindAuthInfo(db, "", model.AuthBindTypeAuth, unionID, unionAuthTypeList); err == nil && len(authBindList) > 0 { // 通过unionID找到至少一个认证方式
if authBindList, err = dao.GetUserBindAuthInfo(db, "", model.AuthBindTypeAuth, unionAuthTypeList, "", unionID); err == nil && len(authBindList) > 0 { // 通过unionID找到至少一个认证方式
authBind = authBindList[0]
authBind.Type = curAuthType
authBind.AuthID = openID

View File

@@ -37,7 +37,7 @@ func init() {
func (a *Auther) VerifySecret(userID, passMD5 string) (authBindEx *auth2.AuthBindEx, err error) {
globals.SugarLogger.Debugf("localpass VerifySecret userID:%s", userID)
var authBind *model.AuthBind
if authBind, err = dao.GetAuthBind(dao.GetDB(), "", model.AuthBindTypeAuth, AuthType, userID); err == nil {
if authBind, err = dao.GetAuthBind(dao.GetDB(), model.AuthBindTypeAuth, AuthType, userID); err == nil {
if err = a.checkPassword(authBind, passMD5); err == nil {
authBindEx = &auth2.AuthBindEx{
AuthBind: *authBind,
@@ -55,7 +55,7 @@ func (a *Auther) ChangePassword(userID, oldPassMD5, newPassMD5 string) (err erro
db := dao.GetDB()
salt := utils.GetUUID()
encryptPwd := a.encryptPassword(newPassMD5, salt)
if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, AuthType, userID); err == nil {
if authBind, err = dao.GetAuthBind(db, model.AuthBindTypeAuth, AuthType, userID); err == nil {
if err = a.checkPassword(authBind, oldPassMD5); err == nil || authBind.AuthSecret == "" { // 如果原密码为空,不判断原密码,代表重置密码
_, err = dao.UpdateEntityLogically(db, authBind, map[string]interface{}{
"AuthSecret": encryptPwd,

View File

@@ -183,7 +183,7 @@ func HandleOrder4Consignee(order *model.GoodsOrder) (err error) {
vendorUserID := order.VendorUserID
if vendorUserID != "" {
authInfo, err2 := dao.GetAuthBind(db, "", model.AuthBindTypeID, authType, vendorUserID)
authInfo, err2 := dao.GetAuthBind(db, model.AuthBindTypeID, authType, vendorUserID)
if err = err2; err != nil && !dao.IsNoRowsError(err) {
return err
}
@@ -309,7 +309,7 @@ func OnDingDingMsg(msg map[string]interface{}) (callbackResponse *dingdingapi.Ca
for _, userID := range msg[dingdingapi.KeyUserID].([]interface{}) {
userIDStr := utils.Interface2String(userID)
globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s left company", userIDStr)
if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, dingding.AuthTypeStaff, userIDStr); err == nil { // 直接找到了
if authBind, err = dao.GetAuthBind(db, model.AuthBindTypeAuth, dingding.AuthTypeStaff, userIDStr); err == nil { // 直接找到了
globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s, userID:%s left company", userIDStr, authBind.UserID)
if err = DisableUser(jxcontext.AdminCtx, authBind.UserID); err != nil {
globals.SugarLogger.Errorf("OnDingDingMsg failed with error:%v", err)

View File

@@ -1066,7 +1066,7 @@ func CreateConsumerFromOrders(ctx *jxcontext.Context, vendorIDs []int, fromDate,
}
}
}
if order2 == nil {
if order2 == nil && order.VendorID == model.VendorIDMTWM && time.Now().Sub(curDate) < 30*24*time.Hour {
order2, err = handler.GetOrder(order.VendorOrderID)
}
if order2 != nil {

View File

@@ -427,14 +427,14 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e
if db == nil {
db = dao.GetDB()
}
openID := ""
openIDs := []string{}
storeID := 0
remark := ""
if !globals.DisableWXAuth1 {
wxinfo, err := dao.GetUserStoreInfo(db, "tel", mobile)
if err == nil {
openID = wxinfo.OpenID
openIDs = []string{wxinfo.OpenID}
storeID = wxinfo.JxStoreID
}
}
@@ -449,9 +449,11 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e
}
}
if userID != "" {
authBind, err2 := dao.GetAuthBind(db, userID, model.AuthBindTypeAuth, weixin.AuthTypeMP, "")
authBindList, err2 := dao.GetUserBindAuthInfo(db, userID, model.AuthBindTypeAuth, []string{weixin.AuthTypeMP}, "", "")
if err = err2; err == nil {
openID = authBind.AuthID
for _, v := range authBindList {
openIDs = append(openIDs, v.AuthID)
}
}
roleList, err2 := api2.RoleMan.GetUserRoleList(userID)
if err = err2; err == nil && len(roleList) > 0 {
@@ -460,7 +462,7 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e
}
}
if openID != "" {
if len(openIDs) > 0 {
if storeID > 0 {
store := &model.Store{}
store.ID = storeID
@@ -475,7 +477,9 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e
}
if err == nil {
if globals.EnableStoreWrite {
err = api.WeixinAPI.CBUpdateRemark(openID, remark)
for _, openID := range openIDs {
err = api.WeixinAPI.CBUpdateRemark(openID, remark)
}
}
}
}

View File

@@ -118,9 +118,11 @@ func GetWeixinOpenIDsFromStoreID(storeID int) (retVal []string) {
if globals.EnableWXAuth2 {
if userIDList, err2 := api2.RoleMan.GetRoleUserList(autils.NewRole(authz.StoreRoleBoss, storeID)); err2 == nil {
for _, v := range userIDList {
if authInfo, err2 := dao.GetAuthBind(db, v, model.AuthBindTypeAuth, weixin.AuthTypeMP, ""); err2 == nil {
retVal = append(retVal, authInfo.AuthID)
openIDMap[authInfo.AuthID] = 1
if authList, err2 := dao.GetUserBindAuthInfo(db, v, model.AuthBindTypeAuth, []string{weixin.AuthTypeMP}, "", ""); err2 == nil {
for _, v := range authList {
retVal = append(retVal, v.AuthID)
openIDMap[v.AuthID] = 1
}
}
}
}

View File

@@ -16,12 +16,12 @@ const (
type AuthBind struct {
ModelIDCULD
UserID string `orm:"size(48);column(user_id)" json:"userID"`
AuthID string `orm:"size(48);column(auth_id)" json:"authID"`
BindType int8 `json:"bindType"`
Type string `orm:"size(16)" json:"type"`
Status int8 `json:"status"`
AuthID string `orm:"size(48);column(auth_id)" json:"authID"`
UserID string `orm:"size(48);column(user_id)" json:"userID"`
Status int8 `json:"status"`
AuthID2 string `orm:"size(48);column(auth_id2);index" json:"authID2"`
AuthSecret string `orm:"size(48)" json:"-"`
AuthSecret2 string `orm:"size(48)" json:"-"`
@@ -31,7 +31,7 @@ type AuthBind struct {
func (*AuthBind) TableUnique() [][]string {
return [][]string{
[]string{"UserID", "Type", "DeletedAt"},
[]string{"AuthID", "Type", "DeletedAt"},
// []string{"UserID", "Type", "DeletedAt"}, // 这个其实UserID是属性而不是key
[]string{"AuthID", "Type", "BindType", "DeletedAt"},
}
}

View File

@@ -7,41 +7,27 @@ import (
"git.rosy.net.cn/jx-callback/business/model"
)
func GetAuthBind(db *DaoDB, userID string, bindType int, authType, authID string) (authBind *model.AuthBind, err error) {
if userID == "" && authID == "" {
return nil, errors.New("userID, authID, authID2不能全为空")
func GetAuthBind(db *DaoDB, bindType int, authType, authID string) (authBind *model.AuthBind, err error) {
if bindType == model.AuthBindTypeAll || authType == "" || authID == "" {
return nil, errors.New("bindType不是能all, authType和authID都要有值")
}
sql := `
SELECT *
FROM auth_bind t1
WHERE t1.deleted_at = ? AND t1.status = ?
`
WHERE t1.deleted_at = ? AND t1.status = ? AND t1.bind_type = ? AND t1.type = ? AND t1.auth_id = ?`
sqlParams := []interface{}{
utils.DefaultTimeValue,
model.AuthBindStatusNormal,
}
if userID != "" {
sql += " AND t1.user_id = ?"
sqlParams = append(sqlParams, userID)
}
if bindType != model.AuthBindTypeAll {
sql += " AND t1.bind_type = ?"
sqlParams = append(sqlParams, bindType)
}
if authType != "" {
sql += " AND t1.type = ?"
sqlParams = append(sqlParams, authType)
}
if authID != "" {
sql += " AND t1.auth_id = ?"
sqlParams = append(sqlParams, authID)
bindType,
authType,
authID,
}
// globals.SugarLogger.Debugf("GetAuthBind sql:%s, sqlParams:%s", sql, utils.Format4Output(sqlParams, false))
err = GetRow(db, &authBind, sql, sqlParams...)
return authBind, err
}
func GetUserBindAuthInfo(db *DaoDB, userID string, bindType int, authID2 string, typeList []string) (authList []*model.AuthBind, err error) {
func GetUserBindAuthInfo(db *DaoDB, userID string, bindType int, typeList []string, authID, authID2 string) (authList []*model.AuthBind, err error) {
sql := `
SELECT *
FROM auth_bind t1
@@ -58,14 +44,18 @@ func GetUserBindAuthInfo(db *DaoDB, userID string, bindType int, authID2 string,
sql += " AND t1.bind_type = ?"
sqlParams = append(sqlParams, bindType)
}
if authID2 != "" {
sql += " AND t1.auth_id2 = ?"
sqlParams = append(sqlParams, authID2)
}
if len(typeList) > 0 {
sql += " AND t1.type IN (" + GenQuestionMarks(len(typeList)) + ")"
sqlParams = append(sqlParams, typeList)
}
if authID != "" {
sql += " AND t1.auth_id = ?"
sqlParams = append(sqlParams, authID)
}
if authID2 != "" {
sql += " AND t1.auth_id2 = ?"
sqlParams = append(sqlParams, authID2)
}
err = GetRows(db, &authList, sql, sqlParams...)
return authList, err