Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

157 lines
5.4KB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "net/http/httputil"
  7. "time"
  8. )
  9. //PathsConfig all system available pathes
  10. type PathsConfig struct {
  11. Angular2App string `json:"angular2_app"`
  12. }
  13. //TODO: setup GlobalPath Config, from reading file
  14. //GlobalPath all global pathes configurations
  15. var GlobalPath = PathsConfig{
  16. "/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist/"}
  17. //KFUsers info cache about current customer service
  18. var KFUsers kfCache
  19. func main() {
  20. err := readConfig() //wechat API config
  21. if err != nil {
  22. log.Println(err)
  23. log.Fatal("unable to read server_config.json, program quit")
  24. return
  25. }
  26. err = readCRMConfig()
  27. if err != nil {
  28. log.Println(err)
  29. log.Fatal("unable to read crm_config.json, program quit")
  30. }
  31. err = IntraAPIConfig.readConfig()
  32. if err != nil {
  33. log.Println(err)
  34. log.Fatal("unable to read intra-api-config, program quit")
  35. }
  36. initAllProc()
  37. setupRootFileServer()
  38. startSessionManager(2048)
  39. KFUsers.kfRenewList()
  40. //always the last one
  41. setupHTTPHandler()
  42. }
  43. func setupHTTPHandler() {
  44. //setup handler
  45. //http.HandleFunc("/", webrootHandler)
  46. http.HandleFunc("/api", apiV1Main)
  47. http.HandleFunc("/upload", uploadHandler)
  48. http.HandleFunc("/crmfiles/", crmAttachmentHandler)
  49. http.HandleFunc("/dumprequest", dumpReuestHandler)
  50. http.HandleFunc("/MP_verify_6JqVkftKr39GMakA.txt", mpDomainAuthSecret)
  51. http.HandleFunc("/profile_newly_register", initialRegistrationHandler)
  52. http.HandleFunc("/iapi/getAccessToken", supplyAccessToken)
  53. http.HandleFunc("/iapi/createQr", iapiCreateQrCode)
  54. http.ListenAndServe(":65500", nil)
  55. }
  56. func startSessionManager(concurrent int) {
  57. m := SessionManager{}
  58. AllInMessage = make(chan InWechatMsg, concurrent)
  59. go m.startSessionManager(AllInMessage)
  60. }
  61. func setupRootFileServer() {
  62. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  63. http.ServeFile(w, r, GlobalPath.Angular2App+r.URL.Path[1:])
  64. })
  65. //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/ng2-admin/dist"))
  66. //http.Handle("/test", http.StripPrefix("/test", fs))
  67. //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist"))
  68. //http.Handle("/", fs)
  69. }
  70. func dumpReuestHandler(w http.ResponseWriter, r *http.Request) {
  71. logRequestDebug(httputil.DumpRequest(r, true))
  72. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  73. w.Header().Set("Access-Control-Allow-Origin", "http://192.168.1.39:4200")
  74. w.Header().Set("Access-Control-Allow-Headers", "Authorziation11,Authorziation12")
  75. w.Header().Set("Access-Control-Allow-Credentials", "true")
  76. w.Header().Set("Access-Control-Expose-Headers", "Set-Cookie,myheader,*")
  77. w.Header().Set("myheader", "myheader-data")
  78. expiration := time.Now().Add(10 * 365 * 24 * time.Hour)
  79. str := time.Now().String()
  80. cookie := http.Cookie{Name: "username", Value: str, Expires: expiration}
  81. http.SetCookie(w, &cookie)
  82. fmt.Fprintf(w, `{"status":"OK"}`)
  83. for _, c := range r.Cookies() {
  84. log.Println(c.Name)
  85. log.Println(c.Value)
  86. }
  87. }
  88. func supplyAccessToken(w http.ResponseWriter, r *http.Request) {
  89. //logRequestDebug(httputil.DumpRequest(r, true))
  90. if checkSignatureByToken(r, IntraAPIConfig.CRMSecrete) {
  91. atk, _ := GetAccessToken()
  92. fmt.Fprint(w, atk)
  93. } else {
  94. w.WriteHeader(401)
  95. fmt.Fprint(w, "unauthorized")
  96. }
  97. }
  98. func iapiCreateQrCode(w http.ResponseWriter, r *http.Request) {
  99. if !checkSignatureByToken(r, IntraAPIConfig.CRMSecrete) {
  100. fmt.Fprint(w, "unauthorized")
  101. }
  102. }
  103. // 用户在网页授权页同意授权给公众号后,微信会将授权数据传给一个回调页面,回调页面需在此域名下,以确保安全可靠。
  104. // 注意事项:
  105. // 1、回调页面域名或路径需使用字母、数字及“-”的组合(例:wx.qq.com或wx.qq.com/mp),不支持IP地址、端口号及短链域名。填写的域名或路径需与实际回调URL中的域名或路径相同。
  106. // 2、填写的域名须通过ICP备案的验证。
  107. // 3、将文件MP_verify_6JqVkftKr39GMakA.txt(点击下载)上传至填写域名或路径指向的web服务器(或虚拟主机)的目录(若填写域名,将文件放置在域名根目录下,例如wx.qq.com/MP_verify_6JqVkftKr39GMakA.txt;若填写路径,将文件放置在路径目录下,例如wx.qq.com/mp/MP_verify_6JqVkftKr39GMakA.txt),并确保可以访问。
  108. func mpDomainAuthSecret(w http.ResponseWriter, r *http.Request) {
  109. fmt.Fprintf(w, "6JqVkftKr39GMakA")
  110. //由于需要什么ICP备案,这个功能不能使用
  111. }
  112. //for user's initial registration, especially for wechat users
  113. //they visit a url that is specifically designed for them to
  114. //auth and input their profile data.
  115. //the url's query string will contains a token and a signature
  116. //so that it's verified, by single get request, to allow people to
  117. //enter their details into the CRM system.
  118. //
  119. //this handler, check's the query sting ,set an auth cookie to the client
  120. //and serve angular app, through an URL "/profile/edit"
  121. //or if the user has already been registered,
  122. //redirect user to a URL "/pages/dashboard"
  123. //
  124. func initialRegistrationHandler(w http.ResponseWriter, r *http.Request) {
  125. //TODO: verify url parameters and make sure its username is correctly set
  126. //
  127. expiration := time.Now().Add(10 * 365 * 24 * time.Hour)
  128. str := time.Now().String()
  129. cookie := http.Cookie{Name: "username", Value: str, Expires: expiration}
  130. http.SetCookie(w, &cookie)
  131. cookie = http.Cookie{Name: "signature", Value: "abcee", Expires: expiration}
  132. http.SetCookie(w, &cookie)
  133. http.Redirect(w, r, "http://192.168.1.39:4200/#pages/charts/chartist-js", 307) //302 temp redirect
  134. }