package marketing_tool import ( "SCRM/controllers" "SCRM/enums" "SCRM/models" "SCRM/service/marketing_tool_service" "SCRM/utils" "encoding/json" "time" "github.com/astaxie/beego" ) func ActivityCtlRegistRouters() { beego.Router("/api/activities", &ActivityAPIController{}, "get:Activities") beego.Router("/api/activity", &ActivityAPIController{}, "get:GetActivity") beego.Router("/api/activity/submit", &ActivityAPIController{}, "post:ActivitySubmit") beego.Router("/api/activity/detailview", &ActivityAPIController{}, "get:GetActivityDetailView") beego.Router("/api/activity/signups", &ActivityAPIController{}, "get:SignupUsers") beego.Router("/api/activity/delete", &ActivityAPIController{}, "post:DeleteActivity") beego.Router("/api/activity/publish", &ActivityAPIController{}, "post:PublishActivity") beego.Router("/api/activity/shareinfo", &ActivityAPIController{}, "get:GetActivityShareInfo") } type ActivityAPIController struct { controllers.BaseAuthAPIController } // /api/activities [get] // @param page?:int // @param keyword?:string // @param status?:int 1.已发布 2.待发布 3.未通过 4.已结束 其他取全部 func (this *ActivityAPIController) Activities() { page, _ := this.GetInt("page") keyword := this.GetString("keyword") status, _ := this.GetInt("status") if page <= 0 { page = 1 } if status < 1 || status > 4 { status = 0 } adminUserInfo := this.GetAdminUserInfo() var activities []*models.Activity var totalCount int64 var getActivityErr error if status == 1 { activities, totalCount, getActivityErr = marketing_tool_service.GetActivitiesWithStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, 1, page, 10) } else if status == 2 { activities, totalCount, getActivityErr = marketing_tool_service.GetActivitiesWithStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, 4, page, 10) } else if status == 3 { activities, totalCount, getActivityErr = marketing_tool_service.GetActivitiesWithStatus(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, 3, page, 10) } else if status == 4 { activities, totalCount, getActivityErr = marketing_tool_service.GetDidEndedActivities(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, page, 10) } else { activities, totalCount, getActivityErr = marketing_tool_service.GetValidActivities(adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, keyword, page, 10) } if getActivityErr != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } activityJSONList := make([]map[string]interface{}, 0, len(activities)) for _, activity := range activities { var json map[string]interface{} // 活动状态 1:已发布 2:待审核 3:未通过 4:草稿 9:已删除 if activity.Status == 1 { // 已发布/已结束 json = this._convertToPublishedActivityViewJSON(activity) } else if activity.Status == 3 { // 未通过 json = this._convertToUnapprovedActivityViewJSON(activity) } else if activity.Status == 4 { // 草稿 json = this._convertToActivityDraftsViewJSON(activity) } activityJSONList = append(activityJSONList, json) } this.ServeSuccessJSON(map[string]interface{}{ "activities": activityJSONList, "total": totalCount, }) } func (this *ActivityAPIController) _convertToPublishedActivityViewJSON(activity *models.Activity) map[string]interface{} { json := make(map[string]interface{}) now := time.Now().Unix() if activity.StartTime < now { json["status"] = 4 } else { json["status"] = 1 } json["id"] = activity.Id json["poster_photo"] = activity.PosterPhoto json["title"] = activity.Title json["is_recommend"] = activity.IsRecommend json["read_num"] = activity.ReadNum json["comment_num"] = activity.CommentNum json["star_num"] = activity.StarNum json["join_num"] = activity.JoinNum json["limit_num"] = activity.LimitNum json["start_time"] = activity.StartTime return json } func (this *ActivityAPIController) _convertToActivityDraftsViewJSON(activity *models.Activity) map[string]interface{} { json := make(map[string]interface{}) json["status"] = 2 json["id"] = activity.Id json["poster_photo"] = activity.PosterPhoto json["title"] = activity.Title return json } func (this *ActivityAPIController) _convertToUnapprovedActivityViewJSON(activity *models.Activity) map[string]interface{} { json := make(map[string]interface{}) json["status"] = 3 json["id"] = activity.Id json["poster_photo"] = activity.PosterPhoto json["title"] = activity.Title json["reason"] = activity.Reason json["start_time"] = activity.StartTime return json } // /api/activity [get] // @param id:int func (this *ActivityAPIController) GetActivity() { activityID, _ := this.GetInt64("id") if activityID <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID) if getActivityErr != nil { this.ErrorLog("获取活动失败:%v", getActivityErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if activity == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist) return } paragraph, getParagraphErr := marketing_tool_service.GetActivityParagraphByActivityID(activity.Id) if getParagraphErr != nil { this.ErrorLog("获取活动段落失败:%v", getParagraphErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } paragraphTitle := "" paragraphContent := "" if paragraph != nil { paragraphTitle = paragraph.Title paragraphContent = paragraph.Text } currentOrg := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId] this.ServeSuccessJSON(map[string]interface{}{ "activity": activity, "paragraph": map[string]interface{}{ "title": paragraphTitle, "content": paragraphContent, }, "org": map[string]interface{}{ "logo": currentOrg.OrgLogo, "name": currentOrg.OrgName, }, }) } // /api/activity/submit [post] // @param publish:bool 是否发布 // @data 如下格式 /* { id?:int title:string subtitle:string poster_photo:string address:string limit_num?:int sign_up_deadline:string ("yyyy-MM-dd HH:mm") start_time:string ("yyyy-MM-dd HH:mm") phone_number?:string sign_up_notice?:string paragraph: { title:string content:string } } */ func (this *ActivityAPIController) ActivitySubmit() { publish, _ := this.GetBool("publish") var activityForm ActivityForm parseErr := json.Unmarshal(this.Ctx.Input.RequestBody, &activityForm) if parseErr != nil { this.ErrorLog("解析 activity form 失败:%v", parseErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong) return } adminUserInfo := this.GetAdminUserInfo() var activity *models.Activity if activityForm.ID > 0 { var getActivityErr error activity, getActivityErr = marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityForm.ID) if getActivityErr != nil { this.ErrorLog("获取活动失败:%v", getActivityErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if activity == nil || activity.Status == 9 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist) return } else if activity.Status == 1 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityPublishedCannotModify) return } } if len(activityForm.Title) == 0 || len(activityForm.Subtitle) == 0 || len(activityForm.PosterPhoto) == 0 || len(activityForm.Address) == 0 || len(activityForm.SignUpDeadline) == 0 || len(activityForm.StartTime) == 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } timeFmt := "2006-01-02 15:04" signUpDeadlineTime, parseSUDErr := utils.ParseTimeStringToTime(timeFmt, activityForm.SignUpDeadline) if parseSUDErr != nil { this.ErrorLog("解析报名截止时间失败:%v", parseSUDErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong) return } activityTime, parseActivityTimeErr := utils.ParseTimeStringToTime(timeFmt, activityForm.StartTime) if parseActivityTimeErr != nil { this.ErrorLog("解析活动开始时间失败:%v", parseActivityTimeErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong) return } if len(activityForm.PhoneNumber) > 0 { if utils.PhoneRegexp().MatchString(activityForm.PhoneNumber) == false { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong) return } } if activityForm.LimitNum < 0 { activityForm.LimitNum = 0 } if activity == nil { activity = &models.Activity{ UserOrgId: adminUserInfo.CurrentOrgId, UserAppId: adminUserInfo.CurrentAppId, Title: activityForm.Title, Subtitle: activityForm.Subtitle, Address: activityForm.Address, SignUpDeadline: signUpDeadlineTime.Unix(), SignUpNotice: activityForm.SignUpNotice, StartTime: activityTime.Unix(), PosterPhoto: activityForm.PosterPhoto, PosterPhotoThumb: activityForm.PosterPhoto, LimitNum: activityForm.LimitNum, PhoneNumber: activityForm.PhoneNumber, CreateTime: time.Now().Unix(), } } else { activity.Title = activityForm.Title activity.Subtitle = activityForm.Subtitle activity.Address = activityForm.Address activity.SignUpDeadline = signUpDeadlineTime.Unix() activity.SignUpNotice = activityForm.SignUpNotice activity.StartTime = activityTime.Unix() activity.PosterPhoto = activityForm.PosterPhoto activity.PosterPhotoThumb = activityForm.PosterPhoto activity.LimitNum = activityForm.LimitNum activity.PhoneNumber = activityForm.PhoneNumber } activity.ModifyTime = time.Now().Unix() if publish { activity.Status = 1 } else { activity.Status = 4 } var paragraph *models.ActivityParagraph if activity.Id > 0 { var getPErr error paragraph, getPErr = marketing_tool_service.GetActivityParagraphByActivityID(activity.Id) if getPErr != nil { this.ErrorLog("获取活动段落失败:%v", getPErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } } if len(activityForm.Paragraph.Title) > 0 { if paragraph == nil { paragraph = &models.ActivityParagraph{ Title: activityForm.Paragraph.Title, Text: activityForm.Paragraph.Content, Status: 1, CreateTime: time.Now().Unix(), ModifyTime: time.Now().Unix(), } } else { paragraph.Title = activityForm.Paragraph.Title paragraph.Text = activityForm.Paragraph.Content paragraph.ModifyTime = time.Now().Unix() } } else { if paragraph != nil { paragraph.Status = 0 paragraph.ModifyTime = time.Now().Unix() } } saveErr := marketing_tool_service.SaveActivityAndParagraph(activity, paragraph) if saveErr != nil { this.ErrorLog("保存活动失败:%v", saveErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } wxShareModel, getWxShareErr := marketing_tool_service.GetActivityWxShareByActivityID(activity.Id) if getWxShareErr != nil { this.ErrorLog("获取活动微信分享信息失败:%v", getWxShareErr) } else if wxShareModel != nil { wxShareModel.ModifyTime = time.Now().Unix() wxShareModel.Status = 1 wxShareModel.Title = activity.Title wxShareModel.Subtitle = activity.Subtitle wxShareModel.Image = activity.PosterPhoto saveWxShareErr := marketing_tool_service.SaveActivityWxShare(wxShareModel) if saveWxShareErr != nil { this.ErrorLog("更新活动微信分享信息失败:%v", saveWxShareErr) } } this.ServeSuccessJSON(map[string]interface{}{ "activity_id": activity.Id, }) } // /api/activity/detailview [get] // @param id:int func (this *ActivityAPIController) GetActivityDetailView() { activityID, _ := this.GetInt64("id") if activityID <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID) if getActivityErr != nil { this.ErrorLog("获取活动失败:%v", getActivityErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if activity == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist) return } if activity.Status != 1 && activity.Status != 4 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist) return } signupUsers, _, getUsersErr := marketing_tool_service.GetValidActivitySignupUsers(activityID, "", 1) if getUsersErr != nil { this.ErrorLog("获取活动报名客户失败:%v", getUsersErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(map[string]interface{}{ "activity": activity, "users": signupUsers, }) } // /api/activity/signups [get] // @param id:int // @param keyword?:string // @param page?:int func (this *ActivityAPIController) SignupUsers() { activityID, _ := this.GetInt64("id") if activityID <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } page, _ := this.GetInt("page") if page <= 0 { page = 1 } keyword := this.GetString("keyword") adminUserInfo := this.GetAdminUserInfo() activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID) if getActivityErr != nil { this.ErrorLog("获取活动失败:%v", getActivityErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if activity == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist) return } signupUsers, total, getUsersErr := marketing_tool_service.GetValidActivitySignupUsers(activityID, keyword, page) if getUsersErr != nil { this.ErrorLog("获取活动报名客户失败:%v", getUsersErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(map[string]interface{}{ "users": signupUsers, "total": total, }) } // /api/activity/delete [post] // @param id:int func (this *ActivityAPIController) DeleteActivity() { activityID, _ := this.GetInt64("id") if activityID <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID) if getActivityErr != nil { this.ErrorLog("获取活动失败:%v", getActivityErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if activity == nil || activity.Status == 9 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist) return } deleteErr := marketing_tool_service.DeleteActivity(adminUserInfo.CurrentOrgId, activityID) if deleteErr != nil { this.ErrorLog("删除活动失败:%v", deleteErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(nil) } // /api/activity/publish [post] // @param id:int func (this *ActivityAPIController) PublishActivity() { activityID, _ := this.GetInt64("id") if activityID <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID) if getActivityErr != nil { this.ErrorLog("获取活动失败:%v", getActivityErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if activity == nil || activity.Status == 9 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist) return } if activity.Status == 1 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityPublishRepeat) return } activity.Status = 1 activity.ModifyTime = time.Now().Unix() updateErr := marketing_tool_service.SaveActivityAndParagraph(activity, nil) if updateErr != nil { this.ErrorLog("发布活动失败:%v", updateErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(nil) } // /api/activity/shareinfo [get] // @param id:int func (this *ActivityAPIController) GetActivityShareInfo() { activityID, _ := this.GetInt64("id") if activityID <= 0 { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() activity, getActivityErr := marketing_tool_service.GetActivityWithID(adminUserInfo.CurrentOrgId, activityID) if getActivityErr != nil { this.ErrorLog("获取活动失败:%v", getActivityErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } else if activity == nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeActivityNotExist) return } wxShareModel, getWxShareErr := marketing_tool_service.GetActivityWxShareByActivityID(activity.Id) if getWxShareErr != nil || wxShareModel == nil { this.ErrorLog("获取活动微信分享信息失败:%v", getWxShareErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(map[string]interface{}{ "url": wxShareModel.ShortURL, }) }