Files
iotlcm/server/internal/library/contexts/contexts.go
T
MiFaZhan 2ed5dfa6a1 chore: 初始开源发布
iotlcm-app:小程序端 + 用户端服务。

- app/:uni-app 小程序前端(基于 unibest,MIT)
  - 微信 AppID → YOUR_WX_APPID,uni-app AppID → __UNI__YOURAPPID
  - 内网后端地址 → admin.iotlcm.example.com
  - 应用标题与 unibest 模板 README 重写(保留上游致谢与 MIT 声明)
- server/:GoFrame 用户端服务(基于 XiuAdmin,Apache-2.0)
  - module xiuadmin → git.5iot.com/iotlcm-public/iotlcm-app(360 文件)
  - 支付回调地址 → app.iotlcm.example.com,商户名与应用名泛化
  - 真实手机号 → 合法 11 位编造号(保留号段前 3 位 + 后 4 位)
  - 依赖号码尾部的断言同步更新
  - 补 server/LICENSE(Apache-2.0)
- 移除内部资产:CI 触发器
- 补根 README,LF 行尾 + .gitattributes

已知:TestSysDept_RefreshDeptAncestors 在源仓同一提交上同样失败,属既有缺陷。
2026-10-26 14:01:25 +08:00

199 lines
4.2 KiB
Go

// Package contexts
// @Link https://github.com/xiujiecn/xiu-admin
// @Copyright Copyright (c) 2025 LiXiujie
// @Author Lxj <li@xiujie.cn>
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
package contexts
import (
"context"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/consts"
"git.5iot.com/iotlcm-public/iotlcm-app/internal/model"
"slices"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
type ContextKey struct {
name string
}
var contextKey = &ContextKey{
name: consts.ContextKey,
}
// Init初始化上下文对象指针到上下文对象中,以便后续的请求流程中可以修改。
func Init(r *ghttp.Request, customCtx *model.Context) {
r.SetCtxVar(contextKey, customCtx)
}
// Set设置本地上下文
func Set(ctx context.Context, customCtx *model.Context) context.Context {
ctx = context.WithValue(ctx, contextKey, customCtx)
return ctx
}
// Get 获取上下文变量;
func Get(ctx context.Context) *model.Context {
customCtx, ok := ctx.Value(contextKey).(*model.Context)
if !ok {
return nil
}
return customCtx
}
// Set 设置上下文变量
func SetUser(ctx context.Context, user *model.Identity) {
customCtx := Get(ctx)
customCtx.User = user
}
// GetUser 获取上下文变量中的用户信息
func GetUser(ctx context.Context) *model.Identity {
customCtx := Get(ctx)
if customCtx == nil {
return nil
}
return customCtx.User
}
// GetUserId 获取上下文变量中的用户ID
func GetUserId(ctx context.Context) int64 {
user := GetUser(ctx)
if user == nil {
return 0
}
return user.ID
}
// GetUserUuid 获取上下文变量中的用户UUID
func GetUserUuid(ctx context.Context) string {
user := GetUser(ctx)
if user == nil {
return ""
}
return user.UUID
}
// GetUserName 获取上下文变量中的用户名
func GetUserName(ctx context.Context) string {
user := GetUser(ctx)
if user == nil {
return ""
}
return user.Username
}
// GetNickName 获取上下文变量中的用户昵称
func GetNickName(ctx context.Context) string {
user := GetUser(ctx)
if user == nil {
return ""
}
return user.NickName
}
// GetDeptId 获取上下文变量中的部门ID
func GetDeptId(ctx context.Context) int64 {
user := GetUser(ctx)
if user == nil {
return 0
}
return user.DeptId
}
// GetTenantId 获取上下文变量中的租户ID
func GetTenantId(ctx context.Context) string {
user := GetUser(ctx)
if user == nil {
return ""
}
return user.TenantId
}
// GetRoles 获取上下文变量中的角色列表
func GetRoles(ctx context.Context) []model.Role {
user := GetUser(ctx)
if user == nil {
return nil
}
return user.Roles
}
// GetLoginAt 获取上下文变量中的登录时间
func GetLoginAt(ctx context.Context) int64 {
user := GetUser(ctx)
if user == nil {
return 0
}
return user.LoginAt
}
// IsSystemTenant 判断是否是系统租户
func IsSystemTenant(ctx context.Context) bool {
tenantId := GetTenantId(ctx)
return tenantId == consts.DefaultSystemTenantCode
}
// IsSuperAdmin 判断是否是超级管理员角色
func IsSuperAdmin(ctx context.Context) bool {
roles := GetRoles(ctx)
return slices.ContainsFunc(roles, func(role model.Role) bool {
return role.RoleId == consts.SuperAdminRoleId
})
}
func IsWebSocket(ctx context.Context) bool {
customCtx := Get(ctx)
if customCtx == nil {
return false
}
return customCtx.IsWebSocket
}
func SetIsWebSocket(ctx context.Context, isWebSocket bool) {
customCtx := Get(ctx)
if customCtx == nil {
return
}
customCtx.IsWebSocket = isWebSocket
g.Log().Debugf(ctx, "SetIsWebSocket: %v", isWebSocket)
}
// GetAccessCodeList 获取上下文变量中的访问码列表
func GetAccessCodeList(ctx context.Context) []string {
user := GetUser(ctx)
if user == nil {
return nil
}
if len(user.AccessCodeList) == 0 {
return nil
}
return user.AccessCodeList
}
func GetDataValue(ctx context.Context, key string) interface{} {
customCtx := Get(ctx)
if customCtx == nil {
return nil
}
if customCtx.Data == nil {
return nil
}
value, ok := customCtx.Data[key]
if !ok {
return nil
}
return value
}
func SetDataValue(ctx context.Context, key string, value interface{}) {
customCtx := Get(ctx)
if customCtx == nil {
return
}
if customCtx.Data == nil {
customCtx.Data = make(g.Map)
}
customCtx.Data[key] = value
}