base.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package frontc
  2. import (
  3. "github.com/astaxie/beego"
  4. "wuyebaoxiuapi/utils"
  5. "strconv"
  6. "wuyebaoxiuapi/models/frontm"
  7. "wuyebaoxiuapi/contanst"
  8. "wuyebaoxiuapi/libs"
  9. "strings"
  10. "time"
  11. "wuyebaoxiuapi/models/backgroundm"
  12. "github.com/patrickmn/go-cache"
  13. )
  14. type BaseController struct {
  15. BBaseController
  16. userInfo *backgroundm.Admin
  17. }
  18. type BBaseController struct {
  19. beego.Controller
  20. }
  21. const (
  22. MSG_OK = 1
  23. MSG_ERR = 0
  24. )
  25. func (self *BaseController) Prepare() {
  26. auth := "1|1"//self.Ctx.GetCookie(contanst.CACHE_USERINFO+"auth")
  27. println("auth:"+auth)
  28. authArr := strings.Split(auth,"|")
  29. var cuserid int
  30. var _ error
  31. if len(authArr) == 2 {
  32. userid := authArr[0]
  33. cuserid,_ = strconv.Atoi(userid)
  34. self.userInfo = self.findCacheUserInfo(cuserid)
  35. if self.userInfo == nil {
  36. self.userInfo,_= backgroundm.AdminGetById(cuserid)
  37. }
  38. }
  39. if cuserid <= 0 {
  40. self.Data["json"] = map[string]interface{}{"code": 0, "message": "登录过期"}
  41. self.ServeJSON()
  42. }
  43. }
  44. // 上传图片
  45. func (self *BaseController)UploadImage() {
  46. self.Prepare()
  47. url := self.GetString("url")
  48. imageUpload := libs.NewImageUpload()
  49. result := imageUpload.Upload(url)
  50. if result["status"] == MSG_ERR {
  51. self.ajaxMsg(MSG_ERR,result["message"],nil)
  52. }
  53. self.ajaxMsg(MSG_OK,"",result["image"])
  54. }
  55. func (self *BaseController)addPicture(data map[string]interface{})(int64, error) {
  56. picture := frontm.NewPicture()
  57. picture.CreateTime = int(time.Now().Unix())
  58. picture.Path = libs.ConvertString(data["image"])
  59. id ,err := picture.Add();
  60. if int(id) > 0 {
  61. repairUserPicture := frontm.NewRepairUserPicture()
  62. repairUserPicture.PictureId = int(id)
  63. if data["types"] == 2 {
  64. repairUserPicture.Types = 2
  65. repairUserPicture.RepairId = data["repairId"].(int)
  66. }else{
  67. repairUserPicture.Types = 1
  68. }
  69. repairUserPicture.UserId = self.userInfo.Id
  70. return repairUserPicture.Add();
  71. }
  72. return 0,err
  73. }
  74. func (self *BaseController)deletePicture(image string)(int64, error) {
  75. picture := frontm.NewPicture()
  76. repairUserPicture := frontm.NewRepairUserPicture()
  77. picture,err := picture.FindPictureByPath(image)
  78. if picture != nil {
  79. return repairUserPicture.DeleteByPictureId(picture.Id)
  80. }
  81. return 0,err
  82. }
  83. // 缓存用户信息
  84. func (self *BBaseController)cacheUserInfo(user *backgroundm.Admin) {
  85. authkey := libs.Md5([]byte( user.Password + user.Salt))
  86. self.Ctx.SetCookie(contanst.CACHE_USERINFO+"auth", strconv.Itoa(user.Id)+"|"+authkey, 7*86400)
  87. utils.Che.Set(contanst.CACHE_USERINFO+strconv.Itoa(user.Id), user, cache.DefaultExpiration)
  88. }
  89. // 获取缓存用户信息
  90. func (self *BaseController)findCacheUserInfo(userid int)(*backgroundm.Admin) {
  91. user,_ := utils.Che.Get(contanst.CACHE_USERINFO+strconv.Itoa(userid))
  92. println(user)
  93. if user != nil {
  94. return user.(*backgroundm.Admin)
  95. }
  96. return nil
  97. }
  98. //ajax返回
  99. func (self *BBaseController) ajaxMsg( msgno int,msg interface{},data interface{}) {
  100. self.Data["json"] = libs.NewResponse().AjaxMsg(msgno,msg,data)
  101. self.ServeJSON()
  102. self.StopRun()
  103. }
  104. // 是否POST提交
  105. func (self *BBaseController) isPost() bool {
  106. return self.Ctx.Request.Method == "POST"
  107. }
  108. //ajax返回 列表
  109. func (self *BBaseController) ajaxList( msgno int,msg interface{}, count int64, data interface{}) {
  110. self.Data["json"] = libs.NewResponse().AjaxList(msgno,msg,count,data)
  111. self.ServeJSON()
  112. self.StopRun()
  113. }
  114. // 注销用户信息
  115. func (this *BBaseController)unsetUserInfo(user *frontm.User,admin *backgroundm.Admin){
  116. if user != nil {
  117. user.Id = admin.Id
  118. user.LoginName = admin.LoginName
  119. user.Phone = admin.Phone
  120. user.CreateTime = admin.CreateTime
  121. user.Gender = admin.Gender
  122. user.NickName = admin.NickName
  123. user.AvatarUrl = admin.AvatarUrl
  124. user.Openid = admin.Openid
  125. user.School = admin.School
  126. user.DailyAddress = admin.DailyAddress
  127. user.Rating = admin.Rating
  128. user.Types = admin.Types
  129. }
  130. }