package admin import ( "fmt" "strings" . "ciyon/zciyon" ) var Gtokenfield string //header api field var Gtokensalt string //登录盐值 var Gdefpass string //默认密码 func init() { Gtokenfield = "ciyadm" Gtokensalt = "ast34h$2" Gdefpass = "1q2w" } func Verifyfast(db *CiyMysql, post *CiyPost) (map[string]any, int) { rsuser, err := Verifyuser(CiyDB, post) if err != nil { ErrJSON(post.W, "请重新登录", 2) return nil, 0 } return rsuser, Toint(rsuser["id"]) } func Verifyuser(db *CiyMysql, post *CiyPost) (map[string]any, error) { ciyauth := post.R.Header.Get(Gtokenfield) if ciyauth == "" { ciyauth = GetQuery("_"+Gtokenfield, post.R) } if ciyauth == "" { return nil, fmt.Errorf("verify nofind %v in header or query", Gtokenfield) } auth := Str_JSON(Encrypt(ciyauth, "D", Gtokensalt)) if auth == nil { return nil, fmt.Errorf("verify ciyauth error") } csql := NewCiySQL("zc_online") csql.Where("id", auth["_o"]) onlinerow, err := db.Getone(csql) if err != nil { return nil, fmt.Errorf("verify read online err:%v", err) } if Toint(onlinerow["user"]) != Toint(auth["id"]) { return nil, fmt.Errorf("verify userid not match oid=%v", onlinerow["id"]) } if Tostr(onlinerow["sid"]) != Tostr(auth["_s"]) { return nil, fmt.Errorf("verify sid not match oid=%v", onlinerow["id"]) } if Toint(onlinerow["usrchg"]) == 9 { csql := NewCiySQL("zc_admin") csql.Where("id", auth["_o"]) userrow, _ := db.Getone(csql) if userrow == nil { return nil, fmt.Errorf("verify user nofind") } if Toint(userrow["stpstatus"]) != 10 { return nil, fmt.Errorf("verify user disabled") } } if Toint(onlinerow["usrchg"]) == 2 { post.W.Header().Set(Gtokenfield+"re", "true") } if Toint(onlinerow["exptimes"]) > Tostamp() { return auth, nil } exptimes := Tostamp() + 86400 sid := Randstr(10) auth["_s"] = sid authstr := JSON_Str(auth) newauth := Encrypt(authstr, "E", Gtokensalt) post.W.Header().Set(Gtokenfield, newauth) userid := Toint(auth["id"]) db.UserID = userid updata := map[string]any{} updata["exptimes"] = exptimes updata["sid"] = sid updata["ip"] = post.GetIP() csql = NewCiySQL("zc_online") csql.Where("id", auth["_o"]) _, err = db.Update(csql, updata) if err != nil { return nil, fmt.Errorf("verify sid online update err:%v", err) } return auth, nil } func Nopower(db *CiyMysql, userid int, chkpower string) bool { csql := NewCiySQL("zc_admin") csql.Where("id", userid) csql.Column("power") mepower := Tostr(CiyDB.Get1(csql)) if mepower == "" { return true } if len(chkpower) < 3 { return true } if userid == 10 { //超级管理员 return false } pows := strings.Split(mepower, ".") for _, p := range pows { if p == "" { continue } if !strings.HasPrefix(chkpower, p) { continue } return false } return true } func SaveLog(db *CiyMysql, types, msg string) { updata := map[string]any{} updata["types"] = types updata["loguser"] = db.UserID updata["logs"] = msg updata["readuser"] = 0 updata["addtimes"] = Tostamp() csql := NewCiySQL("zc_log") _, err := db.Insert(csql, updata) if err != nil { Log.Warn("LOG", fmt.Sprintf("SaveLog Error:%v[%v]", err, types+":"+msg)) return } } func SaveLogDB(db *CiyMysql, types string, oldrow map[string]any, newrow map[string]any) { SaveLog(db, types, LogDBStr(oldrow, newrow)) } func Getconfig(db *CiyMysql, types, defvalue any) any { csql := NewCiySQL("zc_config") csql.Where("types", types) row, _ := db.Getone(csql) if row != nil { return row["params"] } return defvalue } func Setconfig(db *CiyMysql, types, value any) bool { updata := map[string]any{} updata["types"] = types updata["params"] = value csql := NewCiySQL("zc_config") csql.Where("types", types) _, err := db.Update(csql, updata) return err == nil } func Getcatas(db *CiyMysql, cbstr any) []map[string]any { cbid := 0 if Is_int(cbstr) { cbid = Toint(cbstr) } else { csql := NewCiySQL("zc_cata") csql.Where("codeid", cbstr) csql.Where("cbid=0") csql.Column("id") cbid = Toint(db.Get1(csql)) } if cbid == 0 { return []map[string]any{} } csql := NewCiySQL("zc_cata") csql.Where("cbid", cbid) csql.Order("csort,id") csql.Column("codeid as id,name,upid,name,extdata") catarows, _, err := db.Get(csql) if err != nil { return []map[string]any{} } return catarows } // func Getsaascatas(db *CiyMysql, cbstr any, saasid int) []map[string]any { // cbid := 0 // if Is_int(cbstr) { // cbid = Toint(cbstr) // } else { // csql := NewCiySQL("zc_catsaas") // csql.Where("codeid", cbstr) // csql.Where("cbid=0") // csql.Column("id") // cbid = Toint(db.Get1(csql)) // } // if cbid == 0 { // return []map[string]any{} // } // csql := NewCiySQL("zc_catsaas") // csql.Where("saasid", saasid) // csql.Where("cbid", cbid) // csql.Order("csort,id") // csql.Column("codeid as id,name,upid,name,extdata") // catarows, _, err := db.Get(csql) // if err != nil { // return []map[string]any{} // } // if len(catarows) == 0 { // csql = NewCiySQL("zc_cata") // csql.Where("cbid", cbid) // csql.Order("csort,id") // csql.Column("codeid as id,name,upid,name,extdata") // catarows, _, err = db.Get(csql) // if err != nil { // return []map[string]any{} // } // } // return catarows // }