sms_controller.go 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. fmt.Println("tagIDsStr是什么", tagIDsStr)
  77. if len(tagIDsStr) == 0 {
  78. this.ServeSuccessJSON(map[string]interface{}{
  79. "count": 0,
  80. })
  81. return
  82. }
  83. tagIDs := strings.Split(tagIDsStr, ",")
  84. fmt.Println("tagIDs是什么", tagIDs)
  85. count, getCountErr := member_service.GetTagUsersCountWithMobileByTagIDs(tagIDs)
  86. fmt.Println("count是什么", count)
  87. fmt.Println("getCountErr是什么", getCountErr)
  88. if getCountErr != nil {
  89. this.ErrorLog("获取标签人数失败:%v", getCountErr)
  90. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  91. return
  92. }
  93. this.ServeSuccessJSON(map[string]interface{}{
  94. "count": count,
  95. })
  96. }
  97. // /api/sms/send2all [post]
  98. // @param content:string
  99. func (this *SMSController) Send2AllCustomers() {
  100. content := this.GetString("content")
  101. if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") {
  102. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  103. return
  104. }
  105. adminUserInfo := this.GetAdminUserInfo()
  106. customers, getCustomerErr := member_service.GetAllCustomersWithMobile(adminUserInfo.CurrentOrgId)
  107. if getCustomerErr != nil {
  108. this.ErrorLog("获取所有客户失败:%v", getCustomerErr)
  109. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  110. return
  111. } else if len(customers) == 0 {
  112. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer)
  113. return
  114. }
  115. mobiles := make([]string, 0, len(customers))
  116. for _, customer := range customers {
  117. mobiles = append(mobiles, customer.Mobile)
  118. }
  119. sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles)
  120. if sendErr != nil {
  121. if sendErr == sms_service.SMSErrorFreeLimitInsufficient {
  122. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient)
  123. return
  124. } else {
  125. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  126. return
  127. }
  128. } else {
  129. this.ServeSuccessJSON(nil)
  130. }
  131. }
  132. // /api/sms/send2tag [post]
  133. // @param content:string
  134. // @param tags:string 标签id以逗号隔开 (1,2,43)
  135. func (this *SMSController) Send2TagCustomers() {
  136. content := this.GetString("content")
  137. fmt.Println("内容", content)
  138. tagIDsStr := this.GetString("tags")
  139. fmt.Println("tagIDsStr是什么", tagIDsStr)
  140. if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") || len(tagIDsStr) == 0 {
  141. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  142. return
  143. }
  144. tagIDs := strings.Split(tagIDsStr, ",")
  145. fmt.Println("tagIDS是什么", tagIDs)
  146. adminUserInfo := this.GetAdminUserInfo()
  147. customers, getCustomerErr := member_service.GetTagCustomersWithMobileByTagIDs(adminUserInfo.CurrentOrgId, tagIDs)
  148. fmt.Println("customers是什么", customers)
  149. fmt.Println("错误是什么", getCustomerErr)
  150. if getCustomerErr != nil {
  151. this.ErrorLog("获取标签指定客户失败:%v", getCustomerErr)
  152. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  153. return
  154. } else if len(customers) == 0 {
  155. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer)
  156. return
  157. }
  158. mobiles := make([]string, 0, len(customers))
  159. for _, customer := range customers {
  160. mobiles = append(mobiles, customer.Mobile)
  161. }
  162. sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles)
  163. if sendErr != nil {
  164. if sendErr == sms_service.SMSErrorFreeLimitInsufficient {
  165. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient)
  166. return
  167. } else {
  168. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  169. return
  170. }
  171. } else {
  172. this.ServeSuccessJSON(nil)
  173. }
  174. }
  175. // /api/sms/send2specific [post]
  176. // @param content:string
  177. // @param ids:string customer_id以逗号隔开 (4,11)
  178. func (this *SMSController) Send2SpecificCustomers() {
  179. content := this.GetString("content")
  180. customerIDsStr := this.GetString("ids")
  181. if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") || len(customerIDsStr) == 0 {
  182. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  183. return
  184. }
  185. customerIDs := strings.Split(customerIDsStr, ",")
  186. adminUserInfo := this.GetAdminUserInfo()
  187. customers, getCustomerErr := member_service.GetSpecificCustomersWithMobile(adminUserInfo.CurrentOrgId, customerIDs)
  188. if getCustomerErr != nil {
  189. this.ErrorLog("获取指定客户失败:%v", getCustomerErr)
  190. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  191. return
  192. } else if len(customers) == 0 {
  193. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer)
  194. return
  195. }
  196. mobiles := make([]string, 0, len(customers))
  197. for _, customer := range customers {
  198. mobiles = append(mobiles, customer.Mobile)
  199. }
  200. sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles)
  201. if sendErr != nil {
  202. if sendErr == sms_service.SMSErrorFreeLimitInsufficient {
  203. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient)
  204. return
  205. } else {
  206. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  207. return
  208. }
  209. } else {
  210. this.ServeSuccessJSON(nil)
  211. }
  212. }