@seayoo-web/gamer-api 1.0.0

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.
@@ -0,0 +1,151 @@
1
+ import type { AutoLoginHandler } from "./utils";
2
+ import type { NetRequestFactory } from "@seayoo-web/request";
3
+ /** EventApi */
4
+ export declare class EventApi {
5
+ private req;
6
+ private autoLogin;
7
+ private event;
8
+ /**
9
+ * 创建 EventApi 实例
10
+ *
11
+ * @param baseRequest - Gamer 通用的 request 工具实例,baseUrl 仅仅包含 endpoint + version
12
+ * @param autoLogin - 自动登录函数,由上层组件提供
13
+ */
14
+ constructor(baseRequest: ReturnType<NetRequestFactory>, autoLogin: AutoLoginHandler);
15
+ /**
16
+ * 设置 Event Id,以供后续 api 请求中使用
17
+ */
18
+ setEventId(event: number): this;
19
+ /**
20
+ * 获取运营活动的基础配置信息
21
+ *
22
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=wFXxtdNKPm
23
+ */
24
+ getEventConfig(): Promise<import("./event.config").EventConfig | null>;
25
+ /**
26
+ * 获取某个玩法参与用户数量
27
+ *
28
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=ejPOcWMvT1
29
+ */
30
+ getEngagementsUserCount(featureId: number): Promise<number | null>;
31
+ /**
32
+ * 获取某个玩法参与次数
33
+ *
34
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=ejPOcWMvT1
35
+ */
36
+ getEngagementsCount(featureId: number): Promise<number | null>;
37
+ /**
38
+ * 获取某个玩法的参与记录(和特定用户无关)
39
+ *
40
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=THtZ4ewIQW
41
+ */
42
+ getEngagements(featureId: number): Promise<import("./event.other").CommentEngagement[] | import("./event.other").TeamEngagement[]>;
43
+ /**
44
+ * 用户访问某个活动,需要先设置 idToken / weixinToken,返回是否
45
+ *
46
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=DWtJrnrBCn
47
+ */
48
+ userVisit(option?: {
49
+ feature_id?: number;
50
+ server_id?: string;
51
+ role_id?: string;
52
+ /** 来源标识 */
53
+ utm_source?: string;
54
+ /** 邀请人的世游通行证 ID */
55
+ invited_by?: string;
56
+ }): Promise<{
57
+ first_visit: boolean;
58
+ } | null>;
59
+ /**
60
+ * 获取用户在目标活动下的所有玩法的参与状态,用于前端进一步确定如何执行交互逻辑
61
+ *
62
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=WzX5BrMNpL
63
+ */
64
+ getUserFeatureStatus(serverId?: string, roleId?: string): Promise<import("./event.other").UserFeatureStatus[]>;
65
+ /**
66
+ * 获取当前登录用户参与某个玩法的记录
67
+ *
68
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=WSbAeDZc8F
69
+ */
70
+ getUserEngagements(featureId: number): Promise<import("./event.other").UserEngagement[]>;
71
+ /**
72
+ * 查询用户活动道具数量。注意,这里只能是活动道具
73
+ *
74
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=mnGCTeTgMb
75
+ */
76
+ getUserItemCount(itemId: number): Promise<number | null>;
77
+ /**
78
+ * 填写实物奖励收货地址
79
+ *
80
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=QIwGhnaexb
81
+ */
82
+ submitUserRewardAddress(option: {
83
+ reward_id: number;
84
+ /** ⚠️ 用户收货地址 id,后续会删除 */
85
+ address_id?: number;
86
+ /** 收件人昵称 */
87
+ recipient: string;
88
+ mobile: string;
89
+ province: string;
90
+ city: string;
91
+ district: string;
92
+ address: string;
93
+ }): Promise<boolean | null>;
94
+ /**
95
+ * 小程序码携带参数最多 32 个可见字符,若前端携带参数过长,则将参数保存在 api 中,并返回符合小程序码要求的 scene
96
+ *
97
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=TbEeyUmZzN
98
+ *
99
+ * @param params - 前端扫描二维码打开小程序所需参数
100
+ */
101
+ getUnlimitQrcodeScene(params: string): Promise<string | null>;
102
+ /**
103
+ * 根据小程序码携带 scene 值,获取对应的前端所需参数
104
+ *
105
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=SzWPFZDQBV
106
+ *
107
+ * @param scene - 小程序码 scene 值
108
+ */
109
+ getParamsByQrcodeScene(scene: string): Promise<string | null>;
110
+ /**
111
+ * 生成不限制的带参数的小程序码
112
+ *
113
+ * https://kdocs.cn/l/ckWFDcOsYEUA?linkname=Rvmoq8iIy0
114
+ */
115
+ generateUnlimitQrcode(option: {
116
+ app_id: string;
117
+ /**
118
+ * 小程序码所携带参数
119
+ *
120
+ * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/qr-code/getUnlimitedQRCode.html#%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0
121
+ */
122
+ scene: string;
123
+ page?: string;
124
+ /**
125
+ * 检查 page 是否存在;检查时小程序必须已发布,且 page 存在。
126
+ *
127
+ * 0 默认检查 1 检查 2 不检查
128
+ */
129
+ check_path?: 0 | 1 | 2;
130
+ /**
131
+ * 扫描小程序码打开的小程序版本,默认是正式版
132
+ *
133
+ * - release 正式版小程序
134
+ * - trial 体验版小程序
135
+ * - develop 开发版小程序
136
+ */
137
+ env_version?: "release" | "trial" | "develop";
138
+ /** 小程序码的宽度,单位:px,最小 280,最大 1280,默认 430 */
139
+ width?: number;
140
+ /** 小程序码自动配置线条颜色 */
141
+ auto_color?: boolean;
142
+ /** 小程序码线条颜色,autoColor 为 false 时生效,使用 rgb 设置颜色 */
143
+ line_color?: {
144
+ r?: string;
145
+ g?: string;
146
+ b?: string;
147
+ };
148
+ /** 生成的小程序码是否需要透明底色 */
149
+ is_hyaline?: boolean;
150
+ }): Promise<string | null>;
151
+ }
@@ -0,0 +1,151 @@
1
+ import type { EFeatureType, EGameItemType, EQuestObjective } from "./event.config";
2
+ export declare function isCommonCountResponse(data: unknown): data is {
3
+ count: number;
4
+ };
5
+ interface EngamentsResponse {
6
+ engagements: CommentEngagement[] | TeamEngagement[];
7
+ }
8
+ export interface CommentEngagement {
9
+ /** 用户世游通行证 ID */
10
+ user_id: string;
11
+ /** 用户昵称 */
12
+ name: string;
13
+ /** 用户头像地址 */
14
+ avatar_url: string;
15
+ /** 评论信息 */
16
+ comment: string;
17
+ /** 用户发布弹幕的视频点 */
18
+ video_note: number;
19
+ /** 用户发布弹幕的时间 */
20
+ created_at: number;
21
+ }
22
+ export interface TeamEngagement {
23
+ /** 组队码 */
24
+ objective: string;
25
+ /** 队长角色名称 */
26
+ leader_name: string;
27
+ /** 队员数量 */
28
+ total_members: number;
29
+ }
30
+ export declare function isEngamentsResponse(data: unknown): data is EngamentsResponse;
31
+ export declare function isUserVisitResponse(data: unknown): data is {
32
+ first_visit: boolean;
33
+ };
34
+ interface UserFeatureStatusResponse {
35
+ status: UserFeatureStatus[];
36
+ }
37
+ export interface UserFeatureStatus {
38
+ feature_id: number;
39
+ can_engage: boolean;
40
+ has_unclaimed_rewards: boolean;
41
+ }
42
+ export declare function isUserFeatureStatusResponse(data: unknown): data is UserFeatureStatusResponse;
43
+ interface UserEngagementsResponse {
44
+ engagements: UserEngagement[];
45
+ }
46
+ export declare function isUserEngagementsResponse(data: unknown): data is UserEngagementsResponse;
47
+ export interface UserEngagement {
48
+ engagement_id: number;
49
+ sequence: number;
50
+ feature_id: number;
51
+ reward_id: number;
52
+ reward_status: "ineligible" | "unaccalimed" | "received";
53
+ reward_source: "output" | "engage";
54
+ data?: PreregisterEngagementData | LotteryEngagementData | SurveyEngagementData | InviteEngagementData | ShareEngagementData | VoteEngagementData | WeixinSubscribeEngagementData | CashbackEngagementData | GiftCodeEngagementData | TeamEngagementData | QuestEngagementData | LotteryDrawEngagementData;
55
+ server_id?: string;
56
+ role_id?: string;
57
+ created_at: number;
58
+ }
59
+ interface PreregisterEngagementData {
60
+ platforms: string[];
61
+ }
62
+ interface LotteryEngagementData {
63
+ lottery_count: number;
64
+ }
65
+ interface SurveyEngagementData {
66
+ serial_number: number;
67
+ }
68
+ interface InviteEngagementData {
69
+ user_id: string;
70
+ name: string;
71
+ avatar_url: string;
72
+ }
73
+ interface ShareEngagementData {
74
+ platform: string;
75
+ }
76
+ interface VoteEngagementData {
77
+ option_sn: string;
78
+ }
79
+ interface WeixinSubscribeEngagementData {
80
+ weixin_openid: string;
81
+ }
82
+ interface CashbackEngagementData {
83
+ order_total_amount: string | number;
84
+ cashback_total_amount: string | number;
85
+ game_item_count: string | number;
86
+ }
87
+ interface GiftCodeEngagementData {
88
+ gift_code: string;
89
+ }
90
+ interface TeamEngagementData {
91
+ team_code: string;
92
+ visibility: "private" | "public";
93
+ members: {
94
+ is_leader: boolean;
95
+ role_name: string;
96
+ server_name: string;
97
+ }[];
98
+ }
99
+ interface QuestEngagementData {
100
+ objective: EQuestObjective;
101
+ progress: number;
102
+ completion_value?: number;
103
+ team?: {
104
+ progress: number;
105
+ completion_value?: number;
106
+ players: {
107
+ user_id: number;
108
+ role_name: string;
109
+ is_leader: boolean;
110
+ progress?: number;
111
+ }[];
112
+ };
113
+ }
114
+ interface LotteryDrawEngagementData {
115
+ tickets: ({
116
+ ticket: string;
117
+ status: "unknown" | "pending";
118
+ } | {
119
+ ticket: string;
120
+ status: "drawn" | "claimed" | "fail";
121
+ reward: LotteryDrawUserReward;
122
+ })[];
123
+ }
124
+ interface LotteryDrawUserReward {
125
+ reward_id: number;
126
+ reward_item_id: number;
127
+ reward_item_name: string;
128
+ reward_source: "output" | "engage";
129
+ reward_item_type: EGameItemType;
130
+ reward_item_icon_url: string;
131
+ reward_item_desc?: string;
132
+ reward_amount: number;
133
+ reward_status: "unaccalimed" | "received" | "failed" | "delivered";
134
+ event_id: number;
135
+ event_name: string;
136
+ feature_id: number;
137
+ engagement_id: number;
138
+ feature_type: EFeatureType;
139
+ receive_time: number;
140
+ extra_data?: Record<string, unknown>;
141
+ }
142
+ export declare function isGetUnlimitQrcodeSceneResponse(data: unknown): data is {
143
+ scene: string;
144
+ };
145
+ export declare function isGetParamsByQrcodeSceneResponse(data: unknown): data is {
146
+ params: string;
147
+ };
148
+ export declare function isGenerateUnlimitQrcodeResponse(data: unknown): data is {
149
+ img: string;
150
+ };
151
+ export {};
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 自动登录函数(获取 Gamer Token)
3
+ */
4
+ export type AutoLoginHandler = () => Promise<boolean>;
5
+ /**
6
+ * 将 endpoint & verison 转成 request base url
7
+ */
8
+ export declare function getBaseUrl(endpoint: string, version?: string): string;
9
+ export declare const console: Pick<Console, "log" | "error" | "warn">;
@@ -0,0 +1,13 @@
1
+ export interface WeixinLoginResponse {
2
+ /** 微信小程序下,用户的 openid */
3
+ openid: string;
4
+ /** 微信小程序所在的微信开放平台账号下,用户的 unionid */
5
+ unionid: string;
6
+ /**
7
+ * Gamer 为这个微信小程序用户签发的 Weixin Token。长期有效
8
+ *
9
+ * 对 Weixin Token 的详细描述请阅读 [世游用户运营 - JWT 场景与规范](https://365.kdocs.cn/l/cbxbzWc6zSgY)
10
+ */
11
+ weixin_token: string;
12
+ }
13
+ export declare function isWeixinLoginResponse(data: unknown): data is WeixinLoginResponse;
@@ -0,0 +1,48 @@
1
+ import { type AutoLoginHandler } from "./utils";
2
+ import type { NetRequestFactory } from "@seayoo-web/request";
3
+ export declare class WeixinApi {
4
+ private req;
5
+ private autoLogin;
6
+ /**
7
+ * 创建 WeixinApi 实例
8
+ *
9
+ * @param baseRequest - Gamer 通用的 request 工具实例,baseUrl 仅仅包含 endpoint + version
10
+ * @param autoLogin - 自动登录函数,由上层组件提供
11
+ */
12
+ constructor(baseRequest: ReturnType<NetRequestFactory>, autoLogin: AutoLoginHandler);
13
+ /**
14
+ * 微信静默登录,获取 weixinToken / openid / unionid,其中 weixinToken 应该被缓存
15
+ *
16
+ * 使用场景为:
17
+ *
18
+ * - 用户通过任意途径打开小程序的任意页面后,如果本地缓存中不存在微信用户身份信息,则自动调用此 API。
19
+ * - 调用成功后,应当使用存储能力将 response 中的微信用户身份信息缓存在本地。
20
+ *
21
+ * 小程序不应当频繁调用此 API。
22
+ *
23
+ * 由于小程序的存储是以用户维度隔离的,每个微信用户在每个小程序下只需要成功调用一次此 API。
24
+ *
25
+ * [API文档 weixin/login](https://kdocs.cn/l/cf2mO2uRLqh9?linkname=x17CeNlDQx)
26
+ *
27
+ * @param wxcode - 微信小程序调用 wx.login 获得的登录凭证
28
+ * @param appid - 微信小程序的 Appid,后端 api 可以从请求的 referer 中提取,后续会删除此参数
29
+ */
30
+ login(wxcode: string, appid: string): Promise<import("./weixin.guards").WeixinLoginResponse | {
31
+ error: string;
32
+ message: string;
33
+ }>;
34
+ /**
35
+ * 关联游戏账号
36
+ *
37
+ * 用于从游戏内跳转至小程序时,将微信用户身份和游戏内身份关联起来,使得后续的企业微信客服流程能够知道该微信用户在游戏内的身份。
38
+ *
39
+ * https://kdocs.cn/l/cf2mO2uRLqh9?linkname=ci73N833Mk
40
+ *
41
+ * @param weixinToken 调用 login 后,得到的 Weixin Token
42
+ * @param gameCode 从游戏内跳转至小程序时,通过请求参数 query 携带的 game_code
43
+ */
44
+ linkPlayer(weixinToken: string, gameCode: string): Promise<true | {
45
+ error: string;
46
+ message: string;
47
+ }>;
48
+ }
package/types/wx.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { GamerBaseApi } from "./src/base";
2
+ import { ClubApi } from "./src/club";
3
+ import { EventApi } from "./src/event";
4
+ import { WeixinApi } from "./src/wexin";
5
+ export declare class GamerApi extends GamerBaseApi {
6
+ private weixinToken;
7
+ /** event api 集合 */
8
+ event: EventApi;
9
+ /** club api 集合 */
10
+ club: ClubApi;
11
+ /** weixin api 集合 */
12
+ weixin: WeixinApi;
13
+ constructor(endpoint: string, option?: {
14
+ version?: string;
15
+ });
16
+ /**设置 Weixin Token,用于后续请求自动置换 Gamer Token */
17
+ setWeixinToken(weixinToken: string): this;
18
+ /** 读取设置 wexinToken */
19
+ getWexinToken(): string;
20
+ /**
21
+ * 用世游通行证统一登录能力签发的 idToken 和 weixinToken,自动置换 gamerToken,成功后会自动设置后续 Request Headers
22
+ *
23
+ * [世游通行证统一登录](https://kdocs.cn/l/cf2mO2uRLqh9?linkname=FJXYgqvLHs)
24
+ *
25
+ * [世游通行证统一登录 - 微信小程序](https://365.kdocs.cn/l/ccHVuUs0UsZs)
26
+ *
27
+ * - 此方法通常不需要手动调用,在需要登录的接口调用时会自动执行
28
+ * - 为了提高页面执行速度,可以在 setIdToken + setWeixinToken 后立即调用 autoLogin,比如
29
+ *
30
+ * @example
31
+ * ``` js
32
+ * gamerApi.setIdToken("xxxxx").setWeixinToken("yyyyyy").autoLogin()
33
+ * ```
34
+ */
35
+ autoLogin(): Promise<boolean>;
36
+ }