weixin.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package weixin
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "zhiyuan/pkg/app"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/pkg/weixin/mp"
  9. "zhiyuan/services/admin"
  10. "zhiyuan/services/user"
  11. "zhiyuan/services/work/worker"
  12. "github.com/gin-gonic/gin"
  13. )
  14. func GetAccessToken(c *gin.Context) {
  15. id := utils.StrTo(c.Param("id")).MustInt()
  16. if id <= 0 {
  17. app.ErrorMsg(c, "微信id有误", nil)
  18. return
  19. }
  20. client, err := mp.NewClient(id)
  21. if err != nil {
  22. app.Error(c, err.Error())
  23. return
  24. }
  25. response := client.RefreshAccessToken()
  26. if response.ErrMsg != "" {
  27. app.ErrorMsg(c, response.ErrMsg, nil)
  28. return
  29. }
  30. app.Success(c, gin.H{
  31. "access_token": response.AccessToken,
  32. "expires_in": response.ExpiresIn,
  33. })
  34. }
  35. func GetJsapiTicket(c *gin.Context) {
  36. id := utils.StrTo(c.Param("id")).MustInt()
  37. if id <= 0 {
  38. app.ErrorMsg(c, "微信id有误", nil)
  39. return
  40. }
  41. client, err := mp.NewClient(id)
  42. if err != nil {
  43. app.Error(c, err.Error())
  44. return
  45. }
  46. response := client.RefreshJsapiTicket()
  47. if response.ErrMsg != "" {
  48. app.ErrorMsg(c, response.ErrMsg, nil)
  49. return
  50. }
  51. app.Success(c, gin.H{
  52. "ticket": response.Ticket,
  53. "expires_in": response.ExpiresIn,
  54. })
  55. }
  56. func GetJsapiParam(c *gin.Context) {
  57. id := utils.StrTo(c.Param("id")).MustInt()
  58. if id <= 0 {
  59. app.ErrorMsg(c, "微信id有误", nil)
  60. return
  61. }
  62. url := c.Query("url")
  63. if url == "" {
  64. app.ErrorMsg(c, "url 不能为空", nil)
  65. return
  66. }
  67. client, err := mp.NewClient(id)
  68. if err != nil {
  69. app.Error(c, err.Error())
  70. return
  71. }
  72. if res, err := client.GetJsapiParam(url); err == nil {
  73. app.Success(c, res)
  74. return
  75. } else {
  76. app.Error(c, err.Error())
  77. return
  78. }
  79. }
  80. func GetOauthUrl(c *gin.Context) {
  81. id := utils.StrTo(c.Param("id")).MustInt()
  82. if id <= 0 {
  83. app.ErrorMsg(c, "微信id有误", nil)
  84. return
  85. }
  86. client, err := mp.NewClient(id)
  87. if err != nil {
  88. app.Error(c, err.Error())
  89. return
  90. }
  91. url, err := client.GetOauthUrl(c.Query("scope"), c.Query("url"))
  92. if err != nil {
  93. app.Error(c, err.Error())
  94. return
  95. }
  96. app.Success(c, url)
  97. }
  98. func GetUserInfo(c *gin.Context) {
  99. id := utils.StrTo(c.Param("id")).MustInt()
  100. if id <= 0 {
  101. app.ErrorMsg(c, "微信id有误", nil)
  102. return
  103. }
  104. code := c.Query("code")
  105. if code == "" {
  106. app.ErrorMsg(c, "code 不能为空", nil)
  107. return
  108. }
  109. if code == "1" {
  110. app.Success(c, "test")
  111. return
  112. }
  113. client, err := mp.NewClient(id)
  114. if err != nil {
  115. app.Error(c, err.Error())
  116. return
  117. }
  118. response := client.GetOauthToken(code)
  119. if response.ErrMsg != "" {
  120. app.ErrorMsg(c, response.ErrMsg, nil)
  121. return
  122. }
  123. if c.Query("scope") == "snsapi_base" {
  124. app.Success(c, response.OpenID)
  125. return
  126. }
  127. userInfo := client.GetUserInfo(response.AccessToken, response.OpenID)
  128. if response.ErrMsg != "" {
  129. app.ErrorMsg(c, response.ErrMsg, nil)
  130. return
  131. }
  132. if user, _ := user.GetOne(map[string]interface{}{"openid": response.OpenID}, nil, nil); user != nil {
  133. userInfo.IsExists = true
  134. }
  135. app.Success(c, userInfo)
  136. }
  137. func GetAdminInfo(c *gin.Context) {
  138. id := utils.StrTo(c.Param("id")).MustInt()
  139. if id <= 0 {
  140. app.ErrorMsg(c, "微信id有误", nil)
  141. return
  142. }
  143. code := c.Query("code")
  144. if code == "" {
  145. app.ErrorMsg(c, "code 不能为空", nil)
  146. return
  147. }
  148. if code == "1" {
  149. app.Success(c, "test")
  150. return
  151. }
  152. client, err := mp.NewClient(id)
  153. if err != nil {
  154. app.Error(c, err.Error())
  155. return
  156. }
  157. response := client.GetOauthToken(code)
  158. if response.ErrMsg != "" {
  159. app.ErrorMsg(c, response.ErrMsg, nil)
  160. return
  161. }
  162. if c.Query("scope") == "snsapi_base" {
  163. app.Success(c, response.OpenID)
  164. return
  165. }
  166. userInfo := client.GetUserInfo(response.AccessToken, response.OpenID)
  167. if response.ErrMsg != "" {
  168. app.ErrorMsg(c, response.ErrMsg, nil)
  169. return
  170. }
  171. if admin, _ := admin.GetOne(map[string]interface{}{"openid": response.OpenID}, nil, nil); admin != nil {
  172. userInfo.IsExists = true
  173. }
  174. app.Success(c, userInfo)
  175. }
  176. func GetWorkerInfo(c *gin.Context) {
  177. id := utils.StrTo(c.Param("id")).MustInt()
  178. if id <= 0 {
  179. app.ErrorMsg(c, "微信id有误", nil)
  180. return
  181. }
  182. code := c.Query("code")
  183. if code == "" {
  184. app.ErrorMsg(c, "code 不能为空", nil)
  185. return
  186. }
  187. if code == "1" {
  188. app.Success(c, "test")
  189. return
  190. }
  191. client, err := mp.NewClient(id)
  192. if err != nil {
  193. app.Error(c, err.Error())
  194. return
  195. }
  196. response := client.GetOauthToken(code)
  197. if response.ErrMsg != "" {
  198. app.ErrorMsg(c, response.ErrMsg, nil)
  199. return
  200. }
  201. if c.Query("scope") == "snsapi_base" {
  202. app.Success(c, response.OpenID)
  203. return
  204. }
  205. userInfo := client.GetUserInfo(response.AccessToken, response.OpenID)
  206. if response.ErrMsg != "" {
  207. app.ErrorMsg(c, response.ErrMsg, nil)
  208. return
  209. }
  210. if worker, _ := worker.GetOne(map[string]interface{}{"openid": response.OpenID}, nil, nil); worker != nil {
  211. userInfo.IsExists = true
  212. }
  213. app.Success(c, userInfo)
  214. }
  215. func RedictShop(c *gin.Context) {
  216. backUrl := "/"
  217. if c.Query("backUrl") != "" {
  218. backUrl = c.Query("backUrl")
  219. }
  220. if strings.Contains(backUrl, "?") {
  221. backUrl += "&"
  222. } else {
  223. backUrl += "?"
  224. }
  225. baseUrl := "https://shop.nczyzs.com%sscope=%s&back_url=%s&code=%s&state=%s"
  226. url := fmt.Sprintf(baseUrl, backUrl, c.Query("scope"), c.Query("back_url"), c.Query("code"), c.Query("state"))
  227. c.Redirect(http.StatusMovedPermanently, url)
  228. }