選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

184 行
5.5KB

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