xcx_mobile_api_service.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/base64"
  7. "encoding/json"
  8. "errors"
  9. "github.com/jinzhu/gorm"
  10. "strings"
  11. "time"
  12. )
  13. func GetXcxMobileInformation(mobile string) (*models.XcxAdminUserRole, error) {
  14. role := models.XcxAdminUserRole{}
  15. err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
  16. if err == gorm.ErrRecordNotFound {
  17. return nil, err
  18. }
  19. if err != nil {
  20. return nil, err
  21. }
  22. return &role, nil
  23. }
  24. func CreateXcxAdminUser(role models.XcxAdminUserRole) error {
  25. err := UserReadDB().Create(&role).Error
  26. return err
  27. }
  28. func GetLoginInfor(mobile string) (models.XcxAdminUserRole, error) {
  29. role := models.XcxAdminUserRole{}
  30. err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
  31. return role, err
  32. }
  33. func GetMobilePatientInfo(mobile string) (models.XcxPatients, error) {
  34. patients := models.XcxPatients{}
  35. err := XTReadDB().Model(&patients).Where("(phone = ? or home_telephone = ? ) and status = 1", mobile, mobile).Find(&patients).Error
  36. return patients, err
  37. }
  38. func GetPatientListByPatientId(id int64) (models.XcxPatients, error) {
  39. patients := models.XcxPatients{}
  40. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error
  41. return patients, err
  42. }
  43. func DecryptData(app_id, session_key, iv, encrypted_data string) (map[string]interface{}, error) {
  44. if len := strings.Count(session_key, "") - 1; len != 24 {
  45. return nil, errors.New("Invalid value session_key!")
  46. }
  47. aesKey, err := base64.StdEncoding.DecodeString(session_key)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if len := strings.Count(iv, "") - 1; len != 24 {
  52. return nil, errors.New("Invalid value iv!")
  53. }
  54. ivKey, err := base64.StdEncoding.DecodeString(iv)
  55. if err != nil {
  56. return nil, err
  57. }
  58. decodeData, err := base64.StdEncoding.DecodeString(encrypted_data)
  59. if err != nil {
  60. return nil, err
  61. }
  62. dataBytes, err := AesDecrypt(decodeData, aesKey, ivKey)
  63. if err != nil {
  64. return nil, err
  65. }
  66. var result map[string]interface{}
  67. err = json.Unmarshal(dataBytes, &result)
  68. watermark := result["watermark"].(map[string]interface{})
  69. if watermark["appid"] != app_id {
  70. return nil, errors.New("Invalid appid data!")
  71. }
  72. return result, err
  73. }
  74. func AesDecrypt(crypted, key, iv []byte) ([]byte, error) {
  75. block, err := aes.NewCipher(key)
  76. if err != nil {
  77. return nil, err
  78. }
  79. blockMode := cipher.NewCBCDecrypter(block, iv)
  80. origData := make([]byte, len(crypted))
  81. blockMode.CryptBlocks(origData, crypted)
  82. // 去除填充
  83. length := len(origData)
  84. unp := int(origData[length-1])
  85. return origData[:(length - unp)], nil
  86. }
  87. func GetFirstDateOfWeek() (weekMonday string) {
  88. now := time.Now()
  89. offset := int(time.Monday - now.Weekday())
  90. if offset > 0 {
  91. offset = -6
  92. }
  93. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  94. weekMonday = weekStartDate.Format("2006-01-02")
  95. return
  96. }
  97. func GetWeekDayOfWeek() (weekMonday string) {
  98. now := time.Now()
  99. offset := int(time.Monday - now.Weekday())
  100. if offset > 0 {
  101. offset = -6
  102. }
  103. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  104. weekMonday = weekStartDate.Format("2006-01-02")
  105. return
  106. }
  107. func GetMobilePatient(mobile string) (*models.XcxPatients, error) {
  108. patient := models.XcxPatients{}
  109. err := UserReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error
  110. if err == gorm.ErrRecordNotFound {
  111. return nil, err
  112. }
  113. if err != nil {
  114. return nil, err
  115. }
  116. return &patient, nil
  117. }