lib.go 8.3 KB

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