package service import ( "encoding/json" "math" "math/rand" "runtime" pb "sportfitness/base/api/grpc/base" "sportfitness/base/errors" "sportfitness/base/repository/grpc/bsw/im" im2 "sportfitness/base/repository/grpc/bsw/im/im" "strings" "time" "git.beswell.com/gframe/application" "github.com/sirupsen/logrus" ) type base struct { } func RegisterServices() { application.GetApp().SetServiceRegisterHandler(func(servicePathList []string) { logrus.Info("同步服务权限") im.RegisterServiceList(servicePathList) }) err := application.GetApp().RegisterService("", User{}) if err != nil { logrus.Fatalf("服务保存失败: \n%s", err) } } func GetFuncName() string { pc := make([]uintptr, 1) runtime.Callers(2, pc) f := runtime.FuncForPC(pc[0]).Name() println(f) l := strings.Split(f, ".") serviceName := l[1] + "/" + l[2] return serviceName } func (base) checkPermission(token string, shopId int64) (optId int64) { // 用token获取用户ID optId = User{}.SubSessionCheck(token) pc := make([]uintptr, 1) runtime.Callers(2, pc) f := runtime.FuncForPC(pc[0]).Name() println(f) l := strings.Split(f, ".") serviceName := l[1] + "/" + l[2] // 目标商家是否有操作权限 rs := im.SubPermissionListCheck(token, shopId, []string{serviceName}).ResultList[0] if !rs { // todo 暂时忽略权限校验 //panic(errors.PmsnError) } return } func (base) JSONToMap(str string) (map[string]interface{}, error) { var tempMap map[string]interface{} err := json.Unmarshal([]byte(str), &tempMap) if err != nil { err = errors.J2MError return tempMap, err } return tempMap, nil } func (base) WebStatusCheck(s pb.WebStatus) pb.WebStatus { var ws pb.WebStatus switch s { case 0: ws = pb.WebStatus_Ban case 1: ws = pb.WebStatus_On case 9: ws = pb.WebStatus_Delete case 99: ws = pb.WebStatus_All default: panic(errors.WebStatusError) } return ws } func (b base) WebStatusToManageStatus(s pb.WebStatus) im2.Status { var status im2.Status ws := b.WebStatusCheck(s) switch ws { case 99: status = im2.Status_Null case 0: status = im2.Status_Ban case 1: status = im2.Status_On case 9: status = im2.Status_Delete default: panic(errors.WebToImStatusError) } return status } func (base) ManageStatusToWebStatus(s im2.Status) (status pb.WebStatus) { switch s { case 1: status = pb.WebStatus_Ban case 2: status = pb.WebStatus_On case 9: status = pb.WebStatus_Delete default: panic(errors.ImToWebtatusError) } return status } func RandomInt(n int) int { min := int(math.Pow(10, float64(n-1))) max := min*9 - 1 rand1 := rand.New(rand.NewSource(time.Now().UnixNano())) code := rand1.Intn(max) + min return code } func (base) checkRs(rs string) bool { b := false rsMap := make(map[string]interface{}) err := json.Unmarshal([]byte(rs), &rsMap) if err != nil { panic(errors.J2MError) } code := int(rsMap["code"].(float64)) if code == 0 { b = true } else { b = false } return b }