lib.go 8.9 KB

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