广告平台(总站长使用)
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

hdl_wx_open.go 5.9 KiB

4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
4 个月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package hdl
  2. import (
  3. "applet/app/e"
  4. "applet/app/lib/wechat"
  5. "applet/app/md"
  6. "applet/app/utils"
  7. "applet/app/utils/cache"
  8. db "code.fnuoos.com/zhimeng/model.git/src"
  9. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  10. "encoding/xml"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "io/ioutil"
  14. "net/http"
  15. "net/url"
  16. )
  17. type OriginalWxMessage struct {
  18. AppID string `xml:"AppId"`
  19. Encrypt string `xml:"Encrypt"`
  20. }
  21. func SetTicket(c *gin.Context) {
  22. query := c.Request.URL.Query()
  23. var params = map[string]string{}
  24. for key, value := range query {
  25. fmt.Printf("Key: %s, Value: %s\n", key, value[0])
  26. params[key] = value[0]
  27. }
  28. utils.FilePutContents("SetTicket_Get", utils.SerializeStr(params))
  29. var originalWxMessage OriginalWxMessage
  30. // 读取请求体
  31. body, err := ioutil.ReadAll(c.Request.Body)
  32. if err != nil {
  33. c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read request body"})
  34. return
  35. }
  36. utils.FilePutContents("SetTicket_Post", string(body))
  37. err = xml.Unmarshal(body, &originalWxMessage)
  38. if err != nil {
  39. fmt.Println("setTicket>>>>>>>>", err.Error())
  40. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  41. return
  42. }
  43. //1、查找对应 wx_open_third_party_app_list 记录
  44. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  45. wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppListByAppId(originalWxMessage.AppID)
  46. if err != nil {
  47. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  48. return
  49. }
  50. if wxOpenThirdPartyAppList == nil {
  51. c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应App记录"})
  52. return
  53. }
  54. //2、对消息体进行解密
  55. instance := wechat.NewWechatMsgCrypt(wxOpenThirdPartyAppList.Token, wxOpenThirdPartyAppList.AesKey, wxOpenThirdPartyAppList.Appid)
  56. eventRequest := wechat.EventEncryptRequest{
  57. XMLName: xml.Name{},
  58. Encrypt: originalWxMessage.Encrypt,
  59. Appid: originalWxMessage.AppID,
  60. }
  61. reqWxMessage := instance.WechatEventDecrypt(eventRequest, params["msg_signature"], params["timestamp"], params["nonce"])
  62. fmt.Println("解密结果:", reqWxMessage)
  63. utils.FilePutContents("SetTicket_XML", utils.SerializeStr(reqWxMessage))
  64. if reqWxMessage.InfoType == "component_verify_ticket" { //TODO::微信公众平台 验证票据
  65. cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, utils.AnyToString(wxOpenThirdPartyAppList.Uuid))
  66. cacheComponentVerifyTicket, _ := cache.GetString(cacheKey)
  67. if cacheComponentVerifyTicket == "" || cacheComponentVerifyTicket != reqWxMessage.ComponentVerifyTicket {
  68. cache.SetEx(cacheKey, reqWxMessage.ComponentVerifyTicket, 43140)
  69. wxOpenThirdPartyAppList.ComponentVerifyTicket = reqWxMessage.ComponentVerifyTicket
  70. _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(wxOpenThirdPartyAppList, "component_verify_ticket")
  71. if err != nil {
  72. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  73. return
  74. }
  75. }
  76. }
  77. if reqWxMessage.InfoType == "unauthorized" { //TODO::微信公众平台 取消授权
  78. appid := reqWxMessage.AuthorizerAppid
  79. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  80. userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(appid)
  81. if err != nil {
  82. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  83. return
  84. }
  85. if userWxAppletList == nil {
  86. c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应小程序授权记录"})
  87. return
  88. }
  89. userWxAppletList.IsAuth = 0
  90. _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "is_auth")
  91. if err != nil {
  92. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  93. return
  94. }
  95. }
  96. if reqWxMessage.InfoType == "unauthorized" || reqWxMessage.InfoType == "authorized" { //TODO::微信公众平台 授权 || 更新授权
  97. appid := reqWxMessage.AuthorizerAppid
  98. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  99. userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(appid)
  100. if err != nil {
  101. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  102. return
  103. }
  104. if userWxAppletList == nil {
  105. c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应小程序授权记录"})
  106. return
  107. }
  108. userWxAppletList.IsAuth = 1
  109. _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "is_auth")
  110. if err != nil {
  111. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  112. return
  113. }
  114. }
  115. c.String(http.StatusOK, "success")
  116. return
  117. }
  118. func GetPreAuthCode(c *gin.Context) {
  119. masterId := c.DefaultQuery("master_id", "")
  120. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  121. wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId))
  122. if err != nil {
  123. return
  124. }
  125. if wxOpenThirdPartyAppList == nil {
  126. e.OutErr(c, e.ERR_NOT_FAN, "未查询到对应三方应用记录")
  127. return
  128. }
  129. wxApiService, err := wechat.NewWxApiService(masterId, wxOpenThirdPartyAppList.Appid, wxOpenThirdPartyAppList.AppSecret)
  130. if err != nil {
  131. e.OutErr(c, e.ERR, err.Error())
  132. return
  133. }
  134. preAuthCode, err := wxApiService.GetPreAuthCode()
  135. if err != nil {
  136. e.OutErr(c, e.ERR, err.Error())
  137. return
  138. }
  139. var w http.ResponseWriter
  140. w.Header().Set("Access-Control-Allow-Origin", "*")
  141. redirectURI := "http://super.admin.izhyin.com/Wx/getAuthUrlCallBack"
  142. // 对redirectURI进行URL编码
  143. encodedRedirectURI := url.QueryEscape(redirectURI)
  144. // 构造微信登录页面的URL
  145. baseURL := "https://mp.weixin.qq.com/cgi-bin/componentloginpage"
  146. query := url.Values{}
  147. query.Add("component_appid", wxOpenThirdPartyAppList.Appid)
  148. query.Add("pre_auth_code", preAuthCode)
  149. query.Add("redirect_uri", encodedRedirectURI)
  150. query.Add("auth_type", "1")
  151. // 将查询参数附加到基础URL
  152. urlStr := baseURL + "?" + query.Encode()
  153. // 设置JavaScript重定向
  154. fmt.Fprintf(w, `
  155. <script>
  156. window.onload = function () {
  157. window.location.href = '%s';
  158. };
  159. </script>`, urlStr)
  160. }
  161. func WechatMsgRecieve(c *gin.Context) {
  162. return
  163. }