No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

134 líneas
3.5KB

  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. w.WriteHeader(http.StatusUnauthorized)
  18. fmt.Fprintf(w, "User not found ")
  19. return
  20. }
  21. spaEditProfilePopulateLeadInfo(w, lead)
  22. case "POST":
  23. spaEditProfileSaveLead(w, r)
  24. default:
  25. w.WriteHeader(http.StatusUnauthorized)
  26. fmt.Fprintf(w, "Unsupported Method %s", r.Method)
  27. }
  28. //http.ServeFile(w, r, "spa/edit.html")
  29. //lead, _ := crmGetLead("595a323d4d639d84d")
  30. //spaEditProfilePopulateLeadInfo(w, lead)
  31. }
  32. func getLeadFromRequest(r *http.Request) (lead crmdLead, found bool) {
  33. id, found := getLeadIDFromCookie(r)
  34. if !found {
  35. return
  36. }
  37. lead, err := crmGetLead(id)
  38. if err != nil {
  39. found = false
  40. log.Printf("WARNING: valid cookie lead id %s, not found in EntityCRM", id)
  41. return
  42. }
  43. found = true
  44. return
  45. }
  46. func spaEditProfilePopulateLeadInfo(w http.ResponseWriter, lead crmdLead) {
  47. tTest := template.New("spaEditProfile")
  48. str, err := ioutil.ReadFile("spa/edit.html")
  49. if err != nil {
  50. w.WriteHeader(http.StatusInternalServerError)
  51. fmt.Fprintf(w, "Formating information not available.")
  52. return
  53. }
  54. tTest, err = tTest.Parse(string(str))
  55. if err != nil {
  56. w.WriteHeader(http.StatusInternalServerError)
  57. fmt.Fprintf(w, "Formating instruction invalid")
  58. return
  59. }
  60. err = tTest.Execute(w, lead)
  61. if err != nil {
  62. w.WriteHeader(http.StatusInternalServerError)
  63. fmt.Fprintf(w, "Monkey runs into our computer room...")
  64. log.Println("ERROR: Template execution on spa/Edit, failed \n" + err.Error())
  65. }
  66. }
  67. func spaEditProfileSaveLead(w http.ResponseWriter, r *http.Request) {
  68. original, found := getLeadFromRequest(r)
  69. if !found {
  70. w.WriteHeader(http.StatusUnauthorized)
  71. fmt.Fprintf(w, "You are not authorized to change data...")
  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. //check to see if there is file
  93. file, handler, err := r.FormFile("avatar")
  94. if err == nil {
  95. defer file.Close()
  96. log.Printf("%v", handler.Header)
  97. log.Println(handler.Filename)
  98. f, err := ioutil.TempFile(os.TempDir(), "wechat_hitxy_spa_edit_lead")
  99. //os.OpenFile("/tmp/wechat_hitxy_avatar_upload.jpg", os.O_WRONLY|os.O_CREATE, 0666)
  100. if err == nil {
  101. defer f.Close()
  102. io.Copy(f, file)
  103. attch, err := crmCreateAttachment(f.Name())
  104. if err == nil {
  105. lead.AvatarID = attch.ID
  106. os.Remove(f.Name())
  107. }
  108. }
  109. }
  110. }
  111. updatedLead, err := lead.Save()
  112. log.Println(updatedLead.PhoneNumber)
  113. log.Println(updatedLead.EmailAddress)
  114. log.Println(updatedLead.ID)
  115. if err == nil {
  116. spaEditProfilePopulateLeadInfo(w, updatedLead)
  117. //fmt.Fprintf(w, `{"status": "succes"}`)
  118. return
  119. }
  120. }
  121. fmt.Fprintf(w, `{"status": "error"}`)
  122. }