scrm-go

sms_controller.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package sms
  2. import (
  3. base_ctl "SCRM/controllers"
  4. "SCRM/enums"
  5. "SCRM/service/marketing_tool_service"
  6. "SCRM/service/member_service"
  7. "SCRM/service/sms_service"
  8. "fmt"
  9. "strings"
  10. "github.com/astaxie/beego"
  11. )
  12. func SMSCtlRegistRouters() {
  13. beego.Router("/api/sms/records", &SMSController{}, "get:GetSendRecords")
  14. beego.Router("/api/sms/sendinit", &SMSController{}, "get:SendInitData")
  15. beego.Router("/api/sms/tagfiltercount", &SMSController{}, "get:TagFilterCustomerCount")
  16. beego.Router("/api/sms/send2all", &SMSController{}, "post:Send2AllCustomers")
  17. beego.Router("/api/sms/send2tag", &SMSController{}, "post:Send2TagCustomers")
  18. beego.Router("/api/sms/send2specific", &SMSController{}, "post:Send2SpecificCustomers")
  19. }
  20. type SMSController struct {
  21. base_ctl.BaseAuthAPIController
  22. }
  23. // /api/sms/records [get]
  24. // @param page:int
  25. func (this *SMSController) GetSendRecords() {
  26. page, _ := this.GetInt("page")
  27. if page <= 0 {
  28. page = 1
  29. }
  30. adminUserInfo := this.GetAdminUserInfo()
  31. records, total, getRecordErr := sms_service.GetBatchSendRecords(adminUserInfo.CurrentOrgId, page, 10)
  32. if getRecordErr != nil {
  33. this.ErrorLog("获取短信批次记录失败:%v", getRecordErr)
  34. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  35. return
  36. }
  37. this.ServeSuccessJSON(map[string]interface{}{
  38. "records": records,
  39. "total": total,
  40. })
  41. }
  42. // /api/sms/sendinit [get]
  43. // @param action?:int 事件类型 1.活动
  44. // @param id?:int 事件对应的id,如活动id
  45. func (this *SMSController) SendInitData() {
  46. adminUserInfo := this.GetAdminUserInfo()
  47. tags, getTagErr := member_service.GetTagList(adminUserInfo.CurrentOrgId)
  48. if getTagErr != nil {
  49. this.ErrorLog("获取标签失败:%v", getTagErr)
  50. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  51. return
  52. }
  53. action, _ := this.GetInt("action")
  54. id, _ := this.GetInt64("id")
  55. defaultSMSContent := ""
  56. if id > 0 {
  57. if action == 1 {
  58. activity, _ := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, id)
  59. if activity != nil { // 因为 defaultSMSContent 不是必须的,所以不理会出现的错误
  60. shareObj, _ := marketing_tool_service.GetActivityWxShareByActivityID(id)
  61. if shareObj != nil {
  62. defaultSMSContent = fmt.Sprintf("给您分享一个活动,点击参与:%v", shareObj.ShortURL)
  63. }
  64. }
  65. }
  66. }
  67. this.ServeSuccessJSON(map[string]interface{}{
  68. "tags": tags,
  69. "default_content": defaultSMSContent,
  70. })
  71. }
  72. // /api/sms/tagfiltercount [get]
  73. // @param tags:string tag_id 以逗号隔开 (1,2,4)
  74. func (this *SMSController) TagFilterCustomerCount() {
  75. tagIDsStr := this.GetString("tags")
  76. if len(tagIDsStr) == 0 {
  77. this.ServeSuccessJSON(map[string]interface{}{
  78. "count": 0,
  79. })
  80. return
  81. }
  82. tagIDs := strings.Split(tagIDsStr, ",")
  83. count, getCountErr := member_service.GetTagUsersCountWithMobileByTagIDs(tagIDs)
  84. if getCountErr != nil {
  85. this.ErrorLog("获取标签人数失败:%v", getCountErr)
  86. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  87. return
  88. }
  89. this.ServeSuccessJSON(map[string]interface{}{
  90. "count": count,
  91. })
  92. }
  93. // /api/sms/send2all [post]
  94. // @param content:string
  95. func (this *SMSController) Send2AllCustomers() {
  96. content := this.GetString("content")
  97. if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") {
  98. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  99. return
  100. }
  101. adminUserInfo := this.GetAdminUserInfo()
  102. customers, getCustomerErr := member_service.GetAllCustomersWithMobile(adminUserInfo.CurrentOrgId)
  103. if getCustomerErr != nil {
  104. this.ErrorLog("获取所有客户失败:%v", getCustomerErr)
  105. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  106. return
  107. } else if len(customers) == 0 {
  108. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer)
  109. return
  110. }
  111. mobiles := make([]string, 0, len(customers))
  112. for _, customer := range customers {
  113. mobiles = append(mobiles, customer.Mobile)
  114. }
  115. sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles)
  116. if sendErr != nil {
  117. if sendErr == sms_service.SMSErrorFreeLimitInsufficient {
  118. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient)
  119. return
  120. } else {
  121. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  122. return
  123. }
  124. } else {
  125. this.ServeSuccessJSON(nil)
  126. }
  127. }
  128. // /api/sms/send2tag [post]
  129. // @param content:string
  130. // @param tags:string 标签id以逗号隔开 (1,2,43)
  131. func (this *SMSController) Send2TagCustomers() {
  132. content := this.GetString("content")
  133. tagIDsStr := this.GetString("tags")
  134. if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") || len(tagIDsStr) == 0 {
  135. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  136. return
  137. }
  138. tagIDs := strings.Split(tagIDsStr, ",")
  139. adminUserInfo := this.GetAdminUserInfo()
  140. customers, getCustomerErr := member_service.GetTagCustomersWithMobileByTagIDs(adminUserInfo.CurrentOrgId, tagIDs)
  141. if getCustomerErr != nil {
  142. this.ErrorLog("获取标签指定客户失败:%v", getCustomerErr)
  143. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  144. return
  145. } else if len(customers) == 0 {
  146. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer)
  147. return
  148. }
  149. mobiles := make([]string, 0, len(customers))
  150. for _, customer := range customers {
  151. mobiles = append(mobiles, customer.Mobile)
  152. }
  153. sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles)
  154. if sendErr != nil {
  155. if sendErr == sms_service.SMSErrorFreeLimitInsufficient {
  156. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient)
  157. return
  158. } else {
  159. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  160. return
  161. }
  162. } else {
  163. this.ServeSuccessJSON(nil)
  164. }
  165. }
  166. // /api/sms/send2specific [post]
  167. // @param content:string
  168. // @param ids:string customer_id以逗号隔开 (4,11)
  169. func (this *SMSController) Send2SpecificCustomers() {
  170. content := this.GetString("content")
  171. customerIDsStr := this.GetString("ids")
  172. if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") || len(customerIDsStr) == 0 {
  173. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  174. return
  175. }
  176. customerIDs := strings.Split(customerIDsStr, ",")
  177. adminUserInfo := this.GetAdminUserInfo()
  178. customers, getCustomerErr := member_service.GetSpecificCustomersWithMobile(adminUserInfo.CurrentOrgId, customerIDs)
  179. if getCustomerErr != nil {
  180. this.ErrorLog("获取指定客户失败:%v", getCustomerErr)
  181. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  182. return
  183. } else if len(customers) == 0 {
  184. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer)
  185. return
  186. }
  187. mobiles := make([]string, 0, len(customers))
  188. for _, customer := range customers {
  189. mobiles = append(mobiles, customer.Mobile)
  190. }
  191. sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles)
  192. if sendErr != nil {
  193. if sendErr == sms_service.SMSErrorFreeLimitInsufficient {
  194. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient)
  195. return
  196. } else {
  197. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  198. return
  199. }
  200. } else {
  201. this.ServeSuccessJSON(nil)
  202. }
  203. }