package controller import ( "fmt" "github.com/dchest/captcha" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" "kefu/common" "kefu/models" "kefu/tools" "kefu/types" "kefu/ws" "strconv" "time" ) func GetKefuInfo(c *gin.Context) { kefuId, _ := c.Get("kefu_id") entIdStr, _ := c.Get("ent_id") //user := models.FindUserById(kefuId) //info := make(map[string]interface{}) //info["name"] = user.Nickname //info["id"] = user.Name //info["avator"] = user.Avator //info["role_id"] = user.RoleId user := models.User{ID: uint(kefuId.(float64))} result := user.GetOneUser("*") if result.ID == 0 { c.JSON(200, gin.H{ "code": 400, "msg": "客服不存在", }) return } result.Password = "" result.EntId = entIdStr.(string) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": result, }) } func GetKefuInfoAll(c *gin.Context) { id, _ := c.Get("kefu_id") userinfo := models.FindUserRole("user.avator,user.name,user.id, role.name role_name", id) c.JSON(200, gin.H{ "code": 200, "msg": "验证成功", "result": userinfo, }) } func GetOtherKefuList(c *gin.Context) { idInterface, _ := c.Get("kefu_id") entId, _ := c.Get("ent_id") id := idInterface.(float64) result := make([]interface{}, 0) ws.SendPingToKefuClient() kefus := models.FindUsersByEntId(entId) for _, kefu := range kefus { if uint(id) == kefu.ID { continue } item := make(map[string]interface{}) item["name"] = kefu.Name item["nickname"] = kefu.Nickname item["avator"] = kefu.Avator item["status"] = "offline" kefus, ok := ws.KefuList[kefu.Name] if ok && len(kefus.Users) != 0 { item["status"] = "online" } result = append(result, item) } c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": result, }) } func PostTransKefu(c *gin.Context) { kefuId := c.Query("kefu_id") visitorId := c.Query("visitor_id") curKefuId, _ := c.Get("kefu_name") user := models.FindUser(kefuId) visitor := models.FindVisitorByVistorId(visitorId) if user.Name == "" || visitor.Name == "" { c.JSON(200, gin.H{ "code": 400, "msg": "访客或客服不存在", }) return } models.UpdateVisitorKefu(visitorId, kefuId) ws.UpdateVisitorUser(visitorId, kefuId) go ws.VisitorOnline(kefuId, visitor) go ws.VisitorOffline(curKefuId.(string), visitor.VisitorId, visitor.Name) ws.VisitorNotice(visitor.VisitorId, "客服转接到"+user.Nickname) ws.VisitorTransfer(visitor.VisitorId, kefuId) noticeContent := fmt.Sprintf("访客%s转接到您", visitor.Name) go SendVisitorLoginNotice(kefuId, visitor.Name, visitor.Avator, noticeContent, visitor.VisitorId) go SendWechatVisitorMessageTemplate(kefuId, visitor.Name, visitor.VisitorId, noticeContent, visitor.EntId) c.JSON(200, gin.H{ "code": 200, "msg": "转移成功", }) } func GetKefuInfoSettingOwn(c *gin.Context) { kefuId := c.Query("kefu_id") pid, _ := c.Get("kefu_id") roleId, _ := c.Get("role_id") var user models.User var entConfigs []models.EntConfig if roleId.(float64) == 1 { user = models.FindUserById(kefuId) entConfigs = models.FindEntConfigByEntid(kefuId) } else { user = models.FindUserByIdPid(pid, kefuId) } user.Password = "" c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": gin.H{ "user": user, "settings": entConfigs, }, }) } func GetKefuInfoSetting(c *gin.Context) { kefuId := c.Query("kefu_id") user := models.FindUserById(kefuId) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": user, }) } func PostKefuRegister(c *gin.Context) { name := c.PostForm("name") password := c.PostForm("password") rePassword := c.PostForm("rePassword") avator := "/static/images/4.jpg" nickname := c.PostForm("nickname") captchaCode := c.PostForm("captcha") email := c.PostForm("email") emailCode := c.PostForm("emailCode") roleId := 2 if name == "" || password == "" || rePassword == "" || nickname == "" || captchaCode == "" { c.JSON(200, gin.H{ "code": 400, "msg": "参数不能为空", "result": "", }) return } if password != rePassword { c.JSON(200, gin.H{ "code": 400, "msg": "密码不一致", "result": "", }) return } //强密码验证 isCheckStrongPass := models.FindConfig("CheckStrongPass") if password != "" && isCheckStrongPass == "true" && !tools.IsStrongPass(password) { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": "密码必须包含大写字母,小写字母,数字和特殊字符,并且不能少于8位", "result": "", }) return } oldUser := models.FindUserWhere("name = ? or email = ?", name, email) if oldUser.Name != "" { c.JSON(200, gin.H{ "code": 400, "msg": "用户名或邮箱已经存在", "result": "", }) return } session := sessions.DefaultMany(c, "go-session-a") if captchaId := session.Get("captcha"); captchaId != nil { if !captcha.VerifyString(captchaId.(string), captchaCode) { c.JSON(200, gin.H{ "code": 400, "msg": "图片验证码验证失败", "result": "", }) return } } else { c.JSON(200, gin.H{ "code": 400, "msg": "图片验证码失效", "result": "", }) return } session2 := sessions.DefaultMany(c, "go-session") if emailCodeSession := session2.Get("emailCode" + email); emailCodeSession != nil { if emailCodeSession.(string) != emailCode { c.JSON(200, gin.H{ "code": 400, "msg": "邮箱验证码验证失败", "result": "", }) return } } else { c.JSON(200, gin.H{ "code": 400, "msg": "邮箱验证码失效", "result": "", }) return } //插入新用户 dd, _ := time.ParseDuration("180h") dd1 := time.Now().Add(dd) userObj := &models.User{ Name: name, Password: tools.Md5(password), Nickname: nickname, Avator: avator, Pid: 1, RecNum: 0, AgentNum: 0, Status: 1, Email: email, Tel: "", OnlineStatus: 1, Uuid: tools.Uuid2(), ExpiredAt: types.Time{dd1}, } uid, err := userObj.AddUser() if uid == 0 && err != nil { c.JSON(200, gin.H{ "code": 400, "msg": fmt.Sprintf("增加用户失败:%s", err.Error()), "result": "", }) return } models.CreateUserRole(uid, uint(roleId)) logContent := fmt.Sprintf("'%s'注册成功", name) go models.CreateFlyLog(fmt.Sprintf("%d", uid), c.ClientIP(), logContent, "user") go SendRegisterEmail(name, email, nickname) //删除session session.Delete("captcha") session2.Delete("emailCode" + email) _ = session.Save() _ = session2.Save() c.JSON(200, gin.H{ "code": 200, "msg": "注册完成", "result": "", }) } func PostKefuAvator(c *gin.Context) { avator := c.PostForm("avator") if avator == "" { c.JSON(200, gin.H{ "code": 400, "msg": "不能为空", "result": "", }) return } kefuName, _ := c.Get("kefu_name") models.UpdateUserAvator(kefuName.(string), avator) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": "", }) } // 更新资料 func PostUpdateUser(c *gin.Context) { kefuName, _ := c.Get("kefu_name") avator := c.PostForm("avator") companyPic := c.PostForm("company_pic") result := make(map[string]string) result["company_pic"] = companyPic result["avator"] = avator models.UpdateUser2(result, "name = ?", kefuName) c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } func PostKefuinfo(c *gin.Context) { avator := c.PostForm("avator") nickname := c.PostForm("nickname") if avator == "" || nickname == "" { c.JSON(200, gin.H{ "code": 400, "msg": "参数不能为空", "result": "", }) return } kefuName, _ := c.Get("kefu_name") models.UpdateKefuInfoByName(kefuName.(string), avator, nickname) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": "", }) } func PostKefuPass(c *gin.Context) { kefuName, _ := c.Get("kefu_name") newPass := c.PostForm("new_pass") confirmNewPass := c.PostForm("confirm_new_pass") old_pass := c.PostForm("old_pass") if newPass != confirmNewPass { c.JSON(200, gin.H{ "code": 400, "msg": "密码不一致", "result": "", }) return } user := models.FindUser(kefuName.(string)) if user.Password != tools.Md5(old_pass) { c.JSON(200, gin.H{ "code": 400, "msg": "旧密码不正确", "result": "", }) return } //强密码验证 isCheckStrongPass := models.FindConfig("CheckStrongPass") if isCheckStrongPass == "true" && !tools.IsStrongPass(newPass) { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": "密码必须包含大写字母,小写字母,数字和特殊字符,并且不能少于8位", "result": "", }) return } models.UpdateUserPass(kefuName.(string), tools.Md5(newPass)) c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": "", }) } func PostKefuInfoStatus(c *gin.Context) { kefuId, _ := c.Get("kefu_id") roleId, _ := c.Get("role_id") id := c.PostForm("id") status := c.PostForm("status") statusInt, _ := strconv.Atoi(status) query := " id = ? " arg := []interface{}{ id, } if roleId.(float64) != 1 { query = query + " and pid = ? " arg = append(arg, kefuId) } statusTxt := "" if status == "1" { statusTxt = "关闭" } else { statusTxt = "开通" } user := models.FindUserById(id) go SendSystemNoticeEmail(user.Email, "在线客服系统审核通知", fmt.Sprintf("你好,账号 %s,%s", user.Name, statusTxt)) models.UpdateUserStatusWhere(uint(statusInt), query, arg...) c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } type StaffRegisteForm struct { Account string `form:"account" json:"account" uri:"account" xml:"account" binding:"required"` Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"` Avator string `form:"avator" json:"avator" uri:"avator" xml:"avator"` Nickname string `form:"nickname" json:"nickname" uri:"nickname" xml:"nickname" binding:"required"` ExpiredTime string `form:"expiredTime" json:"expiredTime" uri:"expiredTime" xml:"expiredTime" binding:"required"` Tel string `form:"tel" json:"tel" uri:"tel" xml:"tel"` Email string `form:"email" json:"email" uri:"email" xml:"email"` AuthKey string `form:"authKey" json:"authKey" uri:"authKey" xml:"authKey"` EntId string `form:"entId" json:"entId" uri:"entId" xml:"entId"` IdCard string `form:"idCard" json:"idCard" uri:"idCard" xml:"idCard"` } // 直接注册成功客服账户 func PostKefuRegister2(c *gin.Context) { var form StaffRegisteForm 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 } roleId := 3 if form.EntId == "" { form.EntId = "1" roleId = 2 } expired := tools.TimeStrToTime(form.ExpiredTime) entId := uint(tools.Str2Int(form.EntId)) userObj := &models.User{ Name: form.Account, Password: tools.Md5(form.Password), Nickname: form.Nickname, Avator: form.Avator, Pid: entId, Status: 0, Email: form.Email, Tel: form.Tel, OnlineStatus: 1, ExpiredAt: types.Time{expired}, } userAttr := &models.User_attr{ EntId: tools.Int2Str(entId), AuthKey: form.AuthKey, IdCard: form.IdCard, KefuName: form.Account, CreatedAt: types.Time{time.Now()}, } oldUser := models.FindUser(form.Account) if oldUser.Name != "" { userObj.SaveUser("name = ?", form.Account) userAttr.SaveUserAttr("kefu_name = ?", form.Account) c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), }) return } //插入新用户 uid, _ := userObj.AddUser() if uid == 0 { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": types.ApiCode.GetMessage(types.ApiCode.FAILED), }) return } //插入用户角色表 models.CreateUserRole(uid, uint(roleId)) //插入扩展表 if entId == 1 { entId = uid } userAttr.AddUserAttr() logContent := fmt.Sprintf("'%s'注册成功", form.Account) go models.CreateFlyLog(fmt.Sprintf("%d", uid), c.ClientIP(), logContent, "user") c.JSON(200, gin.H{ "code": types.ApiCode.SUCCESS, "msg": types.ApiCode.GetMessage(types.ApiCode.SUCCESS), }) } // 修改客服状态 func PostKefuInfoStatus2(c *gin.Context) { kefuName := c.PostForm("kefuName") status := c.PostForm("status") statusInt, _ := strconv.Atoi(status) query := " name = ? " arg := []interface{}{ kefuName, } models.UpdateUserStatusWhere(uint(statusInt), query, arg...) c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } func PostKefuExpiredTime(c *gin.Context) { kefuId, _ := c.Get("kefu_id") roleId, _ := c.Get("role_id") name := c.PostForm("name") expiredTime := c.PostForm("expired_time") query := " name = ? " arg := []interface{}{ name, } if roleId.(float64) != 1 { query = query + " and pid = ? " arg = append(arg, kefuId) } models.UpdateUserExpiredWhere(expiredTime, query, arg...) c.JSON(200, gin.H{ "code": 200, "msg": "ok", }) } func PostKefuInfo(c *gin.Context) { entId, _ := c.Get("ent_id") kefuName, _ := c.Get("kefu_name") kefuId, _ := c.Get("kefu_id") mRoleId, _ := c.Get("role_id") pidInterface, _ := c.Get("kefu_id") pid := uint(pidInterface.(float64)) kefuInfo := models.FindUser(kefuName.(string)) uuid := tools.Uuid2() var query string var arg = []interface{}{} if mRoleId.(float64) != 1 { uuid = kefuInfo.Uuid query = "pid=?" arg = append(arg, kefuId) count := models.CountUsersWhere(query, arg) if kefuInfo.AgentNum <= count { c.JSON(200, gin.H{ "code": 400, "msg": fmt.Sprintf("子账号个数超过上限数: %d!", kefuInfo.AgentNum), }) return } } id := c.PostForm("id") name := c.PostForm("name") password := c.PostForm("password") avator := c.PostForm("avator") nickname := c.PostForm("nickname") roleId := c.PostForm("role_id") email := c.PostForm("email") tel := c.PostForm("tel") agentNum, _ := strconv.Atoi(c.PostForm("agent_num")) //验证密码强度 //强密码验证 isCheckStrongPass := models.FindConfig("CheckStrongPass") if password != "" && isCheckStrongPass == "true" && !tools.IsStrongPass(password) { c.JSON(200, gin.H{ "code": types.ApiCode.FAILED, "msg": "密码必须包含大写字母,小写字母,数字和特殊字符,并且不能少于8位", "result": "", }) return } if roleId == "" { c.JSON(200, gin.H{ "code": 400, "msg": "请选择角色!", }) return } //如果不是管理员,添加超权限的角色 roleIdInt, _ := strconv.Atoi(roleId) if mRoleId.(float64) != 1 && roleIdInt <= int(mRoleId.(float64)) { c.JSON(200, gin.H{ "code": 400, "msg": "修改角色无权限!", }) return } //添加非坐席账号,pid统一1 if roleIdInt < 3 { pid = 1 } //插入新用户 if id == "" { oldUser := models.FindUser(name) if oldUser.Name != "" { c.JSON(200, gin.H{ "code": 400, "msg": "用户名已经存在", "result": "", }) return } //dd, _ := time.ParseDuration("720h") //dd1 := time.Now().Add(dd) //uid := models.CreateUser(name, tools.Md5(password), avator, nickname, uint(pid.(float64)), uint(agentNum), 0, types.Time{dd1}) //插入新用户 dd, _ := time.ParseDuration("720h") dd1 := time.Now().Add(dd) userObj := &models.User{ Name: name, Password: tools.Md5(password), Nickname: nickname, Avator: avator, Pid: pid, RecNum: 0, AgentNum: 0, Status: 1, Email: email, Tel: tel, OnlineStatus: 1, ExpiredAt: types.Time{dd1}, Uuid: uuid, } uid, err := userObj.AddUser() if uid == 0 && err != nil { c.JSON(200, gin.H{ "code": 400, "msg": fmt.Sprintf("增加用户失败:%s", err.Error()), "result": "", }) return } roleIdInt, _ := strconv.Atoi(roleId) models.CreateUserRole(uid, uint(roleIdInt)) logContent := fmt.Sprintf("'%s'新增用户'%s'成功", kefuName, name) go models.CreateFlyLog(entId.(string), c.ClientIP(), logContent, "user") } else { //更新用户 if password != "" { password = tools.Md5(password) } oldUser := models.FindUser(name) if oldUser.Name != "" && id != fmt.Sprintf("%d", oldUser.ID) { c.JSON(200, gin.H{ "code": 400, "msg": "用户名已经存在", "result": "", }) return } models.UpdateUser(id, name, password, avator, nickname, email, tel, uint(agentNum)) roleIdInt, _ := strconv.Atoi(roleId) uid, _ := strconv.Atoi(id) models.DeleteRoleByUserId(uid) models.CreateUserRole(uint(uid), uint(roleIdInt)) logContent := fmt.Sprintf("'%s'更新用户'%s'成功", kefuName, name) go models.CreateFlyLog(entId.(string), c.ClientIP(), logContent, "user") } c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": "", }) } func GetKefuList(c *gin.Context) { roleId, _ := c.Get("role_id") if roleId.(float64) != 1 { c.JSON(200, gin.H{ "code": 200, "msg": "无权限", "result": "", }) } page, _ := strconv.Atoi(c.Query("page")) count := models.CountUsers() list := models.FindUsersPages(uint(page), common.PageSize) //users := models.FindUsers() c.JSON(200, gin.H{ "code": 200, "msg": "获取成功", "result": gin.H{ "count": count, "page": page, "list": list, "pagesize": common.PageSize, }, }) } func GetKefuListOwn(c *gin.Context) { kefuId, _ := c.Get("kefu_id") roleId, _ := c.Get("role_id") query := "1=1 " var arg = []interface{}{} if roleId.(float64) != 1 { query += "and pid = ? " arg = append(arg, kefuId) } //通过客服名搜索 kefuName := c.Query("kefu_name") if kefuName != "" { query += "and user.name like ? " arg = append(arg, kefuName+"%") } //通过邮箱搜索 email := c.Query("email") if email != "" { query += "and user.email like ? " arg = append(arg, email+"%") } //通过手机搜索 tel := c.Query("tel") if tel != "" { query += "and user.tel like ? " arg = append(arg, tel+"%") } pagesize, _ := strconv.Atoi(c.Query("pagesize")) page, _ := strconv.Atoi(c.Query("page")) count := models.CountUsersWhere(query, arg...) list := models.FindUsersOwn(uint(page), uint(pagesize), query, arg...) //判断实际在线状态 for key, item := range list { kefuConnection, ok := ws.KefuList[item.Name] if !ok || len(kefuConnection.Users) == 0 { item.OnlineStatus = 2 } item.RecNum = ws.VisitorCount(item.Name) list[key] = item } c.JSON(200, gin.H{ "code": 200, "msg": "获取成功", "result": gin.H{ "count": count, "page": page, "list": list, "pagesize": pagesize, }, }) } func GetKefuListMessages(c *gin.Context) { kefuId, _ := c.Get("kefu_id") roleId, _ := c.Get("role_id") id := c.Query("id") page, _ := strconv.Atoi(c.Query("page")) pagesize, _ := strconv.Atoi(c.Query("pagesize")) if pagesize == 0 { pagesize = 30 } user := models.FindUserById(id) fmt.Printf("%T\n", kefuId) fmt.Printf("%T\n", user.Pid) if fmt.Sprintf("%v", user.Pid) != fmt.Sprintf("%v", kefuId) { if roleId.(float64) != 1 { c.JSON(200, gin.H{ "code": 400, "msg": "无权限", "result": "", }) return } } count := models.CountMessage("message.kefu_id=?", user.Name) list := models.FindMessageByPage(uint(page), uint(pagesize), "message.kefu_id=?", user.Name) c.JSON(200, gin.H{ "code": 200, "msg": "获取成功", "result": gin.H{ "count": count, "page": page, "list": list, "pagesize": pagesize, }, }) } func GetVisitorListMessages(c *gin.Context) { visitorId := c.Query("visitor_id") entId, _ := c.Get("ent_id") page, _ := strconv.Atoi(c.Query("page")) count := models.CountMessage("message.ent_id= ? and message.visitor_id=?", entId, visitorId) list := models.FindMessageByPage(uint(page), common.PageSize, "message.ent_id= ? and message.visitor_id=?", entId, visitorId) c.JSON(200, gin.H{ "code": 200, "msg": "获取成功", "result": gin.H{ "count": count, "page": page, "list": list, "pagesize": common.PageSize, }, }) } func DeleteKefuInfo(c *gin.Context) { kefuId := c.Query("id") models.DeleteUserById(kefuId) models.DeleteRoleByUserId(kefuId) c.JSON(200, gin.H{ "code": 200, "msg": "删除成功", "result": "", }) } func DeleteKefuInfoOwn(c *gin.Context) { kefuId := c.Query("id") pid, _ := c.Get("kefu_id") roleId, _ := c.Get("role_id") if kefuId == fmt.Sprintf("%v", pid) { c.JSON(200, gin.H{ "code": 400, "msg": "不能删除自己", }) return } user := models.FindUserByUid(kefuId) if roleId.(float64) == 1 { models.DeleteUserById(kefuId) models.DeleteRoleByUserId(kefuId) models.DelVisitor("ent_id = ?", kefuId) models.DelVisitorBlack("ent_id = ?", kefuId) models.DelVisitorAttr("ent_id = ?", kefuId) models.DelMessage("ent_id = ?", kefuId) models.DelUserAttr("ent_id = ?", kefuId) models.DelEntConfig("ent_id = ?", kefuId) models.DelOauthKefuName(user.Name) } else { models.DeleteUserByIdPid(kefuId, pid) models.DeleteRoleByUserId(kefuId) } c.JSON(200, gin.H{ "code": 200, "msg": "删除成功", "result": "", }) } // 更新用户的在线状态 func GetUpdateOnlineStatus(c *gin.Context) { status := c.Query("status") statusInt, _ := strconv.Atoi(status) kefuId, _ := c.Get("kefu_id") user := models.User{ ID: uint(kefuId.(float64)), OnlineStatus: uint(statusInt), } user.UpdateUser() c.JSON(200, gin.H{ "code": 200, "msg": "成功", }) } // 绑定手机号 func PostKefuBindTel(c *gin.Context) { phone := c.PostForm("phone") code := c.PostForm("code") if phone == "" || code == "" { c.JSON(200, gin.H{ "code": 400, "msg": "参数不能为空", "result": "", }) return } info := models.FindUserWhere("tel = ?", phone) if info.ID != 0 { c.JSON(200, gin.H{ "code": 400, "msg": "该手机号已被绑定!", "result": "", }) return } session := sessions.DefaultMany(c, "go-session-a") if codeSession := session.Get("smsCode" + phone); codeSession != nil { if codeSession.(string) != code { c.JSON(200, gin.H{ "code": 400, "msg": "短信验证码验证失败", }) return } } else { c.JSON(200, gin.H{ "code": 400, "msg": "短信验证码失效", }) return } //删除session session.Delete("smsCode" + phone) _ = session.Save() kefuName, _ := c.Get("kefu_name") models.UpdateUserTel(kefuName.(string), phone) entId, _ := c.Get("ent_id") logContent := fmt.Sprintf("'%s'绑定手机'%s'", kefuName, phone) go models.CreateFlyLog(entId.(string), c.ClientIP(), logContent, "sms") c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": "", }) }