123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- 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,
- })
- }
|