package tmpl import ( "fmt" "github.com/gin-gonic/gin" "kefu/models" "kefu/tools" "kefu/types" "time" ) func PageWechatAuth(c *gin.Context) { //获取微信用户消息 weixinCode := c.Query("code") entId := models.FindConfig("SystemBussinesId") if weixinCode == "" || entId == "" { c.JSON(200, gin.H{ "msg": "请使用微信网页授权链接打开!", }) return } userInfo, err := GetWechatUserInfo(weixinCode, entId) if err != nil { c.JSON(200, gin.H{ "msg": "微信网页授权失败!", "result": err.Error(), }) return } password := "123456" kefuName := userInfo.OpenID avator := userInfo.HeadImgURL nickname := userInfo.Nickname kefuInfo := models.FindUser(kefuName) if kefuInfo.ID == 0 { dd, _ := time.ParseDuration("180h") dd1 := time.Now().Add(dd) kefuInfo = models.User{ Name: kefuName, Password: tools.Md5(password), Nickname: nickname, Avator: avator, Pid: 1, RecNum: 0, AgentNum: 0, Status: 0, OnlineStatus: 1, ExpiredAt: types.Time{dd1}, } uid, err := kefuInfo.AddUser() if uid == 0 && err != nil { c.JSON(200, gin.H{ "code": 400, "msg": fmt.Sprintf("增加用户失败:%s", err.Error()), "result": "", }) return } models.CreateUserRole(uid, 2) logContent := fmt.Sprintf("微信授权新增用户'%s',昵称:'%s',成功", kefuName, nickname) smtp := models.FindConfig("NoticeEmailSmtp") sender := models.FindConfig("NoticeEmailAddress") password := models.FindConfig("NoticeEmailPassword") if smtp != "" && sender != "" && password != "" { content := fmt.Sprintf("账户:%s,昵称:%s,注册成功", kefuName, nickname) go tools.SendSmtp(smtp, sender, password, []string{sender}, "在线客服系统注册成功", content) } go models.CreateFlyLog(tools.Int2Str(uid), c.ClientIP(), logContent, "user") } token := GenUserToken(kefuInfo) c.Redirect(301, "/sso?token="+token+"&redirect=/h5") }