Skip to content

Commit a7b604d

Browse files
committed
feat(dcloud): get user info.
1 parent 3689b5a commit a7b604d

File tree

4 files changed

+99
-6
lines changed

4 files changed

+99
-6
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "coding-hbuilderx",
3-
"description": "your extension description",
4-
"displayName": "your extension display name",
5-
"version": "0.0.0",
6-
"publisher": "your name",
2+
"name": "codingplugin",
3+
"description": "",
4+
"displayName": "CODING 代码仓库插件",
5+
"version": "1.0.0",
6+
"publisher": "coding",
77
"engines": {
88
"HBuilderX": "^2.7.0"
99
},

src/init.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import MRTreeDataProvider from './trees/mr';
55
import toast from './utils/toast';
66
import { getMRUrl } from './utils/repo';
77
import ACTIONS, { dispatch } from './utils/actions';
8-
import { IDepot, IMRItem } from './typings/common';
8+
import { IDepot, IMRItem, IOAuthResponse } from './typings/common';
9+
import * as DCloudService from './services/dcloud';
910

1011
const { registerCommand } = hx.commands;
1112

@@ -60,6 +61,29 @@ export function createTreeViews(context: IContext) {
6061
treeDataProvider: new DepotTreeDataProvider(context),
6162
}),
6263
);
64+
65+
hx.authorize
66+
.login({
67+
scopes: ['basic', 'email', 'phone'],
68+
appId: DCloudService.appId,
69+
})
70+
.then(async (param: IOAuthResponse) => {
71+
const { code, error } = param;
72+
if (error) {
73+
console.log(error);
74+
return;
75+
}
76+
console.info(code);
77+
78+
try {
79+
const token = await DCloudService.applyForToken(code);
80+
const resp = await DCloudService.fetchUser(token.data.access_token);
81+
console.info(resp);
82+
toast.info(`logged in as DCloud user: ${resp.data.nickname} ${resp.data.email}`);
83+
} catch (err) {
84+
console.error(err);
85+
}
86+
});
6387
}
6488

6589
export function workspaceInit() {

src/services/dcloud.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import axios from '../utils/axios';
2+
import { ITokenResponse, IDCloudUser } from '../typings/common';
3+
4+
const appSecret = `eLEDnBuT258P1OmRx1OVFSCj4SZXom`;
5+
export const appId = `gZTr3CuevT`;
6+
7+
export const applyForToken = async (code: string) => {
8+
try {
9+
const resp: ITokenResponse = await axios.get(`https://ide.dcloud.net.cn/dcloudOauthv2/accessToken`, {
10+
params: {
11+
code,
12+
appid: appId,
13+
app_secret: appSecret,
14+
},
15+
});
16+
17+
if (resp.ret) {
18+
return Promise.reject(resp);
19+
}
20+
return resp;
21+
} catch (e) {
22+
return Promise.reject(e);
23+
}
24+
};
25+
26+
export const fetchUser = async (accessToken: string) => {
27+
try {
28+
const resp: IDCloudUser = await axios.get(`https://ide.dcloud.net.cn/dcloudOauthv2/userInfo`, {
29+
params: {
30+
access_token: accessToken,
31+
},
32+
});
33+
34+
if (resp.ret) {
35+
return Promise.reject(resp);
36+
}
37+
return resp;
38+
} catch (e) {
39+
return Promise.reject(e);
40+
}
41+
};

src/typings/common.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,31 @@ export interface IDepot {
5656
svnEnabled: boolean;
5757
vcsType: 'git' | 'svn';
5858
}
59+
60+
export interface IOAuthResponse {
61+
code: string;
62+
error: number;
63+
}
64+
65+
export interface ITokenResponse {
66+
ret: number;
67+
desc: string;
68+
data: {
69+
access_token: string;
70+
access_token_ttl: string;
71+
refresh_token: string;
72+
refresh_token_ttl: string;
73+
};
74+
}
75+
76+
export interface IDCloudUser {
77+
ret: number;
78+
desc: string;
79+
data: {
80+
nickname: string;
81+
avatar: string;
82+
uid: string;
83+
email: string;
84+
phone: string;
85+
};
86+
}

0 commit comments

Comments
 (0)