Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

124 Zeilen
4.1KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. )
  7. //QRPermanentScene 生成永久二维码的信息
  8. type QRPermanentScene struct {
  9. Name string `json:"action_name"`
  10. Info struct {
  11. Scene struct {
  12. ID int32 `json:"scene_id,omitempty"`
  13. IDstr string `json:"scene_str,omitempty"` //对于永久二维码,我们使用 字符串, 用来区分 数字为主的 临时二维码
  14. } `json:"scene"`
  15. } `json:"action_info"`
  16. }
  17. //QRTmpScene 生成临时二维码的信息
  18. type QRTmpScene struct { //only valid for 30 days
  19. Name string `json:"action_name"` // 'QR_SCENE'
  20. Expire int32 `json:"expire_seconds"` //max 2592000 = 30 days
  21. Info struct {
  22. Scene struct {
  23. ID int32 `json:"scene_id"`
  24. } `json:"scene"`
  25. } `json:"action_info"`
  26. }
  27. //QRSrcInfo QrCode Information
  28. type QRSrcInfo struct {
  29. Ticket string `json:"ticket"`
  30. URL string `json:"url"`
  31. Expire int64 `json:"expire_seconds"`
  32. }
  33. //CreateProfileEditQR give user a temp password to login
  34. //for editing one's own profile
  35. func CreateProfileEditQR() (result QRSrcInfo, err error) {
  36. return createPermanentQR("edit_profile")
  37. }
  38. func createPermanentQR(value string) (result QRSrcInfo, err error) {
  39. // http请求方式: POST
  40. // URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json
  41. // POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}
  42. // 或者也可以使用以下POST数据创建字符串形式的二维码参数:
  43. // {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "123"}}}
  44. u := getURL4CreateQR()
  45. s := QRPermanentScene{}
  46. //s.Name = "QR_LIMIT_SCENE" //use integer
  47. s.Name = "QR_LIMIT_STR_SCENE" //use string
  48. s.Info.Scene.IDstr = value
  49. j, _ := json.Marshal(s)
  50. log.Println(string(j))
  51. resp, err := postJSON(j, u)
  52. if err != nil {
  53. return
  54. } //edit_profile gQFR8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyZnNRMVJmZ3VjU2gxMDAwME0wN28AAgTQaSVZAwQAAAAA
  55. //{"ticket":"gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02K_TnQ9gucSh10000M03N"}
  56. //image: https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA
  57. err = json.Unmarshal([]byte(resp), &result)
  58. return
  59. }
  60. func getURL4CreateQR() (URL string) {
  61. atk, _ := GetAccessToken()
  62. u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", atk)
  63. log.Println(u)
  64. return u
  65. }
  66. //CreateTempQr Create a temp QR code
  67. func CreateTempQr(sceneID int32, expire int32) (result QRSrcInfo, err error) {
  68. // http请求方式: POST
  69. // URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json
  70. // POST数据例子:{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
  71. u := getURL4CreateQR()
  72. s := QRTmpScene{}
  73. s.Name = "QR_SCENE" //
  74. s.Info.Scene.ID = sceneID
  75. if expire <= 0 || expire >= 259200 {
  76. log.Println("invalid expire time for Temp QR code, set to 30days instead")
  77. s.Expire = 2592000
  78. } else {
  79. s.Expire = expire
  80. }
  81. j, _ := json.Marshal(s)
  82. log.Println(string(j))
  83. resp, err := postJSON(j, u)
  84. if err != nil {
  85. return
  86. }
  87. //{"ticket":"gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02K_TnQ9gucSh10000M03N"}
  88. //image: https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA
  89. err = json.Unmarshal([]byte(resp), &result)
  90. return
  91. }
  92. func qrImgByTicket(ticket string) (url string) {
  93. return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket
  94. }
  95. func iapiCreateTempQr(qr, expire int32) (jsonB []byte, err error) {
  96. if expire <= 0 {
  97. expire = 30 //30 temp seconds only for 0 value
  98. }
  99. info, err := CreateTempQr(qr, expire)
  100. if err == nil {
  101. jsonB, err = json.Marshal(info)
  102. }
  103. return
  104. }
  105. func isapiCreatePermQr(qr string) (jsonB []byte, err error) {
  106. info, err := createPermanentQR(qr)
  107. if err == nil {
  108. jsonB, err = json.Marshal(info)
  109. }
  110. return
  111. }