@seayoo-web/gamer-api 1.1.5 → 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 +20 -18
  3. package/package.json +2 -2
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
@@ -66,8 +66,10 @@ class Yt {
66
66
  u(this, "$gamerToken", "");
67
67
  u(this, "req");
68
68
  u(this, "storage");
69
- this.storage = m.wx ? Z : Y, this.req = r({
70
- baseURL: O(t) ? t : `https://${j(t)}/v1`,
69
+ this.storage = m.wx ? Z : Y;
70
+ const i = O(t) ? t : `https://${j(t)}`;
71
+ this.req = r({
72
+ baseURL: `${i}/v1`,
71
73
  timeout: 1e4,
72
74
  responseRule: {
73
75
  ok: { resolve: "body" },
@@ -78,8 +80,8 @@ class Yt {
78
80
  }
79
81
  },
80
82
  // 使用箭头函数绑定执行上下文
81
- requestTransformer: (i) => {
82
- this.$gamerToken && (i.headers.Authorization = `Bearer ${this.$gamerToken}`);
83
+ requestTransformer: (c) => {
84
+ this.$gamerToken && (c.headers.Authorization = `Bearer ${this.$gamerToken}`);
83
85
  }
84
86
  }), T() ? this.$gamerToken = h("gamer_token") : this.loadGamerTokenCache();
85
87
  }
@@ -87,7 +89,7 @@ class Yt {
87
89
  const r = this.storage.get(_).match(/^(\d{12,})#(.{8,})$/);
88
90
  if (r) {
89
91
  const i = parseInt(r[1]), c = r[2];
90
- if (i > Date.now() - 2 * 24 * 3600 * 1e3) {
92
+ if (i > Date.now() - (24 * 3600 - 10) * 1e3) {
91
93
  this.$gamerToken = c;
92
94
  return;
93
95
  }
@@ -249,7 +251,7 @@ function re(e) {
249
251
  function f(e) {
250
252
  return n(e, "role_card") && re(e.role_card);
251
253
  }
252
- function U(e) {
254
+ function $(e) {
253
255
  return n(e, "name", "avatar_url", "bio", "gender", "birthday") && n(e.birthday, "year", "month", "day");
254
256
  }
255
257
  function v(e) {
@@ -483,7 +485,7 @@ class Zt {
483
485
  * https://kdocs.cn/l/cgp4gSBMbOf1?linkname=TMWBCFcOr2
484
486
  */
485
487
  async getUserProfile(t) {
486
- const { data: r } = await this.req.get("club/user-profile", U, {
488
+ const { data: r } = await this.req.get("club/user-profile", $, {
487
489
  params: { member_id: t }
488
490
  });
489
491
  return r;
@@ -495,7 +497,7 @@ class Zt {
495
497
  */
496
498
  async updateUserProfile(t) {
497
499
  await this.token.autoLogin();
498
- const { data: r, code: i } = await this.req.post("club/user-profile", t, U);
500
+ const { data: r, code: i } = await this.req.post("club/user-profile", t, $);
499
501
  return r ?? {
500
502
  error: i
501
503
  };
@@ -618,7 +620,7 @@ class Zt {
618
620
  return r || { redemptions: [] };
619
621
  }
620
622
  }
621
- var ke = /* @__PURE__ */ ((e) => (e.EventItem = "event_item", e.GameItem = "game_item", e.PhysicalItem = "physical_item", e.WeixinHongbao = "weixin_hongbao", e.GiftCode = "gift_code", e.LotteryTicket = "lottery_ticket", e.VoidItem = "void_item", e))(ke || {}), be = /* @__PURE__ */ ((e) => (e.UserId = "user_id", e.RoleId = "role_id", e))(be || {}), qe = /* @__PURE__ */ ((e) => (e.None = "none", e.Daily = "daily", e.Weekly = "weekly", e.Monthly = "monthly", e))(qe || {}), o = /* @__PURE__ */ ((e) => (e.Preregister = "preregister", e.Lottery = "lottery", e.Survey = "survey", e.Invite = "invite", e.Share = "share", e.Follow = "follow", e.Subscribe = "weixin_subscribe", e.Comment = "comment", e.Vote = "vote", e.GiftCode = "gift_code", e.Register = "register", e.InvitedRegister = "invited_register", e.ClaimRewards = "claim_rewards", e.Cashback = "cashback", e.Quest = "quest", e.Team = "team", e.LotteryDraw = "lottery_draw", e))(o || {}), Re = /* @__PURE__ */ ((e) => (e.Active = "player_active_points", e.Login = "player_login_days", e.OrderAmount = "order_total_amount", e.Posts = "community_posts", e.Comments = "community_comments", e))(Re || {}), xe = /* @__PURE__ */ ((e) => (e.System = "system", e.UserSubmission = "user_submission", e.Shortlisted = "shortlisted", e))(xe || {}), Ae = /* @__PURE__ */ ((e) => (e.Ineligible = "ineligible", e.Unaccalimed = "unaccalimed", e.Received = "received", e))(Ae || {}), Le = /* @__PURE__ */ ((e) => (e.Unaccalimed = "unaccalimed", e.Received = "received", e.Failed = "failed", e.Delivered = "delivered", e))(Le || {}), Ce = /* @__PURE__ */ ((e) => (e.Ineligible = "ineligible", e.Unaccalimed = "unaccalimed", e.Received = "received", e.Failed = "failed", e))(Ce || {}), Ue = /* @__PURE__ */ ((e) => (e.Output = "output", e.Engage = "engage", e))(Ue || {}), $e = /* @__PURE__ */ ((e) => (e.Private = "private", e.Public = "public", e))($e || {}), Pe = /* @__PURE__ */ ((e) => (e.Unknown = "unknown", e.Pending = "pending", e.Drawn = "drawn", e.Claimed = "claimed", e.Fail = "fail", e))(Pe || {}), Se = /* @__PURE__ */ ((e) => (e.Assemble = "assemble", e.Join = "join", e.Disband = "disband", e.ChangeVisibility = "change_visibility", e.Query = "query", e))(Se || {}), De = /* @__PURE__ */ ((e) => (e.Query = "query", e.Draw = "draw", e.Claim = "claim", e))(De || {});
623
+ var ke = /* @__PURE__ */ ((e) => (e.EventItem = "event_item", e.GameItem = "game_item", e.PhysicalItem = "physical_item", e.WeixinHongbao = "weixin_hongbao", e.GiftCode = "gift_code", e.LotteryTicket = "lottery_ticket", e.VoidItem = "void_item", e))(ke || {}), be = /* @__PURE__ */ ((e) => (e.UserId = "user_id", e.RoleId = "role_id", e))(be || {}), qe = /* @__PURE__ */ ((e) => (e.None = "none", e.Daily = "daily", e.Weekly = "weekly", e.Monthly = "monthly", e))(qe || {}), o = /* @__PURE__ */ ((e) => (e.Preregister = "preregister", e.Lottery = "lottery", e.Survey = "survey", e.Invite = "invite", e.Share = "share", e.Follow = "follow", e.Subscribe = "weixin_subscribe", e.Comment = "comment", e.Vote = "vote", e.GiftCode = "gift_code", e.Register = "register", e.InvitedRegister = "invited_register", e.ClaimRewards = "claim_rewards", e.Cashback = "cashback", e.Quest = "quest", e.Team = "team", e.LotteryDraw = "lottery_draw", e))(o || {}), Re = /* @__PURE__ */ ((e) => (e.Active = "player_active_points", e.Login = "player_login_days", e.OrderAmount = "order_total_amount", e.Posts = "community_posts", e.Comments = "community_comments", e))(Re || {}), xe = /* @__PURE__ */ ((e) => (e.System = "system", e.UserSubmission = "user_submission", e.Shortlisted = "shortlisted", e))(xe || {}), Ae = /* @__PURE__ */ ((e) => (e.Ineligible = "ineligible", e.Unaccalimed = "unaccalimed", e.Received = "received", e))(Ae || {}), Le = /* @__PURE__ */ ((e) => (e.Unaccalimed = "unaccalimed", e.Received = "received", e.Failed = "failed", e.Delivered = "delivered", e))(Le || {}), Ce = /* @__PURE__ */ ((e) => (e.Ineligible = "ineligible", e.Unaccalimed = "unaccalimed", e.Received = "received", e.Failed = "failed", e))(Ce || {}), $e = /* @__PURE__ */ ((e) => (e.Output = "output", e.Engage = "engage", e))($e || {}), Ue = /* @__PURE__ */ ((e) => (e.Private = "private", e.Public = "public", e))(Ue || {}), Pe = /* @__PURE__ */ ((e) => (e.Unknown = "unknown", e.Pending = "pending", e.Drawn = "drawn", e.Claimed = "claimed", e.Fail = "fail", e))(Pe || {}), Se = /* @__PURE__ */ ((e) => (e.Assemble = "assemble", e.Join = "join", e.Disband = "disband", e.ChangeVisibility = "change_visibility", e.Query = "query", e))(Se || {}), De = /* @__PURE__ */ ((e) => (e.Query = "query", e.Draw = "draw", e.Claim = "claim", e))(De || {});
622
624
  function Ee(e) {
623
625
  return n(e, "event_name", "rules", "since", "until", "features") && Array.isArray(e.features) && e.features.every(Ge);
624
626
  }
@@ -1167,16 +1169,16 @@ function q(e) {
1167
1169
  "create_time"
1168
1170
  ) && a(e.topics, b) && F(e.forum);
1169
1171
  }
1170
- function $(e) {
1172
+ function U(e) {
1171
1173
  return n(e, "next_token", "posts") && a(e.posts, q);
1172
1174
  }
1173
- function Ut(e) {
1175
+ function $t(e) {
1174
1176
  return n(e, "post") && q(e.post);
1175
1177
  }
1176
1178
  function R(e) {
1177
1179
  return n(e);
1178
1180
  }
1179
- function $t(e) {
1181
+ function Ut(e) {
1180
1182
  return n(e) && ("post" in e ? q(e.post) : !0) && ("antispam" in e ? R(e.antispam) : !0);
1181
1183
  }
1182
1184
  function g(e) {
@@ -1295,7 +1297,7 @@ class er {
1295
1297
  * https://kdocs.cn/l/cbggfJodHLIz?linkname=XKI9lDVrPq
1296
1298
  */
1297
1299
  async getPosts(t) {
1298
- const { data: r, code: i } = await this.req.get("community/posts", $, {
1300
+ const { data: r, code: i } = await this.req.get("community/posts", U, {
1299
1301
  params: s(t)
1300
1302
  });
1301
1303
  return r ?? { error: i };
@@ -1306,7 +1308,7 @@ class er {
1306
1308
  * https://kdocs.cn/l/cbggfJodHLIz?linkname=yXubbZuDlC
1307
1309
  */
1308
1310
  async getPinnedPosts(t, r, i) {
1309
- const { data: c, code: d } = await this.req.get("community/pinned-posts", $, {
1311
+ const { data: c, code: d } = await this.req.get("community/pinned-posts", U, {
1310
1312
  params: s({ forum_id: t, max_results: r, next_token: i })
1311
1313
  });
1312
1314
  return c ?? { error: d };
@@ -1317,7 +1319,7 @@ class er {
1317
1319
  * https://kdocs.cn/l/cbggfJodHLIz?linkname=hj1AZy5rcP
1318
1320
  */
1319
1321
  async getPost(t) {
1320
- const { data: r, code: i } = await this.req.get("community/post", Ut, {
1322
+ const { data: r, code: i } = await this.req.get("community/post", $t, {
1321
1323
  params: { post_id: t }
1322
1324
  });
1323
1325
  return (r == null ? void 0 : r.post) ?? { error: i };
@@ -1328,7 +1330,7 @@ class er {
1328
1330
  * https://kdocs.cn/l/cbggfJodHLIz?linkname=M5okHBuaVk
1329
1331
  */
1330
1332
  async post(t) {
1331
- const { data: r, code: i } = await this.req.post("community/post", s(t), $t);
1333
+ const { data: r, code: i } = await this.req.post("community/post", s(t), Ut);
1332
1334
  return r ?? {
1333
1335
  error: i
1334
1336
  };
@@ -1625,11 +1627,11 @@ export {
1625
1627
  zt as NotificationType,
1626
1628
  Ht as PostStatus,
1627
1629
  Re as QuestObjective,
1628
- Ue as RewardSoure,
1630
+ $e as RewardSoure,
1629
1631
  Ae as RewardStatusPrefix,
1630
1632
  Le as RewardStatusSuffix,
1631
1633
  Se as TeamAction,
1632
- $e as TeamVisibility,
1634
+ Ue as TeamVisibility,
1633
1635
  xe as VoteOptionSource,
1634
1636
  tr as WeixinApi,
1635
1637
  Jt as WeixinWebLoginErrorCode
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.5",
4
+ "version": "1.1.7",
5
5
  "type": "module",
6
6
  "source": "index.ts",
7
7
  "main": "./dist/index.js",
@@ -23,8 +23,8 @@
23
23
  "devDependencies": {
24
24
  "@types/node": "^22.13.1",
25
25
  "@seayoo-web/combo-webview": "^2.5.0",
26
- "@seayoo-web/request": "^2.1.2",
27
26
  "@seayoo-web/tsconfig": "^1.0.3",
27
+ "@seayoo-web/request": "^2.1.2",
28
28
  "@seayoo-web/scripts": "^1.3.9",
29
29
  "@seayoo-web/utils": "^3.0.10"
30
30
  },