//判断是否登陆 func CheckAdminLogin(c *beego.Controller, t int64) { id, _ := c.GetSession("adminid").(int) username, _ := c.GetSession("adminname").(string) adminrole, _ := c.GetSession("adminrole").(int) if id == 0 || username == "" || adminrole == 0 { switch t { case 0: c.Redirect("/admin/login", 301) case 1: //返回JSON json := &models.TipJSON{} json.Status = models.TipError json.Message = "请先登录,再执行此操作" json.ReturnUrl = "/admin/login" c.Data["json"] = json c.ServeJson() c.StopRun() } } c.Data["AdminName"] = username c.Data["AdminId"] = id c.Layout = "admin/layout.tpl" }
//上传图片 func WebUploadImage(this *beego.Controller) { //filename := this.Input().Get("Filename") f, h, _ := this.GetFile("Filedata") filename := h.Filename f.Close() //关闭,减少缓存 ext := filename[strings.LastIndex(filename, ".")+1:] //获取扩展名 if !strings.Contains(allowImageType, ext) { fmt.Println(filename) this.Ctx.WriteString("{\"state\":\"0\"}") this.StopRun() } newname := strconv.FormatInt(time.Now().Unix(), 10) + "_" + filename err := this.SaveToFile("Filedata", uploadimage+newname) state := "SUCCESS" if err != nil { fmt.Println(err) state = "0" } state = "1" url := website + uploadimage + newname //this.Ctx.WriteString("{'original':'" + filename + "','url':'" + url + "','title':'" + this.Input().Get("pictitle") + "','state':'" + state + "'}") //this.Ctx.WriteString("{\"state\": \"" + state + "\", \"url\": \"" + url + "\", \"title\": \"\",\"original\": \"" + filename + "\"}") this.Ctx.WriteString("{\"status\": " + state + ", \"msg\": \"上传文件成功!\", \"name\": \"1.jpg\", \"path\": \"" + url + "\", \"thumb\": \"" + url + "\", \"size\": 0, \"ext\": \"" + ext + "\"}") this.StopRun() }
//文件管理 func FilesManager(this *beego.Controller) { strRet := "" callbackjson := "{\"state\": \"SUCCESS\",\"list\": [" total := 0 err := filepath.Walk(uploadfile, func(path string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { return nil } ext := path[strings.LastIndex(path, ".")+1:] if strings.Contains(allowFileType, ext) { strRet += (path + "ue_separate_ue") callbackjson += "{\"url\": \"/" + uploadfile + f.Name() + "\"}," total++ fmt.Println("allow:", path) } return nil }) if err != nil { fmt.Printf("filepath.Walk() returned %v\n", err) } callbackjson += "],\"start\": 0,\"total\": " + strconv.Itoa(total) + "}" fmt.Println(strRet) this.Ctx.WriteString(callbackjson) this.StopRun() }
//提示错误页面 func EchoErrorPage(c *beego.Controller, message string, url string) { isGoback := false if url == "" { isGoback = true } c.Data["Message"] = message c.Data["Url"] = url c.Data["IsGoback"] = isGoback c.Layout = "admin/error.tpl" c.StopRun() }
//信息提示,如果异步,返回json、如果同步,则页面提示并返回 func EchoTip(c *beego.Controller, json *models.TipJSON) { if c.IsAjax() { //异步提交 c.Data["json"] = json c.ServeJson() c.StopRun() } else { tpl := "admin/error.tpl" if json.Status == models.TipSuccess { tpl = "admin/success.tpl" } c.Data["Tip"] = json c.Layout = tpl //此处不能用 c.StopRun() 返回,否则会空白页面! //c.StopRun() return } }
//远程抓图 func CatchImage(this *beego.Controller) { //fmt.Println(this.Ctx.Request.Body) urls := this.GetStrings("source[]") //fmt.Println(urls) callbackjson := "{\"state\": \"SUCCESS\",\"list\": [" if len(urls) > 0 { for _, v := range urls { //去掉最后的!后面部分 l := v //判断扩展名是否合法 ext := l[strings.LastIndex(l, ".")+1:] if strings.Contains(allowImageType, ext) { //获取文件名 filename := l[strings.LastIndex(l, "/")+1:] newname := strconv.FormatInt(time.Now().Unix(), 10) + "_" + filename res, err := http.Get(l) defer res.Body.Close() if err != nil { callbackjson += "{\"url\": \"\",\"source\": \"" + l + "\",\"state\": \"ERROR\"}," fmt.Println("Error:远程抓取失败;", err) } else { dst, err := os.Create(uploadimage + newname) if err != nil { callbackjson += "{\"url\": \"\",\"source\": \"" + l + "\",\"state\": \"ERROR\"}," fmt.Println("Error:保存失败;", err) } else { callbackjson += "{\"url\": \"" + uploadimage + newname + "\",\"source\": \"" + l + "\",\"state\": \"SUCCESS\"}," io.Copy(dst, res.Body) } } } else { callbackjson += "{\"url\": \"\",\"source\": \"" + l + "\",\"state\": \"ERROR\"}," } //fmt.Println(l) } } callbackjson += "]}" this.Ctx.WriteString(callbackjson) this.StopRun() return }
func UploadFile(this *beego.Controller) { //filename := this.Input().Get("Filename") f, h, _ := this.GetFile("upfile") filename := h.Filename f.Close() //关闭,减少缓存 index := strings.LastIndex(filename, ".") filetype := "" if index == -1 { this.Ctx.WriteString("{\"state\":\"FAILED\"}") this.StopRun() } filetype = filename[index:] ext := filetype[1:] if !strings.Contains(allowFileType, ext) { this.Ctx.WriteString("{\"state\":\"FAILED\"}") this.StopRun() } newname := strconv.FormatInt(time.Now().Unix(), 10) + "_" + filename err := this.SaveToFile("upfile", uploadfile+newname) state := "SUCCESS" if err != nil { fmt.Println(err) state = "FAILED" } url := website + uploadfile + newname this.Ctx.WriteString("{\"state\": \"" + state + "\", \"url\": \"" + url + "\", \"title\": \"\",\"original\": \"" + filename + "\"}") this.StopRun() }
//上传图片 func UploadImage(this *beego.Controller) { f, h, _ := this.GetFile("upfile") filename := h.Filename f.Close() //关闭,减少缓存 //获取扩展名 ext := filename[strings.LastIndex(filename, ".")+1:] if !strings.Contains(allowImageType, ext) { fmt.Println(filename) this.Ctx.WriteString("{\"state\":\"FAILED\"}") this.StopRun() } newname := strconv.FormatInt(time.Now().Unix(), 10) + "_" + filename err := this.SaveToFile("upfile", uploadimage+newname) state := "SUCCESS" if err != nil { fmt.Println(err) state = "FAILED" } url := website + uploadimage + newname this.Ctx.WriteString("{\"state\": \"" + state + "\", \"url\": \"" + url + "\", \"title\": \"\",\"original\": \"" + filename + "\"}") this.StopRun() }
func Config(this *beego.Controller) { //this.ServeJson(configJson) this.Ctx.WriteString(string(configJson)) this.StopRun() }