apass-opensdk-hugong 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,69 @@
1
+ class Object_{
2
+ #hg = null
3
+ constructor(hg){
4
+ this.#hg = hg
5
+ }
6
+
7
+ async clearData(table,id){
8
+ await application.data.object(table).select('_id').where({_id: id ? id : application.operator.gte(0)}).findStream(async records=>{
9
+ this.#hg.log4('clear data len', records.length)
10
+ await application.data.object(table).batchDelete(records.map(it=>({_id: it._id})))
11
+ })
12
+ }
13
+
14
+ async findList(table,field,where,callback){
15
+ const list = []
16
+ await application.data.object(table).select(field || ['_id','_name']).where(where || {_id : application.operator.gte(0)}).findStream(async records=>{
17
+ if(callback){
18
+ this.#hg.log4('find all len', list.length)
19
+ await callback(records)
20
+ }else{
21
+ this.#hg.log4('find all size', list.length)
22
+ list.push(...records)
23
+ }
24
+ })
25
+ if(!callback){
26
+ this.#hg.log4('find all len', list.length)
27
+ }
28
+ return list
29
+ }
30
+ async findOne(table,field,where){
31
+ return await application.data.object(table).select(field || ['_id','_name']).where(where || {_id : application.operator.gte(0)}).findOne()
32
+ }
33
+ async findOneById(table,id,field){
34
+ return await application.data.object(table).select(field || ['_id','_name']).where({_id: id}).findOne()
35
+ }
36
+
37
+ async batchCreate(table,list){
38
+ if(list.length){
39
+ return await application.data.object(table).batchCreate(list)
40
+ }
41
+ return null
42
+ }
43
+ async create(table,data){
44
+ return await application.data.object(table).create(data)
45
+ }
46
+
47
+ async batchUpdate(table,list){
48
+ if(list.length){
49
+ return application.data.object(table).batchUpdate(list)
50
+ }
51
+ return null
52
+ }
53
+ async update(table,data){
54
+ return application.data.object(table).update(data)
55
+ }
56
+
57
+ async delete(table,_id){
58
+ return application.data.object(table).delete(_id)
59
+ }
60
+ async delete(table,_ids){
61
+ if(_ids.length){
62
+ return application.data.object(table).batchDelete(_ids)
63
+ }
64
+ return null
65
+ }
66
+
67
+
68
+ }
69
+ module.exports = Object_
package/index.js CHANGED
@@ -3,20 +3,24 @@ const Document = require('./opensdk/document')
3
3
  const Utils = require('./utils/index')
4
4
  const Employee = require('./opensdk/employee')
5
5
  const Contract = require('./opensdk/contract')
6
-
6
+ const Object_ = require('./apass/object_')
7
7
  class HG{
8
8
  #logger
9
9
  #app_id
10
10
  #app_secret
11
11
  constructor(logger){
12
+ this.setLogger(logger)
13
+ this._time = null
14
+ }
15
+ setLogger(logger){
12
16
  this.#logger = logger
13
- this.users = new Users(logger, this)
14
- this.document = new Document(logger, this)
15
- this.employee = new Employee(logger, this)
16
- this.contract = new Contract(logger, this)
17
+ this.users = new Users(this)
18
+ this.document = new Document(this)
19
+ this.employee = new Employee(this)
20
+ this.contract = new Contract(this)
21
+ this.object = new Object_(this)
17
22
 
18
- this.utils = new Utils(logger, this)
19
- this._time = null
23
+ this.utils = new Utils(this)
20
24
  }
21
25
  /**
22
26
  * 运行函数
@@ -38,7 +42,7 @@ class HG{
38
42
  * @returns
39
43
  */
40
44
  async sleep(time){
41
- return new Promise((r)=>setTimeout(()=>{ this.#logger.log(`sleep ${(time || 1000)/1000}s`);r();},time || 1000))
45
+ return new Promise((r)=>setTimeout(()=>{ this.log4(`sleep ${(time || 1000)/1000}s`);r();},time || 1000))
42
46
  }
43
47
 
44
48
  /**
@@ -46,9 +50,9 @@ class HG{
46
50
  */
47
51
  newTime(){
48
52
  if(this._time){
49
- this.#logger.log('#time reset to before',(Date.now() - this._time)/1000, 's')
53
+ this.log4('#time reset to before',(Date.now() - this._time)/1000, 's')
50
54
  }
51
- this.#logger.log('#time reset')
55
+ this.log4('#time reset')
52
56
  this._time = Date.now()
53
57
  }
54
58
  /**
@@ -58,7 +62,7 @@ class HG{
58
62
  if(!this._time){
59
63
  return this.#logger.error('#time Error','The time has not been initialized ')
60
64
  }
61
- this.#logger.log('#time',(Date.now() - this._time)/1000, 's')
65
+ this.log4('#time',(Date.now() - this._time)/1000, 's')
62
66
  }
63
67
 
64
68
  /**
@@ -100,7 +104,7 @@ class HG{
100
104
  }else{
101
105
  newHeaders = { ...newHeaders,...(headers || {}) }
102
106
  }
103
- this.#logger.log('req=',url,data,newHeaders)
107
+ this.log4('req =',url,data,newHeaders)
104
108
  let _url = url
105
109
  let params = null
106
110
  if(Array.isArray(url)){
@@ -110,11 +114,11 @@ class HG{
110
114
  return new Promise((r,s)=>{
111
115
  const now = Date.now()
112
116
  axios({ method, params, url: _url, data, headers: newHeaders}).then(response=>{
113
- this.#logger.info('resp=',(Date.now() - now) / 1000, 's',response.status, response.statusText,response.data)
117
+ this.log8('resp =',(Date.now() - now) / 1000, 's',response.status, response.statusText,response.data)
114
118
  r(response.data)
115
119
  }).catch(e=>{
116
120
  console.log(e)
117
- this.#logger.error('resp=',(Date.now() - now) / 1000, 's',e.response.data)
121
+ this.#logger.error('resp =',(Date.now() - now) / 1000, 's',e.response.data)
118
122
  s(e.response.data)
119
123
  })
120
124
  })
@@ -146,20 +150,18 @@ class HG{
146
150
  page_token = result.data.page_token
147
151
  has_more = result.data.has_more
148
152
  if(callback){
149
- await callback(result.data.items || [])
153
+ await callback(result.data.hasOwnProperty('items') ? result.data.items || [] : result.data)
150
154
  }else{
151
- list.push(...(result.data.items || []))
155
+ if(result.data.hasOwnProperty('items')){
156
+ list.push(...(result.data.items || []))
157
+ }else{
158
+ list.push(result.data)
159
+ }
152
160
  }
153
161
  } while (has_more);
154
162
  return list
155
163
  }
156
164
 
157
- async getAllUser(field){
158
- const list = []
159
- await application.data.object('_user').select(field || ['_id','_name']).findStream(async records=>list.push(...records))
160
- return list
161
- }
162
-
163
165
  /**
164
166
  * 生成多语言对象
165
167
  * @param {*} zh
@@ -174,6 +176,11 @@ class HG{
174
176
  const _en = this.toSafeValue(zh.find(it=>it.lang == 'en-US'),'value')
175
177
  return new application.constants.type.Multilingual({ zh: _zh || _en, en: _en || _zh })
176
178
  }
179
+ if(zh[0].hasOwnProperty('locale')){
180
+ const _zh = this.toSafeValue(zh.find(it=>it.locale == 'zh_CN'),'value')
181
+ const _en = this.toSafeValue(zh.find(it=>it.locale == 'en_US'),'value')
182
+ return new application.constants.type.Multilingual({ zh: _zh || _en, en: _en || _zh })
183
+ }
177
184
  }
178
185
  return new application.constants.type.Multilingual({ zh: zh || en, en: en || zh })
179
186
  }
@@ -196,7 +203,30 @@ class HG{
196
203
  return _obj[key] || defValue
197
204
  }
198
205
 
206
+ listFind(list,key,target,defValue){
207
+ return list.find(it=>it[ key ] == target) || defValue
208
+ }
209
+ listMap(list,key){
210
+ return list.map(it=>it[ key ])
211
+ }
199
212
 
213
+ textToFloat(textAmount,defValue = 0) {
214
+ return parseFloat((textAmount || '').replace(/,/g, '') || defValue);
215
+ }
200
216
 
217
+ log(...arg){
218
+ this.#logger.log(...arg)
219
+ }
220
+ log4(...arg){
221
+ this.logm(4,...arg)
222
+ }
223
+ log8(...arg){
224
+ this.logm(8,...arg)
225
+ }
226
+ logm(space,...arg){
227
+ const result = ''.padEnd(space, "-");
228
+ this.#logger.log(result,...arg)
229
+ }
230
+
201
231
  }
202
232
  module.exports = HG
@@ -1,8 +1,6 @@
1
1
  class Contract{
2
2
  #hg = null
3
- #logger = null
4
- constructor(logger,hg){
5
- this.#logger = logger
3
+ constructor(hg){
6
4
  this.#hg = hg
7
5
  }
8
6
 
@@ -1,8 +1,6 @@
1
1
  class Document{
2
- #logger = null
3
2
  #hg = null
4
- constructor(logger,hg){
5
- this.#logger = logger
3
+ constructor(hg){
6
4
  this.#hg = hg
7
5
  }
8
6
  /**
@@ -1,8 +1,6 @@
1
1
  class Employee{
2
2
  #hg = null
3
- #logger = null
4
- constructor(logger,hg){
5
- this.#logger = logger
3
+ constructor(hg){
6
4
  this.#hg = hg
7
5
  }
8
6
 
package/opensdk/users.js CHANGED
@@ -1,8 +1,6 @@
1
1
  class Users{
2
2
  #hg = null
3
- #logger = null
4
- constructor(logger,hg){
5
- this.#logger = logger
3
+ constructor(hg){
6
4
  this.#hg = hg
7
5
  }
8
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apass-opensdk-hugong",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "飞书Apass低代码平台-飞书开放平台-相关的接口整合和常用的方法整合",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,6 +14,6 @@
14
14
  "APASS",
15
15
  "feish"
16
16
  ],
17
- "author": "IAMRuiyu",
17
+ "author": "微信:IAMRuiyu",
18
18
  "license": "ISC"
19
19
  }
package/readme.md CHANGED
@@ -8,6 +8,21 @@ apass-opensdk-hugong 是一个基于apass-opensdk的封装,提供了一些常
8
8
 
9
9
  ```
10
10
  在飞书低代码平台(云函数)依赖管理-右侧-搜索: apass-opensdk-hugong
11
+
12
+ 最近有同学私聊我说Apass无法安装1.0.0以上的包,解决思路: 先安装1.0.0版本,然后打开yarn.lock文件
13
+ ====== 找到这一行 =====
14
+ apass-opensdk-hugong@1.0.0:
15
+ version "1.0.0"
16
+ resolved "https://registry.npmmirror.com/apass-opensdk-hugong/-/apass-opensdk-hugong-1.0.0.tgz#ef2d0a19e793358784cb764de4b885fd86befd66"
17
+ integrity sha512-Q8Gpo9v8a/2G43y/Lh2POyDEOcealFuUlGdqGSA9wyfjET4+LWs/PnzK9EB1YZCjQotW7cGO/9a0MiQ6Sgw1LA==
18
+ ====== 找到这一行 =====
19
+
20
+ 将版本改为最新的版本号后,然后保存,重写部署即可使用NPM源最新的版本
21
+ 修改后的yarn.lock文件如下, 一共三处地方:
22
+ apass-opensdk-hugong@1.0.3:
23
+ version "1.0.3"
24
+ resolved "https://registry.npmmirror.com/apass-opensdk-hugong/-/apass-opensdk-hugong-1.0.3.tgz#ef2d0a19e793358784cb764de4b885fd86befd66"
25
+ integrity sha512-Q8Gpo9v8a/2G43y/Lh2POyDEOcealFuUlGdqGSA9wyfjET4+LWs/PnzK9EB1YZCjQotW7cGO/9a0MiQ6Sgw1LA==
11
26
  ```
12
27
 
13
28
  ## 如何使用
@@ -131,3 +146,10 @@ await hg.paginatedSearchGet(url, params, async (items)=>{
131
146
  // do something 假设服务端有300条数据,每次返回100条,会调用3次, 如果希望一次性返回所有数据,callback传null即可
132
147
  })
133
148
  ```
149
+
150
+
151
+
152
+ ## 交流学习
153
+
154
+ 从事飞书低代码平台(APASS)开发人员3年,欢迎交流学习
155
+ VX:IAMRuiyu
package/utils/date_.js ADDED
@@ -0,0 +1,72 @@
1
+
2
+ class Date_{
3
+ #hg = null
4
+ constructor(hg){
5
+ this.#hg = hg
6
+ }
7
+ moment(){
8
+ const moment = require('moment-timezone');
9
+ moment.tz.setDefault("Asia/Shanghai");
10
+ return moment()
11
+ }
12
+ nowFormat(format) {
13
+ const moment = require('moment-timezone');
14
+ moment.tz.setDefault("Asia/Shanghai");
15
+ return moment().format(format || 'YYYY-MM-DD HH:mm:ss')
16
+ }
17
+
18
+ splitDateRange(startDate, endDate, splitMonths) {
19
+ // 将输入的日期字符串转换为Date对象
20
+ const start = new Date(startDate);
21
+ const end = new Date(endDate);
22
+
23
+ // 检查输入的合法性
24
+ if (start >= end) {
25
+ throw new Error("开始日期必须早于结束日期");
26
+ }
27
+ if (splitMonths <= 0) {
28
+ throw new Error("分割持续月份必须大于0");
29
+ }
30
+
31
+ // 用于存储分割后的日期范围
32
+ const result = [];
33
+
34
+ // 当前处理的开始日期
35
+ let currentStart = new Date(start);
36
+
37
+ // 计算总月份数
38
+ const totalMonths = (end.getFullYear() - currentStart.getFullYear()) * 12 + end.getMonth() - currentStart.getMonth() + 1;
39
+
40
+ // 循环分割日期范围
41
+ while (currentStart < end) {
42
+ // 计算当前分割的结束日期
43
+ const currentEnd = new Date(currentStart);
44
+ currentEnd.setMonth(currentEnd.getMonth() + splitMonths);
45
+
46
+ // 如果当前分割的结束日期超过了总结束日期,则调整为总结束日期
47
+ if (currentEnd > end) {
48
+ currentEnd.setTime(end.getTime());
49
+ }
50
+
51
+ // 计算当前分割的持续月份数
52
+ const len = (currentEnd.getFullYear() - currentStart.getFullYear()) * 12 + currentEnd.getMonth() - currentStart.getMonth() + 1;
53
+
54
+ // 计算当前分割的天数
55
+ const days = (currentEnd - currentStart) / (1000 * 60 * 60 * 24) + 1;
56
+
57
+ // 将当前分割的日期范围添加到结果数组中
58
+ result.push({
59
+ begin: currentStart.toISOString().split("T")[0],
60
+ end: currentEnd.toISOString().split("T")[0],
61
+ len: len,
62
+ days: Math.floor(days) // 向下取整,因为天数应该是整数
63
+ });
64
+
65
+ // 更新当前处理的开始日期为当前分割的结束日期
66
+ currentStart = new Date(currentEnd);
67
+ }
68
+
69
+ return result;
70
+ }
71
+ }
72
+ module.exports = Date_
package/utils/file_.js CHANGED
@@ -1,9 +1,7 @@
1
1
  const fs = require('fs')
2
2
  class File_{
3
3
  #hg = null
4
- #logger = null
5
- constructor(logger,hg){
6
- this.#logger = logger
4
+ constructor(hg){
7
5
  this.#hg = hg
8
6
  }
9
7
  getCurrentTimeFolderName() {
@@ -23,11 +21,11 @@ class File_{
23
21
  * @returns
24
22
  */
25
23
  async downloadFileToUpload(url, header = {}){
26
- this.#logger.log(`download url=${url}`)
24
+ this.#hg.log4(`download url=${url}`)
27
25
  const resp = await axios({ url, method: 'get', responseType: 'stream', headers: {...header} });
28
26
  // 上传文件获取文件 token
29
27
  const file_info = await application.resources.file.upload(resp.data);
30
- this.#logger.log('file_info',file_info)
28
+ this.#hg.log4('file_info',file_info)
31
29
  return file_info
32
30
  }
33
31
  /**
@@ -37,10 +35,10 @@ class File_{
37
35
  * @returns
38
36
  */
39
37
  async downloadFileToTmp(file_info, file_path){
40
- this.#logger.log(`download input=${ JSON.stringify(file_info)}`)
38
+ this.#hg.log4(`download input=${ JSON.stringify(file_info)}`)
41
39
  const _file_path = file_path || `/tmp/${Date.now()}.${ file_info.mime_type }`
42
40
  await application.resources.file.download({id: file_info.id}, _file_path);
43
- this.#logger.log(`download success, path=` + _file_path)
41
+ this.#hg.log4(`download success, path=` + _file_path)
44
42
  return _file_path
45
43
  }
46
44
  /**
@@ -77,5 +75,15 @@ class File_{
77
75
  });
78
76
  })
79
77
  }
78
+
79
+ async excelReader(path,callback){
80
+ const XLSX = require('xlsx');
81
+ const stream = fs.createReadStream(path.join(__dirname, 'example.xlsx'));
82
+ const workbook = XLSX.read(stream, { type: 'buffer' });
83
+ const sheetName = workbook.SheetNames[0];
84
+ const worksheet = workbook.Sheets[sheetName];
85
+ const data = XLSX.utils.sheet_to_json(worksheet);
86
+ console.log(data);
87
+ }
80
88
  }
81
89
  module.exports = File_
package/utils/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  const Url = require('./url')
2
2
  const File_ = require('./file_')
3
+ const Date_ = require('./date_')
3
4
  class Utils{
4
5
  #hg = null
5
- #logger = null
6
- constructor(logger,hg){
7
- this.#logger = logger
6
+ constructor(hg){
8
7
  this.#hg = hg
9
8
 
10
- this.url = new Url(logger,hg)
11
- this.file = new File_(logger,hg)
9
+ this.url = new Url(hg)
10
+ this.file = new File_(hg)
11
+ this.date = new Date_(hg)
12
12
  }
13
13
 
14
14
  /**
@@ -20,8 +20,10 @@ class Utils{
20
20
  */
21
21
  async splitArray(list, chunkSize, callback) {
22
22
  const result = [];
23
+ this.#hg.log4('splitArray len ', list.length)
23
24
  for (let i = 0; i < list.length; i += chunkSize) {
24
25
  const batch = list.slice(i, i + chunkSize)
26
+ this.#hg.log8('splitArray item ',i, i + chunkSize)
25
27
  if(callback){
26
28
  await callback(batch)
27
29
  continue
@@ -30,12 +32,15 @@ class Utils{
30
32
  }
31
33
  return result;
32
34
  }
33
-
34
35
 
35
-
36
-
37
-
38
-
36
+ toMD5(data){
37
+ const crypto = require('crypto');
38
+ const hash = crypto.createHash('md5');
39
+ hash.update(data);
40
+ const md5Hash = hash.digest('hex');
41
+ this.#hg.log4(`MD5 Hash of "${data}": ${md5Hash}`)
42
+ return md5Hash
43
+ }
39
44
 
40
45
  }
41
46
  module.exports = Utils
package/utils/url.js CHANGED
@@ -1,9 +1,7 @@
1
1
 
2
2
  class Url{
3
3
  #hg = null
4
- #logger = null
5
- constructor(logger,hg){
6
- this.#logger = logger
4
+ constructor(hg){
7
5
  this.#hg = hg
8
6
  }
9
7