lib.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package lib
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go.mongodb.org/mongo-driver/bson"
  6. "math/rand"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func init() {
  13. }
  14. const Success = 200
  15. const Error = 201
  16. type JSONR struct {
  17. //必须的大写开头
  18. Code int16
  19. Msg string
  20. Data interface{} // 泛型
  21. }
  22. type JSONS struct {
  23. //必须的大写开头
  24. Code int16
  25. Msg string
  26. List interface{}
  27. Total int16
  28. PageIndex int
  29. PageSize int
  30. }
  31. func C_Page(list interface{}, PageIndex, PageSize int, Total int64) (Jsons JSONS) {
  32. Jsons.List = list
  33. Jsons.Total = int16(Total)
  34. Jsons.PageIndex = PageIndex
  35. Jsons.PageSize = PageSize
  36. //Jsons.PageSize = int16(math.Ceil(float64(Total) / float64(PageSize)))
  37. return Jsons
  38. }
  39. func Strval(value interface{}) string {
  40. var key string
  41. if value == nil {
  42. return key
  43. }
  44. switch value.(type) {
  45. case float64:
  46. ft := value.(float64)
  47. key = strconv.FormatFloat(ft, 'f', -1, 64)
  48. case float32:
  49. ft := value.(float32)
  50. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  51. case int:
  52. it := value.(int)
  53. key = strconv.Itoa(it)
  54. case uint:
  55. it := value.(uint)
  56. key = strconv.Itoa(int(it))
  57. case int8:
  58. it := value.(int8)
  59. key = strconv.Itoa(int(it))
  60. case uint8:
  61. it := value.(uint8)
  62. key = strconv.Itoa(int(it))
  63. case int16:
  64. it := value.(int16)
  65. key = strconv.Itoa(int(it))
  66. case uint16:
  67. it := value.(uint16)
  68. key = strconv.Itoa(int(it))
  69. case int32:
  70. it := value.(int32)
  71. key = strconv.Itoa(int(it))
  72. case uint32:
  73. it := value.(uint32)
  74. key = strconv.Itoa(int(it))
  75. case int64:
  76. it := value.(int64)
  77. key = strconv.FormatInt(it, 10)
  78. case uint64:
  79. it := value.(uint64)
  80. key = strconv.FormatUint(it, 10)
  81. case string:
  82. key = value.(string)
  83. case []byte:
  84. key = string(value.([]byte))
  85. default:
  86. newValue, _ := json.Marshal(value)
  87. key = string(newValue)
  88. }
  89. return key
  90. }
  91. // 取一个
  92. func Take_one(a, b string) string {
  93. if len(a) == 0 {
  94. return b
  95. }
  96. return a
  97. }
  98. func To_int(value interface{}) int {
  99. var key int
  100. if value == nil {
  101. return key
  102. }
  103. switch value.(type) {
  104. case float64:
  105. key = int(value.(float64))
  106. case float32:
  107. key = int(value.(float32))
  108. case int:
  109. key = int(value.(int))
  110. case uint:
  111. key = int(value.(uint))
  112. case int8:
  113. key = int(value.(int8))
  114. case uint8:
  115. key = int(value.(uint8))
  116. case int16:
  117. key = int(value.(int16))
  118. case uint16:
  119. key = int(value.(uint16))
  120. case int32:
  121. key = int(value.(int32))
  122. case uint32:
  123. key = int(value.(uint32))
  124. case int64:
  125. key = int(value.(int64))
  126. case uint64:
  127. key = int(value.(uint64))
  128. case string:
  129. key, _ = strconv.Atoi(value.(string))
  130. case []byte:
  131. key, _ = strconv.Atoi(string(value.([]byte)))
  132. default:
  133. newValue, _ := json.Marshal(value)
  134. key, _ = strconv.Atoi(string(newValue))
  135. }
  136. return key
  137. }
  138. func To_int64(value interface{}) int64 {
  139. var key int64
  140. if value == nil {
  141. return key
  142. }
  143. switch value.(type) {
  144. case float64:
  145. key = int64(value.(float64))
  146. case float32:
  147. key = int64(value.(float32))
  148. case int:
  149. key = int64(value.(int))
  150. case uint:
  151. key = int64(value.(uint))
  152. case int8:
  153. key = int64(value.(int8))
  154. case uint8:
  155. key = int64(value.(uint8))
  156. case int16:
  157. key = int64(value.(int16))
  158. case uint16:
  159. key = int64(value.(uint16))
  160. case int32:
  161. key = int64(value.(int32))
  162. case uint32:
  163. key = int64(value.(uint32))
  164. case int64:
  165. key = int64(value.(int64))
  166. case uint64:
  167. key = int64(value.(uint64))
  168. case string:
  169. key, _ = strconv.ParseInt(value.(string), 10, 64)
  170. case []byte:
  171. key, _ = strconv.ParseInt(string(value.([]byte)), 10, 64)
  172. default:
  173. newValue, _ := json.Marshal(value)
  174. key, _ = strconv.ParseInt(string(newValue), 10, 64)
  175. }
  176. return key
  177. }
  178. func To_float32(value interface{}) float32 {
  179. var key float32
  180. if value == nil {
  181. return key
  182. }
  183. switch value.(type) {
  184. case float64:
  185. key = float32(value.(float64))
  186. case float32:
  187. key = float32(value.(float32))
  188. case int:
  189. key = float32(value.(int))
  190. case uint:
  191. key = float32(value.(uint))
  192. case int8:
  193. key = float32(value.(int8))
  194. case uint8:
  195. key = float32(value.(uint8))
  196. case int16:
  197. key = float32(value.(int16))
  198. case uint16:
  199. key = float32(value.(uint16))
  200. case int32:
  201. key = float32(value.(int32))
  202. case uint32:
  203. key = float32(value.(uint32))
  204. case int64:
  205. key = float32(value.(int64))
  206. case uint64:
  207. key = float32(value.(uint64))
  208. case string:
  209. key_float64, _ := strconv.ParseFloat(value.(string), 32/64)
  210. key = float32(key_float64)
  211. case []byte:
  212. key_float64, _ := strconv.ParseFloat(string(value.([]byte)), 32/64)
  213. key = float32(key_float64)
  214. default:
  215. newValue, _ := json.Marshal(value)
  216. key_float64, _ := strconv.ParseFloat(string(newValue), 32/64)
  217. key = float32(key_float64)
  218. }
  219. key_float64, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", key), 32/64)
  220. key = float32(key_float64)
  221. return key
  222. }
  223. func To_string(value interface{}) string {
  224. var key string
  225. if value == nil {
  226. return key
  227. }
  228. switch value.(type) {
  229. case float64:
  230. ft := value.(float64)
  231. key = strconv.FormatFloat(ft, 'f', -1, 64)
  232. case float32:
  233. ft := value.(float32)
  234. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  235. case int:
  236. it := value.(int)
  237. key = strconv.Itoa(it)
  238. case uint:
  239. it := value.(uint)
  240. key = strconv.Itoa(int(it))
  241. case int8:
  242. it := value.(int8)
  243. key = strconv.Itoa(int(it))
  244. case uint8:
  245. it := value.(uint8)
  246. key = strconv.Itoa(int(it))
  247. case int16:
  248. it := value.(int16)
  249. key = strconv.Itoa(int(it))
  250. case uint16:
  251. it := value.(uint16)
  252. key = strconv.Itoa(int(it))
  253. case int32:
  254. it := value.(int32)
  255. key = strconv.Itoa(int(it))
  256. case uint32:
  257. it := value.(uint32)
  258. key = strconv.Itoa(int(it))
  259. case int64:
  260. it := value.(int64)
  261. key = strconv.FormatInt(it, 10)
  262. case uint64:
  263. it := value.(uint64)
  264. key = strconv.FormatUint(it, 10)
  265. case string:
  266. key = value.(string)
  267. case []byte:
  268. key = string(value.([]byte))
  269. default:
  270. newValue, _ := json.Marshal(value)
  271. key = string(newValue)
  272. }
  273. return key
  274. }
  275. func Random(min, max int) int {
  276. rand.Seed(time.Now().Unix()) //Seed生成的随机数
  277. return rand.Intn(max-min) + min
  278. }
  279. // #取得随机字符串:通过打乱slice来操作
  280. func GetRandstring(length int, char string, rand_x int64) string {
  281. if length < 1 {
  282. return ""
  283. }
  284. if len(char) <= 6 || len(char) <= length {
  285. char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  286. }
  287. charArr := strings.Split(char, "")
  288. ran := rand.New(rand.NewSource(time.Now().Unix() + rand_x))
  289. l := len(charArr)
  290. for i := l - 1; i > 0; i-- {
  291. r := ran.Intn(i)
  292. charArr[r], charArr[i] = charArr[i], charArr[r]
  293. }
  294. rchar := charArr[:length]
  295. return strings.Join(rchar, "")
  296. }
  297. // 判断时间是当年的第几周
  298. func WeekByDate() string {
  299. t := time.Now()
  300. yearDay := t.YearDay()
  301. yearFirstDay := t.AddDate(0, 0, -yearDay+1)
  302. firstDayInWeek := int(yearFirstDay.Weekday())
  303. //今年第一周有几天
  304. firstWeekDays := 1
  305. if firstDayInWeek != 0 {
  306. firstWeekDays = 7 - firstDayInWeek + 1
  307. }
  308. var week int
  309. if yearDay <= firstWeekDays {
  310. week = 1
  311. } else {
  312. week = (yearDay-firstWeekDays)/7 + 2
  313. }
  314. return fmt.Sprintf("%d%02d", t.Year()%100, week) // 2253
  315. }
  316. // 将 时间格式为 bson.M 时间格式
  317. func MongdbJsonHtime(ArticleSlide bson.M) bson.M {
  318. bson_M := bson.M{}
  319. for key, value := range ArticleSlide {
  320. //logs.Println(reflect.TypeOf(value).String())
  321. switch reflect.TypeOf(value).String() {
  322. case "map[string]interface {}":
  323. jsonFind_M := bson.M{}
  324. data, _ := json.Marshal(value.(map[string]interface{}))
  325. err := json.Unmarshal(data, &jsonFind_M)
  326. if err == nil {
  327. bson_M[key] = MongdbJsonHtime(jsonFind_M)
  328. }
  329. break
  330. case "[]interface {}":
  331. bson_M_list := []bson.M{}
  332. for _, valuex := range value.([]interface{}) {
  333. if reflect.TypeOf(valuex).String() == "map[string]interface {}" {
  334. jsonFind_M := bson.M{}
  335. data, _ := json.Marshal(valuex.(map[string]interface{}))
  336. err := json.Unmarshal(data, &jsonFind_M)
  337. if err == nil {
  338. bson_M_list = append(bson_M_list, MongdbJsonHtime(jsonFind_M))
  339. }
  340. }
  341. }
  342. bson_M[key] = bson_M_list
  343. //return json_r
  344. break
  345. default:
  346. t1, err := time.ParseInLocation("2006-01-02 15:04:05", value.(string), time.Local) // +8 时差
  347. if err == nil {
  348. bson_M[key] = t1
  349. } else {
  350. bson_M[key] = value
  351. }
  352. break
  353. }
  354. }
  355. return bson_M
  356. }
  357. // 判断字符串是否在数组中
  358. func StringExistsInSlice(str string, slice []string) bool {
  359. for _, s := range slice {
  360. if s == str {
  361. return true
  362. }
  363. }
  364. return false
  365. }
  366. // 获取 JSON 指定位置 field : "AAA.BBB.d_name"
  367. func JsonGetField(data map[string]interface{}, field string) interface{} {
  368. fields := strings.Split(field, ".")
  369. result := data
  370. for _, f := range fields {
  371. if val, ok := result[f]; ok {
  372. switch val.(type) {
  373. case map[string]interface{}:
  374. result = val.(map[string]interface{})
  375. default:
  376. return val
  377. }
  378. } else {
  379. return nil
  380. }
  381. }
  382. return result
  383. }
  384. // 获取 JSON 指定位置 data:修改的field必须存在 field : "AAA.BBB.d_name" value: 123
  385. func JsonSetField(data map[string]interface{}, field string, value interface{}) {
  386. fields := strings.Split(field, ".")
  387. current := data
  388. for i, fieldv := range fields {
  389. if i == len(fields)-1 {
  390. current[fieldv] = value
  391. } else {
  392. next, ok := current[fieldv].(map[string]interface{})
  393. if !ok {
  394. return
  395. }
  396. current = next
  397. }
  398. }
  399. }
  400. // 复制一份 Map
  401. func CopyMap(inputMap map[string]interface{}) map[string]interface{} {
  402. copiedMap := make(map[string]interface{})
  403. for key, value := range inputMap {
  404. copiedMap[key] = value
  405. }
  406. return copiedMap
  407. }