@xbongbong/xbbcli 2.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.
Files changed (255) hide show
  1. package/README.md +436 -0
  2. package/ai-call-add-task.js +106 -0
  3. package/ai-word-list.js +81 -0
  4. package/bin/xbbcli.js +120 -0
  5. package/buyer-inquiry-detail.js +112 -0
  6. package/buyer-inquiry-list.js +142 -0
  7. package/clue-add-couser.js +86 -0
  8. package/clue-add.js +102 -0
  9. package/clue-back.js +82 -0
  10. package/clue-del.js +75 -0
  11. package/clue-delete-couser.js +84 -0
  12. package/clue-delete-mainuser.js +84 -0
  13. package/clue-detail.js +78 -0
  14. package/clue-distribution.js +91 -0
  15. package/clue-edit.js +101 -0
  16. package/clue-handover.js +103 -0
  17. package/clue-list.js +224 -0
  18. package/clue-thorough-delete.js +82 -0
  19. package/communicate-add.js +144 -0
  20. package/communicate-del.js +118 -0
  21. package/communicate-detail.js +123 -0
  22. package/communicate-edit.js +146 -0
  23. package/communicate-list.js +194 -0
  24. package/communicate-plan-add.js +102 -0
  25. package/communicate-plan-cancel.js +76 -0
  26. package/communicate-plan-del.js +76 -0
  27. package/communicate-plan-detail.js +79 -0
  28. package/communicate-plan-list.js +129 -0
  29. package/communicate-plan-single-operate.js +86 -0
  30. package/contact-add-couser.js +87 -0
  31. package/contact-add-mainuser.js +87 -0
  32. package/contact-add.js +125 -0
  33. package/contact-del.js +70 -0
  34. package/contact-delete-couser.js +87 -0
  35. package/contact-delete-mainuser.js +87 -0
  36. package/contact-detail.js +72 -0
  37. package/contact-edit.js +103 -0
  38. package/contact-handover.js +84 -0
  39. package/contact-list.js +195 -0
  40. package/contract-add-couser.js +129 -0
  41. package/contract-add-mainuser.js +129 -0
  42. package/contract-add.js +148 -0
  43. package/contract-del.js +117 -0
  44. package/contract-delete-couser.js +128 -0
  45. package/contract-delete-mainuser.js +129 -0
  46. package/contract-detail.js +124 -0
  47. package/contract-edit.js +147 -0
  48. package/contract-handover.js +129 -0
  49. package/contract-list.js +235 -0
  50. package/contract-outstock-add.js +104 -0
  51. package/contract-outstock-del.js +78 -0
  52. package/contract-outstock-detail.js +80 -0
  53. package/contract-outstock-edit.js +106 -0
  54. package/contract-outstock-list.js +121 -0
  55. package/customer-add-couser.js +186 -0
  56. package/customer-add.js +152 -0
  57. package/customer-back.js +125 -0
  58. package/customer-del.js +117 -0
  59. package/customer-delete-couser.js +128 -0
  60. package/customer-delete-mainuser.js +129 -0
  61. package/customer-detail.js +148 -0
  62. package/customer-distribution.js +132 -0
  63. package/customer-edit.js +174 -0
  64. package/customer-handover.js +131 -0
  65. package/customer-invoice-address-add.js +101 -0
  66. package/customer-invoice-address-edit.js +102 -0
  67. package/customer-invoice-address.js +86 -0
  68. package/customer-invoice-info-add.js +101 -0
  69. package/customer-invoice-info-edit.js +102 -0
  70. package/customer-invoice-info.js +88 -0
  71. package/customer-list.js +187 -0
  72. package/department-add.js +77 -0
  73. package/department-del.js +76 -0
  74. package/department-edit.js +77 -0
  75. package/department-list.js +190 -0
  76. package/form-data-add-couser.js +84 -0
  77. package/form-data-add-mainuser.js +84 -0
  78. package/form-data-add.js +171 -0
  79. package/form-data-del.js +135 -0
  80. package/form-data-delete-couser.js +85 -0
  81. package/form-data-delete-mainuser.js +84 -0
  82. package/form-data-detail.js +146 -0
  83. package/form-data-edit.js +171 -0
  84. package/form-data-handover.js +84 -0
  85. package/form-data-list.js +188 -0
  86. package/form-get.js +157 -0
  87. package/form-list.js +169 -0
  88. package/invoice-add.js +98 -0
  89. package/invoice-cancel.js +97 -0
  90. package/invoice-del.js +83 -0
  91. package/invoice-detail.js +85 -0
  92. package/invoice-edit.js +101 -0
  93. package/invoice-get-amount-detail.js +84 -0
  94. package/invoice-list.js +125 -0
  95. package/invoice-red.js +101 -0
  96. package/label-add.js +93 -0
  97. package/label-all-list.js +102 -0
  98. package/label-batch-add.js +111 -0
  99. package/label-batch-remove.js +111 -0
  100. package/label-del-completely.js +88 -0
  101. package/label-del.js +98 -0
  102. package/label-edit.js +96 -0
  103. package/label-group-add.js +89 -0
  104. package/label-group-del.js +83 -0
  105. package/label-group-list.js +87 -0
  106. package/label-group-update.js +92 -0
  107. package/label-list.js +97 -0
  108. package/label-reduction.js +92 -0
  109. package/label-repeat.js +89 -0
  110. package/market-activity-add.js +109 -0
  111. package/market-activity-del.js +83 -0
  112. package/market-activity-detail.js +84 -0
  113. package/market-activity-edit.js +106 -0
  114. package/market-activity-handover.js +88 -0
  115. package/market-activity-list.js +121 -0
  116. package/opportunity-add-couser.js +129 -0
  117. package/opportunity-add-mainuser.js +129 -0
  118. package/opportunity-add.js +147 -0
  119. package/opportunity-del.js +118 -0
  120. package/opportunity-delete-couser.js +129 -0
  121. package/opportunity-delete-mainuser.js +128 -0
  122. package/opportunity-detail.js +125 -0
  123. package/opportunity-edit.js +146 -0
  124. package/opportunity-handover.js +130 -0
  125. package/opportunity-list.js +169 -0
  126. package/package.json +34 -0
  127. package/pay-plan-add-couser.js +88 -0
  128. package/pay-plan-add-mainuser.js +88 -0
  129. package/pay-plan-add.js +104 -0
  130. package/pay-plan-delete-couser.js +87 -0
  131. package/pay-plan-delete-mainuser.js +87 -0
  132. package/pay-plan-handover.js +85 -0
  133. package/pay-sheet-add-bad.js +104 -0
  134. package/pay-sheet-add-couser.js +87 -0
  135. package/pay-sheet-add-mainuser.js +88 -0
  136. package/pay-sheet-add-red.js +104 -0
  137. package/pay-sheet-add-write-off.js +104 -0
  138. package/pay-sheet-del.js +86 -0
  139. package/pay-sheet-delete-couser.js +87 -0
  140. package/pay-sheet-delete-mainuser.js +88 -0
  141. package/pay-sheet-detail.js +85 -0
  142. package/pay-sheet-edit-bad.js +110 -0
  143. package/pay-sheet-edit-red.js +110 -0
  144. package/pay-sheet-edit-write-off.js +106 -0
  145. package/pay-sheet-get-amount-detail.js +83 -0
  146. package/pay-sheet-list.js +123 -0
  147. package/payment-add-couser.js +129 -0
  148. package/payment-add-mainuser.js +129 -0
  149. package/payment-add.js +142 -0
  150. package/payment-del.js +117 -0
  151. package/payment-delete-couser.js +128 -0
  152. package/payment-delete-mainuser.js +128 -0
  153. package/payment-detail.js +123 -0
  154. package/payment-edit.js +146 -0
  155. package/payment-handover.js +130 -0
  156. package/payment-list.js +195 -0
  157. package/payment-sheet-add-bad-debt.js +142 -0
  158. package/payment-sheet-add-pre.js +104 -0
  159. package/payment-sheet-add-red.js +142 -0
  160. package/payment-sheet-add-write-off.js +130 -0
  161. package/payment-sheet-del.js +106 -0
  162. package/payment-sheet-detail.js +113 -0
  163. package/payment-sheet-edit-bad-debt.js +131 -0
  164. package/payment-sheet-edit-red.js +146 -0
  165. package/payment-sheet-edit-write-off.js +146 -0
  166. package/payment-sheet-get-amount-detail.js +84 -0
  167. package/payment-sheet-handover.js +85 -0
  168. package/payment-sheet-list.js +206 -0
  169. package/product-add.js +129 -0
  170. package/product-category-add.js +124 -0
  171. package/product-category-del.js +132 -0
  172. package/product-category-list.js +164 -0
  173. package/product-category-update.js +139 -0
  174. package/product-del.js +106 -0
  175. package/product-detail.js +183 -0
  176. package/product-edit.js +131 -0
  177. package/product-list.js +193 -0
  178. package/product-online.js +91 -0
  179. package/quotation-add-couser.js +106 -0
  180. package/quotation-add-mainuser.js +101 -0
  181. package/quotation-add.js +100 -0
  182. package/quotation-del.js +83 -0
  183. package/quotation-delete-couser.js +101 -0
  184. package/quotation-delete-mainuser.js +101 -0
  185. package/quotation-detail.js +85 -0
  186. package/quotation-edit.js +101 -0
  187. package/quotation-handover.js +84 -0
  188. package/quotation-list.js +123 -0
  189. package/refund-add.js +83 -0
  190. package/refund-del.js +68 -0
  191. package/refund-detail.js +71 -0
  192. package/refund-edit.js +84 -0
  193. package/refund-list.js +193 -0
  194. package/role-list.js +85 -0
  195. package/sign-in-list.js +136 -0
  196. package/stage-get.js +100 -0
  197. package/stage-jump.js +109 -0
  198. package/supplier-add.js +142 -0
  199. package/token-generate.js +109 -0
  200. package/token-set.js +222 -0
  201. package/user-add.js +103 -0
  202. package/user-del.js +90 -0
  203. package/user-edit.js +102 -0
  204. package/user-handover.js +78 -0
  205. package/user-list.js +147 -0
  206. package/version.js +18 -0
  207. package/work-order-add.js +171 -0
  208. package/work-order-asset-add-couser.js +101 -0
  209. package/work-order-asset-add-mainuser.js +104 -0
  210. package/work-order-asset-add.js +98 -0
  211. package/work-order-asset-del-couser.js +101 -0
  212. package/work-order-asset-del-mainuser.js +101 -0
  213. package/work-order-asset-del.js +83 -0
  214. package/work-order-asset-detail.js +85 -0
  215. package/work-order-asset-edit.js +101 -0
  216. package/work-order-asset-handover.js +101 -0
  217. package/work-order-asset-list.js +123 -0
  218. package/work-order-del.js +136 -0
  219. package/work-order-detail.js +186 -0
  220. package/work-order-dynamic-list.js +145 -0
  221. package/work-order-edit.js +170 -0
  222. package/work-order-list.js +243 -0
  223. package/work-order-operate.js +167 -0
  224. package/work-order-template-detail.js +157 -0
  225. package/work-order-template-list.js +145 -0
  226. package/work-report-daily-add.js +97 -0
  227. package/work-report-daily-del.js +75 -0
  228. package/work-report-daily-detail.js +78 -0
  229. package/work-report-daily-edit.js +129 -0
  230. package/work-report-daily-get-work-plan.js +80 -0
  231. package/work-report-daily-list.js +116 -0
  232. package/work-report-monthly-add.js +98 -0
  233. package/work-report-monthly-del.js +79 -0
  234. package/work-report-monthly-detail.js +78 -0
  235. package/work-report-monthly-edit.js +98 -0
  236. package/work-report-monthly-list.js +116 -0
  237. package/work-report-weekly-add.js +98 -0
  238. package/work-report-weekly-del.js +76 -0
  239. package/work-report-weekly-detail.js +79 -0
  240. package/work-report-weekly-edit.js +99 -0
  241. package/work-report-weekly-list.js +116 -0
  242. package/work-task-add-couser.js +88 -0
  243. package/work-task-add.js +242 -0
  244. package/work-task-delete-couser.js +85 -0
  245. package/work-task-detail.js +101 -0
  246. package/work-task-edit.js +116 -0
  247. package/work-task-handover.js +85 -0
  248. package/workorder-add-couser.js +128 -0
  249. package/workorder-delete-couser.js +129 -0
  250. package/workorder-handover.js +137 -0
  251. package/workorderlist.js +194 -0
  252. package/workorderproductlist.js +159 -0
  253. package/worktimerecorddetail.js +157 -0
  254. package/worktimerecordlist.js +199 -0
  255. package/xbb-registry.js +21 -0
@@ -0,0 +1,170 @@
1
+ import crypto from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { cli, Strategy } from './xbb-registry.js';
6
+
7
+ const CONFIG_FILE = path.join(os.homedir(), '.xbbcli', 'config.env');
8
+ const WORK_ORDER_EDIT_API_URL = 'https://proapi.xbongbong.com/pro/v2/api/workOrderV2/edit';
9
+ const DEFAULT_BASE_URL = 'https://proapi.xbongbong.com';
10
+ const MISSING_TOKEN_MESSAGE = '缺少 token;请先执行 xbbcli token-set --corpid <CORPID> --token <TOKEN> --userId <USERID>';
11
+
12
+ function readConfig() {
13
+ try {
14
+ return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
15
+ } catch {
16
+ return {};
17
+ }
18
+ }
19
+
20
+ function getRuntimeConfig() {
21
+ const config = readConfig();
22
+ return {
23
+ corpid: String(config.corpid || '').trim(),
24
+ token: String(config.token || '').trim(),
25
+ baseUrl: String(config.baseurl || DEFAULT_BASE_URL).trim(),
26
+ userId: String(config.userId || '').trim(),
27
+ };
28
+ }
29
+
30
+ function buildApiUrl(baseUrl, apiUrl) {
31
+ const apiPath = new URL(apiUrl).pathname;
32
+ return `${baseUrl.replace(/\/+$/, '')}${apiPath}`;
33
+ }
34
+
35
+ function parseDataList(raw) {
36
+ if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
37
+ return raw;
38
+ }
39
+
40
+ const text = String(raw || '').trim();
41
+ if (!text) {
42
+ return null;
43
+ }
44
+
45
+ try {
46
+ const parsed = JSON.parse(text);
47
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
48
+ return parsed;
49
+ }
50
+ return undefined;
51
+ } catch {
52
+ return undefined;
53
+ }
54
+ }
55
+
56
+ function buildPayload(kwargs, parsedDataList, corpid) {
57
+ const payload = {
58
+ corpid,
59
+ dataId: Number(kwargs.dataId || 0),
60
+ };
61
+
62
+ if (kwargs.userId) {
63
+ payload.userId = String(kwargs.userId);
64
+ }
65
+ if (parsedDataList) {
66
+ payload.dataList = parsedDataList;
67
+ }
68
+
69
+ return payload;
70
+ }
71
+
72
+ function getValidationError(payload, token, parsedDataList) {
73
+ if (!payload.corpid) {
74
+ return { code: 'NO_CORPID', msg: '缺少本地 corpid;请先执行 xbbcli token-set --corpid <CORPID> --token <TOKEN> --userId <USERID>' };
75
+ }
76
+ if (!payload.dataId) {
77
+ return { code: 'NO_DATAID', msg: '缺少 --dataId' };
78
+ }
79
+ if (!token) {
80
+ return { code: 'NO_TOKEN', msg: MISSING_TOKEN_MESSAGE };
81
+ }
82
+ if (parsedDataList === null) {
83
+ return { code: 'NO_DATALIST', msg: '缺少 --dataList' };
84
+ }
85
+ if (parsedDataList === undefined) {
86
+ return { code: 'INVALID_DATALIST', msg: '--dataList 必须是 JSON 对象字符串' };
87
+ }
88
+
89
+ return null;
90
+ }
91
+
92
+ function makeErrorRow(code, msg) {
93
+ return [{
94
+ dataId: '',
95
+ resultCode: '',
96
+ resultMsg: '',
97
+ code,
98
+ msg,
99
+ }];
100
+ }
101
+
102
+ function makeSuccessRow(data) {
103
+ const result = data.result || {};
104
+ return [{
105
+ dataId: result.dataId || '',
106
+ resultCode: result.code ?? '',
107
+ resultMsg: result.msg || '',
108
+ code: data.code ?? '',
109
+ msg: data.msg || '',
110
+ }];
111
+ }
112
+
113
+ cli({
114
+ site: 'xbb',
115
+ name: 'work-order-edit',
116
+ description: '编辑工单接口(businessType: 20300)',
117
+ strategy: Strategy.PUBLIC,
118
+ access: 'write',
119
+ browser: false,
120
+ domain: 'proapi.xbongbong.com',
121
+ args: [
122
+ { name: 'dataId', type: 'int', help: '表单数据id(必填)' },
123
+ { name: 'dataList', type: 'str', help: '表单数据JSON字符串(必填)' },
124
+ { name: 'userId', type: 'str', default: '', help: '操作人id(可选)' },
125
+ { name: 'debug', type: 'bool', default: false, help: '输出请求体和返回体调试信息' },
126
+ ],
127
+ columns: ['dataId', 'resultCode', 'resultMsg', 'code', 'msg'],
128
+ func: async function (kwargs) {
129
+ const debug = Boolean(kwargs.debug);
130
+ const { corpid, token, baseUrl, userId } = getRuntimeConfig();
131
+ const parsedDataList = parseDataList(kwargs.dataList);
132
+ const payload = buildPayload(kwargs, parsedDataList, corpid);
133
+ const requestBody = JSON.stringify(payload);
134
+
135
+ const validationError = getValidationError(payload, token, parsedDataList);
136
+ if (validationError) {
137
+ return makeErrorRow(validationError.code, validationError.msg);
138
+ }
139
+
140
+ const sign = crypto.createHash('sha256').update(requestBody + token).digest('hex');
141
+ const headers = Object.assign({
142
+ 'Content-Type': 'application/json;charset=UTF-8',
143
+ sign,
144
+ }, userId ? { userId } : {});
145
+ const apiUrl = buildApiUrl(baseUrl, WORK_ORDER_EDIT_API_URL);
146
+ if (debug) {
147
+ process.stderr.write(`[debug] URL: ${apiUrl}\n[debug] Headers: ${JSON.stringify(headers)}\n[debug] RequestBody: ${requestBody}\n`);
148
+ }
149
+ const resp = await fetch(apiUrl, {
150
+ method: 'POST',
151
+ headers,
152
+ body: requestBody,
153
+ });
154
+
155
+ if (!resp.ok) {
156
+ const responseText = await resp.text();
157
+ if (debug) process.stderr.write(`[debug] ResponseBody: ${responseText}\n`);
158
+ return makeErrorRow(resp.status, `HTTP ${resp.status} ${resp.statusText}`);
159
+ }
160
+
161
+ const data = await resp.json();
162
+ const responseBody = JSON.stringify(data);
163
+ if (debug) process.stderr.write(`[debug] ResponseBody: ${responseBody}\n`);
164
+ if (data.code !== 1) {
165
+ return makeErrorRow(data.code ?? '', data.msg ?? '未知错误');
166
+ }
167
+
168
+ return makeSuccessRow(data);
169
+ },
170
+ });
@@ -0,0 +1,243 @@
1
+ import crypto from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { cli, Strategy } from './xbb-registry.js';
6
+
7
+ const CONFIG_FILE = path.join(os.homedir(), '.xbbcli', 'config.env');
8
+ const WORK_ORDER_LIST_API_URL = 'https://proapi.xbongbong.com/pro/v2/api/workOrderV2/list';
9
+ const DEFAULT_BASE_URL = 'https://proapi.xbongbong.com';
10
+ const MISSING_TOKEN_MESSAGE = '缺少 token;请先执行 xbbcli token-set --corpid <CORPID> --token <TOKEN> --userId <USERID>';
11
+ let lastPagination = null;
12
+
13
+ function readConfig() {
14
+ try {
15
+ return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
16
+ } catch {
17
+ return {};
18
+ }
19
+ }
20
+
21
+ function getRuntimeConfig() {
22
+ const config = readConfig();
23
+ return {
24
+ corpid: String(config.corpid || '').trim(),
25
+ token: String(config.token || '').trim(),
26
+ baseUrl: String(config.baseurl || DEFAULT_BASE_URL).trim(),
27
+ userId: String(config.userId || '').trim(),
28
+ };
29
+ }
30
+
31
+ function buildApiUrl(baseUrl, defaultUrl) {
32
+ const apiPath = new URL(defaultUrl).pathname;
33
+ return `${baseUrl.replace(/\/+$/, '')}${apiPath}`;
34
+ }
35
+
36
+ function parseOptionalInteger(value, code, msg) {
37
+ if (String(value ?? '') === '') {
38
+ return null;
39
+ }
40
+
41
+ const parsed = Number(value);
42
+ if (Number.isNaN(parsed)) {
43
+ throw new Error(`${code}:${msg}`);
44
+ }
45
+
46
+ return parsed;
47
+ }
48
+
49
+ function buildConditions(kwargs) {
50
+ const conditions = kwargs.conditions;
51
+ if (typeof conditions === 'string' && conditions.trim()) {
52
+ const parsed = JSON.parse(conditions);
53
+ if (!Array.isArray(parsed)) {
54
+ throw new Error('INVALID_CONDITIONS:conditions 必须是 JSON 数组');
55
+ }
56
+ return parsed;
57
+ }
58
+
59
+ if (!(kwargs.attr && kwargs.value)) {
60
+ return [];
61
+ }
62
+
63
+ return [{
64
+ attr: String(kwargs.attr),
65
+ value: [String(kwargs.value)],
66
+ symbol: String(kwargs.symbol || 'equal'),
67
+ }];
68
+ }
69
+
70
+ function buildPayload(kwargs, corpid) {
71
+ const payload = {
72
+ corpid,
73
+ formId: Number(kwargs.formId || 0),
74
+ };
75
+
76
+ const page = parseOptionalInteger(kwargs.page, 'INVALID_PAGE', 'page 必须是数字');
77
+ if (page !== null) {
78
+ payload.page = page;
79
+ }
80
+
81
+ const pageSize = parseOptionalInteger(kwargs.pageSize, 'INVALID_PAGE_SIZE', 'pageSize 必须是数字');
82
+ if (pageSize !== null) {
83
+ payload.pageSize = pageSize;
84
+ }
85
+
86
+ const userId = String(kwargs.userId || '');
87
+ if (userId) {
88
+ payload.userId = userId;
89
+ }
90
+
91
+ const viewApproval = parseOptionalInteger(kwargs.viewApproval, 'INVALID_VIEW_APPROVAL', 'viewApproval 必须是数字');
92
+ if (viewApproval !== null) {
93
+ payload.viewApproval = viewApproval;
94
+ }
95
+
96
+ const conditions = buildConditions(kwargs);
97
+ if (conditions.length) {
98
+ payload.conditions = conditions;
99
+ }
100
+
101
+ return payload;
102
+ }
103
+
104
+ function getValidationError(payload, token) {
105
+ if (!payload.corpid) {
106
+ return { code: 'NO_CORPID', msg: '缺少本地 corpid;请先执行 xbbcli token-set --corpid <CORPID> --token <TOKEN> --userId <USERID>' };
107
+ }
108
+ if (!payload.formId) {
109
+ return { code: 'NO_FORMID', msg: '缺少 --formId' };
110
+ }
111
+ if (!token) {
112
+ return { code: 'NO_TOKEN', msg: MISSING_TOKEN_MESSAGE };
113
+ }
114
+
115
+ return null;
116
+ }
117
+
118
+ function makeErrorRow(code, msg) {
119
+ return [{
120
+ rank: '',
121
+ dataId: '',
122
+ formId: '',
123
+ serialNo: '',
124
+ customerId: '',
125
+ ownerId: '',
126
+ status: '',
127
+ creatorId: '',
128
+ addTime: '',
129
+ updateTime: '',
130
+ data: '',
131
+ code,
132
+ msg,
133
+ }];
134
+ }
135
+
136
+ function stringifyValue(value) {
137
+ if (Array.isArray(value) || (value && typeof value === 'object')) {
138
+ return JSON.stringify(value);
139
+ }
140
+ return value ?? '';
141
+ }
142
+
143
+ function makeSuccessRows(list) {
144
+ return list.map((item, index) => ({
145
+ rank: index + 1,
146
+ dataId: item.dataId || '',
147
+ formId: item.formId || '',
148
+ serialNo: item.data?.serialNo || '',
149
+ customerId: stringifyValue(item.data?.text_2),
150
+ ownerId: stringifyValue(item.data?.ownerId),
151
+ status: stringifyValue(item.data?.text_4),
152
+ creatorId: stringifyValue(item.data?.creatorId),
153
+ addTime: item.addTime || '',
154
+ updateTime: item.updateTime || '',
155
+ data: JSON.stringify(item.data || {}),
156
+ code: '',
157
+ msg: '',
158
+ }));
159
+ }
160
+
161
+ cli({
162
+ site: 'xbb',
163
+ name: 'work-order-list',
164
+ description: '工单列表接口(businessType: 20300;formId必填)',
165
+ strategy: Strategy.PUBLIC,
166
+ access: 'read',
167
+ browser: false,
168
+ domain: 'proapi.xbongbong.com',
169
+ args: [
170
+ { name: 'formId', type: 'int', help: '表单id(必填)' },
171
+ { name: 'userId', type: 'str', default: '', help: '操作人id(可选)' },
172
+ { name: 'viewApproval', type: 'str', default: '', help: '是否查询审批中数据,1是,0否' },
173
+ { name: 'conditions', type: 'str', default: '', help: '条件集合 JSON 字符串数组,例如 [{"attr": "ownerId", "value": ["02415643151585"], "symbol": "equal"}]' },
174
+ { name: 'attr', type: 'str', default: '', help: '筛选字段 attr,例如 serialNo工单编号,text_1工单名称,text_2关联客户id' },
175
+ { name: 'value', type: 'str', default: '', help: '筛选值,和 --attr 配合使用' },
176
+ { name: 'symbol', type: 'str', default: 'equal', help: '筛选操作符,默认 equal' },
177
+ { name: 'page', type: 'str', default: '', help: '页码(可选)' },
178
+ { name: 'pageSize', type: 'str', default: '', help: '每页数量(可选,最大 100)' },
179
+ { name: 'debug', type: 'bool', default: false, help: '输出请求体和返回体调试信息' },
180
+ { name: 'raw', type: 'bool', default: false, help: '输出接口返回的原文' },
181
+ ],
182
+ columns: ['rank', 'dataId', 'formId', 'serialNo', 'customerId', 'ownerId', 'status', 'creatorId', 'addTime', 'updateTime', 'data', 'code', 'msg'],
183
+ footerExtra: () => (lastPagination ? `totalCount ${lastPagination.totalCount} / totalPage ${lastPagination.totalPage}` : undefined),
184
+ func: async function (kwargs) {
185
+ const debug = Boolean(kwargs.debug);
186
+ const { corpid, token, baseUrl, userId } = getRuntimeConfig();
187
+ let payload;
188
+ try {
189
+ payload = buildPayload(kwargs, corpid);
190
+ } catch (error) {
191
+ const message = String(error?.message || error);
192
+ const separatorIndex = message.indexOf(':');
193
+ const code = separatorIndex > 0 ? message.slice(0, separatorIndex) : 'INVALID_PAYLOAD';
194
+ const detail = separatorIndex > 0 ? message.slice(separatorIndex + 1) : message;
195
+ return makeErrorRow(code, detail);
196
+ }
197
+
198
+ const body = JSON.stringify(payload);
199
+
200
+ const validationError = getValidationError(payload, token);
201
+ if (validationError) {
202
+ return makeErrorRow(validationError.code, validationError.msg);
203
+ }
204
+
205
+ const sign = crypto.createHash('sha256').update(body + token).digest('hex');
206
+ const headers = Object.assign({
207
+ 'Content-Type': 'application/json;charset=UTF-8',
208
+ sign,
209
+ }, userId ? { userId } : {});
210
+ if (debug) {
211
+ process.stderr.write(`[debug] URL: ${buildApiUrl(baseUrl, WORK_ORDER_LIST_API_URL)}\n[debug] Headers: ${JSON.stringify(headers)}\n[debug] RequestBody: ${body}\n`);
212
+ }
213
+ const resp = await fetch(buildApiUrl(baseUrl, WORK_ORDER_LIST_API_URL), {
214
+ method: 'POST',
215
+ headers,
216
+ body,
217
+ });
218
+
219
+ if (!resp.ok) {
220
+ const responseText = await resp.text();
221
+ if (debug) process.stderr.write(`[debug] ResponseBody: ${responseText}\n`);
222
+ return makeErrorRow(resp.status, `HTTP ${resp.status} ${resp.statusText}`);
223
+ }
224
+
225
+ const data = await resp.json();
226
+ lastPagination = data?.code === 1 && data?.result?.totalCount != null && data?.result?.totalPage != null
227
+ ? { totalCount: data.result.totalCount, totalPage: data.result.totalPage }
228
+ : null;
229
+ const responseBody = JSON.stringify(data);
230
+ if (debug) process.stderr.write(`[debug] ResponseBody: ${responseBody}\n`);
231
+ if (kwargs.raw) return [{ raw: responseBody }];
232
+ if (data.code !== 1) {
233
+ return makeErrorRow(data.code ?? '', data.msg ?? '未知错误');
234
+ }
235
+
236
+ const list = Array.isArray(data.result?.list) ? data.result.list : [];
237
+ if (!list.length) {
238
+ return makeErrorRow('NO_DATA', '接口成功,但 list 为空');
239
+ }
240
+
241
+ return makeSuccessRows(list);
242
+ },
243
+ });
@@ -0,0 +1,167 @@
1
+ import crypto from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { cli, Strategy } from './xbb-registry.js';
6
+
7
+ const CONFIG_FILE = path.join(os.homedir(), '.xbbcli', 'config.env');
8
+ const WORK_ORDER_OPERATE_API_URL = 'https://proapi.xbongbong.com/pro/v2/api/workOrderV2/operate';
9
+ const DEFAULT_BASE_URL = 'https://proapi.xbongbong.com';
10
+ const MISSING_TOKEN_MESSAGE = '缺少 token;请先执行 xbbcli token-set --corpid <CORPID> --token <TOKEN> --userId <USERID>';
11
+
12
+ function readConfig() {
13
+ try {
14
+ return JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
15
+ } catch {
16
+ return {};
17
+ }
18
+ }
19
+
20
+ function getRuntimeConfig() {
21
+ const config = readConfig();
22
+ return {
23
+ corpid: String(config.corpid || '').trim(),
24
+ token: String(config.token || '').trim(),
25
+ baseUrl: String(config.baseurl || DEFAULT_BASE_URL).trim(),
26
+ userId: String(config.userId || '').trim(),
27
+ };
28
+ }
29
+
30
+ function buildApiUrl(baseUrl, apiUrl) {
31
+ const apiPath = new URL(apiUrl).pathname;
32
+ return `${baseUrl.replace(/\/+$/, '')}${apiPath}`;
33
+ }
34
+
35
+ function parseData(raw) {
36
+ if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
37
+ return raw;
38
+ }
39
+
40
+ const text = String(raw || '').trim();
41
+ if (!text) {
42
+ return null;
43
+ }
44
+
45
+ try {
46
+ const parsed = JSON.parse(text);
47
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
48
+ return parsed;
49
+ }
50
+ return undefined;
51
+ } catch {
52
+ return undefined;
53
+ }
54
+ }
55
+
56
+ function buildPayload(kwargs, parsedData, corpid) {
57
+ const payload = {
58
+ corpid,
59
+ dataId: Number(kwargs.dataId || 0),
60
+ operateType: Number(kwargs.operateType || 0),
61
+ };
62
+
63
+ if (kwargs.userId) {
64
+ payload.userId = String(kwargs.userId);
65
+ }
66
+ if (parsedData) {
67
+ payload.data = parsedData;
68
+ }
69
+
70
+ return payload;
71
+ }
72
+
73
+ function getValidationError(payload, token, parsedData) {
74
+ if (!payload.corpid) {
75
+ return { code: 'NO_CORPID', msg: '缺少本地 corpid;请先执行 xbbcli token-set --corpid <CORPID> --token <TOKEN> --userId <USERID>' };
76
+ }
77
+ if (!payload.dataId) {
78
+ return { code: 'NO_DATAID', msg: '缺少 --dataId' };
79
+ }
80
+ if (!payload.operateType) {
81
+ return { code: 'NO_OPERATETYPE', msg: '缺少 --operateType' };
82
+ }
83
+ if (!token) {
84
+ return { code: 'NO_TOKEN', msg: MISSING_TOKEN_MESSAGE };
85
+ }
86
+ if (parsedData === undefined) {
87
+ return { code: 'INVALID_DATA', msg: '--data 必须是 JSON 对象字符串' };
88
+ }
89
+
90
+ return null;
91
+ }
92
+
93
+ function makeErrorRow(code, msg) {
94
+ return [{
95
+ dataId: '',
96
+ code,
97
+ msg,
98
+ }];
99
+ }
100
+
101
+ function makeSuccessRow(kwargs, data) {
102
+ return [{
103
+ dataId: kwargs.dataId || '',
104
+ code: data.code ?? '',
105
+ msg: data.msg || '',
106
+ }];
107
+ }
108
+
109
+ cli({
110
+ site: 'xbb',
111
+ name: 'work-order-operate',
112
+ description: '工单流转接口(businessType: 20300)',
113
+ strategy: Strategy.PUBLIC,
114
+ access: 'write',
115
+ browser: false,
116
+ domain: 'proapi.xbongbong.com',
117
+ args: [
118
+ { name: 'dataId', type: 'int', help: '工单数据id(必填)' },
119
+ { name: 'operateType', type: 'int', help: '操作类型(必填):1取消 2重启 3移交 4分配 5变更工单池 6抢单 7回退至工单池 8接受 9拒绝 10开始 11签到 12完成 13签退 15回退 16结算 17回访 18指派 19自由节点完成 20编辑回执单' },
120
+ { name: 'data', type: 'str', default: '', help: '操作参数JSON字符串(可选;按operateType提供,如{"cancelReason":"xxx"})' },
121
+ { name: 'userId', type: 'str', default: '', help: '操作人id(可选)' },
122
+ { name: 'debug', type: 'bool', default: false, help: '输出请求体和返回体调试信息' },
123
+ ],
124
+ columns: ['dataId', 'code', 'msg'],
125
+ func: async function (kwargs) {
126
+ const debug = Boolean(kwargs.debug);
127
+ const { corpid, token, baseUrl, userId } = getRuntimeConfig();
128
+ const parsedData = parseData(kwargs.data);
129
+ const payload = buildPayload(kwargs, parsedData, corpid);
130
+ const requestBody = JSON.stringify(payload);
131
+
132
+ const validationError = getValidationError(payload, token, parsedData);
133
+ if (validationError) {
134
+ return makeErrorRow(validationError.code, validationError.msg);
135
+ }
136
+
137
+ const sign = crypto.createHash('sha256').update(requestBody + token).digest('hex');
138
+ const headers = Object.assign({
139
+ 'Content-Type': 'application/json;charset=UTF-8',
140
+ sign,
141
+ }, userId ? { userId } : {});
142
+ const apiUrl = buildApiUrl(baseUrl, WORK_ORDER_OPERATE_API_URL);
143
+ if (debug) {
144
+ process.stderr.write(`[debug] URL: ${apiUrl}\n[debug] Headers: ${JSON.stringify(headers)}\n[debug] RequestBody: ${requestBody}\n`);
145
+ }
146
+ const resp = await fetch(apiUrl, {
147
+ method: 'POST',
148
+ headers,
149
+ body: requestBody,
150
+ });
151
+
152
+ if (!resp.ok) {
153
+ const responseText = await resp.text();
154
+ if (debug) process.stderr.write(`[debug] ResponseBody: ${responseText}\n`);
155
+ return makeErrorRow(resp.status, `HTTP ${resp.status} ${resp.statusText}`);
156
+ }
157
+
158
+ const data = await resp.json();
159
+ const responseBody = JSON.stringify(data);
160
+ if (debug) process.stderr.write(`[debug] ResponseBody: ${responseBody}\n`);
161
+ if (data.code !== 1) {
162
+ return makeErrorRow(data.code ?? '', data.msg ?? '未知错误');
163
+ }
164
+
165
+ return makeSuccessRow(kwargs, data);
166
+ },
167
+ });