Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

141 rinda
3.7KB

  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strings"
  11. )
  12. func spaEditProfile(w http.ResponseWriter, r *http.Request) {
  13. switch r.Method {
  14. case "GET":
  15. lead, found := getLeadFromRequest(r)
  16. if !found {
  17. response404Handler(w)
  18. // w.WriteHeader(http.StatusUnauthorized)
  19. // fmt.Fprintf(w, "User not found ")
  20. return
  21. }
  22. spaEditProfilePopulateLeadInfo(w, lead)
  23. case "POST":
  24. spaEditProfileSaveLead(w, r)
  25. default:
  26. w.WriteHeader(http.StatusUnauthorized)
  27. fmt.Fprintf(w, "Unsupported Method %s", r.Method)
  28. }
  29. //http.ServeFile(w, r, "spa/edit.html")
  30. //lead, _ := crmGetLead("595a323d4d639d84d")
  31. //spaEditProfilePopulateLeadInfo(w, lead)
  32. }
  33. func getLeadFromRequest(r *http.Request) (lead crmdLead, found bool) {
  34. id, found := getLeadIDFromCookie(r)
  35. if !found {
  36. return
  37. }
  38. lead, err := crmGetLead(id)
  39. if err != nil {
  40. found = false
  41. log.Printf("WARNING: valid cookie lead id %s, not found in EntityCRM", id)
  42. return
  43. }
  44. found = true
  45. return
  46. }
  47. func spaEditProfilePopulateLeadInfo(w http.ResponseWriter, lead crmdLead) {
  48. tTest := template.New("spaEditProfile")
  49. str, err := ioutil.ReadFile("spa/edit.html")
  50. if err != nil {
  51. w.WriteHeader(http.StatusInternalServerError)
  52. fmt.Fprintf(w, "Formating information not available.")
  53. return
  54. }
  55. tTest, err = tTest.Parse(string(str))
  56. if err != nil {
  57. w.WriteHeader(http.StatusInternalServerError)
  58. fmt.Fprintf(w, "Formating instruction invalid")
  59. return
  60. }
  61. err = tTest.Execute(w, lead)
  62. if err != nil {
  63. w.WriteHeader(http.StatusInternalServerError)
  64. fmt.Fprintf(w, "Monkey runs into our computer room...")
  65. log.Println("ERROR: Template execution on spa/Edit, failed \n" + err.Error())
  66. }
  67. }
  68. func spaEditProfileSaveLead(w http.ResponseWriter, r *http.Request) {
  69. original, found := getLeadFromRequest(r)
  70. if !found {
  71. response403Handler(w)
  72. log.Println("ERROR: trying to spa/edit lead, but not found")
  73. return
  74. }
  75. err := r.ParseMultipartForm(32 << 20)
  76. if err == nil {
  77. for k, v := range r.Form {
  78. fmt.Println("key:", k)
  79. fmt.Println("val:", strings.Join(v, ""))
  80. }
  81. lastname, lok := r.Form["lastname"]
  82. email, eok := r.Form["email"]
  83. phone, pok := r.Form["phone"]
  84. description, dok := r.Form["description"]
  85. lead := crmdLead{} //create a patch info, we cannot reuse original, as it as emailAdressData, phoneNumberData array. it will prevent us from updating email address and phone number, unless we modify them as well.
  86. lead.ID = original.ID
  87. if lok && eok && pok && dok {
  88. lead.LastName = lastname[0]
  89. lead.EmailAddress = email[0]
  90. lead.PhoneNumber = phone[0]
  91. lead.Description = description[0]
  92. if original.FirstName == "Anonymous" {
  93. lead.FirstName = "\u0020" //a visible space
  94. }
  95. log.Println(original.Status)
  96. if original.Status == "New" || original.Status == "Anonymous" {
  97. log.Println("change status to Assigned")
  98. lead.Status = "Assigned"
  99. }
  100. //check to see if there is file
  101. file, handler, err := r.FormFile("avatar")
  102. if err == nil {
  103. defer file.Close()
  104. log.Printf("%v", handler.Header)
  105. log.Println(handler.Filename)
  106. f, err := ioutil.TempFile(os.TempDir(), "wechat_hitxy_spa_edit_lead")
  107. //os.OpenFile("/tmp/wechat_hitxy_avatar_upload.jpg", os.O_WRONLY|os.O_CREATE, 0666)
  108. if err == nil {
  109. defer f.Close()
  110. io.Copy(f, file)
  111. attch, err := crmCreateAttachment(f.Name())
  112. if err == nil {
  113. lead.AvatarID = attch.ID
  114. os.Remove(f.Name())
  115. }
  116. }
  117. }
  118. }
  119. updatedLead, err := lead.Save()
  120. log.Println(updatedLead.PhoneNumber)
  121. log.Println(updatedLead.EmailAddress)
  122. log.Println(updatedLead.ID)
  123. if err == nil {
  124. spaEditProfilePopulateLeadInfo(w, updatedLead)
  125. //fmt.Fprintf(w, `{"status": "succes"}`)
  126. return
  127. }
  128. }
  129. fmt.Fprintf(w, `{"status": "error"}`)
  130. }