|
| 1 | +/* |
| 2 | +* @Author: yongze.chen |
| 3 | +* @Date: 2017-06-19 22:27:09 |
| 4 | +* @Last Modified by: yongze.chen |
| 5 | +* @Last Modified time: 2017-06-22 11:15:33 |
| 6 | + */ |
| 7 | +package controllers |
| 8 | + |
| 9 | +import ( |
| 10 | + "github.com/astaxie/beego" |
| 11 | + "go_cron/libs" |
| 12 | + "go_cron/models" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + MSG_OK = 0 |
| 19 | + MSG_ERR = -1 |
| 20 | +) |
| 21 | + |
| 22 | +type BaseController struct { |
| 23 | + beego.Controller |
| 24 | + controllerName string |
| 25 | + actionName string |
| 26 | + user *models.User |
| 27 | + userId int |
| 28 | + userName string |
| 29 | + pageSize int |
| 30 | +} |
| 31 | + |
| 32 | +func (this *BaseController) Prepare() { |
| 33 | + this.pageSize = 20 |
| 34 | + controllerName, actionName := this.GetControllerAndAction() |
| 35 | + this.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10]) |
| 36 | + this.actionName = strings.ToLower(actionName) |
| 37 | + this.auth() |
| 38 | + |
| 39 | + this.Data["version"] = beego.AppConfig.String("version") |
| 40 | + this.Data["siteName"] = beego.AppConfig.String("site.name") |
| 41 | + this.Data["curRoute"] = this.controllerName + "." + this.actionName |
| 42 | + this.Data["curController"] = this.controllerName |
| 43 | + this.Data["curAction"] = this.actionName |
| 44 | + this.Data["loginUserId"] = this.userId |
| 45 | + this.Data["loginUserName"] = this.userName |
| 46 | + this.Data["menuTag"] = this.controllerName |
| 47 | +} |
| 48 | + |
| 49 | +//登录状态验证 |
| 50 | +func (this *BaseController) auth() { |
| 51 | + arr := strings.Split(this.Ctx.GetCookie("auth"), "|") |
| 52 | + if len(arr) == 2 { |
| 53 | + idstr, password := arr[0], arr[1] |
| 54 | + userId, _ := strconv.Atoi(idstr) |
| 55 | + if userId > 0 { |
| 56 | + user, err := models.UserGetById(userId) |
| 57 | + if err == nil && password == libs.Md5([]byte(this.getClientIp()+"|"+user.Password+user.Salt)) { |
| 58 | + this.userId = user.Id |
| 59 | + this.userName = user.UserName |
| 60 | + this.user = user |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if this.userId == 0 && (this.controllerName != "main" || |
| 66 | + (this.controllerName == "main" && this.actionName != "logout" && this.actionName != "login")) { |
| 67 | + this.redirect(beego.URLFor("MainController.Login")) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +//渲染模版 |
| 72 | +func (this *BaseController) display(tpl ...string) { |
| 73 | + var tplname string |
| 74 | + if len(tpl) > 0 { |
| 75 | + tplname = tpl[0] + ".html" |
| 76 | + } else { |
| 77 | + tplname = this.controllerName + "/" + this.actionName + ".html" |
| 78 | + } |
| 79 | + this.Layout = "public/layout.html" |
| 80 | + this.TplName = tplname |
| 81 | +} |
| 82 | + |
| 83 | +// 重定向 |
| 84 | +func (this *BaseController) redirect(url string) { |
| 85 | + this.Redirect(url, 302) |
| 86 | + this.StopRun() |
| 87 | +} |
| 88 | + |
| 89 | +// 是否POST提交 |
| 90 | +func (this *BaseController) isPost() bool { |
| 91 | + return this.Ctx.Request.Method == "POST" |
| 92 | +} |
| 93 | + |
| 94 | +// 显示错误信息 |
| 95 | +func (this *BaseController) showMsg(args ...string) { |
| 96 | + this.Data["message"] = args[0] |
| 97 | + redirect := this.Ctx.Request.Referer() |
| 98 | + if len(args) > 1 { |
| 99 | + redirect = args[1] |
| 100 | + } |
| 101 | + |
| 102 | + this.Data["redirect"] = redirect |
| 103 | + this.Data["pageTitle"] = "系统提示" |
| 104 | + this.display("error/message") |
| 105 | + this.Render() |
| 106 | + this.StopRun() |
| 107 | +} |
| 108 | + |
| 109 | +// 输出json |
| 110 | +func (this *BaseController) jsonResult(out interface{}) { |
| 111 | + this.Data["json"] = out |
| 112 | + this.ServeJSON() |
| 113 | + this.StopRun() |
| 114 | +} |
| 115 | + |
| 116 | +func (this *BaseController) ajaxMsg(msg interface{}, msgno int) { |
| 117 | + out := make(map[string]interface{}) |
| 118 | + out["status"] = msgno |
| 119 | + out["msg"] = msg |
| 120 | + |
| 121 | + this.jsonResult(out) |
| 122 | +} |
| 123 | + |
| 124 | +//获取用户IP地址 |
| 125 | +func (this *BaseController) getClientIp() string { |
| 126 | + s := strings.Split(this.Ctx.Request.RemoteAddr, ":") |
| 127 | + return s[0] |
| 128 | +} |
0 commit comments