lib.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package lib
  2. import (
  3. "bytes"
  4. "crypto/sha256"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/nats-io/nats.go"
  9. "github.com/thinkeridea/go-extend/exunicode/exutf8"
  10. "golang.org/x/text/encoding/simplifiedchinese"
  11. "golang.org/x/text/transform"
  12. "io/ioutil"
  13. "math/rand"
  14. "plugin"
  15. "runtime"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "unicode/utf8"
  20. )
  21. var Nats *nats.Conn
  22. var TopicMap map[string]bool
  23. var PluginMap map[string]*plugin.Plugin
  24. func init() {
  25. TopicMap = make(map[string]bool)
  26. PluginMap = make(map[string]*plugin.Plugin)
  27. }
  28. const Success = 200
  29. const Error = 201
  30. type JSONR struct {
  31. //必须的大写开头
  32. Code int16
  33. Msg string
  34. Data interface{} // 泛型
  35. }
  36. type JSONS struct {
  37. //必须的大写开头
  38. List interface{}
  39. Total int16
  40. PageIndex int
  41. PageSize int
  42. }
  43. func C_Page(list interface{}, PageIndex, PageSize int, Total int64) (Jsons JSONS) {
  44. Jsons.List = list
  45. Jsons.Total = int16(Total)
  46. Jsons.PageIndex = PageIndex
  47. Jsons.PageSize = PageSize
  48. //Jsons.PageSize = int16(math.Ceil(float64(Total) / float64(PageSize)))
  49. return Jsons
  50. }
  51. func Strval(value interface{}) string {
  52. var key string
  53. if value == nil {
  54. return key
  55. }
  56. switch value.(type) {
  57. case float64:
  58. ft := value.(float64)
  59. key = strconv.FormatFloat(ft, 'f', -1, 64)
  60. case float32:
  61. ft := value.(float32)
  62. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  63. case int:
  64. it := value.(int)
  65. key = strconv.Itoa(it)
  66. case uint:
  67. it := value.(uint)
  68. key = strconv.Itoa(int(it))
  69. case int8:
  70. it := value.(int8)
  71. key = strconv.Itoa(int(it))
  72. case uint8:
  73. it := value.(uint8)
  74. key = strconv.Itoa(int(it))
  75. case int16:
  76. it := value.(int16)
  77. key = strconv.Itoa(int(it))
  78. case uint16:
  79. it := value.(uint16)
  80. key = strconv.Itoa(int(it))
  81. case int32:
  82. it := value.(int32)
  83. key = strconv.Itoa(int(it))
  84. case uint32:
  85. it := value.(uint32)
  86. key = strconv.Itoa(int(it))
  87. case int64:
  88. it := value.(int64)
  89. key = strconv.FormatInt(it, 10)
  90. case uint64:
  91. it := value.(uint64)
  92. key = strconv.FormatUint(it, 10)
  93. case string:
  94. key = value.(string)
  95. case []byte:
  96. key = string(value.([]byte))
  97. default:
  98. newValue, _ := json.Marshal(value)
  99. key = string(newValue)
  100. }
  101. return key
  102. }
  103. func To_int(value interface{}) int {
  104. var key int
  105. if value == nil {
  106. return key
  107. }
  108. switch value.(type) {
  109. case float64:
  110. key = int(value.(float64))
  111. case float32:
  112. key = int(value.(float32))
  113. case int:
  114. key = int(value.(int))
  115. case uint:
  116. key = int(value.(uint))
  117. case int8:
  118. key = int(value.(int8))
  119. case uint8:
  120. key = int(value.(uint8))
  121. case int16:
  122. key = int(value.(int16))
  123. case uint16:
  124. key = int(value.(uint16))
  125. case int32:
  126. key = int(value.(int32))
  127. case uint32:
  128. key = int(value.(uint32))
  129. case int64:
  130. key = int(value.(int64))
  131. case uint64:
  132. key = int(value.(uint64))
  133. case string:
  134. key, _ = strconv.Atoi(value.(string))
  135. case []byte:
  136. key, _ = strconv.Atoi(string(value.([]byte)))
  137. default:
  138. newValue, _ := json.Marshal(value)
  139. key, _ = strconv.Atoi(string(newValue))
  140. }
  141. return key
  142. }
  143. func To_int64(value interface{}) int64 {
  144. var key int64
  145. if value == nil {
  146. return key
  147. }
  148. switch value.(type) {
  149. case float64:
  150. key = int64(value.(float64))
  151. case float32:
  152. key = int64(value.(float32))
  153. case int:
  154. key = int64(value.(int))
  155. case uint:
  156. key = int64(value.(uint))
  157. case int8:
  158. key = int64(value.(int8))
  159. case uint8:
  160. key = int64(value.(uint8))
  161. case int16:
  162. key = int64(value.(int16))
  163. case uint16:
  164. key = int64(value.(uint16))
  165. case int32:
  166. key = int64(value.(int32))
  167. case uint32:
  168. key = int64(value.(uint32))
  169. case int64:
  170. key = int64(value.(int64))
  171. case uint64:
  172. key = int64(value.(uint64))
  173. case string:
  174. key, _ = strconv.ParseInt(value.(string), 10, 64)
  175. case []byte:
  176. key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
  177. default:
  178. newValue, _ := json.Marshal(value)
  179. key, _ = strconv.ParseInt(string(newValue), 10, 64)
  180. }
  181. return key
  182. }
  183. func To_float32(value interface{}) float32 {
  184. var key float32
  185. if value == nil {
  186. return key
  187. }
  188. switch value.(type) {
  189. case float64:
  190. key = float32(value.(float64))
  191. case float32:
  192. key = float32(value.(float32))
  193. case int:
  194. key = float32(value.(int))
  195. case uint:
  196. key = float32(value.(uint))
  197. case int8:
  198. key = float32(value.(int8))
  199. case uint8:
  200. key = float32(value.(uint8))
  201. case int16:
  202. key = float32(value.(int16))
  203. case uint16:
  204. key = float32(value.(uint16))
  205. case int32:
  206. key = float32(value.(int32))
  207. case uint32:
  208. key = float32(value.(uint32))
  209. case int64:
  210. key = float32(value.(int64))
  211. case uint64:
  212. key = float32(value.(uint64))
  213. case string:
  214. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  215. key = float32(key_float64)
  216. case []byte:
  217. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  218. key = float32(key_float64)
  219. default:
  220. newValue, _ := json.Marshal(value)
  221. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  222. key = float32(key_float64)
  223. }
  224. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  225. key = float32(key_float64)
  226. return key
  227. }
  228. func To_string(value interface{}) string {
  229. var key string
  230. if value == nil {
  231. return key
  232. }
  233. switch value.(type) {
  234. case float64:
  235. ft := value.(float64)
  236. key = strconv.FormatFloat(ft, 'f', -1, 64)
  237. case float32:
  238. ft := value.(float32)
  239. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  240. case int:
  241. it := value.(int)
  242. key = strconv.Itoa(it)
  243. case uint:
  244. it := value.(uint)
  245. key = strconv.Itoa(int(it))
  246. case int8:
  247. it := value.(int8)
  248. key = strconv.Itoa(int(it))
  249. case uint8:
  250. it := value.(uint8)
  251. key = strconv.Itoa(int(it))
  252. case int16:
  253. it := value.(int16)
  254. key = strconv.Itoa(int(it))
  255. case uint16:
  256. it := value.(uint16)
  257. key = strconv.Itoa(int(it))
  258. case int32:
  259. it := value.(int32)
  260. key = strconv.Itoa(int(it))
  261. case uint32:
  262. it := value.(uint32)
  263. key = strconv.Itoa(int(it))
  264. case int64:
  265. it := value.(int64)
  266. key = strconv.FormatInt(it, 10)
  267. case uint64:
  268. it := value.(uint64)
  269. key = strconv.FormatUint(it, 10)
  270. case string:
  271. key = value.(string)
  272. case []byte:
  273. key = string(value.([]byte))
  274. default:
  275. newValue, _ := json.Marshal(value)
  276. key = string(newValue)
  277. }
  278. return key
  279. }
  280. func Random(min, max int) int {
  281. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  282. return rand.Intn(max-min) + min
  283. }
  284. // 取文本(字符串)中间
  285. func GetBetweenStr(str, start, end string) string {
  286. n := strings.Index(str, start)
  287. if n == -1 {
  288. n = 0
  289. } else {
  290. n = n + len(start) // 增加了else,不加的会把start带上
  291. }
  292. str = string([]byte(str)[n:])
  293. m := strings.Index(str, end)
  294. if m == -1 {
  295. m = len(str)
  296. }
  297. str = string([]byte(str)[:m])
  298. return str
  299. }
  300. // getYearMonthToDay 查询指定年份指定月份有多少天
  301. // @params year int 指定年份
  302. // @params month int 指定月份
  303. func GetYearMonthToDay(year int, month int) int {
  304. // 有31天的月份
  305. day31 := map[int]bool{
  306. 1: true,
  307. 3: true,
  308. 5: true,
  309. 7: true,
  310. 8: true,
  311. 10: true,
  312. 12: true,
  313. }
  314. if day31[month] == true {
  315. return 31
  316. }
  317. // 有30天的月份
  318. day30 := map[int]bool{
  319. 4: true,
  320. 6: true,
  321. 9: true,
  322. 11: true,
  323. }
  324. if day30[month] == true {
  325. return 30
  326. }
  327. // 计算是平年还是闰年
  328. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  329. // 得出2月的天数
  330. return 29
  331. }
  332. // 得出2月的天数
  333. return 28
  334. }
  335. func Decimal(value float64) float64 {
  336. value, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", value), 64)
  337. return value
  338. }
  339. func Strconv_Atoi(string string) int {
  340. int, _ := strconv.Atoi(string)
  341. return int
  342. }
  343. // 获取正在运行的函数名
  344. func FuncName() string {
  345. pc := make([]uintptr, 1)
  346. runtime.Callers(2, pc)
  347. f := runtime.FuncForPC(pc[0])
  348. return f.Name()
  349. }
  350. func Limit_len(str string, lenx int) string {
  351. if utf8.RuneCountInString(str) > lenx {
  352. return exutf8.RuneSubString(str, 0, lenx-3) + "..."
  353. }
  354. return exutf8.RuneSubString(str, 0, lenx)
  355. }
  356. func Utf8ToGbk(s []byte) ([]byte, error) {
  357. reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
  358. d, e := ioutil.ReadAll(reader)
  359. if e != nil {
  360. return nil, e
  361. }
  362. return d, nil
  363. }
  364. // #取得随机字符串:通过打乱slice来操作
  365. func GetRandstring(length int, char string, rand_x int64) string {
  366. if length < 1 {
  367. return ""
  368. }
  369. if len(char) <= 6 || len(char) <= length {
  370. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  371. }
  372. charArr := strings.Split(char, "")
  373. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  374. l := len(charArr)
  375. for i := l - 1; i > 0; i-- {
  376. r := ran.Intn(i)
  377. charArr[r], charArr[i] = charArr[i], charArr[r]
  378. }
  379. rchar := charArr[:length]
  380. return strings.Join(rchar, "")
  381. }
  382. // Sha256加密
  383. func Sha256(src string) string {
  384. m := sha256.New()
  385. m.Write([]byte(src))
  386. res := hex.EncodeToString(m.Sum(nil))
  387. return res
  388. }
  389. // 判断时间是当年的第几周
  390. func WeekByDate() string {
  391. t := time.Now()
  392. yearDay := t.YearDay()
  393. yearFirstDay := t.AddDate(0, 0, -yearDay+1)
  394. firstDayInWeek := int(yearFirstDay.Weekday())
  395. //今年第一周有几天
  396. firstWeekDays := 1
  397. if firstDayInWeek != 0 {
  398. firstWeekDays = 7 - firstDayInWeek + 1
  399. }
  400. var week int
  401. if yearDay <= firstWeekDays {
  402. week = 1
  403. } else {
  404. week = (yearDay-firstWeekDays)/7 + 2
  405. }
  406. return fmt.Sprintf("%d%02d", t.Year(), week) // 202253
  407. }