common.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**********************************************
  2. ** @Des: base controller
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-07 16:54:40
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-09-18 10:28:01
  7. ***********************************************/
  8. package backgroundc
  9. import (
  10. "fmt"
  11. "strconv"
  12. "strings"
  13. "wuyebaoxiuapi/libs"
  14. "wuyebaoxiuapi/utils"
  15. "github.com/astaxie/beego"
  16. "wuyebaoxiuapi/models/backgroundm"
  17. "github.com/patrickmn/go-cache"
  18. )
  19. const (
  20. MSG_OK = 0
  21. MSG_ERR = -1
  22. )
  23. type BaseController struct {
  24. beego.Controller
  25. controllerName string
  26. actionName string
  27. user *backgroundm.Admin
  28. userId int
  29. userName string
  30. loginName string
  31. pageSize int
  32. allowUrl string
  33. }
  34. //前期准备
  35. func (self *BaseController) Prepare() {
  36. self.pageSize = 20
  37. controllerName, actionName := self.GetControllerAndAction()
  38. self.controllerName = strings.ToLower(controllerName[0 : len(controllerName)-10])
  39. self.actionName = strings.ToLower(actionName)
  40. self.Data["version"] = beego.AppConfig.String("version")
  41. self.Data["siteName"] = beego.AppConfig.String("site.name")
  42. self.Data["curRoute"] = self.controllerName + "." + self.actionName
  43. self.Data["curController"] = self.controllerName
  44. self.Data["curAction"] = self.actionName
  45. // noAuth := "ajaxsave/ajaxdel/table/loginin/loginout/getnodes/start"
  46. // isNoAuth := strings.Contains(noAuth, self.actionName)
  47. fmt.Println(self.controllerName)
  48. if (strings.Compare(self.controllerName, "apidoc")) != 0 {
  49. self.auth()
  50. }
  51. self.Data["loginUserId"] = self.userId
  52. self.Data["loginUserName"] = self.userName
  53. }
  54. //登录权限验证
  55. func (self *BaseController) auth() {
  56. arr := strings.Split(self.Ctx.GetCookie("auth"), "|")
  57. self.userId = 0
  58. if len(arr) == 2 {
  59. idstr, password := arr[0], arr[1]
  60. userId, _ := strconv.Atoi(idstr)
  61. if userId > 0 {
  62. var err error
  63. cheUser, found := utils.Che.Get("uid" + strconv.Itoa(userId))
  64. user := &backgroundm.Admin{}
  65. if found && cheUser != nil { //从缓存取用户
  66. user = cheUser.(*backgroundm.Admin)
  67. } else {
  68. user, err = backgroundm.AdminGetById(userId)
  69. utils.Che.Set("uid"+strconv.Itoa(userId), user, cache.DefaultExpiration)
  70. }
  71. if err == nil && password == libs.Md5([]byte(self.getClientIp()+"|"+user.Password+user.Salt)) {
  72. self.userId = user.Id
  73. self.loginName = user.LoginName
  74. self.userName = user.RealName
  75. self.user = user
  76. self.AdminAuth()
  77. }
  78. isHasAuth := strings.Contains(self.allowUrl, self.controllerName+"/"+self.actionName)
  79. //不需要权限检查
  80. noAuth := "ajaxsave/ajaxdel/table/loginin/loginout/getnodes/start/show/ajaxapisave/index/group/public/env/code/apidetail"
  81. isNoAuth := strings.Contains(noAuth, self.actionName)
  82. if isHasAuth == false && isNoAuth == false {
  83. self.Ctx.WriteString("没有权限")
  84. self.ajaxMsg("没有权限", MSG_ERR)
  85. return
  86. }
  87. }
  88. }
  89. if self.userId == 0 && (self.controllerName != "login" && self.actionName != "loginin") {
  90. self.redirect(beego.URLFor("LoginController.LoginIn"))
  91. }
  92. }
  93. func (self *BaseController) AdminAuth() {
  94. cheMen, found := utils.Che.Get("menu" + strconv.Itoa(self.user.Id))
  95. if found && cheMen != nil { //从缓存取菜单
  96. menu := cheMen.(*CheMenu)
  97. //fmt.Println("调用显示菜单")
  98. self.Data["SideMenu1"] = menu.List1 //一级菜单
  99. self.Data["SideMenu2"] = menu.List2 //二级菜单
  100. self.allowUrl = menu.AllowUrl
  101. } else {
  102. // 左侧导航栏
  103. filters := make([]interface{}, 0)
  104. filters = append(filters, "status", 1)
  105. if self.userId != 1 {
  106. //普通管理员
  107. adminAuthIds, _ := backgroundm.RoleAuthGetByIds(self.user.RoleIds)
  108. adminAuthIdArr := strings.Split(adminAuthIds, ",")
  109. filters = append(filters, "id__in", adminAuthIdArr)
  110. }
  111. result, _ := backgroundm.AuthGetList(1, 1000, filters...)
  112. list := make([]map[string]interface{}, len(result))
  113. list2 := make([]map[string]interface{}, len(result))
  114. allow_url := ""
  115. i, j := 0, 0
  116. for _, v := range result {
  117. if v.AuthUrl != " " || v.AuthUrl != "/" {
  118. allow_url += v.AuthUrl
  119. }
  120. row := make(map[string]interface{})
  121. if v.Pid == 1 && v.IsShow == 1 {
  122. row["Id"] = int(v.Id)
  123. row["Sort"] = v.Sort
  124. row["AuthName"] = v.AuthName
  125. row["AuthUrl"] = v.AuthUrl
  126. row["Icon"] = v.Icon
  127. row["Pid"] = int(v.Pid)
  128. list[i] = row
  129. i++
  130. }
  131. if v.Pid != 1 && v.IsShow == 1 {
  132. row["Id"] = int(v.Id)
  133. row["Sort"] = v.Sort
  134. row["AuthName"] = v.AuthName
  135. row["AuthUrl"] = v.AuthUrl
  136. row["Icon"] = v.Icon
  137. row["Pid"] = int(v.Pid)
  138. list2[j] = row
  139. j++
  140. }
  141. }
  142. self.Data["SideMenu1"] = list[:i] //一级菜单
  143. self.Data["SideMenu2"] = list2[:j] //二级菜单
  144. self.allowUrl = allow_url + "/home/index"
  145. cheM := &CheMenu{}
  146. cheM.AllowUrl = self.allowUrl
  147. cheM.List1 = self.Data["SideMenu1"].([]map[string]interface{})
  148. cheM.List2 = self.Data["SideMenu2"].([]map[string]interface{})
  149. utils.Che.Set("menu"+strconv.Itoa(self.user.Id), cheM, cache.DefaultExpiration)
  150. }
  151. }
  152. type CheMenu struct {
  153. List1 []map[string]interface{}
  154. List2 []map[string]interface{}
  155. AllowUrl string
  156. }
  157. // 是否POST提交
  158. func (self *BaseController) isPost() bool {
  159. return self.Ctx.Request.Method == "POST"
  160. }
  161. //获取用户IP地址
  162. func (self *BaseController) getClientIp() string {
  163. s := self.Ctx.Request.RemoteAddr
  164. l := strings.LastIndex(s, ":")
  165. return s[0:l]
  166. }
  167. // 重定向
  168. func (self *BaseController) redirect(url string) {
  169. self.Redirect(url, 302)
  170. self.StopRun()
  171. }
  172. //加载模板
  173. func (self *BaseController) display(tpl ...string) {
  174. var tplname string
  175. if len(tpl) > 0 {
  176. tplname = strings.Join([]string{tpl[0], "html"}, ".")
  177. } else {
  178. tplname = self.controllerName + "/" + self.actionName + ".html"
  179. }
  180. self.Layout = "public/layout.html"
  181. self.TplName = tplname
  182. }
  183. //ajax返回
  184. func (self *BaseController) ajaxMsg(msg interface{}, msgno int) {
  185. out := make(map[string]interface{})
  186. out["status"] = msgno
  187. out["message"] = msg
  188. self.Data["json"] = out
  189. self.ServeJSON()
  190. self.StopRun()
  191. }
  192. //ajax返回 列表
  193. func (self *BaseController) ajaxList(msg interface{}, msgno int, count int64, data interface{}) {
  194. out := make(map[string]interface{})
  195. out["code"] = msgno
  196. out["msg"] = msg
  197. out["count"] = count
  198. out["data"] = data
  199. self.Data["json"] = out
  200. self.ServeJSON()
  201. self.StopRun()
  202. }
  203. //分组公共方法
  204. type groupList struct {
  205. Id int
  206. GroupName string
  207. }
  208. func groupLists() (gl []groupList) {
  209. groupFilters := make([]interface{}, 0)
  210. groupFilters = append(groupFilters, "status", 1)
  211. groupResult, _ := backgroundm.GroupGetList(1, 1000, groupFilters...)
  212. for _, gv := range groupResult {
  213. groupRow := groupList{}
  214. groupRow.Id = int(gv.Id)
  215. groupRow.GroupName = gv.GroupName
  216. gl = append(gl, groupRow)
  217. }
  218. return gl
  219. }
  220. //获取单个分组信息
  221. func getGroupInfo(gl []groupList, groupId int) (groupInfo groupList) {
  222. for _, v := range gl {
  223. if v.Id == groupId {
  224. groupInfo = v
  225. }
  226. }
  227. return
  228. }
  229. type sourceList struct {
  230. Id int
  231. SourceName string
  232. GroupId int
  233. GroupName string
  234. }
  235. func sourceLists() (sl []sourceList) {
  236. grouplists := groupLists()
  237. var groupinfo groupList
  238. sourceFilters := make([]interface{}, 0)
  239. sourceFilters = append(sourceFilters, "status", 1)
  240. sourceResult, _ := backgroundm.ApiSourceGetList(1, 1000, sourceFilters...)
  241. for _, sv := range sourceResult {
  242. sourceRow := sourceList{}
  243. sourceRow.Id = int(sv.Id)
  244. sourceRow.GroupId = sv.GroupId
  245. groupinfo = getGroupInfo(grouplists, sv.GroupId)
  246. sourceRow.GroupName = groupinfo.GroupName
  247. sourceRow.SourceName = sv.SourceName
  248. sl = append(sl, sourceRow)
  249. }
  250. return sl
  251. }
  252. func getSourceInfo(gl []sourceList, sourceId int) (sourceInfo sourceList) {
  253. for _, v := range gl {
  254. if v.Id == sourceId {
  255. sourceInfo = v
  256. }
  257. }
  258. return
  259. }
  260. type envList struct {
  261. Id int
  262. EnvName string
  263. EnvHost string
  264. }
  265. func envLists() (sl []envList) {
  266. envFilters := make([]interface{}, 0)
  267. envFilters = append(envFilters, "status__in", 1)
  268. envResult, _ := backgroundm.EnvGetList(1, 1000, envFilters...)
  269. for _, sv := range envResult {
  270. envRow := envList{}
  271. envRow.Id = int(sv.Id)
  272. envRow.EnvName = sv.EnvName
  273. envRow.EnvHost = sv.EnvHost
  274. sl = append(sl, envRow)
  275. }
  276. return sl
  277. }
  278. type templateList struct {
  279. Id int
  280. TemplateName string
  281. Detail string
  282. }
  283. func templateLists() (sl []templateList) {
  284. templateFilters := make([]interface{}, 0)
  285. templateFilters = append(templateFilters, "status", 1)
  286. templateResult, _ := backgroundm.TemplateGetList(1, 1000, templateFilters...)
  287. for _, sv := range templateResult {
  288. templateRow := templateList{}
  289. templateRow.Id = int(sv.Id)
  290. templateRow.TemplateName = sv.TemplateName
  291. templateRow.Detail = sv.Detail
  292. sl = append(sl, templateRow)
  293. }
  294. return sl
  295. }
  296. type codeList struct {
  297. Id int
  298. Code string
  299. Desc string
  300. Detail string
  301. }
  302. func codeLists() (sl []codeList) {
  303. codeFilters := make([]interface{}, 0)
  304. codeFilters = append(codeFilters, "status", 1)
  305. codeResult, _ := backgroundm.CodeGetList(1, 1000, codeFilters...)
  306. for _, sv := range codeResult {
  307. codeRow := codeList{}
  308. codeRow.Id = int(sv.Id)
  309. codeRow.Code = sv.Code
  310. codeRow.Desc = sv.Desc
  311. codeRow.Detail = sv.Detail
  312. sl = append(sl, codeRow)
  313. }
  314. return sl
  315. }
  316. type apiPublicList struct {
  317. Id int
  318. ApiPublicName string
  319. Sort int
  320. }
  321. func apiPublicLists() (sl []apiPublicList) {
  322. apiPublicFilters := make([]interface{}, 0)
  323. apiPublicFilters = append(apiPublicFilters, "status", 1)
  324. apiPublicResult, _ := backgroundm.ApiPublicGetList(1, 1000, apiPublicFilters...)
  325. for _, sv := range apiPublicResult {
  326. apiPublicRow := apiPublicList{}
  327. apiPublicRow.Id = int(sv.Id)
  328. apiPublicRow.ApiPublicName = sv.ApiPublicName
  329. apiPublicRow.Sort = sv.Sort
  330. sl = append(sl, apiPublicRow)
  331. }
  332. return sl
  333. }
  334. type UploadController struct {
  335. beego.Controller
  336. }
  337. func (self *UploadController)UploadImage() {
  338. url := self.GetString("value")
  339. imageUpload := libs.NewImageUpload()
  340. result := imageUpload.Upload(url)
  341. self.Data["json"] = result
  342. self.ServeJSON()
  343. self.StopRun()
  344. }