package controller import ( "fmt" "github.com/gin-gonic/gin" "github.com/silenceper/wechat/v2/officialaccount/message" "kefu/lib" "kefu/models" "kefu/types" "log" "strconv" "time" ) //列表 func GetCustomersList(c *gin.Context) { entId, _ := c.Get("ent_id") page, _ := strconv.Atoi(c.Query("page")) if page <= 0 { page = 1 } pagesize, _ := strconv.Atoi(c.Query("pagesize")) if pagesize <= 0 || pagesize > 50 { pagesize = 10 } var args = []interface{}{} search := "ent_id = ? " args = append(args, entId) count := models.CountCustomer(search, args...) list := models.FindCustomers(page, pagesize, search, args...) c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), "result": gin.H{ "list": list, "count": count, "pagesize": pagesize, "page": page, }, }) } //更新 func PostCustomerInfo(c *gin.Context) { nickname := c.PostForm("nickname") avatar := c.PostForm("avatar") tel := c.PostForm("tel") entId := c.PostForm("ent_id") kefuName := c.PostForm("kefu_name") openid := c.PostForm("openid") accountOpenid := c.PostForm("acount_openid") customer := &models.Customer{ Tel: tel, Name: nickname, Openid: openid, Avatar: avatar, KefuName: kefuName, EntId: entId, AcountOpenid: accountOpenid, UpdatedAt: types.Time{time.Now()}, } search := "ent_id = ? " var args = []interface{}{entId} if accountOpenid != "" { search += "and acount_openid = ?" args = append(args, accountOpenid) } else { search += "and openid = ?" args = append(args, openid) } info := models.FindCustomerWhere(search, args...) if info.ID != 0 { customer.SaveCustomer(search, args...) } else { customer.AddCustomer() } //发送模板消息 if accountOpenid != "" { kefuInfo := models.FindUser(kefuName) msgData := make(map[string]*message.TemplateDataItem) var msg *message.TemplateMessage customerName := "待授权" msgData["first"] = &message.TemplateDataItem{ Value: "感谢您关注,欢迎点击注册成为会员", Color: "", } //展示会员积分 msgData["remark"] = &message.TemplateDataItem{ Value: fmt.Sprintf("您的积分为:%d", customer.Score), Color: "", } if customer.Name != "" { customerName = customer.Name } if customer.Tel != "" { msgData["first"] = &message.TemplateDataItem{ Value: "感谢您关注,您的会员信息", Color: "", } customerName = customer.Name + customer.Tel } else { msgData["remark"] = &message.TemplateDataItem{ Value: "点击此处授权完善手机号", Color: "", } } msgData["keyword1"] = &message.TemplateDataItem{ Value: kefuInfo.Nickname, Color: "", } msgData["keyword2"] = &message.TemplateDataItem{ Value: customerName, Color: "", } msgData["keyword3"] = &message.TemplateDataItem{ Value: time.Now().Format("2006-01-02 15:04:05"), Color: "", } systemBussinesId := models.FindConfig("SystemBussinesId") wechatConfig, _ := lib.NewWechatLib(systemBussinesId) msg = &message.TemplateMessage{ ToUser: accountOpenid, Data: msgData, TemplateID: wechatConfig.WechatCustomerTemplateId, URL: "", MiniProgram: struct { AppID string `json:"appid"` PagePath string `json:"pagepath"` }{ AppID: wechatConfig.WechatMiniAppId, PagePath: fmt.Sprintf("/pages/index/index?kefu_name=%s&ent_id=%s&acount_openid=%s", kefuName, entId, accountOpenid), }, } _, err := SendWechatTemplate(wechatConfig, msg) if err != nil { log.Println("发送营销会员模板消息失败:", err.Error(), accountOpenid, wechatConfig.AppId, wechatConfig.WechatCustomerTemplateId) } } c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), }) } //删除 func DeleteCustomer(c *gin.Context) { entId, _ := c.Get("ent_id") id := c.Query("id") models.DelCustomer("ent_id = ? and id = ?", entId, id) c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), }) } //通过手机和客服名删除 func DeleteKefuCustomerByTel(c *gin.Context) { kefuName := c.Query("kefu_name") tel := c.Query("tel") if kefuName == "" || tel == "" { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": "参数不能为空", }) return } models.DelCustomer("kefu_name = ? and tel = ?", kefuName, tel) c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), }) } //获取客户信息 func GetCustomerInfo(c *gin.Context) { entId := c.Query("ent_id") openId := c.Query("openid") if entId == "" || openId == "" { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": "参数不能为空", }) return } customer := models.FindCustomerWhere("ent_id = ? and openid = ?", entId, openId) kefuInfo := models.FindUserByUid(entId) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": gin.H{ "open_id": openId, "avatar": customer.Avatar, "nickname": customer.Name, "tel": customer.Tel, "score": customer.Score, "kefu_nickname": kefuInfo.Nickname, "company_pic": kefuInfo.CompanyPic, }, }) } //通过客服名和手机号获取客户信息 func GetKefuCustomerByTel(c *gin.Context) { kefuName := c.Query("kefu_name") tel := c.Query("tel") if kefuName == "" || tel == "" { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": "参数不能为空", }) return } customer := models.FindCustomerWhere("kefu_name = ? and tel = ?", kefuName, tel) if customer.ID == 0 { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": "营销会员信息不存在!", }) return } c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": customer, }) } //设置顾客信息 type ConsumerForm struct { Id uint `form:"id" json:"id" uri:"id" xml:"id"` Realname string `form:"realname" json:"realname" uri:"realname" xml:"realname" binding:"required"` ConsumerSn string `form:"consumer_sn" json:"consumer_sn" uri:"consumer_sn" xml:"consumer_sn" binding:"required"` Tel string `form:"tel" json:"tel" uri:"tel" xml:"tel"` Wechat string `form:"wechat" json:"wechat" uri:"wechat" xml:"wechat"` Qq string `form:"qq" json:"qq" uri:"qq" xml:"qq"` Email string `form:"email" json:"email" uri:"email" xml:"email"` Score string `form:"score" json:"score" uri:"score" xml:"score"` Company string `form:"company" json:"company" uri:"company" xml:"company"` Remark string `form:"remark" json:"remark" uri:"remark" xml:"remark"` } func PostSetConsumer(c *gin.Context) { var form ConsumerForm err := c.Bind(&form) if err != nil { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": types.ApiCode.GetMessage(types.ApiCode.INVALID), "result": err.Error(), }) return } kefuName, _ := c.Get("kefu_name") entId, _ := c.Get("ent_id") consumer := models.FindConsumer("consumer_sn = ?", form.ConsumerSn) m := &models.Consumer{ EntId: entId.(string), KefuName: kefuName.(string), Realname: form.Realname, Tel: form.Tel, Wechat: form.Wechat, Qq: form.Qq, Email: form.Email, Remark: form.Remark, Score: form.Score, ConsumerSn: form.ConsumerSn, Company: form.Company, CreatedAt: types.Time{}, } if form.Id == 0 { if consumer.ID != 0 { c.JSON(200, gin.H{ "code": types.ApiCode.DATA_NOT_UNIQ, "msg": types.ApiCode.GetMessage(types.ApiCode.DATA_NOT_UNIQ), }) return } m.AddConsumer() } else { if consumer.ID != 0 && consumer.ID != form.Id { c.JSON(200, gin.H{ "code": types.ApiCode.DATA_NOT_UNIQ, "msg": types.ApiCode.GetMessage(types.ApiCode.DATA_NOT_UNIQ), }) return } m.SaveConsumer("id = ? and ent_id = ?", form.Id, entId) } c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), }) } func GetConsumersList(c *gin.Context) { entId, _ := c.Get("ent_id") page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10")) if pageSize > 50 { pageSize = 10 } var args = []interface{}{} search := "ent_id = ? " args = append(args, entId) count := models.CountConsumer(search, args...) list := models.FindConsumerList(page, pageSize, search, args...) c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), "result": gin.H{ "list": list, "count": count, "pagesize": pageSize, "page": page, }, }) } //删除 func DeleteConsumer(c *gin.Context) { entId, _ := c.Get("ent_id") id := c.Query("id") models.DelConsumer("ent_id = ? and id = ?", entId, id) c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), }) }