123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /*
- * @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/iris-contrib/middleware/jwt"
- "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) {
- userInfo := ctx.Values().Get("jwt").(*jwt.Token).Claims.(jwt.MapClaims)
- accountId := userInfo["identity"].(string)
- uid, err := GetDecryptId(accountId)
- // identityIdInt, err := ctx.Values().GetInt("accountId")
- if err != nil {
- return 0, errors.New("项目账号不存在")
- }
- return uid, 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
- }
- // 生成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
- // }
|