lib.go 9.2 KB

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