蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

345 lines
10 KiB

  1. package comm
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. "applet/app/enum"
  7. "applet/app/md"
  8. "applet/app/svc"
  9. "applet/app/svc/sys_cfg"
  10. "applet/app/utils"
  11. "applet/app/utils/cache"
  12. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  13. enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  14. "fmt"
  15. sts20150401 "github.com/alibabacloud-go/sts-20150401/v2/client"
  16. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  17. "github.com/gin-gonic/gin"
  18. "os"
  19. "strings"
  20. )
  21. // MenuList
  22. // @Summary 通用请求-权限列表-菜单栏列表(获取)
  23. // @Tags 权限列表
  24. // @Description 菜单栏列表(获取)
  25. // @Accept json
  26. // @Produce json
  27. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  28. // @Success 200 {object} map[string]interface{} "具体路由"
  29. // @Failure 400 {object} md.Response "具体错误"
  30. // @Router /api/comm/getMenuList [POST]
  31. func MenuList(c *gin.Context) {
  32. engine := db.Db
  33. admin := svc.GetUser(c)
  34. qrcodeWithBatchRecordsDb := implement.NewPermissionGroupDb(engine)
  35. groupList, err := qrcodeWithBatchRecordsDb.FindPermissionGroup()
  36. if err != nil {
  37. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  38. return
  39. }
  40. // 1、查询出当前用户所有角色
  41. adminRoleDb := implement.NewAdminRoleDb(engine)
  42. roles, err := adminRoleDb.FindAdminRole(admin.AdmId)
  43. if err != nil {
  44. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  45. return
  46. }
  47. roleDb := implement.NewRoleDb(engine, 0)
  48. var adminHasPermissionGroupIds []string
  49. for _, v := range *roles {
  50. list, _, err1 := roleDb.FindPermissionGroupByRole(v.RoleId)
  51. if err1 != nil {
  52. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  53. return
  54. }
  55. for _, v1 := range list {
  56. adminHasPermissionGroupIds = append(adminHasPermissionGroupIds, utils.IntToStr(v1.PermissionGroup.Id))
  57. }
  58. }
  59. var tempRespMap = map[string]*md.PermissionGroupListResp{}
  60. var tempRespMapKeys []string
  61. for _, v := range *groupList {
  62. var isCheck bool
  63. if admin.IsSuperAdministrator == enum.IsSuperAdministratorTure {
  64. isCheck = true
  65. } else {
  66. isCheck = false
  67. }
  68. if utils.InArr(utils.IntToStr(v.Id), adminHasPermissionGroupIds) {
  69. isCheck = true
  70. }
  71. if v.State == enum.PermissionGroupStateForDiscard {
  72. isCheck = false
  73. }
  74. tempRespMap[utils.IntToStr(v.Id)] = &md.PermissionGroupListResp{
  75. Id: v.Id,
  76. Name: v.Name,
  77. Key: v.Key,
  78. State: v.State,
  79. ParentId: v.ParentId,
  80. CreateAt: v.CreateAt,
  81. UpdateAt: v.UpdateAt,
  82. IsCheck: isCheck,
  83. }
  84. tempRespMapKeys = append(tempRespMapKeys, utils.IntToStr(v.Id))
  85. }
  86. for _, v := range tempRespMap {
  87. if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId != 0 {
  88. tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
  89. }
  90. }
  91. for _, v := range tempRespMap {
  92. if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId == 0 {
  93. tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
  94. }
  95. }
  96. var resp []*md.PermissionGroupListResp
  97. for _, v := range tempRespMapKeys {
  98. if tempRespMap[v].ParentId == 0 {
  99. resp = append(resp, tempRespMap[v])
  100. }
  101. }
  102. e.OutSuc(c, map[string]interface{}{
  103. "list": resp,
  104. "state": []map[string]interface{}{
  105. {
  106. "name": enum.PermissionGroupState(enum.PermissionGroupStateForNormal).String(),
  107. "value": enum.PermissionGroupStateForNormal,
  108. },
  109. {
  110. "name": enum.PermissionGroupState(enum.PermissionGroupStateForDiscard).String(),
  111. "value": enum.PermissionGroupStateForDiscard,
  112. },
  113. },
  114. }, nil)
  115. return
  116. }
  117. type ImgReqUploadReq struct {
  118. FileName string `json:"file_name" binding:"required" example:"文件名"`
  119. ContentType string `json:"content_type,required" binding:"required" example:"image/jpeg"`
  120. }
  121. type ImgReqUploadResp struct {
  122. SignUrl string `json:"sign_url" example:"签名上传url"`
  123. }
  124. // GetOssUrl
  125. // @Summary 通用请求-对象存储-上传许可链接(获取)
  126. // @Tags 对象存储
  127. // @Description 上传许可链接(获取)
  128. // @Accept json
  129. // @Produce json
  130. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  131. // @Param req body comm.ImgReqUploadReq true "签名上传url"
  132. // @Success 200 {string} "许可链接"
  133. // @Failure 400 {object} md.Response "具体错误"
  134. // @Router /api/comm/getOssUrl [POST]
  135. func GetOssUrl(c *gin.Context) {
  136. var args ImgReqUploadReq
  137. err := c.ShouldBindJSON(&args)
  138. if err != nil {
  139. err = svc.HandleValidateErr(err)
  140. err1 := err.(e.E)
  141. e.OutErr(c, err1.Code, err1.Error())
  142. return
  143. }
  144. CommOss(c, args)
  145. }
  146. func CommOss(c *gin.Context, args ImgReqUploadReq) {
  147. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  148. sysCfgs, err := sysCfgDb.SysCfgGetAll()
  149. if err != nil {
  150. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  151. return
  152. }
  153. if sysCfgs == nil {
  154. e.OutErr(c, e.ERR_CFG_CACHE, nil)
  155. return
  156. }
  157. cfgMap := make(map[string]string, len(*sysCfgs))
  158. for _, cfg := range *sysCfgs {
  159. cfgMap[cfg.Key] = cfg.Val
  160. }
  161. endpoint := cfgMap[enum2.AliyunOssEndpoint]
  162. bucketName := cfgMap[enum2.AliyunOssBucketName]
  163. ossBucketScheme := cfgMap[enum2.AliyunOssBucketScheme]
  164. accessKeyID := cfgMap[enum2.AliyunOssAccessKeyID]
  165. accessKeySecret := cfgMap[enum2.AliyunOssAccessKeySecret]
  166. if cfg.Prd {
  167. accessKeyID = os.Getenv(enum2.AliyunOssAccessKeyID)
  168. accessKeySecret = os.Getenv(enum2.AliyunOssAccessKeySecret)
  169. }
  170. // 创建OSSClient实例。
  171. client, err := oss.New(ossBucketScheme+"://"+endpoint, accessKeyID, accessKeySecret)
  172. if err != nil {
  173. e.OutErr(c, e.ERR, err.Error())
  174. return
  175. }
  176. // 获取存储空间。
  177. bucket, err := client.Bucket(bucketName)
  178. if err != nil {
  179. e.OutErr(c, e.ERR, err.Error())
  180. return
  181. }
  182. options := []oss.Option{
  183. oss.ContentType(args.ContentType),
  184. }
  185. signedURL, err := bucket.SignURL(args.FileName, oss.HTTPPut, 60*5, options...)
  186. if err != nil {
  187. e.OutErr(c, e.ERR_AES_ENCODE, err.Error())
  188. return
  189. }
  190. e.OutSuc(c, signedURL, nil)
  191. }
  192. const STSVoucherRedisKey = "STS_Voucher_Cache_Key"
  193. type GetSTSVoucherResp struct {
  194. STSToken sts20150401.AssumeRoleResponseBodyCredentials `json:"sts_token"` // STS 凭证
  195. Bucket string `json:"bucket"` // oss 桶名称
  196. Region string `json:"region"` // 所在地域
  197. }
  198. // GetSTSVoucher
  199. // @Summary 通用请求-打包机使用-STS临时访问凭证(获取)
  200. // @Tags 打包机使用
  201. // @Description STS临时访问凭证(获取)
  202. // @Accept json
  203. // @Produce json
  204. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  205. // @Success 200 {object} comm.GetSTSVoucherResp "凭证及其他信息"
  206. // @Failure 400 {object} md.Response "具体错误"
  207. // @Router /api/getSTSVoucher [GET]
  208. func GetSTSVoucher(c *gin.Context) {
  209. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  210. sysCfgs, err := sysCfgDb.SysCfgGetAll()
  211. if err != nil {
  212. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  213. return
  214. }
  215. if sysCfgs == nil {
  216. e.OutErr(c, e.ERR_CFG_CACHE, nil)
  217. return
  218. }
  219. cfgMap := make(map[string]string, len(*sysCfgs))
  220. for _, cfg := range *sysCfgs {
  221. cfgMap[cfg.Key] = cfg.Val
  222. }
  223. endpoint := cfgMap[enum2.AliyunOssEndpoint]
  224. redisKey := STSVoucherRedisKey
  225. redisValue, err := cache.GetString(redisKey)
  226. if err != nil {
  227. if err.Error() == "redigo: nil returned" {
  228. assumeRoleAccessKeyID := cfgMap[enum2.AliyunOssAssumeRoleAccessKeyID]
  229. assumeRoleAccessKeySecret := cfgMap[enum2.AliyunOssAssumeRoleAccessKeySecret]
  230. assumeRoleARN := cfgMap[enum2.AliyunOssAssumeRoleARN]
  231. roleSessionName := "STSRam"
  232. endpointList := strings.Split(endpoint, "-")
  233. stsEndPoint := fmt.Sprintf("sts.%s-%s", endpointList[1], endpointList[2])
  234. client, err := svc.CreateSTSClient(&assumeRoleAccessKeyID, &assumeRoleAccessKeySecret, &stsEndPoint)
  235. if err != nil {
  236. e.OutErr(c, e.ERR, err.Error())
  237. return
  238. }
  239. roleArn := assumeRoleARN
  240. durationSeconds := 3600
  241. assumeRoleResponse, err := svc.AssumeRole(client, &roleArn, &roleSessionName, int64(durationSeconds))
  242. if err != nil {
  243. e.OutErr(c, e.ERR, err.Error())
  244. return
  245. }
  246. CredentialsStr := utils.SerializeStr(assumeRoleResponse.Body.Credentials)
  247. // 提早 60s 释放,避免能拿到令牌但无法上传
  248. cache.SetEx(redisKey, CredentialsStr, durationSeconds-60)
  249. credentials := sts20150401.AssumeRoleResponseBodyCredentials{
  250. AccessKeyId: assumeRoleResponse.Body.Credentials.AccessKeyId,
  251. AccessKeySecret: assumeRoleResponse.Body.Credentials.AccessKeySecret,
  252. Expiration: assumeRoleResponse.Body.Credentials.Expiration,
  253. SecurityToken: assumeRoleResponse.Body.Credentials.SecurityToken,
  254. }
  255. bucket := cfgMap[enum2.AliyunOssBucketName]
  256. region := strings.Split(endpoint, ".")[0]
  257. resp := GetSTSVoucherResp{
  258. STSToken: credentials,
  259. Bucket: bucket,
  260. Region: region,
  261. }
  262. e.OutSuc(c, resp, nil)
  263. return
  264. } else {
  265. e.OutErr(c, e.ERR, nil)
  266. return
  267. }
  268. }
  269. var credentials sts20150401.AssumeRoleResponseBodyCredentials
  270. utils.Unserialize([]byte(redisValue), &credentials)
  271. bucket := cfgMap[enum2.AliyunOssBucketName]
  272. region := strings.Split(endpoint, ".")[0]
  273. resp := GetSTSVoucherResp{
  274. STSToken: credentials,
  275. Bucket: bucket,
  276. Region: region,
  277. }
  278. e.OutSuc(c, resp, nil)
  279. return
  280. }
  281. type GetAdminInfoResp struct {
  282. AdmId int `json:"adm_id"` // 管理员id
  283. Username string `json:"username"` // 用户名
  284. State int `json:"state"` // 状态(1:正常 2:冻结)
  285. IsSuperAdministrator int `json:"is_super_administrator"` // 是否为超级管理员(0:否 1:是)
  286. Memo string `json:"memo"` // 备注信息
  287. }
  288. // GetAdminInfo
  289. // @Summary 通用请求-获取管理员信息
  290. // @Tags 通用请求
  291. // @Description 获取管理员信息
  292. // @Accept json
  293. // @Produce json
  294. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  295. // @Success 200 {object} GetAdminInfoResp "管理员信息"
  296. // @Failure 400 {object} md.Response "具体错误"
  297. // @Router /api/comm/adminInfo [POST]
  298. func GetAdminInfo(c *gin.Context) {
  299. admin := svc.GetUser(c)
  300. resp := GetAdminInfoResp{
  301. AdmId: admin.AdmId,
  302. Username: admin.Username,
  303. State: admin.State,
  304. IsSuperAdministrator: admin.IsSuperAdministrator,
  305. Memo: admin.Memo,
  306. }
  307. e.OutSuc(c, resp, nil)
  308. }