lib.go 8.4 KB

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