router.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package router
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/frame/g"
  5. "github.com/gogf/gf/net/ghttp"
  6. "github.com/gogf/gf/os/gtime"
  7. _ "github.com/lin-jim-leon/kuaishou/open/merchant"
  8. "github.com/lin-jim-leon/kuaishou/open/oauth"
  9. _ "github.com/lin-jim-leon/kuaishou/open/oauth"
  10. "github.com/lin-jim-leon/kuaishou/open/user"
  11. _ "github.com/lin-jim-leon/kuaishou/open/user"
  12. youngeetalentapi "youngmini_server/app/api/youngee_talent_api"
  13. "youngmini_server/app/model/youngee_talent_model"
  14. "youngmini_server/app/system/assignment"
  15. "youngmini_server/app/system/sectask"
  16. "youngmini_server/app/system/wxpay"
  17. "youngmini_server/middleware"
  18. )
  19. // MiddlewareCORS 允许跨域请求中间件 所有的路由都会经过这个中间件
  20. func MiddlewareCORS(r *ghttp.Request) {
  21. corsOptions := r.Response.DefaultCORSOptions()
  22. corsOptions.AllowHeaders = "Authorization,Content-Length,X-CSRF-Token,Token,session,X_Requested_With,Accept,Origin,Host,Connection,Accept-Encoding,Accept-Language,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Pragma"
  23. corsOptions.ExposeHeaders = "Content-Length,Access-Control-Allow-Origin,Access-Control-Allow-Headers,Content-Language,Expires,Last-Modified,Pragma,FooBar"
  24. corsOptions.MaxAge = 1728000
  25. //corsOptions := ghttp.CORSOptions{
  26. //AllowOrigin: "*",
  27. //AllowMethods: "POST,GET,OPTIONS,PUT,DELETE,UPDATE",
  28. //AllowCredentials: "false",
  29. //MaxAge: 1728000,
  30. //AllowHeaders: "Authorization,Content-Length,X-CSRF-Token,Token,session,X_Requested_With,Accept,Origin,Host,Connection,Accept-Encoding,Accept-Language,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Pragma",
  31. //ExposeHeaders: "Content-Length,Access-Control-Allow-Origin,Access-Control-Allow-Headers,Content-Language,Expires,Last-Modified,Pragma,FooBar",
  32. //}
  33. //corsOptions.AllowDomain = []string{"goframe.org", "johng.cn"}
  34. //corsOptions.AllowHeaders = "Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,X-Forwarded-*,Pragma,Cache-Control,If-Modified-Since,userToken,appid"
  35. r.Response.CORS(corsOptions)
  36. r.Middleware.Next()
  37. }
  38. // MiddlewareAuth 用户认证中间件
  39. //
  40. // func MiddlewareAuth(r *ghttp.Request) {
  41. // //排除路径
  42. // array := garray.NewFrom(g.Slice{"/admin/account"})
  43. // if array.Contains(r.RequestURI) == false {
  44. // userData, err := backstage_service.Token.AuthorizationGetUserData(r)
  45. // if err != nil {
  46. // r.Response.WriteStatus(http.StatusForbidden)
  47. // response.JsonExit(r, http.StatusForbidden, err.Error())
  48. // }
  49. // if userData == nil {
  50. // r.Response.WriteStatus(http.StatusUnauthorized)
  51. // response.JsonExit(r, http.StatusForbidden, "用户不存在")
  52. // }
  53. // }
  54. // // 中间件处理逻辑
  55. // r.Middleware.Next()
  56. // }
  57. //
  58. // 用于存数据库的验证信息
  59. // json用于序列化和反序列化
  60. // MiddlewareSuperAuth 超级管理员可以操作的登录接口
  61. //
  62. // func MiddlewareSuperAuth(r *ghttp.Request) {
  63. // // 排除路径
  64. // userData, _ := backstage_service.Token.AuthorizationGetUserData(r)
  65. // if userData.User != "10000000" {
  66. // r.Response.WriteStatus(http.StatusPaymentRequired)
  67. // response.JsonExit(r, http.StatusPaymentRequired, "没有权限操作")
  68. // }
  69. // // 中间件处理逻辑
  70. // r.Middleware.Next()
  71. // }
  72. func init() {
  73. s := g.Server()
  74. // 解决跨域
  75. s.BindMiddleware("/*", MiddlewareCORS)
  76. // v2小程序端接口
  77. s.Group("/youngee/c", func(group *ghttp.RouterGroup) {
  78. group.Middleware(middleware.ErrorHandler)
  79. //group.GET("/ping", func(r *ghttp.Request)才表示/youngee/c/ping生效
  80. //s.BindHandler("/hello", func(r *ghttp.Request) {
  81. // ClientKey := "ks651333097154138217"
  82. // //ClientSecret := "dBt0rVRhTpUqcrOYGGpv0A"
  83. // SignSecret := "bf6393dce0a2b669ee348bebb837b0da"
  84. // access_token := "ChFvYXV0aC5hY2Nlc3NUb2tlbhJg3bHg2fx4q7DC3NDjTerISLTMV8M3OSj8lZMVG3oF37XapgezRims_9gq8pkNKlTRi3Vtwjd7tGbUbBN8pLNVBZaJbkig2IPKHswsuGZwC4uHMwiVkAFeSJRt7gJwec4jGhI4jxc1xH1K15kfx1GQn1SwLvsiID1v9alK2K-YywCzsIW3v7WIjFLGRfEqyt9s2m3VOaGXKAUwAQ"
  85. // // 计算七天前的时间戳(毫秒)
  86. // DaysAgo := time.Now().AddDate(0, 0, -7) // 91天前
  87. // beginTime := DaysAgo.UnixNano() / int64(time.Millisecond) // 转换为毫秒
  88. // endTime := time.Now().UnixNano() / int64(time.Millisecond) // 转换为毫秒
  89. // // 计算当前时间戳(毫秒)
  90. // cpsOrderStatus := 0 // 假设的值
  91. // pageSize := 10 // 假设的值
  92. // corlist, _ := merchant.Corderlist(ClientKey, SignSecret, access_token, cpsOrderStatus, pageSize, beginTime, endTime)
  93. // err := r.Response.WriteJson(corlist)
  94. // if err != nil {
  95. // panic("write response error")
  96. // }
  97. //})
  98. //nignx转发含code的请求到此处
  99. s.BindHandler("/kuaishouauth", func(r *ghttp.Request) {
  100. fmt.Println("****进入rounter中的/kuaishouauth*******")
  101. ClientKey := "ks651333097154138217"
  102. ClientSecret := "dBt0rVRhTpUqcrOYGGpv0A"
  103. //SignSecret := "bf6393dce0a2b669ee348bebb837b0da"
  104. code := r.GetString("code")
  105. state := r.GetString("state")
  106. //如果该达人(state)已经有数据了,删除。模拟在快手刷新授权 没有数据则空操作
  107. _, err := g.DB().Model("platform_kuaishou_user_info").
  108. Where("talent_id = ? ", state).
  109. Delete()
  110. if err != nil {
  111. r.Response.WriteJson("platform_kuaishou_user_info delete failed")
  112. }
  113. //获取accesstoken
  114. res_auth, _ := oauth.GetAccessToken(ClientKey, ClientSecret, code)
  115. AccessToken := res_auth.AccessToken
  116. //获取基本信息
  117. res_info, _ := user.GetUserinfo(ClientKey, AccessToken)
  118. //30天销量不在扫码时获取。
  119. err_auth := r.Response.WriteJson(res_auth)
  120. err_info := r.Response.WriteJson(res_info)
  121. if err_auth != nil {
  122. panic("write auth_response error")
  123. }
  124. if err_info != nil {
  125. panic("write auth_response error")
  126. }
  127. if res_auth.Result != 1 {
  128. panic("授权结果出错了")
  129. }
  130. if res_info.Result != 1 {
  131. panic("获取用户信息出错了")
  132. }
  133. //auth中含有最终信息
  134. authInfo := &youngee_talent_model.KuaishouUserInfo{
  135. Code: code,
  136. TalentId: state,
  137. AccessToken: AccessToken,
  138. OpenId: res_auth.OpenId,
  139. PlatformId: 4, //快手平台
  140. RefreshToken: res_auth.RefreshToken,
  141. HeadUri: res_info.Data.Head,
  142. NickName: res_info.Data.Name,
  143. Fan: res_info.Data.Fan,
  144. UpdateTime: gtime.Now(),
  145. CreateTime: gtime.Now(),
  146. }
  147. if _, err := g.DB().Model("platform_kuaishou_user_info").Save(authInfo); err != nil {
  148. r.Response.WriteJson(g.Map{
  149. "error": err.Error(),
  150. })
  151. return
  152. }
  153. })
  154. // 达人不需要鉴权的接口
  155. group.GET("/g", youngeetalentapi.TalentGetApi)
  156. group.POST("/p", youngeetalentapi.TalentPostApi)
  157. // 达人需要鉴权的接口
  158. group.Group("/t", func(group *ghttp.RouterGroup) {
  159. group.Middleware(middleware.DuplicateVerify, middleware.WxAuth)
  160. group.GET("/g", youngeetalentapi.TalentAuthGetApi)
  161. group.POST("/p", youngeetalentapi.TalentAuthPostApi)
  162. })
  163. })
  164. // v2小程序端接口
  165. s.Group("/youngee/c/api", func(group *ghttp.RouterGroup) {
  166. group.Middleware(middleware.ErrorHandler)
  167. //// 达人不需要鉴权的接口
  168. //group.GET("/g", youngeetalentapi.TalentGetApi)
  169. //group.POST("/p", youngeetalentapi.TalentPostApi)
  170. group.Group("/selection", func(group *ghttp.RouterGroup) {
  171. group.GET("/list", sectask.SecTask.List)
  172. })
  173. // 达人需要鉴权的接口
  174. group.Group("/t", func(group *ghttp.RouterGroup) {
  175. group.Middleware(middleware.DuplicateVerify, middleware.WxAuth)
  176. group.Group("/secTask", func(group *ghttp.RouterGroup) {
  177. group.GET("/list", sectask.SecTask.List)
  178. group.GET("/list-tab", sectask.SecTask.ListTab)
  179. group.GET("/logistics/detail", sectask.SecTask.ShowLogisticsDetail)
  180. })
  181. group.Group("/assignment", func(group *ghttp.RouterGroup) {
  182. group.GET("/list", assignment.Assignment.List)
  183. group.POST("/add", assignment.Assignment.Add)
  184. })
  185. group.Group("/wxpay", func(group *ghttp.RouterGroup) {
  186. group.POST("/pay", wxpay.WxPay.WxPay)
  187. })
  188. })
  189. })
  190. }