common.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. package utils
  2. import (
  3. "bufio"
  4. "crypto/md5"
  5. cryRand "crypto/rand"
  6. "crypto/sha1"
  7. "encoding/base64"
  8. "encoding/hex"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "image"
  13. "image/png"
  14. "io"
  15. "io/ioutil"
  16. "math"
  17. "math/big"
  18. "math/rand"
  19. "net"
  20. "net/http"
  21. "net/http/cookiejar"
  22. "os"
  23. "os/exec"
  24. "path"
  25. "regexp"
  26. "strconv"
  27. "strings"
  28. "time"
  29. "unicode"
  30. "github.com/shopspring/decimal"
  31. "github.com/mozillazg/go-pinyin"
  32. )
  33. // 随机数种子
  34. var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
  35. func GetRandString(size int) string {
  36. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "@", "#", "$", "%", "^", "&", "*"}
  37. randomSb := ""
  38. digitSize := len(allLetterDigit)
  39. for i := 0; i < size; i++ {
  40. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  41. }
  42. return randomSb
  43. }
  44. func GetRandStringNoSpecialChar(size int) string {
  45. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  46. randomSb := ""
  47. digitSize := len(allLetterDigit)
  48. for i := 0; i < size; i++ {
  49. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  50. }
  51. return randomSb
  52. }
  53. func StringsToJSON(str string) string {
  54. rs := []rune(str)
  55. jsons := ""
  56. for _, r := range rs {
  57. rint := int(r)
  58. if rint < 128 {
  59. jsons += string(r)
  60. } else {
  61. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  62. }
  63. }
  64. return jsons
  65. }
  66. // 序列化
  67. func ToString(v interface{}) string {
  68. data, _ := json.Marshal(v)
  69. return string(data)
  70. }
  71. // md5加密
  72. func MD5(data string) string {
  73. m := md5.Sum([]byte(data))
  74. return hex.EncodeToString(m[:])
  75. }
  76. // 获取数字随机字符
  77. func GetRandDigit(n int) string {
  78. return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
  79. }
  80. // 获取随机数
  81. func GetRandNumber(n int) int {
  82. return rnd.Intn(n)
  83. }
  84. func GetRandInt(min, max int) int {
  85. if min >= max || min == 0 || max == 0 {
  86. return max
  87. }
  88. return rand.Intn(max-min) + min
  89. }
  90. func GetToday(format string) string {
  91. today := time.Now().Format(format)
  92. return today
  93. }
  94. // 获取今天剩余秒数
  95. func GetTodayLastSecond() time.Duration {
  96. today := GetToday(FormatDate) + " 23:59:59"
  97. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  98. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  99. }
  100. // 处理出生日期函数
  101. func GetBrithDate(idcard string) string {
  102. l := len(idcard)
  103. var s string
  104. if l == 15 {
  105. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  106. return s
  107. }
  108. if l == 18 {
  109. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  110. return s
  111. }
  112. return GetToday(FormatDate)
  113. }
  114. // 处理性别
  115. func WhichSexByIdcard(idcard string) string {
  116. var sexs = [2]string{"女", "男"}
  117. length := len(idcard)
  118. if length == 18 {
  119. sex, _ := strconv.Atoi(string(idcard[16]))
  120. return sexs[sex%2]
  121. } else if length == 15 {
  122. sex, _ := strconv.Atoi(string(idcard[14]))
  123. return sexs[sex%2]
  124. }
  125. return "男"
  126. }
  127. // 截取小数点后几位
  128. func SubFloatToString(f float64, m int) string {
  129. n := strconv.FormatFloat(f, 'f', -1, 64)
  130. if n == "" {
  131. return ""
  132. }
  133. if m >= len(n) {
  134. return n
  135. }
  136. newn := strings.Split(n, ".")
  137. if m == 0 {
  138. return newn[0]
  139. }
  140. if len(newn) < 2 || m >= len(newn[1]) {
  141. return n
  142. }
  143. return newn[0] + "." + newn[1][:m]
  144. }
  145. // 截取小数点后几位
  146. func SubFloatToFloat(f float64, m int) float64 {
  147. newn := SubFloatToString(f, m)
  148. newf, _ := strconv.ParseFloat(newn, 64)
  149. return newf
  150. }
  151. // 截取小数点后几位
  152. func SubFloatToFloatStr(f float64, m int) string {
  153. newn := SubFloatToString(f, m)
  154. return newn
  155. }
  156. // 获取相差时间-年
  157. func GetYearDiffer(start_time, end_time string) int {
  158. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  159. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  160. age := t2.Year() - t1.Year()
  161. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  162. age--
  163. }
  164. return age
  165. }
  166. // 获取相差时间-秒
  167. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  168. diff := end_time.Unix() - start_time.Unix()
  169. return diff
  170. }
  171. func FixFloat(f float64, m int) float64 {
  172. newn := SubFloatToString(f+0.00000001, m)
  173. newf, _ := strconv.ParseFloat(newn, 64)
  174. return newf
  175. }
  176. // 将字符串数组转化为逗号分割的字符串形式 ["str1","str2","str3"] >>> "str1,str2,str3"
  177. func StrListToString(strList []string) (str string) {
  178. if len(strList) > 0 {
  179. for k, v := range strList {
  180. if k == 0 {
  181. str = v
  182. } else {
  183. str = str + "," + v
  184. }
  185. }
  186. return
  187. }
  188. return ""
  189. }
  190. // Token
  191. func GetToken() string {
  192. randStr := GetRandString(64)
  193. token := MD5(randStr + Md5Key)
  194. tokenLen := 64 - len(token)
  195. return strings.ToUpper(token + GetRandString(tokenLen))
  196. }
  197. // 数据没有记录
  198. func ErrNoRow() string {
  199. return "<QuerySeter> no row found"
  200. }
  201. // 判断文件是否存在
  202. func FileIsExist(filePath string) bool {
  203. _, err := os.Stat(filePath)
  204. return err == nil || os.IsExist(err)
  205. }
  206. // 获取图片扩展名
  207. func GetImgExt(file string) (ext string, err error) {
  208. var headerByte []byte
  209. headerByte = make([]byte, 8)
  210. fd, err := os.Open(file)
  211. if err != nil {
  212. return "", err
  213. }
  214. defer fd.Close()
  215. _, err = fd.Read(headerByte)
  216. if err != nil {
  217. return "", err
  218. }
  219. xStr := fmt.Sprintf("%x", headerByte)
  220. switch {
  221. case xStr == "89504e470d0a1a0a":
  222. ext = ".png"
  223. case xStr == "0000010001002020":
  224. ext = ".ico"
  225. case xStr == "0000020001002020":
  226. ext = ".cur"
  227. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  228. ext = ".gif"
  229. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  230. ext = ".tga"
  231. case xStr[:8] == "464f524d":
  232. ext = ".iff"
  233. case xStr[:8] == "52494646":
  234. ext = ".ani"
  235. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  236. ext = ".tiff"
  237. case xStr[:4] == "424d":
  238. ext = ".bmp"
  239. case xStr[:4] == "ffd8":
  240. ext = ".jpg"
  241. case xStr[:2] == "0a":
  242. ext = ".pcx"
  243. default:
  244. ext = ""
  245. }
  246. return ext, nil
  247. }
  248. // 保存图片
  249. func SaveImage(path string, img image.Image) (err error) {
  250. //需要保持的文件
  251. imgfile, err := os.Create(path)
  252. defer imgfile.Close()
  253. // 以PNG格式保存文件
  254. err = png.Encode(imgfile, img)
  255. return err
  256. }
  257. // 下载图片
  258. func DownloadImage(imgUrl string) (filePath string, err error) {
  259. imgPath := "./static/imgs/"
  260. fileName := path.Base(imgUrl)
  261. res, err := http.Get(imgUrl)
  262. if err != nil {
  263. fmt.Println("A error occurred!")
  264. return
  265. }
  266. defer res.Body.Close()
  267. // 获得get请求响应的reader对象
  268. reader := bufio.NewReaderSize(res.Body, 32*1024)
  269. filePath = imgPath + fileName
  270. file, err := os.Create(filePath)
  271. if err != nil {
  272. return
  273. }
  274. // 获得文件的writer对象
  275. writer := bufio.NewWriter(file)
  276. written, _ := io.Copy(writer, reader)
  277. fmt.Printf("Total length: %d \n", written)
  278. return
  279. }
  280. // 保存base64数据为文件
  281. func SaveBase64ToFile(content, path string) error {
  282. data, err := base64.StdEncoding.DecodeString(content)
  283. if err != nil {
  284. return err
  285. }
  286. f, err := os.Create(path)
  287. defer f.Close()
  288. if err != nil {
  289. return err
  290. }
  291. f.Write(data)
  292. return nil
  293. }
  294. func SaveBase64ToFileBySeek(content, path string) (err error) {
  295. data, err := base64.StdEncoding.DecodeString(content)
  296. exist, err := PathExists(path)
  297. if err != nil {
  298. return
  299. }
  300. if !exist {
  301. f, err := os.Create(path)
  302. if err != nil {
  303. return err
  304. }
  305. n, _ := f.Seek(0, 2)
  306. // 从末尾的偏移量开始写入内容
  307. _, err = f.WriteAt([]byte(data), n)
  308. defer f.Close()
  309. } else {
  310. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  311. if err != nil {
  312. return err
  313. }
  314. n, _ := f.Seek(0, 2)
  315. // 从末尾的偏移量开始写入内容
  316. _, err = f.WriteAt([]byte(data), n)
  317. defer f.Close()
  318. }
  319. return nil
  320. }
  321. // 把秒级的时间戳转为time格式
  322. func SecondToTime(sec int64) time.Time {
  323. return time.Unix(sec, 0)
  324. }
  325. func PathExists(path string) (bool, error) {
  326. _, err := os.Stat(path)
  327. if err == nil {
  328. return true, nil
  329. }
  330. if os.IsNotExist(err) {
  331. return false, nil
  332. }
  333. return false, err
  334. }
  335. func StartIndex(page, pagesize int) int {
  336. if page > 1 {
  337. return (page - 1) * pagesize
  338. }
  339. return 0
  340. }
  341. func PageCount(count, pagesize int) int {
  342. if count%pagesize > 0 {
  343. return count/pagesize + 1
  344. } else {
  345. return count / pagesize
  346. }
  347. }
  348. func TrimHtml(src string) string {
  349. //将HTML标签全转换成小写
  350. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  351. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  352. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  353. src = re.ReplaceAllString(src, "[图片]")
  354. re, _ = regexp.Compile("class[\\S\\s]+?>")
  355. src = re.ReplaceAllString(src, "")
  356. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  357. src = re.ReplaceAllString(src, "")
  358. return strings.TrimSpace(src)
  359. }
  360. //1556164246 -> 2019-04-25 03:50:46 +0000
  361. //timestamp
  362. func TimeToTimestamp() {
  363. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  364. }
  365. func ToUnicode(text string) string {
  366. textQuoted := strconv.QuoteToASCII(text)
  367. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  368. return textUnquoted
  369. }
  370. func VersionToInt(version string) int {
  371. version = strings.Replace(version, ".", "", -1)
  372. n, _ := strconv.Atoi(version)
  373. return n
  374. }
  375. func IsCheckInList(list []int, s int) bool {
  376. for _, v := range list {
  377. if v == s {
  378. return true
  379. }
  380. }
  381. return false
  382. }
  383. func round(num float64) int {
  384. return int(num + math.Copysign(0.5, num))
  385. }
  386. func toFixed(num float64, precision int) float64 {
  387. output := math.Pow(10, float64(precision))
  388. return float64(round(num*output)) / output
  389. }
  390. // GetWilsonScore returns Wilson Score
  391. func GetWilsonScore(p, n float64) float64 {
  392. if p == 0 && n == 0 {
  393. return 0
  394. }
  395. return toFixed(((p+1.9208)/(p+n)-1.96*math.Sqrt(p*n/(p+n)+0.9604)/(p+n))/(1+3.8416/(p+n)), 2)
  396. }
  397. // 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
  398. func ChangeWordsToNum(str string) (numStr string) {
  399. words := ([]rune)(str)
  400. num := 0
  401. n := 0
  402. for i := 0; i < len(words); i++ {
  403. word := string(words[i : i+1])
  404. switch word {
  405. case "万":
  406. if n == 0 {
  407. n = 1
  408. }
  409. n = n * 10000
  410. num = num*10000 + n
  411. n = 0
  412. case "千":
  413. if n == 0 {
  414. n = 1
  415. }
  416. n = n * 1000
  417. num += n
  418. n = 0
  419. case "百":
  420. if n == 0 {
  421. n = 1
  422. }
  423. n = n * 100
  424. num += n
  425. n = 0
  426. case "十":
  427. if n == 0 {
  428. n = 1
  429. }
  430. n = n * 10
  431. num += n
  432. n = 0
  433. case "一":
  434. n += 1
  435. case "二":
  436. n += 2
  437. case "三":
  438. n += 3
  439. case "四":
  440. n += 4
  441. case "五":
  442. n += 5
  443. case "六":
  444. n += 6
  445. case "七":
  446. n += 7
  447. case "八":
  448. n += 8
  449. case "九":
  450. n += 9
  451. case "零":
  452. default:
  453. if n > 0 {
  454. num += n
  455. n = 0
  456. }
  457. if num == 0 {
  458. numStr += word
  459. } else {
  460. numStr += strconv.Itoa(num) + word
  461. num = 0
  462. }
  463. }
  464. }
  465. if n > 0 {
  466. num += n
  467. n = 0
  468. }
  469. if num != 0 {
  470. numStr += strconv.Itoa(num)
  471. }
  472. return
  473. }
  474. func Sha1(data string) string {
  475. sha1 := sha1.New()
  476. sha1.Write([]byte(data))
  477. return hex.EncodeToString(sha1.Sum([]byte("")))
  478. }
  479. func GetVideoPlaySeconds(videoPath string) (playSeconds float64, err error) {
  480. cmd := `ffmpeg -i ` + videoPath + ` 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`
  481. out, err := exec.Command("bash", "-c", cmd).Output()
  482. if err != nil {
  483. return
  484. }
  485. outTimes := string(out)
  486. fmt.Println("outTimes:", outTimes)
  487. if outTimes != "" {
  488. timeArr := strings.Split(outTimes, ":")
  489. h := timeArr[0]
  490. m := timeArr[1]
  491. s := timeArr[2]
  492. hInt, err := strconv.Atoi(h)
  493. if err != nil {
  494. return playSeconds, err
  495. }
  496. mInt, err := strconv.Atoi(m)
  497. if err != nil {
  498. return playSeconds, err
  499. }
  500. s = strings.Trim(s, " ")
  501. s = strings.Trim(s, "\n")
  502. sInt, err := strconv.ParseFloat(s, 64)
  503. if err != nil {
  504. return playSeconds, err
  505. }
  506. playSeconds = float64(hInt)*3600 + float64(mInt)*60 + float64(sInt)
  507. }
  508. return
  509. }
  510. func GetMaxTradeCode(tradeCode string) (maxTradeCode string, err error) {
  511. tradeCode = strings.Replace(tradeCode, "W", "", -1)
  512. tradeCode = strings.Trim(tradeCode, " ")
  513. tradeCodeInt, err := strconv.Atoi(tradeCode)
  514. if err != nil {
  515. return
  516. }
  517. tradeCodeInt = tradeCodeInt + 1
  518. maxTradeCode = fmt.Sprintf("W%06d", tradeCodeInt)
  519. return
  520. }
  521. // excel日期字段格式化 yyyy-mm-dd
  522. func ConvertToFormatDay(excelDaysString string) string {
  523. // 2006-01-02 距离 1900-01-01的天数
  524. baseDiffDay := 38719 //在网上工具计算的天数需要加2天,什么原因没弄清楚
  525. curDiffDay := excelDaysString
  526. b, _ := strconv.Atoi(curDiffDay)
  527. // 获取excel的日期距离2006-01-02的天数
  528. realDiffDay := b - baseDiffDay
  529. //fmt.Println("realDiffDay:",realDiffDay)
  530. // 距离2006-01-02 秒数
  531. realDiffSecond := realDiffDay * 24 * 3600
  532. //fmt.Println("realDiffSecond:",realDiffSecond)
  533. // 2006-01-02 15:04:05距离1970-01-01 08:00:00的秒数 网上工具可查出
  534. baseOriginSecond := 1136185445
  535. resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
  536. return resultTime
  537. }
  538. func CheckPwd(pwd string) bool {
  539. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  540. reg := regexp.MustCompile(compile)
  541. flag := reg.MatchString(pwd)
  542. return flag
  543. }
  544. func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate string) {
  545. // 数字月份必须前置补零
  546. if len(myMonth) == 1 {
  547. myMonth = "0" + myMonth
  548. }
  549. yInt, _ := strconv.Atoi(myYear)
  550. timeLayout := "2006-01-02 15:04:05"
  551. loc, _ := time.LoadLocation("Local")
  552. theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
  553. newMonth := theTime.Month()
  554. t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  555. t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  556. return t1, t2
  557. }
  558. // 移除字符串中的空格
  559. func TrimStr(str string) (str2 string) {
  560. return strings.Replace(str, " ", "", -1)
  561. }
  562. // 字符串转换为time
  563. func StrTimeToTime(strTime string) time.Time {
  564. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  565. loc, _ := time.LoadLocation("Local") //重要:获取时区
  566. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  567. return resultTime
  568. }
  569. // 字符串类型时间转周几
  570. func StrDateTimeToWeek(strTime string) string {
  571. var WeekDayMap = map[string]string{
  572. "Monday": "周一",
  573. "Tuesday": "周二",
  574. "Wednesday": "周三",
  575. "Thursday": "周四",
  576. "Friday": "周五",
  577. "Saturday": "周六",
  578. "Sunday": "周日",
  579. }
  580. var ctime = StrTimeToTime(strTime).Format("2006-01-02")
  581. startday, _ := time.Parse("2006-01-02", ctime)
  582. staweek_int := startday.Weekday().String()
  583. return WeekDayMap[staweek_int]
  584. }
  585. // 时间格式转年月日字符串
  586. func TimeToStrYmd(time2 time.Time) string {
  587. var Ymd string
  588. year := time2.Year()
  589. month := time2.Format("1")
  590. day1 := time.Now().Day()
  591. Ymd = strconv.Itoa(year) + "年" + month + "月" + strconv.Itoa(day1) + "日"
  592. return Ymd
  593. }
  594. // 时间格式去掉时分秒
  595. func TimeRemoveHms(strTime string) string {
  596. var Ymd string
  597. var resultTime = StrTimeToTime(strTime)
  598. year := resultTime.Year()
  599. month := resultTime.Format("01")
  600. day1 := resultTime.Day()
  601. Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
  602. return Ymd
  603. }
  604. // 文章上一次编辑时间
  605. func ArticleLastTime(strTime string) string {
  606. var newTime string
  607. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  608. diffTime := time.Now().Unix() - stamp.Unix()
  609. if diffTime <= 60 {
  610. newTime = "当前"
  611. } else if diffTime < 60*60 {
  612. newTime = strconv.FormatInt(diffTime/60, 10) + "分钟前"
  613. } else if diffTime < 24*60*60 {
  614. newTime = strconv.FormatInt(diffTime/(60*60), 10) + "小时前"
  615. } else if diffTime < 30*24*60*60 {
  616. newTime = strconv.FormatInt(diffTime/(24*60*60), 10) + "天前"
  617. } else if diffTime < 12*30*24*60*60 {
  618. newTime = strconv.FormatInt(diffTime/(30*24*60*60), 10) + "月前"
  619. } else {
  620. newTime = "1年前"
  621. }
  622. return newTime
  623. }
  624. // 人民币小写转大写
  625. func ConvertNumToCny(num float64) (str string, err error) {
  626. strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
  627. sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
  628. // log.Println(sliceUnit[:len(sliceUnit)-2])
  629. s := sliceUnit[len(sliceUnit)-len(strNum):]
  630. upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
  631. for k, v := range strNum[:] {
  632. str = str + upperDigitUnit[string(v)] + s[k]
  633. }
  634. reg, err := regexp.Compile(`零角零分$`)
  635. str = reg.ReplaceAllString(str, "整")
  636. reg, err = regexp.Compile(`零角`)
  637. str = reg.ReplaceAllString(str, "零")
  638. reg, err = regexp.Compile(`零分$`)
  639. str = reg.ReplaceAllString(str, "整")
  640. reg, err = regexp.Compile(`零[仟佰拾]`)
  641. str = reg.ReplaceAllString(str, "零")
  642. reg, err = regexp.Compile(`零{2,}`)
  643. str = reg.ReplaceAllString(str, "零")
  644. reg, err = regexp.Compile(`零亿`)
  645. str = reg.ReplaceAllString(str, "亿")
  646. reg, err = regexp.Compile(`零万`)
  647. str = reg.ReplaceAllString(str, "万")
  648. reg, err = regexp.Compile(`零*元`)
  649. str = reg.ReplaceAllString(str, "元")
  650. reg, err = regexp.Compile(`亿零{0, 3}万`)
  651. str = reg.ReplaceAllString(str, "^元")
  652. reg, err = regexp.Compile(`零元`)
  653. str = reg.ReplaceAllString(str, "零")
  654. return
  655. }
  656. // GetNowWeekMonday 获取本周周一的时间
  657. func GetNowWeekMonday() time.Time {
  658. offset := int(time.Monday - time.Now().Weekday())
  659. mondayTime := time.Now().AddDate(0, 0, offset)
  660. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  661. return mondayTime
  662. }
  663. // GetNowWeekLastDay 获取本周最后一天的时间
  664. func GetNowWeekLastDay() time.Time {
  665. offset := int(time.Monday - time.Now().Weekday())
  666. firstDayTime := time.Now().AddDate(0, 0, offset)
  667. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  668. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  669. return lastDayTime
  670. }
  671. // GetNowMonthFirstDay 获取本月第一天的时间
  672. func GetNowMonthFirstDay() time.Time {
  673. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  674. return nowMonthFirstDay
  675. }
  676. // GetNowMonthLastDay 获取本月最后一天的时间
  677. func GetNowMonthLastDay() time.Time {
  678. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  679. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  680. return nowMonthLastDay
  681. }
  682. // GetNowQuarterFirstDay 获取本季度第一天的时间
  683. func GetNowQuarterFirstDay() time.Time {
  684. month := int(time.Now().Month())
  685. var nowQuarterFirstDay time.Time
  686. if month >= 1 && month <= 3 {
  687. //1月1号
  688. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  689. } else if month >= 4 && month <= 6 {
  690. //4月1号
  691. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  692. } else if month >= 7 && month <= 9 {
  693. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  694. } else {
  695. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  696. }
  697. return nowQuarterFirstDay
  698. }
  699. // GetNowQuarterLastDay 获取本季度最后一天的时间
  700. func GetNowQuarterLastDay() time.Time {
  701. month := int(time.Now().Month())
  702. var nowQuarterLastDay time.Time
  703. if month >= 1 && month <= 3 {
  704. //03-31 23:59:59
  705. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  706. } else if month >= 4 && month <= 6 {
  707. //06-30 23:59:59
  708. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  709. } else if month >= 7 && month <= 9 {
  710. //09-30 23:59:59
  711. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  712. } else {
  713. //12-31 23:59:59
  714. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  715. }
  716. return nowQuarterLastDay
  717. }
  718. // GetNowHalfYearFirstDay 获取当前半年的第一天的时间
  719. func GetNowHalfYearFirstDay() time.Time {
  720. month := int(time.Now().Month())
  721. var nowHalfYearLastDay time.Time
  722. if month >= 1 && month <= 6 {
  723. //03-31 23:59:59
  724. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  725. } else {
  726. //12-31 23:59:59
  727. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  728. }
  729. return nowHalfYearLastDay
  730. }
  731. // GetNowHalfYearLastDay 获取当前半年的最后一天的时间
  732. func GetNowHalfYearLastDay() time.Time {
  733. month := int(time.Now().Month())
  734. var nowHalfYearLastDay time.Time
  735. if month >= 1 && month <= 6 {
  736. //03-31 23:59:59
  737. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  738. } else {
  739. //12-31 23:59:59
  740. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  741. }
  742. return nowHalfYearLastDay
  743. }
  744. // GetNowYearFirstDay 获取当前年的最后一天的时间
  745. func GetNowYearFirstDay() time.Time {
  746. //12-31 23:59:59
  747. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  748. return nowYearFirstDay
  749. }
  750. // GetNowYearLastDay 获取当前年的最后一天的时间
  751. func GetNowYearLastDay() time.Time {
  752. //12-31 23:59:59
  753. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  754. return nowYearLastDay
  755. }
  756. // CalculationDate 计算两个日期之间相差n年m月y天
  757. func CalculationDate(startDate, endDate time.Time) (beetweenDay string, err error) {
  758. //startDate := time.Date(2021, 3, 28, 0, 0, 0, 0, time.Now().Location())
  759. //endDate := time.Date(2022, 3, 31, 0, 0, 0, 0, time.Now().Location())
  760. numYear := endDate.Year() - startDate.Year()
  761. numMonth := int(endDate.Month()) - int(startDate.Month())
  762. numDay := 0
  763. //获取截止月的总天数
  764. endDateDays := getMonthDay(endDate.Year(), int(endDate.Month()))
  765. //获取截止月的前一个月
  766. endDatePrevMonthDate := endDate.AddDate(0, -1, 0)
  767. //获取截止日期的上一个月的总天数
  768. endDatePrevMonthDays := getMonthDay(endDatePrevMonthDate.Year(), int(endDatePrevMonthDate.Month()))
  769. //获取开始日期的的月份总天数
  770. startDateMonthDays := getMonthDay(startDate.Year(), int(startDate.Month()))
  771. //判断,截止月是否完全被选中,如果相等,那么代表截止月份全部天数被选择
  772. if endDate.Day() == endDateDays {
  773. numDay = startDateMonthDays - startDate.Day() + 1
  774. //如果剩余天数正好与开始日期的天数是一致的,那么月份加1
  775. if numDay == startDateMonthDays {
  776. numMonth++
  777. numDay = 0
  778. //超过月份了,那么年份加1
  779. if numMonth == 12 {
  780. numYear++
  781. numMonth = 0
  782. }
  783. }
  784. } else {
  785. numDay = endDate.Day() - startDate.Day() + 1
  786. }
  787. //天数小于0,那么向月份借一位
  788. if numDay < 0 {
  789. //向上一个月借一个月的天数
  790. numDay += endDatePrevMonthDays
  791. //总月份减去一个月
  792. numMonth = numMonth - 1
  793. }
  794. //月份小于0,那么向年份借一位
  795. if numMonth < 0 {
  796. //向上一个年借12个月
  797. numMonth += 12
  798. //总年份减去一年
  799. numYear = numYear - 1
  800. }
  801. if numYear < 0 {
  802. err = errors.New("日期异常")
  803. return
  804. }
  805. if numYear > 0 {
  806. beetweenDay += fmt.Sprint(numYear, "年")
  807. }
  808. if numMonth > 0 {
  809. beetweenDay += fmt.Sprint(numMonth, "个月")
  810. }
  811. if numDay > 0 {
  812. beetweenDay += fmt.Sprint(numDay, "天")
  813. }
  814. return
  815. }
  816. // getMonthDay 获取某年某月有多少天
  817. func getMonthDay(year, month int) (days int) {
  818. if month != 2 {
  819. if month == 4 || month == 6 || month == 9 || month == 11 {
  820. days = 30
  821. } else {
  822. days = 31
  823. }
  824. } else {
  825. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  826. days = 29
  827. } else {
  828. days = 28
  829. }
  830. }
  831. return
  832. }
  833. // GetOrmInReplace 获取orm的in查询替换?的方法
  834. func GetOrmInReplace(num int) string {
  835. template := make([]string, num)
  836. for i := 0; i < num; i++ {
  837. template[i] = "?"
  838. }
  839. return strings.Join(template, ",")
  840. }
  841. // InArrayByInt php中的in_array(判断Int类型的切片中是否存在该int值)
  842. func InArrayByInt(idIntList []int, searchId int) (has bool) {
  843. for _, id := range idIntList {
  844. if id == searchId {
  845. has = true
  846. return
  847. }
  848. }
  849. return
  850. }
  851. // InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
  852. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  853. for _, id := range idStrList {
  854. if id == searchId {
  855. has = true
  856. return
  857. }
  858. }
  859. return
  860. }
  861. // RangeRand 取区间随机数
  862. func RangeRand(min, max int64) int64 {
  863. if min > max {
  864. return max
  865. }
  866. if min < 0 {
  867. f64Min := math.Abs(float64(min))
  868. i64Min := int64(f64Min)
  869. result, _ := cryRand.Int(cryRand.Reader, big.NewInt(max+1+i64Min))
  870. return result.Int64() - i64Min
  871. } else {
  872. result, _ := cryRand.Int(cryRand.Reader, big.NewInt(max-min+1))
  873. return min + result.Int64()
  874. }
  875. }
  876. func GetLocalIP() (ip string, err error) {
  877. addrs, err := net.InterfaceAddrs()
  878. if err != nil {
  879. return
  880. }
  881. for _, addr := range addrs {
  882. ipAddr, ok := addr.(*net.IPNet)
  883. if !ok {
  884. continue
  885. }
  886. if ipAddr.IP.IsLoopback() {
  887. continue
  888. }
  889. if !ipAddr.IP.IsGlobalUnicast() {
  890. continue
  891. }
  892. return ipAddr.IP.String(), nil
  893. }
  894. return
  895. }
  896. // HTTP: post请求参数
  897. //
  898. // {
  899. // "A" : 10086,
  900. // "B" : "请求信息"
  901. // }
  902. type RequestParam struct {
  903. A int `json:"A"`
  904. B string `json:"B"`
  905. }
  906. // HTTP返回Body
  907. type HTTPRspBody struct {
  908. Result Results `json:"Result"`
  909. }
  910. type Results struct {
  911. RequestID string `json:"Result"`
  912. HasError bool `json:"HasError"`
  913. ResponseItems ErrorMsg `json:"ResponseItems"`
  914. }
  915. type ErrorMsg struct {
  916. ErrorMsg string `json:"ErrorMsg"`
  917. }
  918. // 修改供应商信息
  919. func HttpPost(url string, reqParam string, headersParams map[string]string) ([]byte, error) {
  920. var (
  921. err error
  922. )
  923. // 准备: HTTP请求
  924. reqBody := strings.NewReader(reqParam)
  925. httpReq, err := http.NewRequest("POST", url, reqBody)
  926. if err != nil {
  927. fmt.Printf("NewRequest fail, url: %s, reqBody: %s, err: %v", url, reqBody, err)
  928. return nil, err
  929. }
  930. for k, v := range headersParams {
  931. httpReq.Header.Set(k, v)
  932. }
  933. //httpReq.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
  934. jar, err := cookiejar.New(nil)
  935. if err != nil {
  936. panic(err)
  937. }
  938. //httpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
  939. client := &http.Client{Jar: jar}
  940. // DO: HTTP请求
  941. httpRsp, err := client.Do(httpReq)
  942. if err != nil {
  943. fmt.Printf("do http fail, url: %s, reqBody: %s, err:%v", url, reqBody, err)
  944. return nil, err
  945. }
  946. defer httpRsp.Body.Close()
  947. // Read: HTTP结果
  948. b, err := ioutil.ReadAll(httpRsp.Body)
  949. if err != nil {
  950. fmt.Printf("ReadAll failed, url: %s, reqBody: %s, err: %v", url, reqBody, err)
  951. return nil, err
  952. }
  953. // unmarshal: 解析HTTP返回的结果
  954. // body: {"Result":{"RequestId":"12131","HasError":true,"ResponseItems":{"ErrorMsg":"错误信息"}}}
  955. return b, err
  956. }
  957. func HttpGet(reqUrl, payload string) (err error) {
  958. client := &http.Client{}
  959. req, err := http.NewRequest("GET", reqUrl, strings.NewReader(payload))
  960. if err != nil {
  961. return
  962. }
  963. res, err := client.Do(req)
  964. if err != nil {
  965. return
  966. }
  967. defer res.Body.Close()
  968. _, err = ioutil.ReadAll(res.Body)
  969. if err != nil {
  970. return
  971. }
  972. Cookie := res.Header.Get("Cookie")
  973. fmt.Println(Cookie)
  974. rcookie := req.Header.Get("Cookie")
  975. fmt.Println("rcookie")
  976. fmt.Println(rcookie)
  977. //fmt.Println("body:" + string(body))
  978. cookiesArr := res.Cookies()
  979. fmt.Println("cookiesArrLen:", len(cookiesArr))
  980. for k, v := range cookiesArr {
  981. fmt.Println(k, v)
  982. }
  983. return
  984. }
  985. func ChineseToPinyin(input string) string {
  986. a := pinyin.NewArgs()
  987. var result strings.Builder
  988. for _, r := range input {
  989. if unicode.Is(unicode.Han, r) {
  990. py := pinyin.Pinyin(string(r), a)
  991. if len(py) > 0 && len(py[0]) > 0 {
  992. result.WriteString(py[0][0])
  993. }
  994. } else if unicode.IsNumber(r) || unicode.IsLetter(r) {
  995. result.WriteRune(r)
  996. }
  997. }
  998. return result.String()
  999. }
  1000. func ChineseToPinyinInitials(input string) string {
  1001. a := pinyin.NewArgs()
  1002. a.Style = pinyin.FirstLetter // 设置风格为首字母
  1003. var result strings.Builder
  1004. for _, r := range input {
  1005. if unicode.Is(unicode.Han, r) {
  1006. py := pinyin.Pinyin(string(r), a)
  1007. if len(py) > 0 && len(py[0]) > 0 {
  1008. result.WriteString(py[0][0])
  1009. }
  1010. } else if unicode.IsNumber(r) || unicode.IsLetter(r) {
  1011. result.WriteRune(r)
  1012. }
  1013. }
  1014. return result.String()
  1015. }
  1016. // GetFirstLetter 英文单词首字母
  1017. func GetFirstLetter(name string) string {
  1018. // 定义一个正则表达式,它匹配由字母组成的单词,并捕获每个单词的首字母
  1019. // 注意:这个正则表达式假设单词由字母组成,并且被非字母字符分隔
  1020. // 你可能需要根据实际情况调整这个正则表达式
  1021. re := regexp.MustCompile(`\b[a-zA-Z]`)
  1022. // FindAllString 方法会返回所有匹配的字符串(在这个例子中,是单词的首字母)
  1023. matches := re.FindAllString(name, -1)
  1024. // 将匹配的首字母连接成一个字符串
  1025. newStr := strings.Join(matches, "")
  1026. return strings.ToLower(newStr)
  1027. }
  1028. // 修改供应商信息
  1029. func HttpPostNoCookie(url string, reqParam string, headersParams map[string]string) ([]byte, error) {
  1030. var (
  1031. err error
  1032. )
  1033. // 准备: HTTP请求
  1034. reqBody := strings.NewReader(reqParam)
  1035. httpReq, err := http.NewRequest("POST", url, reqBody)
  1036. if err != nil {
  1037. fmt.Printf("NewRequest fail, url: %s, reqBody: %s, err: %v", url, reqBody, err)
  1038. return nil, err
  1039. }
  1040. for k, v := range headersParams {
  1041. httpReq.Header.Set(k, v)
  1042. }
  1043. //httpReq.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
  1044. //httpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
  1045. client := &http.Client{}
  1046. // DO: HTTP请求
  1047. httpRsp, err := client.Do(httpReq)
  1048. if err != nil {
  1049. fmt.Printf("do http fail, url: %s, reqBody: %s, err:%v", url, reqBody, err)
  1050. return nil, err
  1051. }
  1052. defer httpRsp.Body.Close()
  1053. if httpRsp.StatusCode != 200 {
  1054. err = fmt.Errorf("do http fail, url: %s, statusCode: %d, status:%s ", url, httpRsp.StatusCode, httpRsp.Status)
  1055. return nil, err
  1056. }
  1057. // Read: HTTP结果
  1058. b, err := ioutil.ReadAll(httpRsp.Body)
  1059. if err != nil {
  1060. fmt.Printf("ReadAll failed, url: %s, reqBody: %s, err: %v", url, reqBody, err)
  1061. return nil, err
  1062. }
  1063. // unmarshal: 解析HTTP返回的结果
  1064. // body: {"Result":{"RequestId":"12131","HasError":true,"ResponseItems":{"ErrorMsg":"错误信息"}}}
  1065. return b, err
  1066. }
  1067. func HttpGetNoCookie(url string) ([]byte, error) {
  1068. res, err := http.Get(url)
  1069. if err != nil {
  1070. return nil, err
  1071. }
  1072. defer res.Body.Close()
  1073. return ioutil.ReadAll(res.Body)
  1074. }
  1075. // PostEdbLib 调用指标接口
  1076. func PostEdbLib(param map[string]interface{}, method string) (result []byte, err error) {
  1077. // todo postUrl := EDB_LIB_URL + method
  1078. postUrl := EDB_LIB_URL + method
  1079. postData, err := json.Marshal(param)
  1080. if err != nil {
  1081. return
  1082. }
  1083. result, err = httpPostEtaLib(postUrl, string(postData), "application/json")
  1084. if err != nil {
  1085. return
  1086. }
  1087. return
  1088. }
  1089. type BaseEdbLibResponse struct {
  1090. Ret int
  1091. Msg string
  1092. ErrMsg string
  1093. ErrCode string
  1094. Data interface{}
  1095. }
  1096. func httpPostEtaLib(url, postData string, params ...string) ([]byte, error) {
  1097. fmt.Println("HttpPost Url:" + url)
  1098. body := ioutil.NopCloser(strings.NewReader(postData))
  1099. client := &http.Client{}
  1100. req, err := http.NewRequest("POST", url, body)
  1101. if err != nil {
  1102. return nil, err
  1103. }
  1104. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  1105. if len(params) > 0 && params[0] != "" {
  1106. contentType = params[0]
  1107. }
  1108. req.Header.Set("Content-Type", contentType)
  1109. req.Header.Set("authorization", MD5(APP_EDB_LIB_NAME_EN+EDB_LIB_Md5_KEY))
  1110. //req.Header.Set("authorization", MD5("eta_index_lib"+"2342342341asd"))
  1111. resp, err := client.Do(req)
  1112. if err != nil {
  1113. fmt.Println("client.Do err:" + err.Error())
  1114. return nil, err
  1115. }
  1116. defer resp.Body.Close()
  1117. b, err := ioutil.ReadAll(resp.Body)
  1118. if err != nil {
  1119. fmt.Println("HttpPost:" + string(b))
  1120. }
  1121. return b, err
  1122. }
  1123. func FloatFormatRound(val float64, num int32) float64 {
  1124. val, _ = decimal.NewFromFloat(val).Round(num).Float64()
  1125. return val
  1126. }