get_signature_response_logic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package avatar
  2. import (
  3. "context"
  4. "crypto/hmac"
  5. "crypto/sha1"
  6. "encoding/base64"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  10. "time"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type GetSignatureResponseLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewGetSignatureResponseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSignatureResponseLogic {
  21. return &GetSignatureResponseLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx}
  25. }
  26. func (l *GetSignatureResponseLogic) GetSignatureResponse() (resp *types.AvatarSignatureResp, err error) {
  27. userId := l.ctx.Value("userId").(uint64)
  28. helper := NewMpUploadOssHelper(l.svcCtx.Config.Aliyun.ACCESS_KEY_ID, l.svcCtx.Config.Aliyun.ACCESS_KEY_SECRET, 1, 10)
  29. params := helper.CreateUploadParams()
  30. OSSAccessKeyId := params["OSSAccessKeyId"]
  31. policy := params["policy"]
  32. signature := params["signature"]
  33. key := fmt.Sprintf("mini_program/qingmasiyuai/avatar/%d.jpg", userId)
  34. return &types.AvatarSignatureResp{
  35. BaseDataInfo: types.BaseDataInfo{
  36. Code: 0,
  37. Msg: errormsg.Success,
  38. },
  39. Data: types.SignatureInfo{
  40. Key: &key,
  41. OSSAccessKeyId: &OSSAccessKeyId,
  42. Policy: &policy,
  43. Signature: &signature,
  44. },
  45. }, nil
  46. }
  47. type MpUploadOssHelper struct {
  48. AccessKeyId string
  49. AccessKeySecret string
  50. Timeout int
  51. MaxSize int
  52. }
  53. type Policy struct {
  54. Expiration string `json:"expiration"`
  55. Conditions [][]interface{} `json:"conditions"`
  56. }
  57. func NewMpUploadOssHelper(accessKeyId, accessKeySecret string, timeout, maxSize int) *MpUploadOssHelper {
  58. if timeout == 0 {
  59. timeout = 1
  60. }
  61. if maxSize == 0 {
  62. maxSize = 10
  63. }
  64. return &MpUploadOssHelper{
  65. AccessKeyId: accessKeyId,
  66. AccessKeySecret: accessKeySecret,
  67. Timeout: timeout,
  68. MaxSize: maxSize,
  69. }
  70. }
  71. func (helper *MpUploadOssHelper) CreateUploadParams() map[string]string {
  72. policy := helper.getPolicyBase64()
  73. signature := helper.signature(policy)
  74. return map[string]string{
  75. "OSSAccessKeyId": helper.AccessKeyId,
  76. "policy": policy,
  77. "signature": signature,
  78. }
  79. }
  80. func (helper *MpUploadOssHelper) getPolicyBase64() string {
  81. expiration := time.Now().Add(time.Duration(helper.Timeout) * time.Hour).UTC().Format(time.RFC3339)
  82. policy := Policy{
  83. Expiration: expiration,
  84. Conditions: [][]interface{}{
  85. {"content-length-range", 0, helper.MaxSize * 1024 * 1024},
  86. },
  87. }
  88. policyBytes, _ := json.Marshal(policy)
  89. return base64.StdEncoding.EncodeToString(policyBytes)
  90. }
  91. func (helper *MpUploadOssHelper) signature(policy string) string {
  92. h := hmac.New(sha1.New, []byte(helper.AccessKeySecret))
  93. h.Write([]byte(policy))
  94. return base64.StdEncoding.EncodeToString(h.Sum(nil))
  95. }