123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- package controllers
-
- import (
- "XT/models"
- "fmt"
- "net/url"
-
- "XT/enums"
- "XT/service"
- "XT/utils"
-
- "github.com/astaxie/beego"
- )
-
- func VerifyUserLoginControllerRegistRouters() {
- beego.Router("/login", &VerifyUserLoginController{}, "get:Login")
- beego.Router("/logout", &VerifyUserLoginController{}, "get,post:Logout")
- beego.Router("/handle_error", &VerifyUserLoginController{}, "get:HandleError")
-
- beego.Router("/api/token/verify", &VerifyUserLoginAPIController{}, "post:VerifyToken")
- beego.Router("/api/admin/edit_info", &VerifyUserLoginAPIController{}, "post:EditAdminUserInfo")
-
- beego.Router("/api/password/code", &PersonAPIController{}, "post:CodeOfModifyPwd")
- beego.Router("/api/password/modify", &PersonAPIController{}, "post:ModifyPwd")
- }
-
- type VerifyUserLoginController struct {
- BaseViewController
- }
-
-
-
-
- func (this *VerifyUserLoginController) Login() {
- token := this.Ctx.Input.Query("token")
- if len(token) > 0 {
- utils.TraceLog("SSO Login 回调: token=%v", token)
- xtFrontEndDomain := beego.AppConfig.String("front_end_domain") + "?lt=" + token
- this.Redirect302(xtFrontEndDomain)
-
- } else {
- relogin, _ := this.GetBool("relogin", false)
- returnURL := url.QueryEscape(fmt.Sprintf("%v%v", beego.AppConfig.String("httpdomain"), this.Ctx.Request.RequestURI))
- ssoDomain := beego.AppConfig.String("sso_domain")
- ssoLoginURL := fmt.Sprintf("%v/login?returnurl=%v&app_type=3&relogin=%v", ssoDomain, returnURL, relogin)
- this.Redirect302(ssoLoginURL)
- }
- }
-
-
- func (this *VerifyUserLoginController) Logout() {
- if this.Ctx.Request.Method == "GET" {
- this.DelSession("admin_user_info")
- this.Redirect302(fmt.Sprintf("%v/logout", beego.AppConfig.String("sso_domain")))
- } else if this.Ctx.Request.Method == "POST" {
- this.DelSession("admin_user_info")
- }
- }
-
-
-
- func (this *VerifyUserLoginController) HandleError() {
- code, _ := this.GetInt("code")
- if code == enums.ErrorCodeNeverCreateTypeApp {
- ssoDomain := beego.AppConfig.String("sso_domain")
- createAppURL := fmt.Sprintf("%v/org/app/create", ssoDomain)
- this.Redirect302(createAppURL)
-
- } else if code == enums.ErrorCodeContactSuperAdminCreateTypeApp {
- ssoDomain := beego.AppConfig.String("sso_domain")
- hitURL := fmt.Sprintf("%v/create_app_hint", ssoDomain)
- this.Redirect302(hitURL)
-
- } else {
- this.Abort404()
- }
- }
-
- type VerifyUserLoginAPIController struct {
- BaseAPIController
- }
-
-
-
- func (this *VerifyUserLoginAPIController) VerifyToken() {
- if this.Ctx.Request.Method == "OPTIONS" {
- this.Abort("200")
-
- } else {
- token := this.GetString("token")
- utils.TraceLog("token: %v", token)
- if len(token) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- ip := utils.GetIP(this.Ctx.Request)
- fmt.Println("ip是什么", ip)
- sessionID := this.Ctx.GetCookie("s")
- fmt.Println("sessionID", sessionID)
- utils.TraceLog("Request: %v", this.Ctx.Request)
- utils.TraceLog("cookie session id: %v", sessionID)
- adminUserInfo, err, errCode := service.VerifyToken(token, ip, sessionID)
- fmt.Println("错误是什么", err)
- fmt.Println("errCode是什么", errCode)
- if err != nil {
- if errCode == 903 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNeverCreateTypeApp)
-
- } else if errCode == 904 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeContactSuperAdminCreateTypeApp)
-
- } else {
- utils.ErrorLog("令牌验证失败:%v", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInvalidToken)
- }
- return
-
- } else {
- adminUser := adminUserInfo.AdminUser
- fmt.Println("adminUser", adminUser)
- appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
- userInfo := map[string]interface{}{
- "id": adminUser.Id,
- "mobile": adminUser.Mobile,
- "user_name": appRole.UserName,
- "avatar": appRole.Avatar,
- "intro": appRole.Intro,
- "user_type": appRole.UserType,
- "user_title": appRole.UserTitle,
- }
- curOrg := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
- org := map[string]interface{}{
- "id": curOrg.Id,
- "org_name": curOrg.OrgName,
- "org_short_name": curOrg.OrgShortName,
- "org_intro": curOrg.OrgIntroduction,
- "org_logo": curOrg.OrgLogo,
- "province": curOrg.Province,
- "city": curOrg.City,
- "district": curOrg.District,
- "address": curOrg.Address,
- }
-
- tempInfo, _ := service.GetOrgInfoTemplate(curOrg.Id)
- fmt.Println("teimpInfo", tempInfo)
- template_info := map[string]interface{}{
- "id": tempInfo.ID,
- "org_id": tempInfo.OrgId,
- "template_id": tempInfo.TemplateId,
- }
- var FiledList []*models.FiledConfig
-
- FiledList, _ = service.FindFiledByOrgId(curOrg.Id)
- if len(FiledList) == 0 {
- err := service.BatchInsertFiledConfig(curOrg.Id)
- if err == nil {
- FiledList, _ = service.FindFiledByOrgId(curOrg.Id)
-
- } else {
- utils.ErrorLog("字段批量插入失败:%v", err)
- }
- }
- curApp := adminUserInfo.OrgApps[adminUserInfo.CurrentOrgId][adminUserInfo.CurrentAppId]
- if curApp.OpenStatus != 1 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNeverCreateTypeApp)
- return
- }
-
- curAppUrlfors := adminUserInfo.AppUrlfors[adminUserInfo.CurrentAppId]
-
- subscibe := adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId]
- fmt.Println("subscibe", subscibe)
- if err := service.GetOrgSubscibeState(subscibe); err != nil {
- this.ErrorLog("没有机构订阅信息,数据有误")
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- orgApps := adminUserInfo.OrgApps[curOrg.Id]
- didRegistedForSCRM := false
- didRegistedForMall := false
- didRegistedForCDM := false
- for _, app := range orgApps {
- if app.AppType == 1 && app.OpenStatus == 1 {
- didRegistedForSCRM = true
- }
- if app.AppType == 4 && app.OpenStatus == 1 {
- didRegistedForCDM = true
- }
- if app.AppType == 5 && app.OpenStatus == 1 {
- didRegistedForMall = true
- }
- }
-
- this.SetSession("admin_user_info", adminUserInfo)
-
- this.ServeSuccessJSON(map[string]interface{}{
- "user": userInfo,
- "org": org,
- "urlfors": curAppUrlfors,
- "current_org_id": adminUserInfo.CurrentOrgId,
- "current_app_id": adminUserInfo.CurrentAppId,
- "subscibe": subscibe,
- "scrm_role_exist": didRegistedForSCRM,
- "cdm_role_exist": didRegistedForCDM,
- "mall_role_exist": didRegistedForMall,
- "template_info": template_info,
- "fileds": FiledList,
- })
- return
- }
- }
- }
-
-
-
-
-
-
- func (this *VerifyUserLoginAPIController) EditAdminUserInfo() {
- adminUserInfo := this.GetAdminUserInfo()
-
- avatar := this.GetString("avatar")
- name := this.GetString("name")
- if len(name) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMissingUserName)
- return
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- modifyErr := service.ModifyAdminUserInfo(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, avatar, "")
- if modifyErr != nil {
- this.ErrorLog("修改个人信息失败:%v", modifyErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
-
- } else {
- appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
- appRole.UserName = name
- appRole.Avatar = avatar
- this.ServeSuccessJSON(nil)
- }
- }
-
- type PersonAPIController struct {
- BaseAuthAPIController
- }
-
-
- func (this *PersonAPIController) CodeOfModifyPwd() {
- adminUserInfo := this.GetAdminUserInfo()
- mobile := adminUserInfo.AdminUser.Mobile
- if err := service.SMSSendVerificationCode(mobile); err != nil {
- utils.ErrorLog("修改密码发送验证码失败:%v", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else {
- this.ServeSuccessJSON(map[string]interface{}{
- "msg": "短信发送成功,有效期为10分钟",
- })
- }
- }
-
-
-
-
- func (this *PersonAPIController) ModifyPwd() {
- new_pwd := this.GetString("password")
- code := this.GetString("code")
- if len(new_pwd) == 0 || len(code) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- adminUserInfo := this.GetAdminUserInfo()
- mobile := adminUserInfo.AdminUser.Mobile
-
- redisClient := service.RedisClient()
- defer redisClient.Close()
- cachedCode, err := redisClient.Get("xt_modify_pwd_" + mobile).Result()
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
- return
- }
- if code != cachedCode {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
- return
- }
-
- if modifyErr := service.ModifyPassword(adminUserInfo.AdminUser.Id, new_pwd); modifyErr != nil {
- this.ErrorLog("修改密码失败:%v", modifyErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
-
- redisClient.Del("xt_modify_pwd_" + mobile)
- this.ServeSuccessJSON(map[string]interface{}{
- "msg": "密码已修改",
- })
- }
|