package frontc import ( "github.com/astaxie/beego" "wuyebaoxiuapi/utils" "strconv" "wuyebaoxiuapi/models/frontm" "wuyebaoxiuapi/contanst" "wuyebaoxiuapi/libs" "strings" "time" "wuyebaoxiuapi/models/backgroundm" "github.com/patrickmn/go-cache" ) type BaseController struct { BBaseController userInfo *backgroundm.Admin } type BBaseController struct { beego.Controller } const ( MSG_OK = 1 MSG_ERR = 0 ) func (self *BaseController) Prepare() { auth := "1|1"//self.Ctx.GetCookie(contanst.CACHE_USERINFO+"auth") println("auth:"+auth) authArr := strings.Split(auth,"|") var cuserid int var _ error if len(authArr) == 2 { userid := authArr[0] cuserid,_ = strconv.Atoi(userid) self.userInfo = self.findCacheUserInfo(cuserid) if self.userInfo == nil { self.userInfo,_= backgroundm.AdminGetById(cuserid) } } if cuserid <= 0 { self.Data["json"] = map[string]interface{}{"code": 0, "message": "登录过期"} self.ServeJSON() } } // 上传图片 func (self *BaseController)UploadImage() { self.Prepare() url := self.GetString("url") imageUpload := libs.NewImageUpload() result := imageUpload.Upload(url) if result["status"] == MSG_ERR { self.ajaxMsg(MSG_ERR,result["message"],nil) } self.ajaxMsg(MSG_OK,"",result["image"]) } func (self *BaseController)addPicture(data map[string]interface{})(int64, error) { picture := frontm.NewPicture() picture.CreateTime = int(time.Now().Unix()) picture.Path = libs.ConvertString(data["image"]) id ,err := picture.Add(); if int(id) > 0 { repairUserPicture := frontm.NewRepairUserPicture() repairUserPicture.PictureId = int(id) if data["types"] == 2 { repairUserPicture.Types = 2 repairUserPicture.RepairId = data["repairId"].(int) }else{ repairUserPicture.Types = 1 } repairUserPicture.UserId = self.userInfo.Id return repairUserPicture.Add(); } return 0,err } func (self *BaseController)deletePicture(image string)(int64, error) { picture := frontm.NewPicture() repairUserPicture := frontm.NewRepairUserPicture() picture,err := picture.FindPictureByPath(image) if picture != nil { return repairUserPicture.DeleteByPictureId(picture.Id) } return 0,err } // 缓存用户信息 func (self *BBaseController)cacheUserInfo(user *backgroundm.Admin) { authkey := libs.Md5([]byte( user.Password + user.Salt)) self.Ctx.SetCookie(contanst.CACHE_USERINFO+"auth", strconv.Itoa(user.Id)+"|"+authkey, 7*86400) utils.Che.Set(contanst.CACHE_USERINFO+strconv.Itoa(user.Id), user, cache.DefaultExpiration) } // 获取缓存用户信息 func (self *BaseController)findCacheUserInfo(userid int)(*backgroundm.Admin) { user,_ := utils.Che.Get(contanst.CACHE_USERINFO+strconv.Itoa(userid)) println(user) if user != nil { return user.(*backgroundm.Admin) } return nil } //ajax返回 func (self *BBaseController) ajaxMsg( msgno int,msg interface{},data interface{}) { self.Data["json"] = libs.NewResponse().AjaxMsg(msgno,msg,data) self.ServeJSON() self.StopRun() } // 是否POST提交 func (self *BBaseController) isPost() bool { return self.Ctx.Request.Method == "POST" } //ajax返回 列表 func (self *BBaseController) ajaxList( msgno int,msg interface{}, count int64, data interface{}) { self.Data["json"] = libs.NewResponse().AjaxList(msgno,msg,count,data) self.ServeJSON() self.StopRun() } // 注销用户信息 func (this *BBaseController)unsetUserInfo(user *frontm.User,admin *backgroundm.Admin){ if user != nil { user.Id = admin.Id user.LoginName = admin.LoginName user.Phone = admin.Phone user.CreateTime = admin.CreateTime user.Gender = admin.Gender user.NickName = admin.NickName user.AvatarUrl = admin.AvatarUrl user.Openid = admin.Openid user.School = admin.School user.DailyAddress = admin.DailyAddress user.Rating = admin.Rating user.Types = admin.Types } }