广告平台(总站长使用)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

hdl_wx_open.go 7.6 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ヶ月前
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ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package hdl
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/e"
  5. "applet/app/lib/wechat"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. db "code.fnuoos.com/zhimeng/model.git/src"
  10. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  11. "encoding/xml"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. "io/ioutil"
  15. "net/http"
  16. "net/url"
  17. )
  18. type OriginalWxMessage struct {
  19. AppID string `xml:"AppId"`
  20. Encrypt string `xml:"Encrypt"`
  21. }
  22. func SetTicket(c *gin.Context) {
  23. query := c.Request.URL.Query()
  24. var params = map[string]string{}
  25. for key, value := range query {
  26. fmt.Printf("Key: %s, Value: %s\n", key, value[0])
  27. params[key] = value[0]
  28. }
  29. utils.FilePutContents("SetTicket_Get", utils.SerializeStr(params))
  30. var originalWxMessage OriginalWxMessage
  31. // 读取请求体
  32. body, err := ioutil.ReadAll(c.Request.Body)
  33. if err != nil {
  34. c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read request body"})
  35. return
  36. }
  37. utils.FilePutContents("SetTicket_Post", string(body))
  38. err = xml.Unmarshal(body, &originalWxMessage)
  39. if err != nil {
  40. fmt.Println("setTicket>>>>>>>>", err.Error())
  41. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  42. return
  43. }
  44. //1、查找对应 wx_open_third_party_app_list 记录
  45. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  46. wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppListByAppId(originalWxMessage.AppID)
  47. if err != nil {
  48. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  49. return
  50. }
  51. if wxOpenThirdPartyAppList == nil {
  52. c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应App记录"})
  53. return
  54. }
  55. //2、对消息体进行解密
  56. instance := wechat.NewWechatMsgCrypt(wxOpenThirdPartyAppList.Token, wxOpenThirdPartyAppList.AesKey, wxOpenThirdPartyAppList.Appid)
  57. eventRequest := wechat.EventEncryptRequest{
  58. XMLName: xml.Name{},
  59. Encrypt: originalWxMessage.Encrypt,
  60. Appid: originalWxMessage.AppID,
  61. }
  62. reqWxMessage := instance.WechatEventDecrypt(eventRequest, params["msg_signature"], params["timestamp"], params["nonce"])
  63. fmt.Println("解密结果:", reqWxMessage)
  64. utils.FilePutContents("SetTicket_XML", utils.SerializeStr(reqWxMessage))
  65. if reqWxMessage.InfoType == "component_verify_ticket" { //TODO::微信公众平台 验证票据
  66. cacheKey := fmt.Sprintf(md.MasterComponentVerifyTicket, utils.AnyToString(wxOpenThirdPartyAppList.Uuid))
  67. cacheComponentVerifyTicket, _ := cache.GetString(cacheKey)
  68. if cacheComponentVerifyTicket == "" || cacheComponentVerifyTicket != reqWxMessage.ComponentVerifyTicket {
  69. cache.SetEx(cacheKey, reqWxMessage.ComponentVerifyTicket, 43140)
  70. wxOpenThirdPartyAppList.ComponentVerifyTicket = reqWxMessage.ComponentVerifyTicket
  71. _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(wxOpenThirdPartyAppList, "component_verify_ticket")
  72. if err != nil {
  73. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  74. return
  75. }
  76. }
  77. }
  78. if reqWxMessage.InfoType == "unauthorized" { //TODO::微信公众平台 取消授权
  79. appid := reqWxMessage.AuthorizerAppid
  80. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  81. userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(appid)
  82. if err != nil {
  83. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  84. return
  85. }
  86. if userWxAppletList == nil {
  87. c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应小程序授权记录"})
  88. return
  89. }
  90. userWxAppletList.IsAuth = 0
  91. _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "is_auth")
  92. if err != nil {
  93. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  94. return
  95. }
  96. }
  97. if reqWxMessage.InfoType == "unauthorized" || reqWxMessage.InfoType == "authorized" { //TODO::微信公众平台 授权 || 更新授权
  98. appid := reqWxMessage.AuthorizerAppid
  99. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  100. userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(appid)
  101. if err != nil {
  102. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  103. return
  104. }
  105. if userWxAppletList == nil {
  106. c.JSON(http.StatusBadRequest, gin.H{"error": "未查询到对应小程序授权记录"})
  107. return
  108. }
  109. userWxAppletList.IsAuth = 1
  110. _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "is_auth")
  111. if err != nil {
  112. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  113. return
  114. }
  115. }
  116. c.String(http.StatusOK, "success")
  117. return
  118. }
  119. func GetPreAuthCode(c *gin.Context) {
  120. masterId := c.DefaultQuery("master_id", "")
  121. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  122. wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId))
  123. if err != nil {
  124. return
  125. }
  126. if wxOpenThirdPartyAppList == nil {
  127. e.OutErr(c, e.ERR_NOT_FAN, "未查询到对应三方应用记录")
  128. return
  129. }
  130. wxApiService, err := wechat.NewWxApiService(masterId, wxOpenThirdPartyAppList.Appid, wxOpenThirdPartyAppList.AppSecret)
  131. if err != nil {
  132. e.OutErr(c, e.ERR, err.Error())
  133. return
  134. }
  135. preAuthCode, err := wxApiService.GetPreAuthCode()
  136. if err != nil {
  137. e.OutErr(c, e.ERR, err.Error())
  138. return
  139. }
  140. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  141. redirectURI := "http://super.advertisement.dengbiao.top/api/wxOpen/getAuthUrlCallBack?masterId=" + masterId
  142. if cfg.Prd {
  143. redirectURI = "http://www.baidu.com/api/wxOpen/getAuthUrlCallBack?masterId=" + masterId
  144. }
  145. // 对redirectURI进行URL编码
  146. //encodedRedirectURI := url.QueryEscape(redirectURI)
  147. // 构造微信登录页面的URL
  148. baseURL := "https://mp.weixin.qq.com/cgi-bin/componentloginpage"
  149. query := url.Values{}
  150. query.Add("component_appid", wxOpenThirdPartyAppList.Appid)
  151. query.Add("pre_auth_code", preAuthCode)
  152. query.Add("redirect_uri", redirectURI)
  153. query.Add("auth_type", "2")
  154. // 将查询参数附加到基础URL
  155. authURL := baseURL + "?" + query.Encode()
  156. // 设置JavaScript重定向
  157. t ad c.Header("Content-Type", "text/html") //TODO::必须指定相应内容为 text/html, 否则浏览器不能正确解析
  158. c.String(200, `
  159. <script>
  160. window.onload = function () {
  161. window.location.href = '%s';
  162. };
  163. </script>`, authURL)
  164. }
  165. func GetAuthUrlCallBack(c *gin.Context) {
  166. authCode := c.DefaultQuery("auth_code", "")
  167. if authCode == "" {
  168. e.OutErr(c, e.ERR_NOT_FAN, "auth_code获取失败")
  169. return
  170. }
  171. fmt.Println("auth_code>>>>>>>>", authCode)
  172. masterId := c.DefaultQuery("masterId", "")
  173. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  174. wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId))
  175. if err != nil {
  176. return
  177. }
  178. if wxOpenThirdPartyAppList == nil {
  179. e.OutErr(c, e.ERR_NOT_FAN, "未查询到对应三方应用记录")
  180. return
  181. }
  182. wxApiService, err := wechat.NewWxApiService(masterId, wxOpenThirdPartyAppList.Appid, wxOpenThirdPartyAppList.AppSecret)
  183. if err != nil {
  184. e.OutErr(c, e.ERR, err.Error())
  185. return
  186. }
  187. resp, err := wxApiService.GetAuthorizerAccessTokenByAuthCode(authCode)
  188. if err != nil {
  189. e.OutErr(c, e.ERR, err.Error())
  190. return
  191. }
  192. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  193. userWxAppletList, err := userWxAppletListDb.GetUserWxAppletListByAppId(resp.AuthorizationInfo.AuthorizerAppid)
  194. if err != nil {
  195. return
  196. }
  197. if userWxAppletList == nil {
  198. e.OutErr(c, e.ERR_NOT_FAN, "未查询到小程序应用记录")
  199. return
  200. }
  201. userWxAppletList.AuthorizerRefreshToken = resp.AuthorizationInfo.AuthorizerRefreshToken
  202. _, err = userWxAppletListDb.UpdateUserWxAppletList(userWxAppletList, "authorizer_refresh_token")
  203. if err != nil {
  204. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  205. return
  206. }
  207. c.String(http.StatusOK, "ok")
  208. return
  209. }
  210. func WechatMsgRecieve(c *gin.Context) {
  211. return
  212. }