分销商城(微商城)接口项目

verify_login_controller.go 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package controllers
  2. import (
  3. "fmt"
  4. "net/url"
  5. "wsc-go/enums"
  6. "wsc-go/service"
  7. "wsc-go/utils"
  8. "github.com/astaxie/beego"
  9. )
  10. func VerifyUserLoginControllerRegistRouters() {
  11. beego.Router("/login", &VerifyUserLoginController{}, "get:Login")
  12. beego.Router("/logout", &VerifyUserLoginController{}, "get,post:Logout")
  13. beego.Router("/handle_error", &VerifyUserLoginController{}, "get:HandleError")
  14. beego.Router("/api/token/verify", &VerifyUserLoginAPIController{}, "post:VerifyToken")
  15. beego.Router("/api/admin/edit_info", &VerifyUserLoginAPIController{}, "post:EditAdminUserInfo")
  16. beego.Router("/api/password/code", &PersonAPIController{}, "post:CodeOfModifyPwd")
  17. beego.Router("/api/password/modify", &PersonAPIController{}, "post:ModifyPwd")
  18. }
  19. type VerifyUserLoginController struct {
  20. BaseViewController
  21. }
  22. // /login [get]
  23. // @param token?:string
  24. // @param relogin?:bool
  25. func (this *VerifyUserLoginController) Login() {
  26. token := this.Ctx.Input.Query("token")
  27. if len(token) > 0 { // 带 token 参数的一般是从 SSO 回调回来的
  28. utils.TraceLog("SSO Login 回调: token=%v", token)
  29. xtFrontEndDomain := beego.AppConfig.String("front_end_domain") + "?lt=" + token
  30. this.Redirect302(xtFrontEndDomain)
  31. } else {
  32. relogin, _ := this.GetBool("relogin", false)
  33. returnURL := url.QueryEscape(fmt.Sprintf("%v%v", beego.AppConfig.String("httpdomain"), this.Ctx.Request.RequestURI))
  34. ssoDomain := beego.AppConfig.String("sso_domain")
  35. ssoLoginURL := fmt.Sprintf("%v/login?returnurl=%v&app_type=3&relogin=%v", ssoDomain, returnURL, relogin)
  36. this.Redirect302(ssoLoginURL)
  37. }
  38. }
  39. // /logout [get/post]
  40. func (this *VerifyUserLoginController) Logout() {
  41. if this.Ctx.Request.Method == "GET" {
  42. this.DelSession("admin_user_info")
  43. this.Redirect302(fmt.Sprintf("%v/logout", beego.AppConfig.String("sso_domain")))
  44. } else if this.Ctx.Request.Method == "POST" {
  45. this.DelSession("admin_user_info")
  46. }
  47. }
  48. // /handle_error [get]
  49. // @param code:int
  50. func (this *VerifyUserLoginController) HandleError() {
  51. code, _ := this.GetInt("code")
  52. if code == enums.ErrorCodeNeverCreateTypeApp {
  53. ssoDomain := beego.AppConfig.String("sso_domain")
  54. createAppURL := fmt.Sprintf("%v/org/app/create", ssoDomain)
  55. this.Redirect302(createAppURL)
  56. } else if code == enums.ErrorCodeContactSuperAdminCreateTypeApp {
  57. ssoDomain := beego.AppConfig.String("sso_domain")
  58. hitURL := fmt.Sprintf("%v/create_app_hint", ssoDomain)
  59. this.Redirect302(hitURL)
  60. } else {
  61. this.Abort404()
  62. }
  63. }
  64. type VerifyUserLoginAPIController struct {
  65. BaseAPIController
  66. }
  67. // /api/token/verify [post]
  68. // @param token:string
  69. func (this *VerifyUserLoginAPIController) VerifyToken() {
  70. if this.Ctx.Request.Method == "OPTIONS" {
  71. this.Abort("200")
  72. } else {
  73. token := this.GetString("token")
  74. utils.TraceLog("token: %v", token)
  75. if len(token) == 0 {
  76. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  77. return
  78. }
  79. ip := utils.GetIP(this.Ctx.Request)
  80. sessionID := this.Ctx.GetCookie("s")
  81. this.TraceLog("cookie session id: %v", sessionID)
  82. adminUserInfo, err, errCode := service.VerifyToken(token, ip, sessionID)
  83. if err != nil {
  84. if errCode == 903 { // 未创建应用
  85. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNeverCreateTypeApp)
  86. } else if errCode == 904 { // 联系超管来开通
  87. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeContactSuperAdminCreateTypeApp)
  88. } else {
  89. utils.ErrorLog("令牌验证失败:%v", err)
  90. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInvalidToken)
  91. }
  92. return
  93. } else {
  94. this.SetSession("admin_user_info", adminUserInfo)
  95. adminUser := adminUserInfo.AdminUser
  96. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  97. userInfo := map[string]interface{}{
  98. "id": adminUser.Id,
  99. "mobile": adminUser.Mobile,
  100. "user_name": appRole.UserName,
  101. "avatar": appRole.Avatar,
  102. "intro": appRole.Intro,
  103. "user_type": appRole.UserType,
  104. "user_title": appRole.UserTitle,
  105. }
  106. curOrg := adminUserInfo.Orgs[adminUserInfo.CurrentOrgId]
  107. org := map[string]interface{}{
  108. "id": curOrg.Id,
  109. "org_name": curOrg.OrgName,
  110. "org_short_name": curOrg.OrgShortName,
  111. "org_intro": curOrg.OrgIntroduction,
  112. "org_logo": curOrg.OrgLogo,
  113. "province": curOrg.Province,
  114. "city": curOrg.City,
  115. "district": curOrg.District,
  116. "address": curOrg.Address,
  117. }
  118. curAppUrlfors := adminUserInfo.AppUrlfors[adminUserInfo.CurrentAppId]
  119. subscibe := adminUserInfo.Subscibes[adminUserInfo.CurrentOrgId]
  120. if err := service.GetOrgSubscibeState(subscibe); err != nil {
  121. this.ErrorLog("没有机构订阅信息,数据有误")
  122. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  123. return
  124. }
  125. orgApps := adminUserInfo.OrgApps[curOrg.Id]
  126. didRegistedForSCRM := false
  127. didRegistedForMall := false
  128. didRegistedForCDM := false
  129. didRegistedForXT := false
  130. for _, app := range orgApps {
  131. if app.AppType == 1 {
  132. didRegistedForSCRM = true
  133. }
  134. if app.AppType == 4 {
  135. didRegistedForCDM = true
  136. }
  137. if app.AppType == 5 {
  138. didRegistedForMall = true
  139. }
  140. if app.AppType == 3 {
  141. didRegistedForXT = true
  142. }
  143. }
  144. this.ServeSuccessJSON(map[string]interface{}{
  145. "user": userInfo,
  146. "org": org,
  147. "urlfors": curAppUrlfors,
  148. "current_org_id": adminUserInfo.CurrentOrgId,
  149. "current_app_id": adminUserInfo.CurrentAppId,
  150. "subscibe": subscibe,
  151. "scrm_role_exist": didRegistedForSCRM,
  152. "cdm_role_exist": didRegistedForCDM,
  153. "mall_role_exist": didRegistedForMall,
  154. "xt_role_exist": didRegistedForXT,
  155. })
  156. return
  157. }
  158. }
  159. }
  160. // /api/admin/edit_info [post]
  161. // @param avatar:string
  162. // @param name:string
  163. // @param opwd?:string 没有原始密码的时候,认为不修改密码
  164. // @param npwd?:string
  165. func (this *VerifyUserLoginAPIController) EditAdminUserInfo() {
  166. adminUserInfo := this.GetAdminUserInfo()
  167. avatar := this.GetString("avatar")
  168. name := this.GetString("name")
  169. if len(name) == 0 {
  170. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMissingUserName)
  171. return
  172. }
  173. // oldPwd := this.GetString("opwd")
  174. // newPwd := this.GetString("npwd")
  175. // modifyPwd := len(oldPwd) != 0
  176. // if modifyPwd {
  177. // if len(newPwd) == 0 {
  178. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  179. // this.ServeJSON()
  180. // return
  181. // }
  182. // pwdRight, err := service.IsPasswordRight(adminUserInfo.AdminUser.Id, oldPwd)
  183. // if err != nil {
  184. // utils.ErrorLog("判断旧密码是否错误失败:%v", err)
  185. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  186. // this.ServeJSON()
  187. // return
  188. // }
  189. // if !pwdRight {
  190. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeOldPasswordWrong)
  191. // this.ServeJSON()
  192. // return
  193. // }
  194. // } else {
  195. // newPwd = ""
  196. // }
  197. modifyErr := service.ModifyAdminUserInfo(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, avatar, "")
  198. if modifyErr != nil {
  199. this.ErrorLog("修改个人信息失败:%v", modifyErr)
  200. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  201. } else {
  202. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  203. appRole.UserName = name
  204. appRole.Avatar = avatar
  205. this.ServeSuccessJSON(nil)
  206. }
  207. }
  208. type PersonAPIController struct {
  209. BaseAuthAPIController
  210. }
  211. // /api/password/code [post]
  212. func (this *PersonAPIController) CodeOfModifyPwd() {
  213. adminUserInfo := this.GetAdminUserInfo()
  214. mobile := adminUserInfo.AdminUser.Mobile
  215. if err := service.SMSSendVerificationCode(mobile); err != nil {
  216. utils.ErrorLog("修改密码发送验证码失败:%v", err)
  217. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  218. return
  219. } else {
  220. this.ServeSuccessJSON(map[string]interface{}{
  221. "msg": "短信发送成功,有效期为10分钟",
  222. })
  223. }
  224. }
  225. // /api/password/modify [post]
  226. // @param password:string
  227. // @param code:string
  228. func (this *PersonAPIController) ModifyPwd() {
  229. new_pwd := this.GetString("password")
  230. code := this.GetString("code")
  231. if len(new_pwd) == 0 || len(code) == 0 {
  232. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  233. return
  234. }
  235. adminUserInfo := this.GetAdminUserInfo()
  236. mobile := adminUserInfo.AdminUser.Mobile
  237. redisClient := service.RedisClient()
  238. defer redisClient.Close()
  239. cachedCode, err := redisClient.Get("xt_modify_pwd_" + mobile).Result()
  240. if err != nil {
  241. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  242. return
  243. }
  244. if code != cachedCode {
  245. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  246. return
  247. }
  248. if modifyErr := service.ModifyPassword(adminUserInfo.AdminUser.Id, new_pwd); modifyErr != nil {
  249. this.ErrorLog("修改密码失败:%v", modifyErr)
  250. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  251. return
  252. }
  253. // 清除验证码
  254. redisClient.Del("xt_modify_pwd_" + mobile)
  255. this.ServeSuccessJSON(map[string]interface{}{
  256. "msg": "密码已修改",
  257. })
  258. }