lib.go 9.0 KB

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