Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

180 lignes
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.HandleFunc("/crmcache", crmcache)
  58. http.ListenAndServe(":65500", nil)
  59. }
  60. func startSessionManager(concurrent int) {
  61. m := SessionManager{}
  62. AllInMessage = make(chan *InWechatMsg, concurrent)
  63. go m.startSessionManager(AllInMessage)
  64. }
  65. func setupRootFileServer() {
  66. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  67. http.ServeFile(w, r, GlobalPath.Angular2App+r.URL.Path[1:])
  68. })
  69. //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/ng2-admin/dist"))
  70. //http.Handle("/test", http.StripPrefix("/test", fs))
  71. //fs := http.FileServer(http.Dir("/mnt/data/workspace/angular.ts/wechat/ng2-admin/dist"))
  72. //http.Handle("/", fs)
  73. }
  74. func dumpReuestHandler(w http.ResponseWriter, r *http.Request) {
  75. logRequestDebug(httputil.DumpRequest(r, true))
  76. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  77. w.Header().Set("Access-Control-Allow-Origin", "http://192.168.1.39:4200")
  78. w.Header().Set("Access-Control-Allow-Headers", "Authorziation11,Authorziation12")
  79. w.Header().Set("Access-Control-Allow-Credentials", "true")
  80. w.Header().Set("Access-Control-Expose-Headers", "Set-Cookie,myheader,*")
  81. w.Header().Set("myheader", "myheader-data")
  82. expiration := time.Now().Add(10 * 365 * 24 * time.Hour)
  83. str := time.Now().String()
  84. cookie := http.Cookie{Name: "username", Value: str, Expires: expiration}
  85. http.SetCookie(w, &cookie)
  86. fmt.Fprintf(w, `{"status":"OK"}`)
  87. for _, c := range r.Cookies() {
  88. log.Println(c.Name)
  89. log.Println(c.Value)
  90. }
  91. }
  92. func supplyAccessToken(w http.ResponseWriter, r *http.Request) {
  93. //logRequestDebug(httputil.DumpRequest(r, true))
  94. if checkSignatureByToken(r, IntraAPIConfig.CRMSecrete) {
  95. atk, _ := GetAccessToken()
  96. fmt.Fprint(w, atk)
  97. } else {
  98. w.WriteHeader(401)
  99. fmt.Fprint(w, "unauthorized")
  100. }
  101. }
  102. func iapiCreateWechatQrCode(w http.ResponseWriter, r *http.Request) {
  103. logRequestDebug(httputil.DumpRequest(r, true))
  104. if !checkSignatureByToken(r, IntraAPIConfig.CRMSecrete) {
  105. w.WriteHeader(http.StatusUnauthorized)
  106. fmt.Fprint(w, "unauthorized")
  107. return
  108. }
  109. rq := r.URL.RawQuery
  110. m, _ := url.ParseQuery(rq)
  111. qrValue, qok := m["qrValue"]
  112. expire, eok := m["expire"]
  113. if !qok || !eok {
  114. w.WriteHeader(http.StatusUnprocessableEntity)
  115. fmt.Fprintf(w, "parameter not correct, bad api call %s", time.Now().Format(getCrmTimeLayout()))
  116. return
  117. }
  118. if isStrInt(qrValue[0]) && isStrInt(expire[0]) {
  119. intVal, _ := strconv.Atoi(qrValue[0])
  120. intExpire, _ := strconv.Atoi(expire[0])
  121. jsonB, err := iapiCreateTempQr(int32(intVal), int32(intExpire))
  122. if err == nil {
  123. w.Write(jsonB)
  124. } else {
  125. fmt.Fprintf(w, "%s", err)
  126. }
  127. return
  128. }
  129. if !isStrInt(qrValue[0]) && expire[0] == "0" {
  130. jsonB, err := isapiCreatePermQr(qrValue[0])
  131. if err == nil {
  132. w.Write(jsonB)
  133. } else {
  134. fmt.Fprintf(w, "%s", err)
  135. }
  136. return
  137. }
  138. }
  139. func isStrInt(v string) bool {
  140. if _, err := strconv.Atoi(v); err == nil {
  141. return true
  142. }
  143. return false
  144. }
  145. // 用户在网页授权页同意授权给公众号后,微信会将授权数据传给一个回调页面,回调页面需在此域名下,以确保安全可靠。
  146. // 注意事项:
  147. // 1、回调页面域名或路径需使用字母、数字及“-”的组合(例:wx.qq.com或wx.qq.com/mp),不支持IP地址、端口号及短链域名。填写的域名或路径需与实际回调URL中的域名或路径相同。
  148. // 2、填写的域名须通过ICP备案的验证。
  149. // 3、将文件MP_verify_6JqVkftKr39GMakA.txt(点击下载)上传至填写域名或路径指向的web服务器(或虚拟主机)的目录(若填写域名,将文件放置在域名根目录下,例如wx.qq.com/MP_verify_6JqVkftKr39GMakA.txt;若填写路径,将文件放置在路径目录下,例如wx.qq.com/mp/MP_verify_6JqVkftKr39GMakA.txt),并确保可以访问。
  150. func mpDomainAuthSecret(w http.ResponseWriter, r *http.Request) {
  151. fmt.Fprintf(w, "6JqVkftKr39GMakA")
  152. //由于需要什么ICP备案,这个功能不能使用
  153. }