@seayoo-web/gamer-api 1.1.6 → 1.1.7

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 (3) hide show
  1. package/README.md +63 -24
  2. package/dist/index.js +1 -1
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -5,30 +5,58 @@
5
5
  ```js
6
6
  import { NetRequest } from "@seayoo-web/request";
7
7
  import { AuthToken, EventApi, ClubApi, CommunityApi } from "@seayoo-web/gamer-api";
8
+ import { captureException } from "@sentry/vue";
8
9
 
9
- const authToken = new AuthToken("https://gamer-api.seayoo.com", NetRequest);
10
+ const authToken = new AuthToken(SY_GAMER_API, NetRequest);
10
11
  const eventApi = new EventApi(authToken, eventId);
11
12
  const clubApi = new ClubApi(authToken);
12
13
  const communityApi = new CommunityApi(authToken);
13
14
 
14
- // 设置 idToken 后即可认为登录(会在内部自动进行 gamerToken 的置换操作)
15
- // idToken 通过通行证登录组件获取
16
- export function login(idToken: string) {
17
- authToken.idToken = idToken;
15
+ // 调整请求配置
16
+ authToken.req.setConfig({
17
+ messageHandler(_, message) {
18
+ toast(message);
19
+ },
20
+ errorHandler({ status, rawError, sentryError, sentryTags, sentryExtra }) {
21
+ if (status === 401) {
22
+ logout();
23
+ // 继续其他处理:提示或直接登录
24
+ //
25
+ return;
26
+ }
27
+ // 意外错误进行上报
28
+ if (rawError) {
29
+ captureException(sentryError, {
30
+ extra: sentryExtra,
31
+ tags: sentryTags,
32
+ });
33
+ }
34
+ },
35
+ });
36
+
37
+ // 触发登录
38
+ export async function login() {
39
+ // idToken 通过通行证登录组件获取
40
+ const idToken = await accountLogin();
41
+ if (idToken) {
42
+ authToken.idToken = idToken;
18
43
  // 可选自动登录调用,后续每个 api 都会自动检查并 autoLogin
19
- authToken.autoLogin();
44
+ authToken.autoLogin();
45
+ }
20
46
  }
21
47
 
22
- // 检测是否登录,登录后获取用户数据
48
+ // 获取用户数据
23
49
  export async function getUserInfo() {
24
- return authToken.isLogined ? await authToken.getSession() : null
50
+ return authToken.isLogined ? await authToken.getSession() : null;
25
51
  }
26
52
 
27
- // 如果在 ComboWebView 中,url query 会直接提供 gamer_token
28
- // authToken 会自动从 url query 中读取并更新 gamer_token
53
+ // 退出登录(同步操作)
54
+ export function logout() {
55
+ authToken.logout();
56
+ }
29
57
 
30
58
  // 导出供其他功能使用
31
- export { eventApi, clubApi, communityApi, weixinApi };
59
+ export { eventApi, clubApi, communityApi };
32
60
  ```
33
61
 
34
62
  ## 小程序初始化
@@ -37,30 +65,41 @@ export { eventApi, clubApi, communityApi, weixinApi };
37
65
  import { NetRequest } from "@seayoo-web/request/wx";
38
66
  import { AuthToken, EventApi, ClubApi, WeixinApi } from "@seayoo-web/gamer-api";
39
67
 
40
- const authToken = new AuthToken("https://gamer-api.seayoo.com", NetRequest);
68
+ const authToken = new AuthToken(SY_GAMER_API, NetRequest);
41
69
  const eventApi = new EventApi(authToken, eventId);
42
70
  const clubApi = new ClubApi(authToken);
43
71
  const communityApi = new CommunityApi(authToken);
44
72
  const weixinApi = new WeixinApi(authToken, appId);
45
73
 
46
- // 获取 weixinToken / unionid / openid
47
- export async function weixinLogin() {
48
- const result = await weixinApi.login()
49
- if("error" in result) {
50
- /* handle error */
51
- return null
74
+ // 登录
75
+ export function login() {
76
+ // idToken 通过通行证登录组件获取
77
+ const idToken = await accountLogin();
78
+ if(!idToken) {
79
+ return
52
80
  }
53
- return result; // { weixin_token, openid, unionid }
54
- }
55
-
56
- // idToken 通过通行证登录组件获取,weixinToken 通过上述方法获取
57
- export function login(idToken: string, weixinToken: string) {
81
+ // 获取 weixinToken,成功时返回 { weixin_token, openid, unionid }
82
+ const result = await weixinApi.login();
83
+ if(!("weixin_token" in result)) {
84
+ return
85
+ }
86
+ // 设置
58
87
  authToken.idToken = idToken
59
- authToken.weixinToken = weixinToken;
88
+ authToken.weixinToken = result.weixin_token;
60
89
  // 可选自动登录调用,后续每个 api 都会自动检查并 autoLogin
61
90
  authToken.autoLogin();
62
91
  }
63
92
 
93
+ // 获取用户数据
94
+ export async function getUserInfo() {
95
+ return authToken.isLogined ? await authToken.getSession() : null;
96
+ }
97
+
98
+ // 退出登录(同步操作)
99
+ export function logout() {
100
+ authToken.logout();
101
+ }
102
+
64
103
  // 导出供其他功能使用
65
104
  export { eventApi, clubApi, communityApi, weixinApi };
66
105
  ```
package/dist/index.js CHANGED
@@ -89,7 +89,7 @@ class Yt {
89
89
  const r = this.storage.get(_).match(/^(\d{12,})#(.{8,})$/);
90
90
  if (r) {
91
91
  const i = parseInt(r[1]), c = r[2];
92
- if (i > Date.now() - 2 * 24 * 3600 * 1e3) {
92
+ if (i > Date.now() - (24 * 3600 - 10) * 1e3) {
93
93
  this.$gamerToken = c;
94
94
  return;
95
95
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@seayoo-web/gamer-api",
3
3
  "description": "agent for gamer api",
4
- "version": "1.1.6",
4
+ "version": "1.1.7",
5
5
  "type": "module",
6
6
  "source": "index.ts",
7
7
  "main": "./dist/index.js",
@@ -24,9 +24,9 @@
24
24
  "@types/node": "^22.13.1",
25
25
  "@seayoo-web/combo-webview": "^2.5.0",
26
26
  "@seayoo-web/tsconfig": "^1.0.3",
27
+ "@seayoo-web/request": "^2.1.2",
27
28
  "@seayoo-web/scripts": "^1.3.9",
28
- "@seayoo-web/utils": "^3.0.10",
29
- "@seayoo-web/request": "^2.1.2"
29
+ "@seayoo-web/utils": "^3.0.10"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "@seayoo-web/combo-webview": "^2.5.0",