附近小店
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

sdk.go 3.8 KiB

2ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package arkid
  2. import (
  3. "applet/app/utils/cache"
  4. "applet/app/utils/logx"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. )
  9. type SDK struct {
  10. response []byte
  11. fmap map[string]func(RequestBody)
  12. fname string
  13. err error
  14. }
  15. //Init is init sdk
  16. func (s *SDK) Init() {
  17. s.fmap = make(map[string]func(RequestBody))
  18. }
  19. //SelectFunction is choose func
  20. func (s *SDK) SelectFunction(fname string) *SDK {
  21. s.fname = fname
  22. return s
  23. }
  24. //WithArgs is request args
  25. func (s *SDK) WithArgs(r RequestBody) *SDK {
  26. f := s.fmap[s.fname]
  27. f(r)
  28. return s
  29. }
  30. //Result is result to p
  31. func (s *SDK) Result(p interface{}) error {
  32. if s.err != nil {
  33. return s.err
  34. }
  35. if string(s.response) == "" {
  36. return nil
  37. }
  38. if err := json.Unmarshal(s.response, p); err != nil {
  39. return logx.Error(string(s.response), err)
  40. }
  41. return nil
  42. }
  43. // Register is register func
  44. func (s *SDK) Register(name string, f func(RequestBody)) {
  45. s.fmap[name] = f
  46. }
  47. //getAdmin arkid 用户的信息 ,主要是token
  48. func (s *SDK) arkidLogin(r RequestBody) {
  49. postData := map[string]interface{}{
  50. "username": r.Username,
  51. "password": r.Password,
  52. }
  53. s.response, s.err = arkidLogin(postData)
  54. }
  55. func (s *SDK) arkidRegister(r RequestBody) {
  56. postData := map[string]interface{}{}
  57. b, err := json.Marshal(r)
  58. if err != nil {
  59. s.err = err
  60. }
  61. if err := json.Unmarshal(b, &postData); err != nil {
  62. s.err = err
  63. }
  64. s.response, s.err = arkidRegister(postData)
  65. }
  66. func (s *SDK) arkidAppAccessWhiteList(r RequestBody) {
  67. postData := map[string]interface{}{}
  68. b, err := json.Marshal(r)
  69. if err != nil {
  70. s.err = err
  71. }
  72. if err := json.Unmarshal(b, &postData); err != nil {
  73. s.err = err
  74. }
  75. s.response, s.err = arkidAppAccessWhiteList(postData, r.PermName)
  76. }
  77. func (s *SDK) arkidUserInfo(r RequestBody) {
  78. s.response, s.err = arkidUserInfo(r.Token)
  79. }
  80. func (s *SDK) arkidUserDelete(r RequestBody) {
  81. s.response, s.err = arkidUserDelete(r.Username)
  82. }
  83. func (s *SDK) arkidUserUpdate(r RequestBody) {
  84. postData := map[string]interface{}{}
  85. b, err := json.Marshal(r.User)
  86. if err != nil {
  87. s.err = err
  88. }
  89. if err := json.Unmarshal(b, &postData); err != nil {
  90. s.err = err
  91. }
  92. s.response, s.err = arkidUserUpdate(r.Username, postData)
  93. }
  94. func (s *SDK) arkidLogout(r RequestBody) {
  95. s.response, s.err = arkidLogout(r.Token)
  96. }
  97. // NewArkID is con
  98. func NewArkID() *SDK {
  99. sdk := new(SDK)
  100. sdk.Init()
  101. sdk.Register("arkid_login", sdk.arkidLogin)
  102. sdk.Register("arkid_register", sdk.arkidRegister)
  103. sdk.Register("arkid_app_access_white_list", sdk.arkidAppAccessWhiteList)
  104. sdk.Register("arkid_delete_user", sdk.arkidUserDelete)
  105. sdk.Register("arkid_user_info", sdk.arkidUserInfo)
  106. sdk.Register("arkid_user_update", sdk.arkidUserUpdate)
  107. sdk.Register("arkid_logout", sdk.arkidLogout)
  108. return sdk
  109. }
  110. // GetArkIDUser is get arkid token if redis is existed unless send request to arkid
  111. func GetArkIDUser(username string, MD5passowrd string) (*ArkIDUser, error) {
  112. key := fmt.Sprintf("arkid_user_%s", username)
  113. arkidUser := new(ArkIDUser)
  114. c, err := cache.GetBytes(key)
  115. if c != nil && err == nil {
  116. if err := json.Unmarshal(c, arkidUser); err != nil {
  117. return arkidUser, err
  118. }
  119. if arkidUser.Token == "" {
  120. return arkidUser, errors.New("Get Arkid User error, Token missing")
  121. }
  122. return arkidUser, err
  123. }
  124. arkidSdk := NewArkID()
  125. err = arkidSdk.SelectFunction("arkid_login").WithArgs(RequestBody{
  126. Username: username,
  127. Password: MD5passowrd,
  128. }).Result(arkidUser)
  129. if arkidUser.Token == "" {
  130. return arkidUser, errors.New("Get Arkid User error, Token missing")
  131. }
  132. // 缓存30天
  133. // cache.SetEx(key, utils.Serialize(arkidUser), 2592000)
  134. return arkidUser, err
  135. }
  136. // RegisterRollback is 注册时的错误回滚
  137. func RegisterRollback(username string) error {
  138. sdk := NewArkID()
  139. err := sdk.SelectFunction("arkid_delete_user").WithArgs(RequestBody{
  140. Username: username,
  141. }).Result(nil)
  142. if err != nil {
  143. return err
  144. }
  145. return nil
  146. }