Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

179 linhas
5.4KB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "net/http/httputil"
  7. "net/url"
  8. "strconv"
  9. "time"
  10. )
  11. //PathsConfig all system available pathes
  12. type PathsConfig struct {
  13. Angular2App string `json:"angular2_app"`
  14. }
  15. //TODO: setup GlobalPath Config, from reading file
  16. //GlobalPath all global pathes configurations
  17. var GlobalPath = PathsConfig{
  18. "/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist/"}
  19. //KFUsers info cache about current customer service
  20. var KFUsers kfCache
  21. func main() {
  22. err := readConfig() //wechat API config
  23. if err != nil {
  24. log.Println(err)
  25. log.Fatal("unable to read server_config.json, program quit")
  26. return
  27. }
  28. err = readCRMConfig()
  29. if err != nil {
  30. log.Println(err)
  31. log.Fatal("unable to read crm_config.json, program quit")
  32. }
  33. err = IntraAPIConfig.readConfig()
  34. if err != nil {
  35. log.Println(err)
  36. log.Fatal("unable to read intra-api-config, program quit")
  37. }
  38. initAllProc()
  39. setupRootFileServer()
  40. startSessionManager(2048)
  41. KFUsers.kfRenewList()
  42. //always the last one
  43. setupHTTPHandler()
  44. }
  45. func setupHTTPHandler() {
  46. //setup handler
  47. //http.HandleFunc("/", webrootHandler)
  48. http.HandleFunc("/api", apiV1Main)
  49. http.HandleFunc("/upload", uploadHandler)
  50. http.HandleFunc("/crmfiles/", crmAttachmentHandler)
  51. http.HandleFunc("/dumprequest", dumpReuestHandler)
  52. http.HandleFunc("/MP_verify_6JqVkftKr39GMakA.txt", mpDomainAuthSecret)
  53. http.HandleFunc("/redirect", setTrackingCookieAndRecirect)
  54. http.HandleFunc("/iapi/getAccessToken", supplyAccessToken)
  55. http.HandleFunc("/iapi/createWechatQr", iapiCreateWechatQrCode)
  56. http.HandleFunc("/crmpixel.png", crmpixel) //tracking pixel.
  57. http.ListenAndServe(":65500", nil)
  58. }
  59. func startSessionManager(concurrent int) {
  60. m := SessionManager{}
  61. AllInMessage = make(chan InWechatMsg, concurrent)
  62. go m.startSessionManager(AllInMessage)
  63. }
  64. func setupRootFileServer() {
  65. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  66. http.ServeFile(w, r, GlobalPath.Angular2App+r.URL.Path[1:])
  67. })
  68. //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/ng2-admin/dist"))
  69. //http.Handle("/test", http.StripPrefix("/test", fs))
  70. //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist"))
  71. //http.Handle("/", fs)
  72. }
  73. func dumpReuestHandler(w http.ResponseWriter, r *http.Request) {
  74. logRequestDebug(httputil.DumpRequest(r, true))
  75. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  76. w.Header().Set("Access-Control-Allow-Origin", "http://192.168.1.39:4200")
  77. w.Header().Set("Access-Control-Allow-Headers", "Authorziation11,Authorziation12")
  78. w.Header().Set("Access-Control-Allow-Credentials", "true")
  79. w.Header().Set("Access-Control-Expose-Headers", "Set-Cookie,myheader,*")
  80. w.Header().Set("myheader", "myheader-data")
  81. expiration := time.Now().Add(10 * 365 * 24 * time.Hour)
  82. str := time.Now().String()
  83. cookie := http.Cookie{Name: "username", Value: str, Expires: expiration}
  84. http.SetCookie(w, &cookie)
  85. fmt.Fprintf(w, `{"status":"OK"}`)
  86. for _, c := range r.Cookies() {
  87. log.Println(c.Name)
  88. log.Println(c.Value)
  89. }
  90. }
  91. func supplyAccessToken(w http.ResponseWriter, r *http.Request) {
  92. //logRequestDebug(httputil.DumpRequest(r, true))
  93. if checkSignatureByToken(r, IntraAPIConfig.CRMSecrete) {
  94. atk, _ := GetAccessToken()
  95. fmt.Fprint(w, atk)
  96. } else {
  97. w.WriteHeader(401)
  98. fmt.Fprint(w, "unauthorized")
  99. }
  100. }
  101. func iapiCreateWechatQrCode(w http.ResponseWriter, r *http.Request) {
  102. logRequestDebug(httputil.DumpRequest(r, true))
  103. if !checkSignatureByToken(r, IntraAPIConfig.CRMSecrete) {
  104. w.WriteHeader(http.StatusUnauthorized)
  105. fmt.Fprint(w, "unauthorized")
  106. return
  107. }
  108. rq := r.URL.RawQuery
  109. m, _ := url.ParseQuery(rq)
  110. qrValue, qok := m["qrValue"]
  111. expire, eok := m["expire"]
  112. if !qok || !eok {
  113. w.WriteHeader(http.StatusUnprocessableEntity)
  114. fmt.Fprintf(w, "parameter not correct, bad api call %s", time.Now().Format(getCrmTimeLayout()))
  115. return
  116. }
  117. if isStrInt(qrValue[0]) && isStrInt(expire[0]) {
  118. intVal, _ := strconv.Atoi(qrValue[0])
  119. intExpire, _ := strconv.Atoi(expire[0])
  120. jsonB, err := iapiCreateTempQr(int32(intVal), int32(intExpire))
  121. if err == nil {
  122. w.Write(jsonB)
  123. } else {
  124. fmt.Fprintf(w, "%s", err)
  125. }
  126. return
  127. }
  128. if !isStrInt(qrValue[0]) && expire[0] == "0" {
  129. jsonB, err := isapiCreatePermQr(qrValue[0])
  130. if err == nil {
  131. w.Write(jsonB)
  132. } else {
  133. fmt.Fprintf(w, "%s", err)
  134. }
  135. return
  136. }
  137. }
  138. func isStrInt(v string) bool {
  139. if _, err := strconv.Atoi(v); err == nil {
  140. return true
  141. }
  142. return false
  143. }
  144. // 用户在网页授权页同意授权给公众号后,微信会将授权数据传给一个回调页面,回调页面需在此域名下,以确保安全可靠。
  145. // 注意事项:
  146. // 1、回调页面域名或路径需使用字母、数字及“-”的组合(例:wx.qq.com或wx.qq.com/mp),不支持IP地址、端口号及短链域名。填写的域名或路径需与实际回调URL中的域名或路径相同。
  147. // 2、填写的域名须通过ICP备案的验证。
  148. // 3、将文件MP_verify_6JqVkftKr39GMakA.txt(点击下载)上传至填写域名或路径指向的web服务器(或虚拟主机)的目录(若填写域名,将文件放置在域名根目录下,例如wx.qq.com/MP_verify_6JqVkftKr39GMakA.txt;若填写路径,将文件放置在路径目录下,例如wx.qq.com/mp/MP_verify_6JqVkftKr39GMakA.txt),并确保可以访问。
  149. func mpDomainAuthSecret(w http.ResponseWriter, r *http.Request) {
  150. fmt.Fprintf(w, "6JqVkftKr39GMakA")
  151. //由于需要什么ICP备案,这个功能不能使用
  152. }