// 个人信息 func (this *MainController) Profile() { beego.ReadFromRequest(&this.Controller) user, _ := models.UserGetById(this.userId) if this.isPost() { flash := beego.NewFlash() user.Email = this.GetString("email") user.Update() password1 := this.GetString("password1") password2 := this.GetString("password2") if password1 != "" { if len(password1) < 6 { flash.Error("密码长度必须大于6位") flash.Store(&this.Controller) this.redirect(beego.URLFor(".Profile")) } else if password2 != password1 { flash.Error("两次输入的密码不一致") flash.Store(&this.Controller) this.redirect(beego.URLFor(".Profile")) } else { user.Salt = string(utils.RandomCreateBytes(10)) user.Password = libs.Md5([]byte(password1 + user.Salt)) user.Update() } } flash.Success("修改成功!") flash.Store(&this.Controller) this.redirect(beego.URLFor(".Profile")) } this.Data["pageTitle"] = "个人信息" this.Data["user"] = user this.display() }
//登录状态验证 func (this *BaseController) auth() { arr := strings.Split(this.Ctx.GetCookie("auth"), "|") if len(arr) == 2 { idstr, password := arr[0], arr[1] userId, _ := strconv.Atoi(idstr) if userId > 0 { user, err := models.UserGetById(userId) if err == nil && password == libs.Md5([]byte(this.getClientIp()+"|"+user.Password+user.Salt)) { this.userId = user.Id this.userName = user.UserName this.user = user } } } if this.userId == 0 && (this.controllerName != "main" || (this.controllerName == "main" && this.actionName != "logout" && this.actionName != "login")) { this.redirect(beego.UrlFor("MainController.Login")) } }
func (j *Job) Run() { defer func() { if err := recover(); err != nil { beego.Error(err, "\n", string(debug.Stack())) } }() t := time.Now() if j.Concurrent { j.running.Lock() defer j.running.Unlock() } if workPool != nil { workPool <- true defer func() { <-workPool }() } j.status = 1 defer func() { j.status = 0 }() bout, berr, err := j.runFunc() ut := time.Now().Sub(t) / time.Millisecond // 插入日志 log := new(models.TaskLog) log.TaskId = j.id log.Output = string(bout) log.Error = string(berr) log.ProcessTime = int(ut) log.CreateTime = t.Unix() if err != nil { log.Status = -1 log.Error = err.Error() + ":" + string(berr) } models.TaskLogAdd(log) // 更新上次执行时间 j.task.PrevTime = t.Unix() j.task.ExecuteTimes++ j.task.Update() // 发送邮件通知 if (j.task.Notify == 1 && err != nil) || j.task.Notify == 2 { user, uerr := models.UserGetById(j.task.UserId) if uerr != nil { return } title := "" if err != nil { title = fmt.Sprintf("任务执行结果通知 #%d: %s", j.task.Id, "失败") } else { title = fmt.Sprintf("任务执行结果通知 #%d: %s", j.task.Id, "成功") } data := make(map[string]interface{}) data["task_id"] = j.task.Id data["username"] = user.UserName data["task_name"] = j.task.TaskName data["start_time"] = beego.Date(t, "Y-m-d H:i:s") data["process_time"] = float64(ut) / 1000 if err != nil { data["status"] = "失败(" + err.Error() + ")" data["output"] = string(berr) } else { data["status"] = "成功" data["output"] = string(bout) } content := new(bytes.Buffer) mailTpl.Execute(content, data) ccList := make([]string, 0) if j.task.NotifyEmail != "" { ccList = strings.Split(j.task.NotifyEmail, "\n") } if !mail.SendMail(user.Email, user.UserName, title, content.String(), ccList) { beego.Error("发送邮件超时:", user.Email) } } }