github.com/zhigang2008/qrserver@v0.0.0-20150521135340-51313e45d270/dqs/util/funcs.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"html/template"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  //判断相等
    11  func Equals(a, b interface{}) bool {
    12  	switch a.(type) {
    13  	case int:
    14  		_, btype := b.(int)
    15  		if btype {
    16  			return a == b
    17  		} else {
    18  			return false
    19  		}
    20  	case uint:
    21  		_, btype := b.(uint)
    22  		if btype {
    23  			return a == b
    24  		} else {
    25  			return false
    26  		}
    27  	case int64:
    28  		_, btype := b.(int64)
    29  		if btype {
    30  			return a == b
    31  		} else {
    32  			return false
    33  		}
    34  	case uint64:
    35  		_, btype := b.(uint64)
    36  		if btype {
    37  			return a == b
    38  		} else {
    39  			return false
    40  		}
    41  	case byte:
    42  		_, btype := b.(byte)
    43  		if btype {
    44  			return a == b
    45  		} else {
    46  			return false
    47  		}
    48  	case float32:
    49  		_, btype := b.(float32)
    50  		if btype {
    51  			return a == b
    52  		} else {
    53  			return false
    54  		}
    55  	case string:
    56  		_, btype := b.(string)
    57  		if btype {
    58  			return a == b
    59  		} else {
    60  			return false
    61  		}
    62  	default:
    63  		return false
    64  	}
    65  }
    66  
    67  //生成列表序号
    68  func GenerateSeqNo(seq, step, pos int) int {
    69  	return seq + 1 + (step * (pos - 1))
    70  }
    71  
    72  //生成查询参数连接url
    73  func GenerateParamUrl(p map[string]interface{}) string {
    74  	purl := ""
    75  	for k, o := range p {
    76  		purl += "&" + k + "="
    77  		switch v := o.(type) {
    78  		case int:
    79  			purl += strconv.Itoa(v)
    80  		case bool:
    81  			purl += strconv.FormatBool(v)
    82  		case int64:
    83  			purl += strconv.FormatInt(v, 10)
    84  		case uint64:
    85  			purl += strconv.FormatUint(v, 10)
    86  		default:
    87  			purl += fmt.Sprintf("%s", v)
    88  
    89  		}
    90  	}
    91  	return purl
    92  }
    93  
    94  //判断是否包含在数组中
    95  func Contain(c interface{}, b interface{}) bool {
    96  	switch c.(type) {
    97  	case []string:
    98  		n, ok := c.([]string)
    99  		if ok {
   100  			for _, v := range n {
   101  				if strings.TrimSpace(fmt.Sprintf("%v", v)) == strings.TrimSpace(fmt.Sprintf("%v", b)) {
   102  					return true
   103  				}
   104  			}
   105  		}
   106  	case []int:
   107  		n, ok := c.([]int)
   108  		if ok {
   109  			for _, v := range n {
   110  				if strings.TrimSpace(fmt.Sprintf("%v", v)) == strings.TrimSpace(fmt.Sprintf("%v", b)) {
   111  					return true
   112  				}
   113  			}
   114  		}
   115  	case []int32:
   116  		n, ok := c.([]int32)
   117  		if ok {
   118  			for _, v := range n {
   119  				if strings.TrimSpace(fmt.Sprintf("%v", v)) == strings.TrimSpace(fmt.Sprintf("%v", b)) {
   120  					return true
   121  				}
   122  			}
   123  		}
   124  	case []int64:
   125  		n, ok := c.([]int64)
   126  		if ok {
   127  			for _, v := range n {
   128  				if strings.TrimSpace(fmt.Sprintf("%v", v)) == strings.TrimSpace(fmt.Sprintf("%v", b)) {
   129  					return true
   130  				}
   131  			}
   132  		}
   133  	default:
   134  		return false
   135  	}
   136  
   137  	return false
   138  }
   139  
   140  /**/
   141  func HasRoles(userRoles []string, roles ...string) bool {
   142  	if userRoles != nil {
   143  		for _, role := range roles {
   144  			if Contain(userRoles, role) {
   145  				return true
   146  			}
   147  		}
   148  	} else {
   149  		return false
   150  	}
   151  	return false
   152  }
   153  
   154  //返回原始html信息
   155  func RawHTML(text string) template.HTML {
   156  	return template.HTML(text)
   157  }
   158  func RawHTMLAttr(text string) template.HTMLAttr {
   159  	return template.HTMLAttr(text)
   160  }
   161  func RawCSS(text string) template.CSS {
   162  	return template.CSS(text)
   163  }
   164  func RawJS(text string) template.JS {
   165  	return template.JS(text)
   166  }
   167  func RawJSStr(text string) template.JSStr {
   168  	return template.JSStr(text)
   169  }
   170  func RawURL(text string) template.URL {
   171  	return template.URL(text)
   172  }