functions.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * @description: 常用函数
  3. * @Author: CP
  4. * @Date: 2020-08-21 11:12:56
  5. * @FilePath: \construction_management\comm\functions.go
  6. */
  7. package comm
  8. import (
  9. "bytes"
  10. "crypto/aes"
  11. "crypto/cipher"
  12. "encoding/base64"
  13. "errors"
  14. "math/rand"
  15. "time"
  16. "unicode"
  17. "crypto/md5"
  18. "encoding/binary"
  19. "fmt"
  20. "strconv"
  21. "strings"
  22. "go.mod/conf"
  23. "go.mod/models"
  24. "go.mod/web/viewmodels"
  25. )
  26. // 账号-构造视图层models
  27. func MakeProjectAccountVM(modelsAccount *models.CmProjectAccount) viewmodels.ProjectAccount {
  28. viewAccountData := viewmodels.ProjectAccount{}
  29. aesId, _ := AesEncrypt(strconv.Itoa(modelsAccount.Id), conf.SignSecret)
  30. aesProjectId, _ := AesEncrypt(strconv.Itoa(modelsAccount.ProjectId), conf.SignSecret)
  31. viewAccountData.Id = aesId
  32. viewAccountData.ProjectId = aesProjectId
  33. viewAccountData.Account = modelsAccount.Account
  34. viewAccountData.Name = modelsAccount.Name
  35. viewAccountData.Company = modelsAccount.Company
  36. viewAccountData.Role = modelsAccount.Role
  37. viewAccountData.Mobile = modelsAccount.Mobile
  38. viewAccountData.Telephone = modelsAccount.Telephone
  39. viewAccountData.IsAdmin = modelsAccount.IsAdmin
  40. return viewAccountData
  41. }
  42. // 创建合同目录树
  43. func MakeFolderContract(Data []*viewmodels.FolderContract, node *viewmodels.FolderContract) { //参数为父节点,添加父节点的子节点指针切片
  44. childs, _ := HaveChildContract(Data, node) //判断节点是否有子节点并返回
  45. if childs != nil {
  46. // 子节点总数
  47. Total := len(childs)
  48. node.ChildsTotal = Total
  49. // 标记最后一个元素
  50. end := Total - 1
  51. childs[end].IsEnd = true
  52. // 往父节点添加子节点
  53. node.Children = append(node.Children, childs[0:]...) //添加子节点
  54. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  55. _, has := HaveChildContract(Data, v)
  56. if has {
  57. // 递归添加节点
  58. MakeFolderContract(Data, v)
  59. }
  60. // 目录下是否包含标段
  61. if v.Isfolder == 0 {
  62. node.IsBid = true
  63. } else {
  64. node.HasFolder = true
  65. }
  66. }
  67. }
  68. }
  69. // 是否有子树
  70. func HaveChildContract(Data []*viewmodels.FolderContract, node *viewmodels.FolderContract) (child []*viewmodels.FolderContract, yes bool) {
  71. for _, v := range Data {
  72. if v.ParentId == node.Id {
  73. child = append(child, v)
  74. }
  75. }
  76. if child != nil {
  77. yes = true
  78. }
  79. return
  80. }
  81. // 创建合同项目节树
  82. func MakeSectionContract(Data []*viewmodels.TreeSectionContract, node *viewmodels.TreeSectionContract) { //参数为父节点,添加父节点的子节点指针切片
  83. childs, _ := haveChildSectionContract(Data, node) //判断节点是否有子节点并返回
  84. if childs != nil {
  85. // 往父节点添加子节点
  86. // 孩子们从小到大排序
  87. sectionSelectionSort(childs)
  88. node.Children = append(node.Children, childs[0:]...) //添加子节点
  89. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  90. _, has := haveChildSectionContract(Data, v)
  91. if has {
  92. // 递归添加节点
  93. MakeSectionContract(Data, v)
  94. }
  95. }
  96. }
  97. }
  98. // 合同项目节孩子排序
  99. func sectionSelectionSort(child []*viewmodels.TreeSectionContract) {
  100. for i := 0; i < len(child); i++ {
  101. minIndex := i
  102. // 查找最小值
  103. for j := i; j < len(child); j++ {
  104. if child[j].Serial < child[minIndex].Serial {
  105. minIndex = j
  106. }
  107. }
  108. swapContract(child, i, minIndex)
  109. }
  110. }
  111. func swapContract(child []*viewmodels.TreeSectionContract, i int, minIndex int) {
  112. t := child[i]
  113. child[i] = child[minIndex]
  114. child[minIndex] = t
  115. }
  116. // 是否有子树
  117. func haveChildSectionContract(Data []*viewmodels.TreeSectionContract, node *viewmodels.TreeSectionContract) (child []*viewmodels.TreeSectionContract, yes bool) {
  118. for _, v := range Data {
  119. if v.ParentId == node.Id {
  120. child = append(child, v)
  121. }
  122. }
  123. if child != nil {
  124. yes = true
  125. }
  126. return
  127. }
  128. // 当前时间的时间戳
  129. func NowUnix() int {
  130. return int(time.Now().In(conf.SysTimeLocation).Unix())
  131. }
  132. // 将unix时间戳格式化为yyyymmdd H:i:s格式字符串
  133. func FormatFromUnixTime(t int64) string {
  134. if t > 0 {
  135. return time.Unix(t, 0).Format(conf.SysTimeform)
  136. } else {
  137. return time.Now().Format(conf.SysTimeform)
  138. }
  139. }
  140. // 将unix时间戳格式化为yyyymmdd格式字符串
  141. func FormatFromUnixTimeShort(t int64) string {
  142. if t > 0 {
  143. return time.Unix(t, 0).Format(conf.SysTimeformShort)
  144. } else {
  145. return time.Now().Format(conf.SysTimeformShort)
  146. }
  147. }
  148. // 将字符串转成时间
  149. func ParseTime(str string) (time.Time, error) {
  150. return time.ParseInLocation(conf.SysTimeform, str, conf.SysTimeLocation)
  151. }
  152. // 得到一个随机数
  153. func Random(max int) int {
  154. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  155. if max < 1 {
  156. return r.Int()
  157. } else {
  158. return r.Intn(max)
  159. }
  160. }
  161. // 加密密码
  162. func CreatePasswordSign(password string, account string) string {
  163. str := string(conf.SignSecret) + password + account
  164. str1 := fmt.Sprintf("%x", md5.Sum([]byte(str)))
  165. sign := fmt.Sprintf("%x", md5.Sum([]byte(str1)))
  166. return sign
  167. }
  168. // 对字符串进行签名
  169. func CreateSign(str string) string {
  170. str = string(conf.SignSecret) + str
  171. str1 := fmt.Sprintf("%x", md5.Sum([]byte(str)))
  172. sign := fmt.Sprintf("%x", md5.Sum([]byte(str1)))
  173. return sign
  174. }
  175. // 对一个字符串进行加密
  176. func Encrypt(key, text []byte) (string, error) {
  177. block, err := aes.NewCipher(key)
  178. if err != nil {
  179. return "", err
  180. }
  181. b := base64.StdEncoding.EncodeToString(text)
  182. ciphertext := make([]byte, aes.BlockSize+len(b))
  183. iv := ciphertext[:aes.BlockSize]
  184. //if _, err := io.ReadFull(rand.Reader, iv); err != nil {
  185. // return nil, err
  186. //}
  187. cfb := cipher.NewCFBEncrypter(block, iv)
  188. cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
  189. return base64.RawURLEncoding.EncodeToString(ciphertext), nil
  190. }
  191. // 对一个字符串进行解密
  192. func Decrypt(key, text []byte) ([]byte, error) {
  193. block, err := aes.NewCipher(key)
  194. if err != nil {
  195. return nil, err
  196. }
  197. if len(text) < aes.BlockSize {
  198. return nil, errors.New("ciphertext too short")
  199. }
  200. iv := text[:aes.BlockSize]
  201. text = text[aes.BlockSize:]
  202. cfb := cipher.NewCFBDecrypter(block, iv)
  203. cfb.XORKeyStream(text, text)
  204. data, err := base64.StdEncoding.DecodeString(string(text))
  205. if err != nil {
  206. return nil, err
  207. }
  208. return data, nil
  209. }
  210. // 加密
  211. func AesEncrypt(orig string, key string) (string, error) {
  212. // 转成字节数组
  213. origData := []byte(orig)
  214. k := []byte(key)
  215. // 分组秘钥
  216. block, err := aes.NewCipher(k)
  217. if err != nil {
  218. return "", err
  219. }
  220. // 获取秘钥块的长度
  221. blockSize := block.BlockSize()
  222. // 补全码
  223. origData = PKCS7Padding(origData, blockSize)
  224. // 加密模式
  225. blockMode := cipher.NewCBCEncrypter(block, k[:blockSize])
  226. // 创建数组
  227. cryted := make([]byte, len(origData))
  228. // 加密
  229. blockMode.CryptBlocks(cryted, origData)
  230. //使用RawURLEncoding 不要使用StdEncoding
  231. //不要使用StdEncoding 放在url参数中回导致错误
  232. return base64.RawURLEncoding.EncodeToString(cryted), nil
  233. }
  234. // 解密
  235. func AesDecrypt(cryted string, key string) (string, error) {
  236. //使用RawURLEncoding 不要使用StdEncoding
  237. //不要使用StdEncoding 放在url参数中回导致错误
  238. crytedByte, err := base64.RawURLEncoding.DecodeString(cryted)
  239. if err != nil {
  240. return "", err
  241. }
  242. k := []byte(key)
  243. // 分组秘钥
  244. block, err := aes.NewCipher(k)
  245. if err != nil {
  246. return "", err
  247. }
  248. // 获取秘钥块的长度
  249. blockSize := block.BlockSize()
  250. // 加密模式
  251. blockMode := cipher.NewCBCDecrypter(block, k[:blockSize])
  252. // 创建数组
  253. orig := make([]byte, len(crytedByte))
  254. // 解密--传入不正确的字符时会报错-TODO 需捕获
  255. blockMode.CryptBlocks(orig, crytedByte)
  256. // 去补全码
  257. orig = PKCS7UnPadding(orig)
  258. return string(orig), nil
  259. }
  260. //补码
  261. func PKCS7Padding(ciphertext []byte, blocksize int) []byte {
  262. padding := blocksize - len(ciphertext)%blocksize
  263. padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  264. return append(ciphertext, padtext...)
  265. }
  266. //去码
  267. func PKCS7UnPadding(origData []byte) []byte {
  268. length := len(origData)
  269. unpadding := int(origData[length-1])
  270. return origData[:(length - unpadding)]
  271. }
  272. // addslashes() 函数返回在预定义字符之前添加反斜杠的字符串。
  273. // 预定义字符是:
  274. // 单引号(')
  275. // 双引号(")
  276. // 反斜杠(\)
  277. func Addslashes(str string) string {
  278. tmpRune := []rune{}
  279. strRune := []rune(str)
  280. for _, ch := range strRune {
  281. switch ch {
  282. case []rune{'\\'}[0], []rune{'"'}[0], []rune{'\''}[0]:
  283. tmpRune = append(tmpRune, []rune{'\\'}[0])
  284. tmpRune = append(tmpRune, ch)
  285. default:
  286. tmpRune = append(tmpRune, ch)
  287. }
  288. }
  289. return string(tmpRune)
  290. }
  291. // stripslashes() 函数删除由 addslashes() 函数添加的反斜杠。
  292. func Stripslashes(str string) string {
  293. dstRune := []rune{}
  294. strRune := []rune(str)
  295. strLenth := len(strRune)
  296. for i := 0; i < strLenth; i++ {
  297. if strRune[i] == []rune{'\\'}[0] {
  298. i++
  299. }
  300. dstRune = append(dstRune, strRune[i])
  301. }
  302. return string(dstRune)
  303. }
  304. // 将字符串的IP转化为数字
  305. func Ip4toInt(ip string) int64 {
  306. bits := strings.Split(ip, ".")
  307. if len(bits) == 4 {
  308. b0, _ := strconv.Atoi(bits[0])
  309. b1, _ := strconv.Atoi(bits[1])
  310. b2, _ := strconv.Atoi(bits[2])
  311. b3, _ := strconv.Atoi(bits[3])
  312. var sum int64
  313. sum += int64(b0) << 24
  314. sum += int64(b1) << 16
  315. sum += int64(b2) << 8
  316. sum += int64(b3)
  317. return sum
  318. } else {
  319. return 0
  320. }
  321. }
  322. // 得到当前时间到下一天零点的延时
  323. func NextDayDuration() time.Duration {
  324. year, month, day := time.Now().Add(time.Hour * 24).Date()
  325. next := time.Date(year, month, day, 0, 0, 0, 0, conf.SysTimeLocation)
  326. return next.Sub(time.Now())
  327. }
  328. // 从接口类型安全获取到int64
  329. func GetInt64(i interface{}, d int64) int64 {
  330. if i == nil {
  331. return d
  332. }
  333. switch i.(type) {
  334. case string:
  335. num, err := strconv.Atoi(i.(string))
  336. if err != nil {
  337. return d
  338. } else {
  339. return int64(num)
  340. }
  341. case []byte:
  342. bits := i.([]byte)
  343. if len(bits) == 8 {
  344. return int64(binary.LittleEndian.Uint64(bits))
  345. } else if len(bits) <= 4 {
  346. num, err := strconv.Atoi(string(bits))
  347. if err != nil {
  348. return d
  349. } else {
  350. return int64(num)
  351. }
  352. }
  353. case uint:
  354. return int64(i.(uint))
  355. case uint8:
  356. return int64(i.(uint8))
  357. case uint16:
  358. return int64(i.(uint16))
  359. case uint32:
  360. return int64(i.(uint32))
  361. case uint64:
  362. return int64(i.(uint64))
  363. case int:
  364. return int64(i.(int))
  365. case int8:
  366. return int64(i.(int8))
  367. case int16:
  368. return int64(i.(int16))
  369. case int32:
  370. return int64(i.(int32))
  371. case int64:
  372. return i.(int64)
  373. case float32:
  374. return int64(i.(float32))
  375. case float64:
  376. return int64(i.(float64))
  377. }
  378. return d
  379. }
  380. // 从接口类型安全获取到字符串类型
  381. func GetString(str interface{}, d string) string {
  382. if str == nil {
  383. return d
  384. }
  385. switch str.(type) {
  386. case string:
  387. return str.(string)
  388. case []byte:
  389. return string(str.([]byte))
  390. }
  391. return fmt.Sprintf("%s", str)
  392. }
  393. // 从map中得到指定的key
  394. func GetInt64FromMap(dm map[string]interface{}, key string, dft int64) int64 {
  395. data, ok := dm[key]
  396. if !ok {
  397. return dft
  398. }
  399. return GetInt64(data, dft)
  400. }
  401. // 从map中得到指定的key
  402. func GetInt64FromStringMap(dm map[string]string, key string, dft int64) int64 {
  403. data, ok := dm[key]
  404. if !ok {
  405. return dft
  406. }
  407. return GetInt64(data, dft)
  408. }
  409. // 从map中得到指定的key
  410. func GetStringFromMap(dm map[string]interface{}, key string, dft string) string {
  411. data, ok := dm[key]
  412. if !ok {
  413. return dft
  414. }
  415. return GetString(data, dft)
  416. }
  417. // 从map中得到指定的key
  418. func GetStringFromStringMap(dm map[string]string, key string, dft string) string {
  419. data, ok := dm[key]
  420. if !ok {
  421. return dft
  422. }
  423. return data
  424. }
  425. // 首字母大写
  426. func Ucfirst(str string) string {
  427. for i, v := range str {
  428. return string(unicode.ToUpper(v)) + str[i+1:]
  429. }
  430. return ""
  431. }