广告平台(总站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

240 regels
7.6 KiB

  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"
  142. if cfg.Prd {
  143. redirectURI = "http://super.advertisement.dengbiao.top/api/wxOpen/getAuthUrlCallBack"
  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", encodedRedirectURI)
  153. query.Add("auth_type", "1")
  154. // 将查询参数附加到基础URL
  155. authURL := baseURL + "?" + query.Encode()
  156. // 设置JavaScript重定向
  157. 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. }