Skip to content

Commit 13a06e2

Browse files
committed
feat: create team api
1 parent c3e7717 commit 13a06e2

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

src/init.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@ import { IDepot, IMRItem, IUserInfo } from './typings/common';
1010
import * as DCloudService from './services/dcloud';
1111
import CodingServer from './services/codingServer';
1212

13-
const { registerCommand } = hx.commands;
13+
const { registerCommand, executeCommand } = hx.commands;
1414
const { createTreeView, showQuickPick } = hx.window;
1515

1616
export function registerCommands(context: IContext) {
1717
const { codingServer, token } = context;
1818

1919
context.subscriptions.push(
2020
registerCommand('codingPlugin.pickDepot', async function () {
21+
/////////////// Test
22+
context.codingServer.createTeam();
23+
executeCommand('codingPlugin.password');
24+
2125
const options: IQuickPickOption[] = [];
2226
context.depots.forEach((depot: IDepot) => {
2327
const { name, depotPath } = depot;
@@ -96,6 +100,23 @@ export function registerCommands(context: IContext) {
96100
}
97101
}),
98102
);
103+
104+
context.subscriptions.push(
105+
registerCommand('codingPlugin.auth', async function () {
106+
initCredentials(context);
107+
}),
108+
);
109+
110+
context.subscriptions.push(
111+
registerCommand('codingPlugin.password', async function () {
112+
const password = await hx.window.showInputBox({
113+
prompt: '配置 CODING 服务密码',
114+
password: true,
115+
});
116+
117+
console.log('password => ', password);
118+
}),
119+
);
99120
}
100121

101122
export function createTreeViews(context: IContext) {
@@ -148,17 +169,24 @@ export function registerCustomEditors(context: IContext) {
148169
async function initCredentials(context: IContext) {
149170
try {
150171
let hbToken = await DCloudService.readConfig(`hbToken`);
172+
console.log('hbToken => ', hbToken);
173+
151174
if (!hbToken) {
152175
const code = await DCloudService.grantForUserInfo();
153176
const tokenResult = await DCloudService.applyForToken(code);
177+
console.log('tokenResult => ', tokenResult);
154178
hbToken = tokenResult.data.access_token;
155179
}
180+
156181
const resp = await DCloudService.fetchUser(hbToken);
182+
console.log('resp => ', resp);
157183
toast.info(`logged in as DCloud user: ${resp.data.nickname} ${resp.data.email}`);
184+
158185
const {
159186
ctx: { codingServer, repoInfo, token },
160187
} = context;
161188
const userData = await codingServer.getUserInfo(token);
189+
console.log('userData => ', userData);
162190
toast.info(`logged in as coding user: ${userData.name} @ ${userData.team}`);
163191
} catch (err) {
164192
console.error(err);

src/services/codingServer.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import axios from '../utils/axios';
55
import git from 'isomorphic-git';
66
import { IDepot, IRepoInfo, ISessionData } from '../typings/common';
77
import { parseCloneUrl } from '../utils/repo';
8-
import toast from '../utils/toast';
8+
import { getIp } from '../utils/ip';
9+
import { password } from '../utils/password';
910

1011
export default class CodingServer {
1112
_session: ISessionData;
@@ -172,4 +173,30 @@ export default class CodingServer {
172173
console.error(err);
173174
}
174175
}
176+
177+
async createTeam() {
178+
try {
179+
const ip = getIp();
180+
const pwd = password('coding123');
181+
console.log('ip => ', ip);
182+
console.log('pwd => ', pwd);
183+
184+
const result = await axios({
185+
method: 'post',
186+
url: `https://e.coding.net/open-api`,
187+
data: {
188+
Action: 'CreateTeam',
189+
Domain: '',
190+
TeamName: '',
191+
Ip: ip,
192+
Password: '',
193+
Phone: '',
194+
Email: '',
195+
},
196+
});
197+
console.log('result => ', result);
198+
} catch (err) {
199+
console.error(err);
200+
}
201+
}
175202
}

src/services/dcloud.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ export const readConfig = async (prop: string) => {
7070
return token;
7171
};
7272

73+
const GRANT_ERROR: Record<string, string> = {
74+
1: '当前没有登录用户',
75+
2: '用户取消了授权',
76+
3: '上一次授权的CODE码还未过期(有效期5分钟)',
77+
4: '插件状态异常',
78+
1002: '服务器参数错误',
79+
2001: '应用信息不存在',
80+
3004: '超时',
81+
3203: '404',
82+
};
83+
7384
export const grantForUserInfo = (): Promise<string | null> =>
7485
new Promise((resolve, reject) => {
7586
hx.authorize
@@ -78,9 +89,11 @@ export const grantForUserInfo = (): Promise<string | null> =>
7889
appId: appId,
7990
})
8091
.then((param: IOAuthResponse) => {
92+
console.log('param => ', param);
8193
const { code, error } = param;
8294
if (error || !code) {
83-
return reject(null);
95+
console.error(`授权出错码 ${error}${GRANT_ERROR[error]}`);
96+
return reject(error);
8497
}
8598

8699
console.log(`hbuilder oauth code: `, code);

src/utils/ip.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os from 'os';
2+
3+
export const getIp = () => {
4+
let address;
5+
const ifaces = os.networkInterfaces();
6+
7+
for (const dev in ifaces) {
8+
const iface = ifaces[dev]?.filter((details: os.NetworkInterfaceInfo) => {
9+
return details.family === 'IPv4' && details.internal === false;
10+
});
11+
12+
if (iface && iface.length > 0) {
13+
address = iface[0].address;
14+
}
15+
}
16+
17+
return address;
18+
};

src/utils/password.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import crypto from 'crypto';
2+
3+
export const password = (pwd: string) => {
4+
const md5 = crypto.createHash('sha1');
5+
const result = md5.update(pwd).digest('base64');
6+
return result;
7+
};

0 commit comments

Comments
 (0)