lib.go 8.3 KB

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