1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- /*
- * @description:工具函数
- * @Author: CP
- * @Date: 2020-09-03 10:08:52
- * @FilePath: \construction_management\web\utils\utils.go
- */
- package utils
- import (
- "errors"
- "strconv"
- "strings"
- "github.com/dgrijalva/jwt-go"
- "github.com/kataras/iris/v12"
- "go.mod/comm"
- "go.mod/conf"
- )
- // 字符串转换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, ":")
- strMap[strings.Replace(parts[0], " ", "", -1)] = strings.Replace(parts[1], " ", "", -1)
- }
- return strMap
- }
- // 获得项目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
- }
|