1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /*
- * @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/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) {
- //account := ctx.Values().Get("account").(*viewmodels.ProjectAccount)
- // 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) {
- 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
- }
- // // 获得项目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
- // }
|