/* * @description:工具函数 * @Author: CP * @Date: 2020-09-03 10:08:52 * @FilePath: \construction_management\web\utils\utils.go */ package utils import ( "errors" "fmt" "strconv" "strings" "github.com/kataras/iris/v12" "go.mod/comm" "go.mod/conf" ) // 发送短信 func SendSMS() { } // 字符串转换MAP func StringToMap(str string, split string) map[string]string { entries := strings.Split(str, split) strMap := make(map[string]string) for _, e := range entries { parts := strings.Split(e, ":") if len(parts) <= 1 { strMap["error"] = strings.Replace(parts[0], " ", "", -1) } else { strMap[strings.Replace(parts[0], " ", "", -1)] = strings.Replace(parts[1], " ", "", -1) } } return strMap } // 获得项目ID func GetBackstageKey(ctx iris.Context, key string) (string, error) { //account := ctx.Values().Get("account").(*models.CmProjectAccount) // fmt.Println(account) value := ctx.Values().GetString(key) if value == "" { return "", errors.New("项目不存在") } return value, nil } // 获得项目ID func GetProjectId(ctx iris.Context) (int, error) { //account := ctx.Values().Get("account").(*models.CmProjectAccount) // fmt.Println(account) projectId, err := ctx.Values().GetInt("projectId") if err != nil { return 0, errors.New("项目不存在") } return projectId, nil } // 获得项目账号ID func GetProjectAccountId(ctx iris.Context) (int, error) { identityIdInt, err := ctx.Values().GetInt("accountId") if err != nil { return 0, errors.New("项目账号不存在") } return identityIdInt, nil } // 获得解密后的ID func GetDecryptId(id string) (int, error) { if id == "" { return 0, errors.New("ID 解析错误") } id, err := comm.AesDecrypt(id, conf.SignSecret) if err != nil { return 0, errors.New("ID 解析错误") } idInt, err := strconv.Atoi(id) if err != nil { return 0, errors.New("ID 转换错误") } return idInt, nil } // 金额转字符串 保留2位小数 func PriceToStringFormat(price float64) string { return fmt.Sprintf("%.2f", price) } // 生成code func CreateRuleCode(code int64, count int64, len int) string { // fmt.Println("rule", rule, "counts", count) // egRule := strings.Split(rule.Eg, "-") // for _, value := range egRule { // if strings.Contains(value, "_") { // // fmt.Println("rule", rule.Code, count) // code := fmt.Printf("%0*d", rule.Code, count+1) // strings.Replace(rule.Eg, ) // } // } // fmt.Println("egRule:", egRule) // return "123" // var newCode string if code == 0 { a := fmt.Sprintf("%0*d", len, count) return a } else { b := fmt.Sprintf("%0*d", len, count+code) return b } } // // 获得项目ID // func GetProjectId(ctx iris.Context) (int, error) { // jwtInfo := ctx.Values().Get("jwt").(*jwt.Token) // project := jwtInfo.Claims.(jwt.MapClaims)["project"].(string) // projectId, err := comm.AesDecrypt(project, conf.CookieSecret) // if err != nil { // return 0, errors.New("项目不存在") // } // projectIdInt, err := strconv.Atoi(projectId) // if err != nil { // return 0, errors.New("项目不存在") // } // return projectIdInt, nil // } // // 获得项目账号ID // func GetProjectAccountId(ctx iris.Context) (int, error) { // jwtInfo := ctx.Values().Get("jwt").(*jwt.Token) // identity := jwtInfo.Claims.(jwt.MapClaims)["identity"].(string) // identityId, err := comm.AesDecrypt(identity, conf.CookieSecret) // if err != nil { // return 0, errors.New("项目账号不存在") // } // identityIdInt, err := strconv.Atoi(identityId) // if err != nil { // return 0, errors.New("项目账号不存在") // } // return identityIdInt, nil // }