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.

166 lignes
5.5KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "net/http"
  7. "time"
  8. )
  9. type crmdLead struct {
  10. ID string `json:"id,omitempty"`
  11. Name string `json:"name,omitempty"`
  12. Deleted bool `json:"deleted,omitempty"`
  13. SalutationName string `json:"salutationName,omitempty"`
  14. FirstName string `json:"firstName,omitempty"`
  15. LastName string `json:"lastName,omitempty"`
  16. Title string `json:"title,omitempty"`
  17. Status string `json:"status,omitempty"`
  18. Source string `json:"source,omitempty"`
  19. Industry string `json:"industry,omitempty"`
  20. OpportunityAmount int `json:"opportunityAmount,omitempty"`
  21. Website string `json:"website,omitempty"`
  22. AddressStreet string `json:"addressStreet,omitempty"`
  23. AddressCity string `json:"addressCity,omitempty"`
  24. AddressState string `json:"addressState,omitempty"`
  25. AddressCountry string `json:"addressCountry,omitempty"`
  26. AddressPostalCode string `json:"addresPostalCode,omitempty"`
  27. EmailAddress string `json:"emailAddress,omitempty"`
  28. PhoneNumber string `json:"phoneNumber,omitempty"`
  29. DoNotCall bool `json:"doNotCall,omitempty"`
  30. Description string `json:"description,omitempty"`
  31. CreatedAt string `json:"createdAt,omitempty"`
  32. ModifiedAt string `json:"ModifiedAt,omitempty"`
  33. AccountName string `json:"accountName,omitempty"`
  34. Password string `json:"password,omitempty"`
  35. WechatHitxyID string `json:"wechat_hitxy_id,omitempty"`
  36. Verifier []string `json:"verifier,omitempty"`
  37. OpportunityAmountCurrency string `json:"opportunityAmountCurrency,omitempty"`
  38. OpportunityAmountConverted int `json:"opportunityAmountConverted,omitempty"`
  39. EmailAddressData []struct {
  40. EmailAddress string `json:"emailAddress,omitempty"`
  41. Lower string `json:"lower,omitempty"`
  42. Primary bool `json:"primary,omitempty"`
  43. OptOut bool `json:"optOut,omitempty"`
  44. Invalid bool `json:"invalid,omitempty"`
  45. } `json:"emailAddressData,omitempty"`
  46. PhoneNumberData []struct {
  47. PhoneNumber string `json:"phoneNumber,omitempty"`
  48. Primary bool `json:"primary,omitempty"`
  49. Type string `json:"type,omitempty"`
  50. } `json:"phoneNumberData,omitempty"`
  51. CreatedByID string `json:"createdById,omitempty"`
  52. CreatedByName string `json:"createdByName,omitempty"`
  53. ModifiedByID string `json:"modifiedById,omitempty"`
  54. ModifiedByName string `json:"modifiedByName,omitempty"`
  55. AssignedUserID string `json:"assignedUserId,omitempty"`
  56. AssignedUserName string `json:"assignedUserName,omitempty"`
  57. CampaignID string `json:"campaignId,omitempty"`
  58. CreatedAccountID string `json:"createdAccountId,omitempty"`
  59. CreatedContactID string `json:"createdContactId,omitempty"`
  60. CreatedOpportunityID string `json:"createdOpportunityId,omitempty"`
  61. ForceDuplicate bool `json:"forceDuplicate,omitempty"` //when purposely creating duplicated record, we need to set this to true
  62. }
  63. type crmdSearchLead struct {
  64. Total int `json:"total"`
  65. List []crmdLead `json:"list"`
  66. }
  67. func (m *crmdLead) getCreatedAt() (r time.Time) {
  68. layout := m.getTimeLayout()
  69. r, _ = time.Parse(layout, m.CreatedAt)
  70. return
  71. }
  72. func (m *crmdLead) setCreatedAt(v time.Time) string {
  73. layout := m.getTimeLayout()
  74. m.CreatedAt = v.Format(layout)
  75. return m.CreatedAt
  76. }
  77. func (m *crmdLead) getModifiedAt() (r time.Time) {
  78. layout := m.getTimeLayout()
  79. r, _ = time.Parse(layout, m.ModifiedAt)
  80. return
  81. }
  82. func (m *crmdLead) setModifiedAt(v time.Time) string {
  83. layout := m.getTimeLayout()
  84. m.ModifiedAt = v.Format(layout)
  85. return m.ModifiedAt
  86. }
  87. func (m *crmdLead) getTimeLayout() string {
  88. return "2006-01-02 15:04:05"
  89. }
  90. func crmFindOpenID(openID string) (info crmdLead, found bool, err error) {
  91. found = false
  92. req, err := http.NewRequest("GET", CRMConfig.apiLeadURL(), nil)
  93. if err != nil {
  94. log.Println("crmGetRecord: cannot form good http Request")
  95. log.Print(err)
  96. return
  97. }
  98. //auth header
  99. req.Header.Set("Authorization", crmAuthHeader())
  100. req.Header.Set("Accept", "application/json, text/javascript, */*; q=0.01")
  101. req.Header.Set("Content-Type", "application/json")
  102. //query string
  103. q := req.URL.Query()
  104. q.Add("maxSize", "20")
  105. q.Add("maxSize", "0")
  106. q.Add("sortBy", "createdAt")
  107. q.Add("asc", "false")
  108. q.Add("where[0][type]", "equals")
  109. q.Add("where[0][attribute]", "wechat_hitxy_id")
  110. q.Add("where[0][value]", openID)
  111. req.URL.RawQuery = q.Encode()
  112. // dump, err := httputil.DumpRequest(req, true)
  113. // if err != nil {
  114. // log.Fatal(err)
  115. // }
  116. // fmt.Printf("dump : %q", dump)
  117. client := &http.Client{}
  118. r, err := client.Do(req)
  119. if err != nil {
  120. return
  121. }
  122. defer r.Body.Close()
  123. b, _ := ioutil.ReadAll(r.Body)
  124. search := crmdSearchLead{}
  125. err = json.Unmarshal(b, &search)
  126. if err != nil || search.Total <= 0 {
  127. return
  128. }
  129. if search.Total > 1 {
  130. log.Printf("ERROR It's wrong to have multiple record for same wechat id %s", openID)
  131. for _, v := range search.List {
  132. log.Println(v)
  133. }
  134. return
  135. }
  136. //we successfully reach here
  137. info = search.List[0]
  138. found = true
  139. return
  140. }
  141. func crmGetLead(id string) (r crmdLead, err error) {
  142. entity, err := crmGetEntity("Lead", id)
  143. r = entity.(crmdLead)
  144. return
  145. }