lib.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package lib
  2. import (
  3. "ColdVerify_server/models/Account"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/signintech/gopdf"
  7. "log"
  8. "math/rand"
  9. "os"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. var Run_My_Server = false // 运行当期服务
  16. type Cl_ struct {
  17. Uuid_list map[string]string // 泛型
  18. }
  19. var CountrySnMap map[string]Cl_ /*创建集合 */
  20. var CountryRead_DeviceParameterSnMap map[string]string /*创建集合 */
  21. func init() {
  22. CountrySnMap = make(map[string]Cl_)
  23. CountryRead_DeviceParameterSnMap = make(map[string]string)
  24. }
  25. type JSONS struct {
  26. //必须的大写开头
  27. Code int16
  28. Msg string
  29. Data interface{} // 泛型
  30. }
  31. type R_JSONS struct {
  32. //必须的大写开头
  33. List interface{}
  34. Num int
  35. Page int
  36. Page_size int
  37. Pages []Page_T
  38. }
  39. type R_JSONS_s struct {
  40. //必须的大写开头
  41. List []interface{}
  42. Num int
  43. Page int
  44. Page_size int
  45. Pages []Page_T
  46. }
  47. type LOG_JSONS struct {
  48. //必须的大写开头
  49. Class_1 string
  50. Class_List interface{}
  51. List interface{}
  52. Num int
  53. Page int
  54. Page_size int
  55. Pages []Page_T
  56. }
  57. // 登录验证
  58. func Verification(GetCookie string, GetString string) (Account.User, bool) {
  59. // 自适应 参数
  60. User_tokey := GetCookie
  61. if len(User_tokey) == 0 {
  62. User_tokey = GetString
  63. }
  64. if len(User_tokey) == 0 {
  65. return Account.User{}, false
  66. }
  67. // 判断 tokey 是否存在
  68. tokey, is := Account.Read_Tokey(User_tokey)
  69. if !is {
  70. return Account.User{}, false
  71. }
  72. err, user_r := Account.Read_User_ByT_uuid(tokey)
  73. if err != nil {
  74. return Account.User{}, false
  75. }
  76. log.Println("登录 User_name 为:", user_r.T_name)
  77. return user_r, true
  78. }
  79. // 登录验证
  80. func Verification_Admin(GetCookie string, GetString string) (Account.Admin, bool) {
  81. Run_My_Server = true // 运行当期服务
  82. // 自适应 参数
  83. User_tokey := GetCookie
  84. if len(User_tokey) == 0 {
  85. User_tokey = GetString
  86. }
  87. if len(User_tokey) == 0 {
  88. return Account.Admin{}, false
  89. }
  90. // 判断 tokey 是否存在
  91. tokey, is := Account.Read_Tokey(User_tokey)
  92. if !is {
  93. return Account.Admin{}, false
  94. }
  95. err, user_r := Account.Read_Admin_ByT_uuid(tokey)
  96. if err != nil {
  97. return Account.Admin{}, false
  98. }
  99. log.Println("登录 Admin_name 为:", user_r.T_name)
  100. return user_r, true
  101. }
  102. // func_page 分页 [{3 1} {4 2} {4 3} {4 4} {4 5} {4 6} {4 7} {4 8} {4 9} {5 2}]-
  103. type Page_T struct {
  104. A int
  105. V int64
  106. }
  107. func Func_page(Page int64, Page_size int64) (page_t_list []Page_T) {
  108. if Page > 1 {
  109. page_t_list = append(page_t_list, Page_T{A: 1, V: Page - 1})
  110. }
  111. i := int64(0)
  112. for aa := int64(1); aa < 5; aa++ {
  113. if Page-aa <= 0 {
  114. break
  115. }
  116. page_t_list = append(page_t_list, Page_T{A: 2, V: Page - aa})
  117. i++
  118. }
  119. page_t_list = append(page_t_list, Page_T{A: 3, V: Page})
  120. for aa := int64(1); aa < 10-i; aa++ {
  121. if Page_size < Page+aa {
  122. break
  123. }
  124. page_t_list = append(page_t_list, Page_T{A: 4, V: Page + aa})
  125. }
  126. sort.Slice(page_t_list, func(i, j int) bool {
  127. if page_t_list[i].V < page_t_list[j].V {
  128. return true
  129. }
  130. return false
  131. })
  132. sort.Slice(page_t_list, func(i, j int) bool {
  133. if page_t_list[i].A < page_t_list[j].A {
  134. return true
  135. }
  136. return false
  137. })
  138. if Page < Page_size {
  139. page_t_list = append(page_t_list, Page_T{A: 5, V: Page + 1})
  140. }
  141. return page_t_list
  142. }
  143. func Strval(value interface{}) string {
  144. var key string
  145. if value == nil {
  146. return key
  147. }
  148. switch value.(type) {
  149. case float64:
  150. ft := value.(float64)
  151. key = strconv.FormatFloat(ft, 'f', -1, 64)
  152. case float32:
  153. ft := value.(float32)
  154. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  155. case int:
  156. it := value.(int)
  157. key = strconv.Itoa(it)
  158. case uint:
  159. it := value.(uint)
  160. key = strconv.Itoa(int(it))
  161. case int8:
  162. it := value.(int8)
  163. key = strconv.Itoa(int(it))
  164. case uint8:
  165. it := value.(uint8)
  166. key = strconv.Itoa(int(it))
  167. case int16:
  168. it := value.(int16)
  169. key = strconv.Itoa(int(it))
  170. case uint16:
  171. it := value.(uint16)
  172. key = strconv.Itoa(int(it))
  173. case int32:
  174. it := value.(int32)
  175. key = strconv.Itoa(int(it))
  176. case uint32:
  177. it := value.(uint32)
  178. key = strconv.Itoa(int(it))
  179. case int64:
  180. it := value.(int64)
  181. key = strconv.FormatInt(it, 10)
  182. case uint64:
  183. it := value.(uint64)
  184. key = strconv.FormatUint(it, 10)
  185. case string:
  186. key = value.(string)
  187. case []byte:
  188. key = string(value.([]byte))
  189. default:
  190. newValue, _ := json.Marshal(value)
  191. key = string(newValue)
  192. }
  193. return key
  194. }
  195. func To_int(value interface{}) int {
  196. var key int
  197. if value == nil {
  198. return key
  199. }
  200. switch value.(type) {
  201. case float64:
  202. key = int(value.(float64))
  203. case float32:
  204. key = int(value.(float32))
  205. case int:
  206. key = int(value.(int))
  207. case uint:
  208. key = int(value.(uint))
  209. case int8:
  210. key = int(value.(int8))
  211. case uint8:
  212. key = int(value.(uint8))
  213. case int16:
  214. key = int(value.(int16))
  215. case uint16:
  216. key = int(value.(uint16))
  217. case int32:
  218. key = int(value.(int32))
  219. case uint32:
  220. key = int(value.(uint32))
  221. case int64:
  222. key = int(value.(int64))
  223. case uint64:
  224. key = int(value.(uint64))
  225. case string:
  226. key, _ = strconv.Atoi(value.(string))
  227. case []byte:
  228. key, _ = strconv.Atoi(string(value.([]byte)))
  229. default:
  230. newValue, _ := json.Marshal(value)
  231. key, _ = strconv.Atoi(string(newValue))
  232. }
  233. return key
  234. }
  235. func To_float32(value interface{}) float32 {
  236. var key float32
  237. if value == nil {
  238. return key
  239. }
  240. switch value.(type) {
  241. case float64:
  242. key = float32(value.(float64))
  243. case float32:
  244. key = float32(value.(float32))
  245. case int:
  246. key = float32(value.(int))
  247. case uint:
  248. key = float32(value.(uint))
  249. case int8:
  250. key = float32(value.(int8))
  251. case uint8:
  252. key = float32(value.(uint8))
  253. case int16:
  254. key = float32(value.(int16))
  255. case uint16:
  256. key = float32(value.(uint16))
  257. case int32:
  258. key = float32(value.(int32))
  259. case uint32:
  260. key = float32(value.(uint32))
  261. case int64:
  262. key = float32(value.(int64))
  263. case uint64:
  264. key = float32(value.(uint64))
  265. case string:
  266. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  267. key = float32(key_float64)
  268. case []byte:
  269. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  270. key = float32(key_float64)
  271. default:
  272. newValue, _ := json.Marshal(value)
  273. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  274. key = float32(key_float64)
  275. }
  276. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  277. key = float32(key_float64)
  278. return key
  279. }
  280. func To_string(value interface{}) string {
  281. var key string
  282. if value == nil {
  283. return key
  284. }
  285. switch value.(type) {
  286. case float64:
  287. ft := value.(float64)
  288. key = strconv.FormatFloat(ft, 'f', -1, 64)
  289. case float32:
  290. ft := value.(float32)
  291. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  292. case int:
  293. it := value.(int)
  294. key = strconv.Itoa(it)
  295. case uint:
  296. it := value.(uint)
  297. key = strconv.Itoa(int(it))
  298. case int8:
  299. it := value.(int8)
  300. key = strconv.Itoa(int(it))
  301. case uint8:
  302. it := value.(uint8)
  303. key = strconv.Itoa(int(it))
  304. case int16:
  305. it := value.(int16)
  306. key = strconv.Itoa(int(it))
  307. case uint16:
  308. it := value.(uint16)
  309. key = strconv.Itoa(int(it))
  310. case int32:
  311. it := value.(int32)
  312. key = strconv.Itoa(int(it))
  313. case uint32:
  314. it := value.(uint32)
  315. key = strconv.Itoa(int(it))
  316. case int64:
  317. it := value.(int64)
  318. key = strconv.FormatInt(it, 10)
  319. case uint64:
  320. it := value.(uint64)
  321. key = strconv.FormatUint(it, 10)
  322. case string:
  323. key = value.(string)
  324. case []byte:
  325. key = string(value.([]byte))
  326. default:
  327. newValue, _ := json.Marshal(value)
  328. key = string(newValue)
  329. }
  330. return key
  331. }
  332. func Random(min, max int) int {
  333. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  334. return rand.Intn(max-min) + min
  335. }
  336. //取文本(字符串)中间
  337. func GetBetweenStr(str, start, end string) string {
  338. n := strings.Index(str, start)
  339. if n == -1 {
  340. n = 0
  341. } else {
  342. n = n + len(start) // 增加了else,不加的会把start带上
  343. }
  344. str = string([]byte(str)[n:])
  345. m := strings.Index(str, end)
  346. if m == -1 {
  347. m = len(str)
  348. }
  349. str = string([]byte(str)[:m])
  350. return str
  351. }
  352. // getYearMonthToDay 查询指定年份指定月份有多少天
  353. // @params year int 指定年份
  354. // @params month int 指定月份
  355. func GetYearMonthToDay(year int, month int) int {
  356. // 有31天的月份
  357. day31 := map[int]bool{
  358. 1: true,
  359. 3: true,
  360. 5: true,
  361. 7: true,
  362. 8: true,
  363. 10: true,
  364. 12: true,
  365. }
  366. if day31[month] == true {
  367. return 31
  368. }
  369. // 有30天的月份
  370. day30 := map[int]bool{
  371. 4: true,
  372. 6: true,
  373. 9: true,
  374. 11: true,
  375. }
  376. if day30[month] == true {
  377. return 30
  378. }
  379. // 计算是平年还是闰年
  380. if (year%4 == 0 && year%100 != 0) || year%400 == 0 {
  381. // 得出2月的天数
  382. return 29
  383. }
  384. // 得出2月的天数
  385. return 28
  386. }
  387. const (
  388. AlignLeft = 4
  389. AlignCenter = 5
  390. AlignRight = 6
  391. )
  392. const (
  393. ValignTop = 1
  394. ValignMiddle = 2
  395. ValignBottom = 3
  396. )
  397. func RectFillColor(pdf *gopdf.GoPdf,
  398. text string,
  399. fontSize int,
  400. x, y, w, h float64,
  401. r, g, b uint8,
  402. align, valign int,
  403. ) {
  404. pdf.SetLineWidth(0.1)
  405. pdf.SetFillColor(r, g, b) //setup fill color
  406. pdf.SetLineType("") // 线条样式
  407. pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
  408. pdf.SetFillColor(0, 0, 0)
  409. if align == AlignCenter {
  410. textw, _ := pdf.MeasureTextWidth(text)
  411. x = x + (w / 2) - (textw / 2)
  412. } else if align == AlignRight {
  413. textw, _ := pdf.MeasureTextWidth(text)
  414. x = x + w - textw
  415. }
  416. pdf.SetX(x)
  417. if valign == ValignMiddle {
  418. y = y + (h / 2) - (float64(fontSize) / 2)
  419. } else if valign == ValignBottom {
  420. y = y + h - float64(fontSize)
  421. }
  422. pdf.SetY(y)
  423. pdf.Cell(nil, text)
  424. }
  425. func Create_Dir(path string) error {
  426. _, err := os.Stat(path)
  427. // 如果返回的错误为nil,说明文件或文件夹存在
  428. if err == nil {
  429. return nil
  430. }
  431. if err = os.Mkdir(path, 0777); err != nil {
  432. return err
  433. }
  434. return nil
  435. }