/** * @Title: * @Description: * @Author: Yuki Wong(iyuki0430@msn.com) * @Update: * @Date: 2020/7/20 11:50 * @File: AccountOpenAPI.go * @Software: GoLand **/ package AccountOpenAPI import ( "dsSupport/MyModel/Account/AccountService" "dsSupport/MyModel/MySwagger" "dsSupport/Utils/SessionUtil" "fmt" "github.com/gin-gonic/gin" "net/http" ) var globalSessions *SessionUtil.Manager type CurrentUserInfo struct { Name string `json:"name" example:"管理员"` Avatar string `json:"avatar" example:"admin.png"` Userid string `json:"userid" example:"00000001"` Email string `json:"email" example:"admin@edusoa.com"` Signature string `json:"signature" example:"海纳百川,有容乃大"` Title string `json:"title" example:"大数据专家"` Group string `json:"group" example:"东北师大理想股份有限公司-数智创新中心"` Tags []map[string]interface{} `json:"tags" example:"[{key:0,label:大数据},{key:1,label:人工智能},{key:2,label:物联网},{key:3,label:架构},{key:4,label:数据分析},{key:5,label:海纳百川}]"` NotifyCount int `json:"notifyCount" example:"12"` UnreadCount int `json:"unreadCount" example:"11"` Country string `json:"country" example:"China"` Geographic map[string]interface{} `json:"geographic" example:"{province:{label:吉林省,key:130000},city:{label:长春市,key:130000}}"` Address string `json:"address" example:"净月开发区"` Phone string `json:"phone" example:"400-0400-662"` } func init() { var err error globalSessions, err = SessionUtil.NewSessionManager("memory", "goSessionid", 3600) if err != nil { fmt.Println(err) return } go globalSessions.GC() fmt.Println("fd") } // 后台登陆 godoc // @Summary 后台登陆 // @Description json:"username" xorm:"not null comment('账号') VARCHAR(100)" example:"example" // @Description json:"password" xorm:"not null comment('密码') VARCHAR(100)" example:"123456" // @Tags account // @ID loginAccount // @Accept json // @Produce json // @Param input body MySwagger.AccountSwag true "账号密码" // @Success 200 {object} MySwagger.Result // @Failure 400 {object} MySwagger.Result // @Router /v1/openapi/account/login [post] func Login(c *gin.Context) { var raw MySwagger.AccountSwag if err := c.ShouldBindJSON(&raw); err != nil { c.JSON(http.StatusBadRequest, MySwagger.Result{Success: false, Message: "接入系统数据JSON格式错误"}) return } var w http.ResponseWriter = c.Writer var r *http.Request = c.Request sess := globalSessions.SessionStart(w, r) val := sess.Get("username") if val != nil { fmt.Println(val) } else { username := raw.Username password := raw.Password success, _ := AccountService.Login(username, password) if success { sess.Set("username", username) fmt.Println("set session") c.JSON(http.StatusOK, gin.H{ "status" : "ok", "type" : "account", "currentAuthority" : username, }) return } else { c.JSON(http.StatusOK, gin.H{ "status" : "error", "type" : "account", "currentAuthority" : "guest", }) return } } return } // 当前登陆者信息 godoc // @Summary 当前登陆者信息 // @Tags account // @ID currentUser // @Accept json // @Produce json // @Success 200 {object} MySwagger.Result // @Failure 400 {object} MySwagger.Result // @Router /v1/openapi/account/currentUser [post] func CurrentUser(c *gin.Context) { var w http.ResponseWriter = c.Writer var r *http.Request = c.Request sess := globalSessions.SessionStart(w, r) val := sess.Get("username") if val != nil { tags := make([]map[string]interface{}, 6) tags[0] = make(map[string]interface{}, 1) tags[0]["key"] = 0 tags[0]["label"] = "大数据" tags[1] = make(map[string]interface{}, 2) tags[1]["key"] = 1 tags[1]["label"] = "人工智能" tags[2] = make(map[string]interface{}, 3) tags[2]["key"] = 2 tags[2]["label"] = "物联网" tags[3] = make(map[string]interface{}, 4) tags[3]["key"] = 3 tags[3]["label"] = "架构" tags[4] = make(map[string]interface{}, 5) tags[4]["key"] = 4 tags[4]["label"] = "数据分析" tags[5] = make(map[string]interface{}, 6) tags[5]["key"] = 5 tags[5]["label"] = "海纳百川" geographic := make(map[string]interface{}) geographic["province"] = make(map[string]interface{}) province := make(map[string]interface{}) province["label"] = "吉林省" province["key"] = "130000" geographic["province"] = province geographic["city"] = make(map[string]interface{}) city := make(map[string]interface{}) city["label"] = "长春市" city["key"] = "130000" geographic["city"] = city c.JSON(http.StatusOK, CurrentUserInfo{ Name: "管理员", Avatar: "admin.png", Userid: "00000001", Email: "admin@edusoa.com", Signature: "海纳百川,有容乃大", Title: "大数据专家", Group: "东北师大理想股份有限公司-数智创新中心", Tags: tags, NotifyCount: 12, UnreadCount: 11, Country: "China", Geographic: geographic, Address: "净月开发区", Phone: "400-0400-662", }) return } else { c.JSON(http.StatusOK, gin.H{ "status" : "error", "message" : "失败", }) return } return } // 登出 godoc // @Summary 登出 // @Tags account // @ID currentUser // @Accept json // @Produce json // @Success 200 {object} MySwagger.Result // @Failure 400 {object} MySwagger.Result // @Router /v1/openapi/account/logout [post] func Logout(c *gin.Context) { var w http.ResponseWriter = c.Writer var r *http.Request = c.Request sess := globalSessions.SessionStart(w, r) val := sess.Get("username") if val != nil { //销毁 globalSessions.SessionDestroy(w, r) fmt.Println("session destroy") c.JSON(http.StatusOK, gin.H{ "status" : true, "message" : "已退出", }) return } else { c.JSON(http.StatusOK, gin.H{ "status" : false, "message" : "未登陆", }) return } return } // 是否已登陆 godoc // @Summary 是否已登陆 // @Tags account // @ID currentUser // @Accept json // @Produce json // @Success 200 {object} MySwagger.Result // @Failure 400 {object} MySwagger.Result // @Router /v1/openapi/account/isLogin [post] func IsLogin(c *gin.Context) { var w http.ResponseWriter = c.Writer var r *http.Request = c.Request sess := globalSessions.SessionStart(w, r) val := sess.Get("username") if val != nil { c.JSON(http.StatusOK, gin.H{ "status" : true, "message" : "已登陆", }) return } else { c.JSON(http.StatusOK, gin.H{ "status" : false, "message" : "未登陆", }) return } return }