124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
package auth
|
|
|
|
import (
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
authsvc "server/services/auth"
|
|
"server/services/wechatmp"
|
|
)
|
|
|
|
func (c *AuthLoginController) WechatScanStart() {
|
|
scene, qrURL, notice, expireAt, err := wechatmp.StartScanLogin()
|
|
if err != nil {
|
|
c.serveJSON(map[string]interface{}{"code": 500, "msg": "发起扫码登录失败:" + err.Error()})
|
|
return
|
|
}
|
|
c.serveJSON(map[string]interface{}{
|
|
"code": 200,
|
|
"msg": "success",
|
|
"data": map[string]interface{}{
|
|
"scene": scene,
|
|
"qrcode_url": qrURL,
|
|
"follow_qrcode": wechatmp.FollowQrcodeURL(),
|
|
"notice": notice,
|
|
"expire_in": int(time.Until(expireAt).Seconds()),
|
|
},
|
|
})
|
|
}
|
|
|
|
func (c *AuthLoginController) WechatScanStatus() {
|
|
redirectParam := c.GetString("redirect")
|
|
clientID := strings.TrimSpace(c.GetString("client_id"))
|
|
row, err := wechatmp.PickScanLogin(c.GetString("scene"))
|
|
if err != nil {
|
|
if err == wechatmp.ErrScanLoginInvalid {
|
|
c.serveJSON(map[string]interface{}{"code": 200, "data": map[string]interface{}{"status": 2}})
|
|
return
|
|
}
|
|
c.serveJSON(map[string]interface{}{"code": 200, "data": map[string]interface{}{"status": 0, "msg": err.Error()}})
|
|
return
|
|
}
|
|
|
|
var identity models.AuthIdentity
|
|
if err := models.Orm.QueryTable(new(models.AuthIdentity)).
|
|
Filter("id", row.BindID).One(&identity); err != nil || identity.Status != models.AuthIdentityStatusEnabled {
|
|
c.serveJSON(map[string]interface{}{"code": 400, "msg": "账号已被禁用或锁定,无法登录"})
|
|
return
|
|
}
|
|
|
|
tenants, lerr := authsvc.ListTenantOptions(row.BindID)
|
|
if lerr != nil || len(tenants) == 0 {
|
|
c.serveJSON(map[string]interface{}{"code": 400, "msg": "该账号未加入任何企业,无法登录"})
|
|
return
|
|
}
|
|
tid := uint64(authsvc.PendingTenantID)
|
|
if len(tenants) == 1 {
|
|
tid = tenants[0].Tid
|
|
}
|
|
|
|
username := "微信用户"
|
|
if identity.Nickname != nil && strings.TrimSpace(*identity.Nickname) != "" {
|
|
username = *identity.Nickname
|
|
}
|
|
ip := c.Ctx.Input.IP()
|
|
userAgent := c.Ctx.Request.UserAgent()
|
|
|
|
sess, serr := authsvc.CreateSession(authsvc.SessionInfo{
|
|
IdentityID: row.BindID,
|
|
Tid: tid,
|
|
ClientID: clientID,
|
|
IP: ip,
|
|
UserAgent: userAgent,
|
|
LoginType: "wechat_mp",
|
|
Amr: "wechat_mp",
|
|
})
|
|
if serr != nil {
|
|
c.serveJSON(map[string]interface{}{"code": 400, "msg": "登录失败:" + serr.Error()})
|
|
return
|
|
}
|
|
if _, terr := authsvc.IssueTokens(authsvc.TokenIssue{
|
|
IdentityID: row.BindID,
|
|
Tid: tid,
|
|
ClientID: clientID,
|
|
Sid: sess.Sid,
|
|
Username: username,
|
|
Amr: "wechat_mp",
|
|
}); terr != nil {
|
|
c.serveJSON(map[string]interface{}{"code": 500, "msg": "签发令牌失败:" + terr.Error()})
|
|
return
|
|
}
|
|
setSessionCookieForCtx(c.Ctx, sess.Sid)
|
|
|
|
identityID := row.BindID
|
|
amr := "wechat_mp"
|
|
_, _ = models.Orm.Insert(&models.AuthLoginLog{
|
|
Tid: &tid,
|
|
IdentityID: &identityID,
|
|
UserName: username,
|
|
ClientID: clientID,
|
|
LoginType: "wechat_mp",
|
|
Amr: &amr,
|
|
Status: 1,
|
|
Message: "公众号扫码登录成功",
|
|
IP: ip,
|
|
UserAgent: userAgent,
|
|
})
|
|
|
|
back := decodeRedirect(redirectParam)
|
|
if len(tenants) > 1 {
|
|
back = "/auth/login?step=tenant&client_id=" + url.QueryEscape(clientID) +
|
|
"&redirect=" + url.QueryEscape(redirectParam)
|
|
}
|
|
|
|
c.serveJSON(map[string]interface{}{
|
|
"code": 200,
|
|
"data": map[string]interface{}{
|
|
"status": 1,
|
|
"redirect": back,
|
|
},
|
|
})
|
|
}
|