package sms import ( base_ctl "SCRM/controllers" "SCRM/enums" "SCRM/service/marketing_tool_service" "SCRM/service/member_service" "SCRM/service/sms_service" "fmt" "strings" "github.com/astaxie/beego" ) func SMSCtlRegistRouters() { beego.Router("/api/sms/records", &SMSController{}, "get:GetSendRecords") beego.Router("/api/sms/sendinit", &SMSController{}, "get:SendInitData") beego.Router("/api/sms/tagfiltercount", &SMSController{}, "get:TagFilterCustomerCount") beego.Router("/api/sms/send2all", &SMSController{}, "post:Send2AllCustomers") beego.Router("/api/sms/send2tag", &SMSController{}, "post:Send2TagCustomers") beego.Router("/api/sms/send2specific", &SMSController{}, "post:Send2SpecificCustomers") } type SMSController struct { base_ctl.BaseAuthAPIController } // /api/sms/records [get] // @param page:int func (this *SMSController) GetSendRecords() { page, _ := this.GetInt("page") if page <= 0 { page = 1 } adminUserInfo := this.GetAdminUserInfo() records, total, getRecordErr := sms_service.GetBatchSendRecords(adminUserInfo.CurrentOrgId, page, 10) if getRecordErr != nil { this.ErrorLog("获取短信批次记录失败:%v", getRecordErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(map[string]interface{}{ "records": records, "total": total, }) } // /api/sms/sendinit [get] // @param action?:int 事件类型 1.活动 // @param id?:int 事件对应的id,如活动id func (this *SMSController) SendInitData() { adminUserInfo := this.GetAdminUserInfo() tags, getTagErr := member_service.GetTagList(adminUserInfo.CurrentOrgId) if getTagErr != nil { this.ErrorLog("获取标签失败:%v", getTagErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } action, _ := this.GetInt("action") id, _ := this.GetInt64("id") defaultSMSContent := "" if id > 0 { if action == 1 { activity, _ := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, id) if activity != nil { // 因为 defaultSMSContent 不是必须的,所以不理会出现的错误 shareObj, _ := marketing_tool_service.GetActivityWxShareByActivityID(id) if shareObj != nil { defaultSMSContent = fmt.Sprintf("给您分享一个活动,点击参与:%v", shareObj.ShortURL) } } } } this.ServeSuccessJSON(map[string]interface{}{ "tags": tags, "default_content": defaultSMSContent, }) } // /api/sms/tagfiltercount [get] // @param tags:string tag_id 以逗号隔开 (1,2,4) func (this *SMSController) TagFilterCustomerCount() { tagIDsStr := this.GetString("tags") fmt.Println("tagIDsStr是什么",tagIDsStr) if len(tagIDsStr) == 0 { this.ServeSuccessJSON(map[string]interface{}{ "count": 0, }) return } tagIDs := strings.Split(tagIDsStr, ",") fmt.Println("tagIDs是什么",tagIDs) count, getCountErr := member_service.GetTagUsersCountWithMobileByTagIDs(tagIDs) fmt.Println("count是什么",count) fmt.Println("getCountErr是什么",getCountErr) if getCountErr != nil { this.ErrorLog("获取标签人数失败:%v", getCountErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(map[string]interface{}{ "count": count, }) } // /api/sms/send2all [post] // @param content:string func (this *SMSController) Send2AllCustomers() { content := this.GetString("content") if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() customers, getCustomerErr := member_service.GetAllCustomersWithMobile(adminUserInfo.CurrentOrgId) if getCustomerErr != nil { this.ErrorLog("获取所有客户失败:%v", getCustomerErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if len(customers) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer) return } mobiles := make([]string, 0, len(customers)) for _, customer := range customers { mobiles = append(mobiles, customer.Mobile) } sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles) if sendErr != nil { if sendErr == sms_service.SMSErrorFreeLimitInsufficient { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient) return } else { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } } else { this.ServeSuccessJSON(nil) } } // /api/sms/send2tag [post] // @param content:string // @param tags:string 标签id以逗号隔开 (1,2,43) func (this *SMSController) Send2TagCustomers() { content := this.GetString("content") fmt.Println("内容",content) tagIDsStr := this.GetString("tags") fmt.Println("tagIDsStr是什么",tagIDsStr) if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") || len(tagIDsStr) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } tagIDs := strings.Split(tagIDsStr, ",") fmt.Println("tagIDS是什么",tagIDs) adminUserInfo := this.GetAdminUserInfo() customers, getCustomerErr := member_service.GetTagCustomersWithMobileByTagIDs(adminUserInfo.CurrentOrgId, tagIDs) fmt.Println("customers是什么",customers) fmt.Println("错误是什么",getCustomerErr) if getCustomerErr != nil { this.ErrorLog("获取标签指定客户失败:%v", getCustomerErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if len(customers) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer) return } mobiles := make([]string, 0, len(customers)) for _, customer := range customers { mobiles = append(mobiles, customer.Mobile) } sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles) if sendErr != nil { if sendErr == sms_service.SMSErrorFreeLimitInsufficient { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient) return } else { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } } else { this.ServeSuccessJSON(nil) } } // /api/sms/send2specific [post] // @param content:string // @param ids:string customer_id以逗号隔开 (4,11) func (this *SMSController) Send2SpecificCustomers() { content := this.GetString("content") customerIDsStr := this.GetString("ids") if len(content) == 0 || strings.Contains(content, "【") || strings.Contains(content, "】") || len(customerIDsStr) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } customerIDs := strings.Split(customerIDsStr, ",") adminUserInfo := this.GetAdminUserInfo() customers, getCustomerErr := member_service.GetSpecificCustomersWithMobile(adminUserInfo.CurrentOrgId, customerIDs) if getCustomerErr != nil { this.ErrorLog("获取指定客户失败:%v", getCustomerErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if len(customers) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSNoCustomer) return } mobiles := make([]string, 0, len(customers)) for _, customer := range customers { mobiles = append(mobiles, customer.Mobile) } sendErr := sms_service.SMSOrgSendWithCustomContent(adminUserInfo.CurrentOrgId, content, mobiles) if sendErr != nil { if sendErr == sms_service.SMSErrorFreeLimitInsufficient { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSMSLimitInsufficient) return } else { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } } else { this.ServeSuccessJSON(nil) } }