@seayoo-web/gamer-api 1.0.1 → 1.0.3

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.
package/README.md CHANGED
@@ -1,11 +1,74 @@
1
1
  # Gamer Api Agent
2
2
 
3
+ ## 页面初始化
4
+
3
5
  ```js
4
- // 在页面中使用
5
- import { GamerApi } from "@seayoo-web/gamer-api";
6
- const gamerApi1 = new GamerApi(Endpoint, eventId);
6
+ import { NetRequest } from "@seayoo-web/request";
7
+ import { AuthToken, EventApi, ClubApi, CommunityApi } from "@seayoo-web/gamer-api";
8
+
9
+ const authToken = new AuthToken("https://gamer-api.seayoo.com", NetRequest);
10
+ const eventApi = new EventApi(authToken, eventId);
11
+ const clubApi = new ClubApi(authToken);
12
+ const communityApi = new CommunityApi(authToken);
13
+
14
+ // 设置 idToken 后即可认为登录(会在内部自动进行 gamerToken 的置换操作)
15
+ // idToken 通过通行证登录组件获取
16
+ export function login(idToken: string) {
17
+ authToken.idToken = idToken;
18
+ // 可选自动登录调用,后续每个 api 都会自动检查并 autoLogin
19
+ authToken.autoLogin();
20
+ }
21
+
22
+ // 如果在 ComboWebView 中,url query 会直接提供 gamer_token
23
+ // authToken 会自动从 url query 中读取并更新 gamer_token
7
24
 
8
- // 在小程序中使用
9
- import { GamerApi } from "@seayoo-web/gamer-api/wx";
10
- const gamerApi1 = new GamerApi(Endpoint, eventId);
25
+ // 导出供其他功能使用
26
+ export { eventApi, clubApi, communityApi, weixinApi };
11
27
  ```
28
+
29
+ ## 小程序初始化
30
+
31
+ ```js
32
+ import { NetRequest } from "@seayoo-web/request/wx";
33
+ import { AuthToken, EventApi, ClubApi, WeixinApi } from "@seayoo-web/gamer-api";
34
+ import { usePromise } from "@seayoo-web/utils";
35
+
36
+ const authToken = new AuthToken("https://gamer-api.seayoo.com", NetRequest);
37
+ const eventApi = new EventApi(authToken, eventId);
38
+ const clubApi = new ClubApi(authToken);
39
+ const communityApi = new CommunityApi(authToken);
40
+ const weixinApi = new WeixinApi(authToken, appId);
41
+
42
+ // 获取 weixinToken / unionid / openid
43
+ export async function weixinLogin() {
44
+ const result = await weixinApi.login(() => {
45
+ const { promise, resolve } = usePromise<string>()
46
+ // 调用 wx.login 获取 weixinCode
47
+ wx.login({
48
+ success(code) { resolve(code) },
49
+ fail({ errMsg, errno }) {
50
+ /* handle error */
51
+ resolve("")
52
+ }
53
+ })
54
+ return promise
55
+ })
56
+ if("error" in result) {
57
+ /* handle error */
58
+ return null
59
+ }
60
+ return result; // { weixin_token, openid, unionid }
61
+ }
62
+
63
+ // idToken 通过通行证登录组件获取,weixinToken 通过上述方法获取
64
+ export function login(idToken: string, weixinToken: string) {
65
+ authToken.idToken = idToken
66
+ authToken.weixinToken = weixinToken;
67
+ // 可选自动登录调用,后续每个 api 都会自动检查并 autoLogin
68
+ authToken.autoLogin();
69
+ }
70
+
71
+ // 导出供其他功能使用
72
+ export { eventApi, clubApi, communityApi, weixinApi };
73
+ ```
74
+