123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package service
-
- import (
- "Xcx_New/models"
- "crypto/aes"
- "crypto/cipher"
- "encoding/base64"
- "encoding/json"
- "errors"
- "github.com/jinzhu/gorm"
- "strings"
- "time"
- )
-
- func GetXcxMobileInformation(mobile string) (*models.XcxAdminUserRole, error) {
- role := models.XcxAdminUserRole{}
-
- err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
- if err == gorm.ErrRecordNotFound {
- return nil, err
- }
-
- if err != nil {
- return nil, err
- }
- return &role, nil
- }
-
- func CreateXcxAdminUser(role models.XcxAdminUserRole) error {
-
- err := UserReadDB().Create(&role).Error
- return err
- }
-
- func GetLoginInfor(mobile string) (models.XcxAdminUserRole, error) {
- role := models.XcxAdminUserRole{}
- err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
- return role, err
- }
-
- func GetMobilePatientInfo(mobile string) (models.XcxPatients, error) {
-
- patients := models.XcxPatients{}
- err := XTReadDB().Model(&patients).Where("(phone = ? or home_telephone = ? ) and status = 1", mobile, mobile).Find(&patients).Error
- return patients, err
- }
-
- func GetPatientListByPatientId(id int64) (models.XcxPatients, error) {
-
- patients := models.XcxPatients{}
- err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error
- return patients, err
- }
-
- func DecryptData(app_id, session_key, iv, encrypted_data string) (map[string]interface{}, error) {
- if len := strings.Count(session_key, "") - 1; len != 30 {
- return nil, errors.New("Invalid value session_key!")
- }
- aesKey, err := base64.StdEncoding.DecodeString(session_key)
- if err != nil {
- return nil, err
- }
-
- if len := strings.Count(iv, "") - 1; len != 30 {
- return nil, errors.New("Invalid value iv!")
- }
- ivKey, err := base64.StdEncoding.DecodeString(iv)
- if err != nil {
- return nil, err
- }
-
- decodeData, err := base64.StdEncoding.DecodeString(encrypted_data)
- if err != nil {
- return nil, err
- }
-
- dataBytes, err := AesDecrypt(decodeData, aesKey, ivKey)
- if err != nil {
- return nil, err
- }
-
- var result map[string]interface{}
- err = json.Unmarshal(dataBytes, &result)
-
- watermark := result["watermark"].(map[string]interface{})
- if watermark["appid"] != app_id {
- return nil, errors.New("Invalid appid data!")
- }
-
- return result, err
- }
-
- func AesDecrypt(crypted, key, iv []byte) ([]byte, error) {
- block, err := aes.NewCipher(key)
- if err != nil {
- return nil, err
- }
-
- blockMode := cipher.NewCBCDecrypter(block, iv)
- origData := make([]byte, len(crypted))
- blockMode.CryptBlocks(origData, crypted)
-
- // 去除填充
- length := len(origData)
- unp := int(origData[length-1])
- return origData[:(length - unp)], nil
-
- }
-
- func GetFirstDateOfWeek() (weekMonday string) {
- now := time.Now()
-
- offset := int(time.Monday - now.Weekday())
- if offset > 0 {
- offset = -6
- }
-
- weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
- weekMonday = weekStartDate.Format("2006-01-02")
- return
- }
-
- func GetWeekDayOfWeek() (weekMonday string) {
- now := time.Now()
-
- offset := int(time.Monday - now.Weekday())
- if offset > 0 {
- offset = -6
- }
-
- weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
- weekMonday = weekStartDate.Format("2006-01-02")
- return
- }
-
- func GetMobilePatient(mobile string) (*models.XcxPatients, error) {
-
- patient := models.XcxPatients{}
-
- err := UserReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error
- if err == gorm.ErrRecordNotFound {
- return nil, err
- }
-
- if err != nil {
- return nil, err
- }
- return &patient, nil
-
- }
|