apass-opensdk-hugong 1.0.0

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.
package/index.js ADDED
@@ -0,0 +1,102 @@
1
+ const Users = require('./opensdk/users')
2
+ const Document = require('./opensdk/document')
3
+ class HG{
4
+ #logger
5
+ #app_id
6
+ #app_secret
7
+ constructor(logger){
8
+ this.#logger = logger
9
+ this.users = new Users(logger, this)
10
+ this.document = new Document(logger, this)
11
+ this._time = null
12
+ }
13
+ /**
14
+ * 运行函数
15
+ * @param {*} fn
16
+ * @returns
17
+ */
18
+ async timeRun(fn){
19
+ this.newTime()
20
+ let result = null
21
+ if(fn){
22
+ result = await fn()
23
+ }
24
+ this.printTime()
25
+ return result
26
+ }
27
+ /**
28
+ * 线程睡眠
29
+ * @param {*} time 毫秒
30
+ * @returns
31
+ */
32
+ async sleep(time){
33
+ return new Promise((r)=>setTimeout(()=>{ this.#logger.log(`sleep ${(time || 1000)/1000}s`);r();},time || 1000))
34
+ }
35
+
36
+ /**
37
+ * 时间计划-记录
38
+ */
39
+ newTime(){
40
+ if(this._time){
41
+ this.#logger.log('#time reset to before',(Date.now() - this._time)/1000, 's')
42
+ }
43
+ this.#logger.log('#time reset')
44
+ this._time = Date.now()
45
+ }
46
+ /**
47
+ * 时间计划-打印耗时
48
+ */
49
+ printTime(){
50
+ if(!this._time){
51
+ return this.#logger.error('#time Error','The time has not been initialized ')
52
+ }
53
+ this.#logger.log('#time',(Date.now() - this._time)/1000, 's')
54
+ }
55
+
56
+ /**
57
+ * 设置app_id
58
+ * @param {*} app_id
59
+ * @param {*} app_secret
60
+ */
61
+ setAppId(app_id,app_secret){
62
+ this.#app_id = app_id
63
+ this.#app_secret = app_secret
64
+ }
65
+ /**
66
+ * 开平token
67
+ * @returns
68
+ */
69
+ async getToken(){
70
+ const result = await this.request('https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal',{ app_id: this.#app_id, app_secret: this.#app_secret})
71
+ return result.tenant_access_token
72
+ }
73
+
74
+ /**
75
+ * 统一网络请求
76
+ * @param {*} url
77
+ * @param {*} data
78
+ * @param {*} headers
79
+ * @returns
80
+ */
81
+ async request(url, data, headers){
82
+ let HGheader = { 'Content-Type': 'application/json; charset=utf-8' }
83
+ if(typeof headers === 'boolean' && headers === true){
84
+ HGheader['Authorization'] = `Bearer ${ await this.getToken() }`
85
+ }else{
86
+ HGheader = { ...HGheader,...(headers || {}) }
87
+ }
88
+ this.#logger.log('req=',url,data,HGheader)
89
+ return new Promise((r,s)=>{
90
+ const now = Date.now()
91
+ axios({ method: 'POST', url, data, headers: HGheader}).then(response=>{
92
+ this.#logger.info('resp=',(Date.now() - now) / 1000, 's',response.status, response.statusText,response.data)
93
+ r(response.data)
94
+ }).catch(e=>{
95
+ this.#logger.error('resp=',(Date.now() - now) / 1000, 's',e.response.data)
96
+ s(e.response.data)
97
+ })
98
+ })
99
+ }
100
+
101
+ }
102
+ module.exports = HG
@@ -0,0 +1,48 @@
1
+ class Document{
2
+ #logger = null
3
+ #hg = null
4
+ constructor(logger,hg){
5
+ this.#logger = logger
6
+ this.#hg = hg
7
+ }
8
+ /**
9
+ * 增加协作者权限 https://open.feishu.cn/document/server-docs/docs/permission/permission-member/create
10
+ * @param {*} app_token app_token 多维表格 App 的唯一标识
11
+ * @param {*} type 文件类型 bitable:多维表格
12
+ * @param {*} data {
13
+ "member_type": "openchat",
14
+ "member_id": "oc_b962e8debdc37712a1d60bb97087a8e8",
15
+ "perm": "edit",
16
+ "perm_type": "container",
17
+ "type": "chat"
18
+ }
19
+ */
20
+ async permissions(app_token,type,data){
21
+ if(!type){
22
+ throw 'type is not empty'
23
+ }
24
+ return await this.#hg.request(`https://open.feishu.cn/open-apis/drive/v1/permissions/${app_token}/members?type=${type}&need_notification=false`,data,true)
25
+ }
26
+ /**
27
+ * 复制多维表格 https://open.feishu.cn/document/server-docs/docs/bitable-v1/app/copy
28
+ * @param {*} app_token 要复制的多维表格 App 的唯一标识
29
+ * @param {*} name 多维表格 App 名称。最长为 255 个字符
30
+ * @param {*} folder_token 文件夹
31
+ * @returns
32
+ */
33
+ async copy(app_token,name,folder_token,without_content,time_zone){
34
+ return await this.#hg.request(`https://open.feishu.cn/open-apis/bitable/v1/apps/${app_token}/copy`,{ name,folder_token,without_content: without_content || false,time_zone: time_zone || 'Asia/Shanghai' },true)
35
+ }
36
+ /**
37
+ * 创建多维表格 https://open.feishu.cn/document/server-docs/docs/bitable-v1/app/create
38
+ * @param {*} name 多维表格 App 名称
39
+ * @param {*} folder_token 多维表格 App 归属文件夹
40
+ * @param {*} time_zone
41
+ * @returns
42
+ */
43
+ async create(name, folder_token, time_zone){
44
+ return await this.#hg.request('https://open.feishu.cn/open-apis/bitable/v1/apps',{ name, folder_token, time_zone: time_zone || 'Asia/Shanghai' },true)
45
+ }
46
+
47
+ }
48
+ module.exports = Document
@@ -0,0 +1,19 @@
1
+ class Users{
2
+ #hg = null
3
+ #logger = null
4
+ constructor(logger,hg){
5
+ this.#logger = logger
6
+ this.#hg = hg
7
+ }
8
+
9
+ /**
10
+ * 通过手机号或邮箱获取用户 ID
11
+ * @param {*} user_id_type user_id、open_id、union_id
12
+ * @returns
13
+ */
14
+ async batch_get_id(data,user_id_type){
15
+ return await this.#hg.request(`https://open.feishu.cn/open-apis/contact/v3/users/batch_get_id?user_id_type=${user_id_type || 'open_id'}`,data,true)
16
+ }
17
+
18
+ }
19
+ module.exports = Users
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "apass-opensdk-hugong",
3
+ "version": "1.0.0",
4
+ "description": "飞书Apass低代码平台-飞书开放平台-相关的接口整合和常用的方法整合",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "apass",
11
+ "飞书",
12
+ "飞书低代码",
13
+ "低代码,低代码平台",
14
+ "APASS",
15
+ "feish"
16
+ ],
17
+ "author": "IAMRuiyu",
18
+ "license": "ISC"
19
+ }