123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- package controllers
-
- import (
- "XT_New/enums"
- "XT_New/models"
- "XT_New/service"
- "XT_New/utils"
- "fmt"
- "github.com/astaxie/beego"
- "net/url"
- "strconv"
- "strings"
- )
-
- 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
- }
-
- // /login [get]
- // @param token?:string
- // @param relogin?:bool
- func (this *VerifyUserLoginController) Login() {
- token := this.Ctx.Input.Query("token")
- if len(token) > 0 { // 带 token 参数的一般是从 SSO 回调回来的
- 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)
- }
- }
-
- // /logout [get/post]
- 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")
- }
- }
-
- // /handle_error [get]
- // @param code:int
- 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
- }
-
- // /api/token/verify [post]
- // @param token:string
- 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
- 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)
- }
- }
-
- var pruviews []*models.Purview
- var curAppUrlfors []string
- if len(curAppUrlfors) == 0 {
- if adminUser.Id == curOrg.Creator { //超级管理员
- urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(3)
- //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(4)
- //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(5)
- //urlfors, _, _ := service.GetSuperAdminUsersPurviewTreeAndUrlfors(6)
-
- curAppUrlfors = urlfors
- } else {
- appRole, _ := service.FindAdminUserIDA(appRole.Id)
- if appRole.Id > 0 && len(appRole.RoleIds) > 0 {
- role_arr := strings.Split(appRole.RoleIds, ",")
- var ids string
- for _, role_id := range role_arr {
- id, _ := strconv.ParseInt(role_id, 10, 64)
- purview_ids, _ := service.GetRolePurviewIds(id)
- if len(ids) == 0 {
- ids = purview_ids
- } else {
- ids = ids + "," + purview_ids
- }
- }
- if len(ids) != 0 {
- pruviews, _ = service.GetPurviewById(ids)
- for _, item := range pruviews {
- if item.Module == 3 && item.Parentid > 0 {
- fmt.Println(item.Urlfor)
- curAppUrlfors = append(curAppUrlfors, item.Urlfor)
- }
- }
- } else {
-
- curAppUrlfors = append(curAppUrlfors, "")
- }
- } else {
- curAppUrlfors = append(curAppUrlfors, "")
-
- }
- }
- }
-
- var didRegistedForSCRM bool = false
- var didRegistedForCDM bool = false
- var didRegistedForMall bool = false
-
- for _, item := range pruviews {
- if item.Module == 6 {
- didRegistedForSCRM = true
- }
- if item.Module == 4 {
- didRegistedForCDM = true
- }
- if item.Module == 7 {
- didRegistedForMall = true
- }
- }
-
- subscibe, _ := service.GetOrgSubscibe(adminUserInfo.CurrentOrgId)
-
- 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
- }
- }
- }
-
- // /api/admin/edit_info [post]
- // @param avatar:string
- // @param name:string
- // @param opwd?:string 没有原始密码的时候,认为不修改密码
- // @param npwd?:string
- func (this *VerifyUserLoginAPIController) EditAdminUserInfo() {
- adminUserInfo := this.GetAdminUserInfo()
-
- avatar := this.GetString("avatar")
- name := this.GetString("name")
- if len(name) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMissingUserName)
- return
- }
- // oldPwd := this.GetString("opwd")
- // newPwd := this.GetString("npwd")
- // modifyPwd := len(oldPwd) != 0
- // if modifyPwd {
- // if len(newPwd) == 0 {
- // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
- // this.ServeJSON()
- // return
- // }
- // pwdRight, err := service.IsPasswordRight(adminUserInfo.AdminUser.Id, oldPwd)
- // if err != nil {
- // utils.ErrorLog("判断旧密码是否错误失败:%v", err)
- // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- // this.ServeJSON()
- // return
- // }
- // if !pwdRight {
- // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeOldPasswordWrong)
- // this.ServeJSON()
- // return
- // }
- // } else {
- // newPwd = ""
- // }
- 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
- }
-
- // /api/password/code [post]
- 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分钟",
- })
- }
- }
-
- // /api/password/modify [post]
- // @param password:string
- // @param code:string
- 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": "密码已修改",
- })
- }
|