xcx_mobile_api_service.go 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/base64"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "github.com/jinzhu/gorm"
  11. "strings"
  12. "time"
  13. )
  14. func GetXcxMobileInformation(mobile string) (*models.XcxAdminUserRole, error) {
  15. role := models.XcxAdminUserRole{}
  16. err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
  17. if err == gorm.ErrRecordNotFound {
  18. return nil, err
  19. }
  20. if err != nil {
  21. return nil, err
  22. }
  23. return &role, nil
  24. }
  25. func CreateXcxAdminUser(role models.XcxAdminUserRole) error {
  26. err := UserReadDB().Create(&role).Error
  27. return err
  28. }
  29. func GetLoginInfor(mobile string) (models.XcxAdminUserRole, error) {
  30. role := models.XcxAdminUserRole{}
  31. err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
  32. return role, err
  33. }
  34. func GetMobilePatientInfo(mobile string) (models.XcxPatients, error) {
  35. patients := models.XcxPatients{}
  36. err := XTReadDB().Model(&patients).Where("(phone = ? or home_telephone = ? ) and status = 1", mobile, mobile).Find(&patients).Error
  37. return patients, err
  38. }
  39. func GetPatientListByPatientId(id int64) (models.XcxPatients, error) {
  40. patients := models.XcxPatients{}
  41. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error
  42. return patients, err
  43. }
  44. func DecryptData(app_id, session_key, iv, encrypted_data string) (map[string]interface{}, error) {
  45. if len := strings.Count(session_key, "") - 1; len != 24 {
  46. return nil, errors.New("Invalid value session_key!")
  47. }
  48. aesKey, err := base64.StdEncoding.DecodeString(session_key)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if len := strings.Count(iv, "") - 1; len != 24 {
  53. return nil, errors.New("Invalid value iv!")
  54. }
  55. ivKey, err := base64.StdEncoding.DecodeString(iv)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if len := strings.Count(encrypted_data, "") - 1; len != 24 {
  60. return nil, errors.New("Invalid value encrypted_data!")
  61. }
  62. decodeData, err := base64.StdEncoding.DecodeString(encrypted_data)
  63. if err != nil {
  64. return nil, err
  65. }
  66. dataBytes, err := AesDecrypt(decodeData, aesKey, ivKey)
  67. fmt.Println("err232332232332232232323", err)
  68. if err != nil {
  69. return nil, err
  70. }
  71. var result map[string]interface{}
  72. err = json.Unmarshal(dataBytes, &result)
  73. watermark := result["watermark"].(map[string]interface{})
  74. if watermark["appid"] != app_id {
  75. return nil, errors.New("Invalid appid data!")
  76. }
  77. return result, err
  78. }
  79. func AesDecrypt(crypted, key, iv []byte) ([]byte, error) {
  80. block, err := aes.NewCipher(key)
  81. fmt.Println("w2222", err)
  82. if err != nil {
  83. return nil, err
  84. }
  85. blockMode := cipher.NewCBCDecrypter(block, iv)
  86. origData := make([]byte, len(crypted))
  87. fmt.Println("4443434344334434334343434", origData)
  88. blockMode.CryptBlocks(origData, crypted)
  89. // 去除填充
  90. length := len(origData)
  91. unp := int(origData[length-1])
  92. fmt.Println("hhh23h2323232332", origData[:(length-unp)], nil)
  93. return origData[:(length - unp)], nil
  94. }
  95. func GetFirstDateOfWeek() (weekMonday string) {
  96. now := time.Now()
  97. offset := int(time.Monday - now.Weekday())
  98. if offset > 0 {
  99. offset = -6
  100. }
  101. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  102. weekMonday = weekStartDate.Format("2006-01-02")
  103. return
  104. }
  105. func GetWeekDayOfWeek() (weekMonday string) {
  106. now := time.Now()
  107. offset := int(time.Monday - now.Weekday())
  108. if offset > 0 {
  109. offset = -6
  110. }
  111. weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
  112. weekMonday = weekStartDate.Format("2006-01-02")
  113. return
  114. }
  115. func GetMobilePatient(mobile string) (*models.XcxPatients, error) {
  116. patient := models.XcxPatients{}
  117. err := UserReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error
  118. if err == gorm.ErrRecordNotFound {
  119. return nil, err
  120. }
  121. if err != nil {
  122. return nil, err
  123. }
  124. return &patient, nil
  125. }
  126. func Dncrypt(rawData, key, iv string) (string, error) {
  127. data, err := base64.StdEncoding.DecodeString(rawData)
  128. key_b, err_1 := base64.StdEncoding.DecodeString(key)
  129. iv_b, _ := base64.StdEncoding.DecodeString(iv)
  130. if err != nil {
  131. return "", err
  132. }
  133. if err_1 != nil {
  134. return "", err_1
  135. }
  136. dnData, err := AesCBCDncrypt(data, key_b, iv_b)
  137. if err != nil {
  138. return "", err
  139. }
  140. return string(dnData), nil
  141. }
  142. // 解密
  143. func AesCBCDncrypt(encryptData, key, iv []byte) ([]byte, error) {
  144. block, err := aes.NewCipher(key)
  145. if err != nil {
  146. panic(err)
  147. }
  148. blockSize := block.BlockSize()
  149. if len(encryptData) < blockSize {
  150. panic("ciphertext too short")
  151. }
  152. if len(encryptData)%blockSize != 0 {
  153. panic("ciphertext is not a multiple of the block size")
  154. }
  155. mode := cipher.NewCBCDecrypter(block, iv)
  156. mode.CryptBlocks(encryptData, encryptData)
  157. // 解填充
  158. encryptData = PKCS7UnPadding(encryptData)
  159. return encryptData, nil
  160. }
  161. //去除填充
  162. func PKCS7UnPadding(origData []byte) []byte {
  163. length := len(origData)
  164. unpadding := int(origData[length-1])
  165. if length-unpadding < 0 {
  166. return origData[:0]
  167. }
  168. fmt.Println("changdu", length)
  169. fmt.Println("data", unpadding)
  170. return origData[:(length - unpadding)]
  171. }
  172. func GetScheduleInfo(startime int64, endtime int64, patient_id int64) (schedule []*models.VmBloodSchedule, err error) {
  173. //err = XTReadDB().Model(&schedule).Where("schedule_date >=? and schedule_date<=? and status = 1", startime, endtime, patient_id).Find(&schedule).Error
  174. //return schedule,err
  175. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  176. table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
  177. fmt.Println(table)
  178. if startime > 0 {
  179. db = db.Where("x.schedule_date >=?", startime)
  180. }
  181. if endtime > 0 {
  182. db = db.Where("x.schedule_date <=?", endtime)
  183. }
  184. if patient_id > 0 {
  185. db = db.Where("x.patient_id = ?", patient_id)
  186. }
  187. err = db.Select("x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,t.name").Joins("left join xt_patients as t on t.id = x.patient_id").Scan(&schedule).Error
  188. return schedule, err
  189. }