LAPP 打印服务 支持条码打印和表单打印 通过Socket或windows打印方式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.5 KiB

  1. package glog
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. )
  8. func GetCurrentDir() string {
  9. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
  10. if err != nil {
  11. c, _ := os.Getwd()
  12. return c
  13. }
  14. return filepath.ToSlash(dir)
  15. }
  16. func GetAbsolutePath(path string) string {
  17. if strings.Index(path, "./") == 0 || strings.Index(path, ".\\") == 0 {
  18. r, _ := filepath.Abs(GetCurrentDir() + path[1:])
  19. return filepath.ToSlash(r)
  20. }
  21. r, _ := filepath.Abs(path)
  22. return filepath.ToSlash(r)
  23. }
  24. func GetDir(path string) string {
  25. if strings.Index(path, "./") == 0 || strings.Index(path, ".\\") == 0 {
  26. return filepath.ToSlash(filepath.Dir(GetCurrentDir() + path[1:]))
  27. }
  28. return filepath.ToSlash(filepath.Dir(path))
  29. }
  30. func EnsureDir(dir string) string {
  31. fullDir := GetAbsolutePath(dir)
  32. if IsExists(fullDir) {
  33. return fullDir
  34. }
  35. os.MkdirAll(fullDir, 777)
  36. return fullDir
  37. }
  38. func EnsureFilePath(filePath string) string {
  39. filePath = GetAbsolutePath(filePath)
  40. EnsureDir(GetDir(filePath))
  41. return filePath
  42. }
  43. func IsExists(path string) bool {
  44. _, err := os.Stat(path)
  45. if err != nil && os.IsNotExist(err) {
  46. return false
  47. }
  48. return true
  49. }
  50. func StructToString(obj interface{}) string {
  51. if s, ok := obj.(string); ok {
  52. return s
  53. }
  54. if s, ok := obj.(*string); ok {
  55. return *s
  56. }
  57. if s, ok := obj.([]byte); ok {
  58. n := len(s)
  59. if n > 0 && s[0] == '{' && s[n-1] == '}' {
  60. return string(s)
  61. }
  62. }
  63. res, err := json.MarshalIndent(obj, "", " ")
  64. if err != nil {
  65. Errorln(err)
  66. }
  67. s := string(res)
  68. return s
  69. }