wangrong
7 天以前 a9a1c709f56bb7c4361aa04b7cc647baaed74fcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import { Provide, Inject } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import axios from 'axios';
import { BaseSysParamEntity } from '../../base/entity/sys/param';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
 
 
interface Ys7TokenData {
  accessToken: string;
  /** 萤石返回的毫秒级过期时间戳 */
  expireTime: number;
}
 
@Provide()
export class Ys7Service extends BaseService {
  @InjectEntityModel(BaseSysParamEntity)
  baseSysParamEntity: Repository<BaseSysParamEntity>;
 
  /** 内存并发锁 */
  private refreshing: Promise<Ys7TokenData> | null = null;
 
  /**
   * 对外暴露:拿 token(自动读缓存 / 续签)
   */
  async getAccessToken() {
    const token = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.accessToken' });
    if (
      !token ||
      !token.updateTime ||
      Date.now() - new Date(token.updateTime).getTime() > 6 * 24 * 60 * 60 * 1000
    ) {
      await this.fetchAndSaveRemote();
    }
  }
 
  /** 真正调萤石 */
  private async fetchAndSaveRemote(): Promise<Ys7TokenData> {
    const appKey_result = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.appKey' });
    const appSecret_result = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.appSecret' });
    if (!appKey_result || !appSecret_result) {
      throw new Error(
        '萤石云 appKey/appSecret 未配置,请在【系统参数】中维护 ys7.appKey / ys7.appSecret'
      );
    }
    const { data: res } = await axios.post(
      'https://open.ys7.com/api/lapp/token/get',
      new URLSearchParams({ appKey: appKey_result.data, appSecret: appSecret_result.data }),
      {
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        timeout: 8000,
      }
    );
    if (String(res.code) !== '200') {
      throw new Error(`萤石云获取 Token 失败:${res.msg}(code: ${res.code})`);
    }
 
    const tokenData: Ys7TokenData = {
      accessToken: res.data.accessToken,
      expireTime: res.data.expireTime,
    };
    const token = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.accessToken' });
    this.baseSysParamEntity.save({
      ...token,
      data: tokenData.accessToken,
      updateTime: new Date(),
    }).catch(() => {
      // 忽略写回失败
    });
    return tokenData;
  }
 
  async getSpaceID(): Promise<string> {
    const entity = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.storageSpaceID' });
    if (!entity ||
      !entity.updateTime ||
      Date.now() - new Date(entity.updateTime).getTime() >
      6 * 24 * 60 * 60 * 1000) {
      const spaceId = await this.createCaptureSpace();
      await this.baseSysParamEntity.save({
        id: entity?.id,
        keyName: 'ys7.storageSpaceID',
        data: spaceId,
        updateTime: new Date(),
      });
      return spaceId;
    }
    return entity!.data as string;
  }
 
  /**
   * 创建萤石云【设备抓图】存储空间
   * - spaceName 取自系统参数 ys7.spaceName
   * - 过期天数默认 7 天
   * - 创建成功后 storageSpaceID 写入 ys7.storageSpaceID
   * - 调用萤石云空间创建接口:https://open.ys7.com/api/service/open/storage/engine/space
   * - 接口说明地址:https://open.ys7.com/help/5236
   * - 将创建好的存储空间ID存入参数:ys7.storageSpaceID
   */
  async createCaptureSpace(expireDays = 7): Promise<string> {
    // 1. accessToken
    const tokenEntity = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.accessToken' });
    if (!tokenEntity?.data) {
      console.error('[Ys7Service] ys7.accessToken 为空');
      throw new Error('ys7.accessToken 为空');
    }
    const accessToken = String(tokenEntity.data).trim();
 
    // 2. spaceName
    const spaceEntity = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.spaceName' });
    if (!spaceEntity?.data) {
      console.error('[Ys7Service] ys7.spaceName 未配置');
      throw new Error('ys7.spaceName 未配置');
    }
    const spaceName = String(spaceEntity.data).trim();
    // 4. 萤石侧是否已存在同名空间
    const existId = await this.getSpaceIdByName(accessToken, spaceName);
    if (existId) {
      console.log('[Ys7Service] 萤石侧已存在同名空间,spaceId=%s', existId);
      await this.saveStorageSpaceID(existId);
      return existId;
    }
    // 5. 真正创建
    const params = new URLSearchParams({
      bizType: 'capture',
      expireDays: String(expireDays),
      spaceName,
      storageType: '1',
    });
 
    let res: any;
    try {
      const resp = await axios.post(
        'https://open.ys7.com/api/service/open/storage/engine/space',
        params,
        {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            accessToken,
          },
          timeout: 8000,
        }
      );
      res = resp.data;
    } catch (err: any) {
      console.error('[Ys7Service] 萤石接口请求失败');
      if (err.response) {
        console.error('[Ys7Service] RESPONSE BODY=%s', JSON.stringify(err.response.data));
      } else {
        console.error('[Ys7Service] ERROR MSG=%s', err.message);
      }
      throw err;
    }
    // 6. 校验萤石业务返回
    if (!res || res.meta?.code !== 200) {
      console.error('[Ys7Service] 萤石创建空间业务失败: %s', JSON.stringify(res?.meta));
      throw new Error(`萤石创建空间失败: ${JSON.stringify(res?.meta)}`);
    }
    const storageSpaceID = String(res.data); // data 就是 spaceId
    // 7. 保存到系统参数
    await this.saveStorageSpaceID(storageSpaceID);
    return storageSpaceID;
  }
 
  /**
   * 修改萤石云【设备抓图】存储空间
   * - spaceId 取自 ys7.storageSpaceID
   * - accessToken 取自 ys7.accessToken
   * - 默认过期天数 7 天
   */
  async updateCaptureSpace(expireDays = 7): Promise<boolean> {
    // 1. accessToken
    const tokenEntity = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.accessToken' });
    if (!tokenEntity?.data) {
      throw new Error('萤石 accessToken 不存在,请先获取 Token');
    }
    const accessToken = tokenEntity.data as string;
 
    // 2. spaceId
    const spaceEntity = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.storageSpaceID' });
    if (!spaceEntity?.data) {
      throw new Error('萤石 storageSpaceID 不存在,请先创建抓图空间');
    }
    const spaceId = Number(spaceEntity.data);
 
    // 3. 组装参数(PUT 使用 URLSearchParams)
    const params = new URLSearchParams();
    params.append('spaceId', String(spaceId));
    params.append('expireDays', String(expireDays));
 
    const url = 'https://open.ys7.com/api/service/open/storage/engine/space';
    const { data: res } = await axios.put(url, params, {
      headers: {
        accessToken,
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      timeout: 8000,
    });
    if (res.meta?.code !== 200) {
      throw new Error(
        `萤石云修改存储空间失败:${res.meta?.message}(code: ${res.meta?.code})`
      );
    }
    if (res.data) {
      this.baseSysParamEntity.save({
        ...spaceEntity,
        updateTime: new Date(),
      }).catch(() => {
        // 忽略写回失败
      });
    }
    return !!res.data;
  }
 
  /**
   * 删除萤石云【设备抓图】存储空间
   * - spaceId 取自 ys7.storageSpaceID
   * - accessToken 取自 ys7.accessToken
   * - 删除成功后,清空 ys7.storageSpaceID
   */
  async deleteCaptureSpace(): Promise<boolean> {
    // 1. accessToken
    const tokenEntity = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.accessToken' });
    if (!tokenEntity?.data) {
      throw new Error('萤石 accessToken 不存在,请先获取 Token');
    }
    const accessToken = tokenEntity.data as string;
 
    // 2. spaceId
    const spaceEntity = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.storageSpaceID' });
    if (!spaceEntity?.data) {
      throw new Error('萤石 storageSpaceID 不存在,无法删除');
    }
    const spaceId = Number(spaceEntity.data);
 
    // 3. DELETE 请求(body 使用 URLSearchParams)
    const params = new URLSearchParams();
    params.append('spaceId', String(spaceId));
 
    const url = 'https://open.ys7.com/api/service/open/storage/engine/space';
    const { data: res } = await axios.delete(url, {
      headers: {
        accessToken,
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      data: params.toString(),
      timeout: 8000,
    });
    if (res.meta?.code !== 200) {
      throw new Error(
        `萤石云删除存储空间失败:${res.meta?.message}(code: ${res.meta?.code})`
      );
    }
    // 4. 清理本地参数
    spaceEntity.data = '';
    spaceEntity.updateTime = new Date();
    await this.baseSysParamEntity.save(spaceEntity);
 
    return !!res.data;
  }
 
  /**
   * 根据 spaceName 查询萤石是否已存在【设备抓图】空间
   * 使用官方 listById 接口,支持翻页
   * 返回 storageSpaceID 或 null
   */
  private async getSpaceIdByName(
    accessToken: string,
    spaceName: string
  ): Promise<string | null> {
 
    const url = 'https://open.ys7.com/api/service/open/storage/engine/space/listById';
 
    // 查询时间窗口:最近 30 天(萤石强制要求)
    const endTime = new Date();
    const startTime = new Date(endTime.getTime() - 30 * 24 * 60 * 60 * 1000);
 
    const format = (d: Date) =>
      d.getFullYear() +
      '-' +
      String(d.getMonth() + 1).padStart(2, '0') +
      '-' +
      String(d.getDate()).padStart(2, '0') +
      ' ' +
      String(d.getHours()).padStart(2, '0') +
      ':' +
      String(d.getMinutes()).padStart(2, '0') +
      ':' +
      String(d.getSeconds()).padStart(2, '0');
 
    const queryParams = new URLSearchParams({
      startTime: format(startTime),
      endTime: format(endTime),
      pageSize: '50',
      bizTypeList: 'capture', // 只查设备抓图空间
    });
 
    let lastId: number | null = null;
    let page = 1;
 
    try {
      while (true) {
        if (lastId !== null) {
          queryParams.set('lastSpaceId', String(lastId));
        }
 
        const fullUrl = `${url}?${queryParams.toString()}`;
 
        const resp = await axios.get(fullUrl, {
          headers: {
            accessToken,
          },
          timeout: 8000,
        });
        const res = resp.data;
        if (res?.meta?.code !== 200) {
          console.error('[Ys7Service] 查询空间列表失败: %s', JSON.stringify(res.meta));
          throw new Error(`查询萤石空间失败: ${JSON.stringify(res.meta)}`);
        }
 
        const data = res.data || {};
        const list = data.result || [];
 
        // 精确匹配 spaceName
        const match = list.find(
          (item: any) => item.spaceName === spaceName && item.bizType === 'capture'
        );
 
        if (match) {
          console.log(
            '[Ys7Service] ✅ 找到已存在的空间:spaceId=%s, spaceName=%s, bizType=%s',
            match.spaceId,
            match.spaceName,
            match.bizType
          );
          return String(match.spaceId);
        }
 
        // 没有下一页,终止
        if (!data.hasNext) {
          break;
        }
 
        lastId = data.lastId;
        page++;
      }
      return null;
    } catch (err: any) {
      console.error('[Ys7Service] 查询萤石空间异常');
      if (err.response) {
        console.error('[Ys7Service] HTTP STATUS=%d', err.response.status);
        console.error('[Ys7Service] RESPONSE=%s', JSON.stringify(err.response.data));
      } else {
        console.error('[Ys7Service] ERROR=%s', err.message);
      }
      throw err;
    }
  }
 
  /** 公共保存方法 */
  private async saveStorageSpaceID(spaceId: string) {
    let e = await this.baseSysParamEntity.findOneBy({ keyName: 'ys7.storageSpaceID' });
    if (!e) {
      e = this.baseSysParamEntity.create({
        keyName: 'ys7.storageSpaceID',
        name: '萤石云抓图空间ID',
        dataType: 1,
        data: spaceId,
        updateTime: new Date(),
      });
    } else {
      e.data = spaceId;
      e.updateTime = new Date();
    }
    await this.baseSysParamEntity.save(e);
  }
 
}