share.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package servers
  2. import (
  3. . "app.yhyue.com/moapp/jybase/api"
  4. "app.yhyue.com/moapp/jybase/common"
  5. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  6. "fmt"
  7. "github.com/SKatiyar/qr"
  8. "jy-docs/config"
  9. "log"
  10. )
  11. type Share struct {
  12. *xweb.Action
  13. shareUrl xweb.Mapper `xweb:"/share/url"` //地址分享
  14. shareImg xweb.Mapper `xweb:"/share/img"` //链接分享
  15. }
  16. func (share *Share) ShareUrl() {
  17. userId := common.ObjToString(share.GetSession("userId"))
  18. rData, errMsg := func() (interface{}, error) {
  19. docId := share.GetString("docId") //分享地址
  20. return map[string]interface{}{
  21. "url": getShareUrl(userId, docId),
  22. }, nil
  23. }()
  24. if errMsg != nil {
  25. log.Printf("%s Share err:%s\n", errMsg.Error(), userId)
  26. }
  27. share.ServeJson(NewResult(rData, errMsg))
  28. }
  29. func (share *Share) ShareImg() {
  30. userId := common.ObjToString(share.GetSession("userId"))
  31. docId := share.GetString("docId") //分享地址
  32. data := getShareUrl(userId, docId)
  33. w := share.ResponseWriter
  34. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  35. w.Header().Set("Pragma", "no-cache")
  36. w.Header().Set("Expires", "0")
  37. w.Header().Set("Content-Type", "image/png")
  38. r, _ := qr.Encode(data, qr.M)
  39. _, _ = w.Write(r.PNG())
  40. }
  41. func getShareUrl(userId, docId string) string {
  42. return fmt.Sprintf(config.JyDocsAppConfig.ShareUrl+"?userId=%s", docId, userId)
  43. }