share.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/encrypt"
  6. "app.yhyue.com/moapp/jybase/go-xweb/xweb"
  7. "fmt"
  8. "github.com/SKatiyar/qr"
  9. "jy-docs/config"
  10. "log"
  11. "net/url"
  12. )
  13. type Share struct {
  14. *xweb.Action
  15. shareUrl xweb.Mapper `xweb:"/share/url"` //地址分享
  16. shareImg xweb.Mapper `xweb:"/share/img"` //链接分享
  17. }
  18. func (share *Share) ShareUrl() {
  19. userId := common.ObjToString(share.GetSession("userId"))
  20. rData, errMsg := func() (interface{}, error) {
  21. docId := share.GetString("docId") //分享地址
  22. return map[string]interface{}{
  23. "url": getDocShareUrl(userId, docId),
  24. }, nil
  25. }()
  26. if errMsg != nil {
  27. log.Printf("%s Share err:%s\n", errMsg.Error(), userId)
  28. }
  29. share.ServeJson(NewResult(rData, errMsg))
  30. }
  31. func (share *Share) ShareImg() {
  32. userId := common.ObjToString(share.GetSession("userId"))
  33. docId := share.GetString("docId") //分享地址
  34. data := getDocShareUrl(userId, docId)
  35. w := share.ResponseWriter
  36. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  37. w.Header().Set("Pragma", "no-cache")
  38. w.Header().Set("Expires", "0")
  39. w.Header().Set("Content-Type", "image/png")
  40. r, _ := qr.Encode(data, qr.M)
  41. _, _ = w.Write(r.PNG())
  42. }
  43. func getDocShareUrl(userId, docId string) string {
  44. enUserId := encrypt.SE.Encode2Hex(userId)
  45. toHref := url.QueryEscape(fmt.Sprintf("/page_docs_app/details/%s?from=%s", docId, enUserId)) //关注跳转页面
  46. unHref := url.QueryEscape(fmt.Sprintf("/swordfish/about?from=%s", enUserId)) //为关注跳转页面
  47. log.Println(fmt.Sprintf(config.JyDocsAppConfig.ShareUrl+"?toHref=%s&unHref=%s", toHref, unHref))
  48. return fmt.Sprintf(config.JyDocsAppConfig.ShareUrl+"?toHref=%s&unHref=%s", toHref, unHref)
  49. }