werun.go 843 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package werun
  2. import (
  3. "encoding/json"
  4. "github.com/silenceper/wechat/v2/miniprogram/context"
  5. "github.com/silenceper/wechat/v2/miniprogram/encryptor"
  6. )
  7. // WeRun 微信运动
  8. type WeRun struct {
  9. *context.Context
  10. }
  11. // Data 微信运动数据
  12. type Data struct {
  13. StepInfoList []struct {
  14. Timestamp int `json:"timestamp"`
  15. Step int `json:"step"`
  16. } `json:"stepInfoList"`
  17. }
  18. // NewWeRun 实例化
  19. func NewWeRun(ctx *context.Context) *WeRun {
  20. return &WeRun{Context: ctx}
  21. }
  22. // GetWeRunData 解密数据
  23. func (werun *WeRun) GetWeRunData(sessionKey, encryptedData, iv string) (*Data, error) {
  24. cipherText, err := encryptor.GetCipherText(sessionKey, encryptedData, iv)
  25. if err != nil {
  26. return nil, err
  27. }
  28. var weRunData Data
  29. err = json.Unmarshal(cipherText, &weRunData)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &weRunData, nil
  34. }