SView Help Center
English (US)

给资源分配电子围栏

Author: sviewngts Last Updated: 2026-08-25 16:32
将已创建的电子围栏绑定到指定资源(PROJECT/ACCOUNT/FILE),绑定后该资源访问时将受围栏约束
仅企业超管和管理员可操作
接口名 POST: /login-register/open-api/geofences/assignments
入参:(body传值) application/json
是否必传 默认值 描述 备注
resourceType 是 - 资源类型 企业:ENTERPRISE 项目:
PROJECT (暂未开放)
账号:ACCOUNT (暂未开放)
文件: FILE (暂未开放)
文件夹: FOLDER (暂未开放)
resourceId 是 - 资源ID  
fenceId 是 - 围栏ID  
入参:(Header传值)
Authorization 是 - 传递AccessToken 格式:Bearer {AccessToken}

 

入参示例:(application/json)

 

{
    "fenceId": "fence_1717123456789",
    "resourceType": "ENTERPRISE",
    "resourceId": "123456"
}
回参示例:(application/json)

 

{
    "code": 0,
    "msg": null,
    "data": {
        "fenceId": "fence_789"
    },
    "ok": true
}
package com.hoteam.sview.demo;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 资源分配电子围栏 Demo
 * <p>
 * 接口:POST /login-register/open-api/geofences/assignments
 * 鉴权:Header Authorization: Bearer {AccessToken}
 * 仅企业超管和管理员可操作
 * 作用:将已创建好的电子围栏绑定到指定资源
 */
@Slf4j
public class AssignGeofenceToResourceDemo {

    // 业务网关地址,部署时替换为实际 gateway 地址
    private static final String ASSIGN_GEOFENCE_URL = "http://your-host/login-register/open-api/geofences/assignments";

    // 资源类型常量(文档定义)
    public static final String RES_TYPE_ENTERPRISE = "ENTERPRISE";
    public static final String RES_TYPE_PROJECT = "PROJECT";
    public static final String RES_TYPE_ACCOUNT = "ACCOUNT";
    public static final String RES_TYPE_FILE = "FILE";
    public static final String RES_TYPE_FOLDER = "FOLDER";

    /**
     * 分配围栏到资源
     * @param accessToken 身份令牌
     * @param resourceType 资源类型 ENTERPRISE/PROJECT/ACCOUNT/FILE/FOLDER
     * @param resourceId 资源唯一ID
     * @param fenceId 已创建的电子围栏ID
     * @return true分配成功,false失败
     */
    public static boolean assignFenceToResource(String accessToken,
                                                String resourceType,
                                                String resourceId,
                                                String fenceId) {
        // 参数校验
        if (StrUtil.isBlank(accessToken)) {
            log.warn("accessToken 不能为空");
            return false;
        }
        if (StrUtil.hasBlank(resourceType, resourceId, fenceId)) {
            log.warn("resourceType / resourceId / fenceId 不能为空");
            return false;
        }

        // 组装请求体
        JSONObject body = new JSONObject();
        body.set("resourceType", resourceType);
        body.set("resourceId", resourceId);
        body.set("fenceId", fenceId);

        HttpRequest request = HttpRequest.post(ASSIGN_GEOFENCE_URL)
                .header(Header.AUTHORIZATION, "Bearer " + accessToken)
                .header(Header.CONTENT_TYPE, ContentType.JSON.getValue())
                .timeout(10_000)
                .body(body.toString());

        // 可按需注入公共请求头
        // CommonHeaderUtil.withCommonHeaders(request, deviceId, deviceModel, deviceType, lang);

        try (HttpResponse response = request.execute()) {
            int status = response.getStatus();
            String respBody = response.body();
            log.info("围栏分配响应: status=[{}], body=[{}]", status, respBody);

            if (status == HttpStatus.HTTP_UNAUTHORIZED) {
                log.warn("认证失败,请确认账号权限为企业超管/管理员");
                return false;
            }
            if (!response.isOk() || StrUtil.isBlank(respBody)) {
                log.warn("围栏分配接口异常: status=[{}], body=[{}]", status, respBody);
                return false;
            }

            JSONObject json = JSONUtil.parseObj(respBody);
            Integer code = json.getInt("code");
            Boolean okFlag = json.getBool("ok", false);

            if (code != null && code == 0 && Boolean.TRUE.equals(okFlag)) {
                log.info("围栏分配成功,资源:{}, 围栏:{}", resourceId, fenceId);
                return true;
            } else {
                log.warn("围栏分配业务失败: code=[{}], msg=[{}]", code, json.getStr("msg"));
                return false;
            }
        } catch (Exception e) {
            log.error("围栏分配接口调用异常", e);
            return false;
        }
    }

    // ===================== 调用示例入口 =====================
    public static void main(String[] args) {
        // 替换为有效token
        String token = "your-access-token-here";
        // 待绑定资源
        String resType = RES_TYPE_ENTERPRISE;
        String resId = "123456";
        // 已创建好的围栏ID
        String targetFenceId = "fence_789";

        boolean assignResult = assignFenceToResource(token, resType, resId, targetFenceId);
        log.info("围栏分配是否成功: {}", assignResult);
    }
}